11import numpy as np
2- import subprocess
32import sys
43import os
5- import pickle
6- import itertools
74from subprocess import call
5+ import re
86
97# Check if we are using python 3
108python_version = sys .version_info [0 ]
@@ -27,7 +25,7 @@ class pythonProgramOptions:
2725 # in the bin directory (rather than the src directory)
2826 compas_executable_override = os .environ .get ('COMPAS_EXECUTABLE_PATH' )
2927 print ('compas_executable_override' , compas_executable_override )
30-
28+
3129 if (compas_executable_override is None ):
3230 git_directory = os .environ .get ('COMPAS_ROOT_DIR' )
3331 compas_executable = os .path .join (git_directory , 'src/COMPAS' )
@@ -52,7 +50,7 @@ class pythonProgramOptions:
5250 # if COMPAS_LOGS_OUTPUT_DIR_PATH is not set (== None) the current working directory
5351 # is used as the value for the --output-path option
5452 compas_logs_output_override = os .environ .get ('COMPAS_LOGS_OUTPUT_DIR_PATH' )
55-
53+
5654 if (compas_logs_output_override is None ):
5755 output = os .getcwd ()
5856 output_container = None # names the directory to be created and in which log files are created. Default in COMPAS is "COMPAS_Output"
@@ -66,7 +64,7 @@ class pythonProgramOptions:
6664 # if COMPAS_INPUT_DIR_PATH is not set (== None) the current working directory
6765 # is prepended to input filenames
6866 compas_input_path_override = os .environ .get ('COMPAS_INPUT_DIR_PATH' )
69-
67+
7068 #-- option to make a grid of hyperparameter values at which to produce populations.
7169 #-- If this is set to true, it will divide the number_of_binaries parameter equally
7270 #-- amoungst the grid points (as closely as possible). See the hyperparameterGrid method below
@@ -82,17 +80,17 @@ class pythonProgramOptions:
8280
8381 if grid_filename != None :
8482 if compas_input_path_override == None :
85- grid_filename = os .getcwd () + '/' + grid_filename
83+ grid_filename = os .getcwd () + '/' + grid_filename . strip ( "' \" " )
8684 else :
87- grid_filename = compas_input_path_override + '/' + grid_filename
85+ grid_filename = compas_input_path_override + '/' + grid_filename . strip ( "' \" " )
8886
8987 logfile_definitions = None # logfile record definitions file name (e.g. 'logdefs.txt')
9088
9189 if logfile_definitions != None :
9290 if compas_input_path_override == None :
93- logfile_definitions = os .getcwd () + '/' + logfile_definitions
91+ logfile_definitions = os .getcwd () + '/' + logfile_definitions . strip ( "' \" " )
9492 else :
95- logfile_definitions = compas_input_path_override + '/' + logfile_definitions
93+ logfile_definitions = compas_input_path_override + '/' + logfile_definitions . strip ( "' \" " )
9694
9795 initial_mass = None # initial mass for SSE
9896 initial_mass_1 = None # primary initial mass for BSE
@@ -212,7 +210,7 @@ class pythonProgramOptions:
212210
213211 neutrino_mass_loss_BH_formation = "FIXED_MASS" # "FIXED_FRACTION"
214212 neutrino_mass_loss_BH_formation_value = 0.1 # Either fraction or mass (Msol) to lose
215-
213+
216214 remnant_mass_prescription = 'FRYER2012' #
217215 fryer_supernova_engine = 'DELAYED'
218216 black_hole_kicks = 'FALLBACK'
@@ -301,7 +299,6 @@ class pythonProgramOptions:
301299 debug_to_file = False
302300 errors_to_file = False
303301
304-
305302 def booleanChoices (self ):
306303 booleanChoices = [
307304 self .enable_warnings ,
@@ -660,12 +657,12 @@ def generateCommandLineOptionsDict(self):
660657 and run directly as a terminal command, or passed to the stroopwafel interface
661658 where some of them may be overwritten. Options not to be included in the command
662659 line should be set to pythons None (except booleans, which should be set to False)
663-
660+
664661 Parameters
665662 -----------
666663 self : pythonProgramOptions
667664 Contains program options
668-
665+
669666 Returns
670667 --------
671668 commands : str or list of strs
@@ -674,17 +671,17 @@ def generateCommandLineOptionsDict(self):
674671 booleanCommands = self .booleanCommands ()
675672 nBoolean = len (booleanChoices )
676673 assert len (booleanCommands ) == nBoolean
677-
674+
678675 numericalChoices = self .numericalChoices ()
679676 numericalCommands = self .numericalCommands ()
680677 nNumerical = len (numericalChoices )
681678 assert len (numericalCommands ) == nNumerical
682-
679+
683680 stringChoices = self .stringChoices ()
684681 stringCommands = self .stringCommands ()
685682 nString = len (stringChoices )
686683 assert len (stringCommands ) == nString
687-
684+
688685 listChoices = self .listChoices ()
689686 listCommands = self .listCommands ()
690687 nList = len (listChoices )
@@ -694,23 +691,23 @@ def generateCommandLineOptionsDict(self):
694691 ### Collect all options into a dictionary mapping option name to option value
695692
696693 command = {'compas_executable' : self .compas_executable }
697-
694+
698695 for i in range (nBoolean ):
699696 if booleanChoices [i ] == True :
700697 command .update ({booleanCommands [i ] : '' })
701-
698+
702699 for i in range (nNumerical ):
703700 if not numericalChoices [i ] == None :
704701 command .update ({numericalCommands [i ] : str (numericalChoices [i ])})
705-
702+
706703 for i in range (nString ):
707704 if not stringChoices [i ] == None :
708- command .update ({stringCommands [i ] : stringChoices [i ]})
709-
705+ command .update ({stringCommands [i ] : cleanStringParameter ( stringChoices [i ]) })
706+
710707 for i in range (nList ):
711708 if listChoices [i ]:
712709 command .update ({listCommands [i ] : ' ' .join (map (str ,listChoices [i ]))})
713-
710+
714711 return command
715712
716713
@@ -722,13 +719,26 @@ def combineCommandLineOptionsDictIntoShellCommand(commandOptions):
722719 """
723720
724721 shellCommand = commandOptions ['compas_executable' ]
725- del commandOptions ['compas_executable' ]
722+ del commandOptions ['compas_executable' ]
726723 for key , val in commandOptions .items ():
727724 shellCommand += ' ' + key + ' ' + val
728725
729726 return shellCommand
730727
731728
729+ def cleanStringParameter (str_param ):
730+ """ clean up string parameters to avoid confusing Boost """
731+ if str_param is not None :
732+ # strip any quotes from the ends of the string
733+ str_param = str_param .strip ("'\" " )
734+
735+ # escape any unescaped spaces or quotes within the string
736+ escapes = [" " , "'" , "\" " ]
737+ for escape in escapes :
738+ str_param = re .sub (r"(?<!\\){}" .format (escape ), r"\{}" .format (escape ), str_param )
739+ return str_param
740+
741+
732742if __name__ == "__main__" :
733743
734744 #-- Get the program options
0 commit comments