Skip to content

Commit

Permalink
Add config combinor function
Browse files Browse the repository at this point in the history
  • Loading branch information
kozzy97 committed Nov 21, 2022
1 parent 82dc2ec commit ce6bc12
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
59 changes: 38 additions & 21 deletions config_combinor/Combine-Config.ps1
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
## Combine-Configs function
# Combine-Configs function

# This function takes an input path to a directory containing some configs, a regex for which files to include, a regex for which files to exclude, an output path, and a name for a the combined file.
# Written with Powershell version 5.1.22621.608

# This function takes, a regex for which files to include, and a combined name and path for a the combined file.
# It outputs a combined YML config at the path of your choice.
# input and output paths default to the current working directory, and regexes default to all '.yml' or '.yaml' files in the working directory
# regexes default to all '.yml' or '.yaml' files in the working directory. An error appears if you don't include the .yml or .yaml extension in the file path

function Combine-Configs{
param(
$inputpath = cd
$regexinclude = '*.yml|*.yaml'
$regexexclude = "!"
$outputpath = cd
$regex = '*.yml|*.yaml',
$name
)
$AllYAMLs = Get-ChildItem -include $regexinclude -exclude $regexexclude
Write-Host "Number of files found"
Write-Host $AllYAMLs.Count
Write-Host "Combining files"
$outputpathname = $outputpath + $name
foreach ($i in $AllYAMLs){
$filename = Split-Path $i -Leaf
$content = Get-Content $i
$content = $content.Replace("!ArenaConfig", "`# $filename")
$content = $content.Replace("arenas:", "")
$content = $content.Replace("0: !Arena", "$index`: !Arena")
$content = $content.Replace("-1: !Arena", "$index`: !Arena")
Add-Content -Path 'C:\Users\kvoud\OneDrive - University of Cambridge\Documents\PhD Year 2\Animal-AI OP Battery Building\O-PIAAGETS\OP_configs\V3_Configs\Combined2022-03-15b.yml' -Value $content

if (!$name.Contains(".yml") -and !$name.Contains(".yaml")){
Write-Host "Name must contain the file extension .yml or .yaml"
} else {
$AllYMLs = Get-ChildItem $regex -recurse #get all the YMLs you want
$NumYMLs = $AllYMLs.Count #count them and save the number for later
Write-Host "Number of files found"
Write-Host $NumYMLs
Write-Host "Combining files to"
Write-Host $name

New-Item $name -Force #make the new file
$firstfilename = Split-Path $AllYMLs[0] -Leaf #get yml file config name to put in combined config as comment
$firstfilecontent = Get-Content $AllYMLs[0] #get the content of the file
$firstfilecontent = $firstfilecontent.Replace("!ArenaConfig", "!ArenaConfig # $firstfilename") #add in the comment at the top so we know which config the below refers to
$firstfilecontent = $firstfilecontent.Replace("-1: !Arena", "0: !Arena")
Set-Content -path $name -value $firstfilecontent #set the content to the new file

foreach ($i in $AllYMLs[1..$NumYMLs]){ #for the rest of the configs
$index = $AllYMLs.IndexOf($i)
$filename = Split-Path $i -Leaf #get the yml config name to put at top of config so we can keep track when reading config
$content = Get-Content $i #get the content of the config
$content = $content.Replace("!ArenaConfig", "`# $filename") #replace the `!ArenaConfig` call with just the commented file name, as we only need it once at the top of the doc
$content = $content.Replace("arenas:", "") #replace the `arenas` item call, as we only need it once at top of config.
$content = $content.Replace("-1: !Arena", "$index`: !Arena")
Add-Content -Path $name -Value $content
}
Write-Host "Success."
}
}


}

13 changes: 11 additions & 2 deletions config_combinor/Combinor_README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# Combination Scripts

## Intro

To make training and testing run faster, it is necessary to combine multiple configs into a single file, so that we only need load the AAI environment once.

In this folder, there is a PowerShell function for combining YML files flexibly and quickly. The function combines YML files with `-1` as the index for the `arenas` Item. This means that configs are presented in a random order once the environment is loaded. You may set a seed in the Python call to fix the order of presentation, for reproducibility.
In this folder, there is a PowerShell function for combining YML files flexibly and quickly. The function combines YML files in alphabetical order and indexes them appropriately.

If you wish to do curriculum learning, at the moment, we advise creating separate configs for each stage of the curriculum, and recalling the environment with each config as the curriculum progresses.

A function that generates configs in order is in development.
A function that generates configs in order is in development.

## How to call the function

Call the function using dot sourcing. Check it has successfully loaded by calling `dir function:`. You should see a function called `Combine-Configs` in your list.

Now call the function with something like
`Combine-Configs -regex "OP-PTPC-Size-ImpExp-0.5Exp1-CloseLeft*.yml" -name "config_combinor\combined_config.yml"`

0 comments on commit ce6bc12

Please sign in to comment.