Speech:Train 02.pl

Summary
Title: train_02.pl

Author: Eric Beikman

Location: mnt/main/scripts/user/train_02.pl

Date: June, 2013

Revisions:
 * Version 1.6:
 * Expanded -c to also accept a simple experiment number instead of a full filepath.
 * Fixed a bug which causes the script to fail if an experiment doesn't have a config file.
 * Version: 1.5
 * Added option -c for "Copy and edit". Its a cross between the "dumb" -R or replace flag, and the normal operation of editing a new config file. Useful for copying and adapting a modified config file to a new experiment.
 * Fixed a bug which was created by $0's inconsistent output.
 * Version 1.0:
 * Initial release.

Usage:

Description
This Script is designed to automate the in running a train, along with adapting existing experiment configuration files to new experiments, or replacing an existing experiment configuration file completely with a another. The script number requires an experiment number to be given to it to specify what experiment to perform the operation on; the script isn't dependent on where it is executed; it effectively can be executed anywhere.

Normal operation and the -c flag will have the script edit the lines 6-8, to adapt the experiment to the specified experiment (-e). Not using the -c flag will have the script also edit lines 79-80, switching the config to create a sphinx-2 format semi-continuous model, to a Sphinx-3 format continuous model. The -r flag is a simple dumb replacement of a config file with a specified folder.

Options:
 * -e:
 * Experiment Number. This is a REQUIRED argument. States which experiment's config file the actions will be performed on. Not supplying this argument, or supplying an invalid experiment number will result in the script erroring out.


 * -h:
 * Help, This text. When this flag is given, the script will not perform any other actions.


 * -b:
 * Backup. When used, the script will make a backup of the sphinx_train.cfg file within the experiment's 'etc' directory before performing any operations on it. The filename of this backup will have a '.old', if a file exists using this name, the script will append the lowest possible number at the end of '.old' to create a unique filename. This flag is implied when using the -r or -c flags.


 * -r:
 * Replace. Instead of editing an existing config file, the script will replace it with the one specified. Give an ABSOLUTE filepath with this argument or the script probably will error out. When using this flag, it implies -b is used too; in other words, it will make a backup of the old config file before replacing it. This is to ensure that you have something to to back to in the event something goes wrong.


 * -c:
 * Copy. Similar to -r, except that a config file is adapted for use for this experiment. The script will only edit the lines which deal with the experiment directories. You can either put in an absolute filepath to a config file OR you can put in the experiment number you wish to you wish to copy and adapt the experiment for.";

The script will create a log within the experiment's logdir/aes directory called train_02.pl.log. Within this log, the script will insert the arguments given, the calling process, and the steps taken.

When finished, the script will simply exit, returning '0'. In case of script failure, the script will 'die', printing what went wrong to standard error, and returning '1' to the calling process. Script output for normal operation is designed to be minimal to facilitate its integration into a larger series of scripts.

Code

 * 1) !/usr/bin/perl
 * 2)   Title: train_02.pl
 * 3)   Author: Eric Beikman
 * 4)       Date : June, 2013
 * 5) 	 Version 1.6:
 * 6) 		Expanded -c to also accept a simple experiment number instead
 * 7) 			of a full filepath.
 * 8) 		Fixed a bug which causes the script to fail if an experiment
 * 9) 			doesn't have a config file.
 * 10) 	 Version 1.5:
 * 11) 	 	Added option -c for "Copy and edit".
 * 12) 	 		Its a cross between the "dumb" -R or replace, and the normal operation of editing the lines. Useful for copying a modified config file. 		Fixed a bug resulting from the use of $0.
 * 13)       Version: 1.0
 * 14) 		Initial Release
 * 15) Experiment Configuration file setup script.
 * 16) Prepares a given Experiment config file (sphinx_train.cfg) for an experiment.
 * 17) In this version, it will:
 * 18) 	1) Edit the configs on lines 6-8 & lines 79-80.
 * 19) 	2) (Optionally with -r) Replace a config file with a supplied one.
 * 20) 	3) (Optionally with -b flag, implied with -r) Backup the existing config file by appending a ".old" to the end of the filename. If a prospective backup filename is already used, the script will append successively higher numbers until it finds a unique one.
 * 1) 	3) (Optionally with -b flag, implied with -r) Backup the existing config file by appending a ".old" to the end of the filename. If a prospective backup filename is already used, the script will append successively higher numbers until it finds a unique one.

use Getopt::Std;#Simplifies CMD line Options use File::Path; use File::Copy; use File::Basename;

$cfgFileName = "sphinx_train.cfg"; $rootExpDir = "/mnt/main/Exp/"; $scriptName = basename($0); $cfgFileLocation = "/etc/"; $help = "Experiment Configuration setup script:

Usage: $scriptName -e [-bh] [-r ] [-c ]

In this version, it will: 1) Edit the configs on lines 6-8 & lines 79-80.     2) (Optionally with -r) Replace a config file with a supplied one without editing it. 3) (Optionally with -b flag, implied with -r & -c) Backup the existing config file by appending a '.old' to the end of the filename. If a prospective backup filename is already used, the script will append successively higher numbers until it finds a unique one.	4) (Optionally with -c), copies over a pre-existing config file at the specified location OR experiment and adapt it for use for the given experiment.

Usage: -e: Experiment Number. This is a REQUIRED argument. States which experiment's config file the actions will be performed on. Not supplying this argument, or supplying an invalid experiment number will result in the script erroring out.

-h: Help, This text. When this flag is given, the script will not perform any other actions.

-b: Backup. When used, the script will make a backup of the sphinx_train.cfg file within the experiment's 'etc' directory before performing any operations on it. The filename of this backup will have a '.old', if a file exists using this name, the script will append the lowest possible number at the end of '.old' to create a unique filename. This flag is implied when using the -r or -c flags.

-r: Replace. Instead of editing an existing config file, the script will replace it with the one specified. Give an ABSOLUTE filepath with this argument or the script probably will error out. When using this flag, it implies -b is used too; in other words, it will make a backup of the old config file before replacing it. This is to ensure that you have something to to back to in the event something goes wrong.

-c: Copy. Similar to -r, except that a config file is adapted for use for this experiment. The script will only edit the lines which deal with the experiment directories. You can either put in an absolute filepath to a config file OR you can put in the experiment number you wish to you wish to copy and adapt the experiment for.";

$defaultDBName = "train1";#Defined on Line 6. $defaultBaseDir ="/root/speechtools/SphinxTrain-1.0/train1"; #Defined on line 7. $defaultRootDir = "/root/speechtools/SphinxTrain-1.0";#Defined on line 8;

getopts('bhr:e:c:', \%flags) or die $help;#get option flags.

if($flags{h}){ die $help; } if(!(defined $flags{e})){ die "Error: No experiment number given!" } $expNumber = $flags{e}; @keys = keys %flags; $log = "$scriptName: Log:\nExperiment number: $expNumber\nGiven flags: ". $keys ."\n-\n";

if( -e $rootExpDir . $expNumber . $cfgFileLocation . $cfgFileName){ -w $rootExpDir. $expNumber. $cfgFileLocation. $cfgFileName or die "Error: Cannot open config file for editing!\nDoes it exist? And if so, do you have write access to it?\n"; }

if($flags{c} && $flags{r}){ die "Error: Cannot have both -c and -r!"; }

if(defined $flags{c}){ if($flags{c} =~ m/\d+/){ $replacement = $rootExpDir. $flags{c}. $cfgFileLocation. $cfgFileName; }else{ $replacement = $flags{c}; }		-e $replacement or die "Error: Specified config file ($replacement) does not exist!"; } elsif(defined $flags{r}){ $replacement = $flags{r}; -e $replacement or die "Error: Specified Replacement file does not exist!"; }

$cfgFilePath = $rootExpDir. $expNumber. $cfgFileLocation. $cfgFileName; $append; if(($flags{b} || $flags{c} || $flags{r}) && (-e $cfgFilePath)){#Make a backup of the existing config file if asked and it exists. while(1){ if(-e $cfgFilePath . ".old$append"){ $append++; } else { if(!($flags{-r})){ move($rootExpDir . $expNumber . $cfgFileLocation . $cfgFileName, $rootExpDir . $expNumber . $cfgFileLocation . $cfgFileName . ".old$append") or die "Error: Cannot make a backup of the existing config file!"; }			else{ copy($rootExpDir . $expNumber . $cfgFileLocation . $cfgFileName, $rootExpDir . $expNumber . $cfgFileLocation . $cfgFileName . ".old$append") or die "Error: Cannot make a backup of the existing config file!"; }		$log .= "Made a backup of $cfgFilePath with the name $cfgFileName". ".old$append \n"; last; }	} }

if($flags{c}){ copy($replacement, $cfgFilePath) or die "Error: Cannot copy $replacement over to $cfgFilePath!"; $log .= "Copied $replacement over to $cfgFilePath \n";

}


 * 1) Now: the fun stuff. #############

if(!($flags{r})){ open(CFGFILE, '<', $cfgFilePath) or die "Error: Cannot open config file!";

while(){ chomp;#get rid of newlines and the such. push(@cfg, $_); }	close CFGFILE;

#Default cfg file operations. @cfg[5] = "\$CFG_DB_NAME = \"$expNumber\""; $baseDir = $rootExpDir. $expNumber; @cfg[6] = "\$CFG_BASE_DIR = \"$baseDir\""; @cfg[7] = "\$CFG_SPHINXTRAIN_DIR = \"$rootExpDir\""; if(!$flags{c}){ @cfg[78] = substr($cfg[78], 1); @cfg[79] = "#". $cfg[79]; }	open(CFGFILE, ">", $cfgFilePath) or die "Error: Cannot write to config file!"; foreach(@cfg){ print CFGFILE $_. "\n"; }	close CFGFILE;

$log .= "Edited lines 6-8"; if(!$flags{c}){ $log .= " and 9-80"; }      	$log .= ". re-created $cfgFilePath based on modified values.\n"; }

$logfile = $rootExpDir. $expNumber. "/logdir/aes/". $scriptName. ".log"; $log .= "Done! All operations completed sucessfully.\nCreating logfile at $logfile, then returning '0' to calling process";

open( LOGFILE, '>>', $logfile);

print LOGFILE $log;

close(LOGFILE);

exit 0;