changeset 113:8b8131f13692 draft

Uploaded
author luca_milaz
date Tue, 02 Jul 2024 19:51:40 +0000
parents dff08700bbb8
children e32ee8b88c2e
files marea_2_0/flux_sampling.py
diffstat 1 files changed, 31 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/marea_2_0/flux_sampling.py	Tue Jul 02 19:51:23 2024 +0000
+++ b/marea_2_0/flux_sampling.py	Tue Jul 02 19:51:40 2024 +0000
@@ -88,15 +88,22 @@
 
 def OPTGP_sampler(model:cobra.Model, model_name:str, n_samples:int=1000, thinning:int=100, n_batches:int=1, seed:int=0)-> None:
 
-    if not os.path.exists(ARGS.output_folder + "OPTGP/" + ARGS.model_name):
-        os.makedirs(ARGS.output_folder + "OPTGP/" + ARGS.model_name)
+    if not os.path.exists(ARGS.output_folder + "OPTGP/"):
+        os.makedirs(ARGS.output_folder + "OPTGP/")
 
     for i in range(0, n_batches):
         optgp = OptGPSampler(model, thinning, seed)
         samples = optgp.sample(n_samples)
-        write_to_file(samples, ARGS.output_folder + "OPTGP/" + + ARGS.model_name + '/OPTGP_thinning' + str(thinning) + '_samples_' + str(n_samples) + '_batch_' + str(i) + '.tsv')
+        samples.to_csv(ARGS.output_folder + "OPTGP/" +  ARGS.model_name + '_'+ str(i)+'.csv')
         i+=1
         seed+=1
+    samplesTotal = pd.DataFrame()
+    for i in range(0, n_batches):
+        samples_batch = pd.read_csv(ARGS.output_folder + "OPTGP/" +  ARGS.model_name + '_'+ str(i)+'.csv')
+        samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True)
+    write_to_file(samplesTotal, ARGS.output_folder + "OPTGP/" + ARGS.model_name)
+    for i in range(0, n_batches):
+        os.remove(ARGS.output_folder + "OPTGP/" +  ARGS.model_name + '_'+ str(i)+'.csv')
     pass
 
 
@@ -120,8 +127,14 @@
             ARGS.out_log)
             CBS_backend.randomObjectiveFunctionSampling_cobrapy(model, n_samples, df_coefficients.iloc[:,i*n_samples:(i+1)*n_samples], 
                                                     samples)
+        samples.to_csv(ARGS.output_folder + "CBS/" +  ARGS.model_name + '_'+ str(i)+'.csv')
 
-        write_to_file(samples, ARGS.output_folder + "CBS/" + ARGS.model_name + '/CBS_samples_' + str(n_samples)  + '_batch_' + str(i) + '.tsv')
+    for i in range(0, n_batches):
+        samples_batch = pd.read_csv(ARGS.output_folder + "CBS/" +  ARGS.model_name + '_'+ str(i)+'.csv')
+        samplesTotal = pd.concat([samplesTotal, samples_batch], ignore_index = True)
+    write_to_file(samplesTotal, ARGS.output_folder + "CBS/" + ARGS.model_name)
+    for i in range(0, n_batches):
+        os.remove(ARGS.output_folder + "CBS/" +  ARGS.model_name + '_'+ str(i)+'.csv')
     pass
 
 
@@ -167,12 +180,22 @@
     ARGS = process_args(sys.argv)
 
     ARGS.output_folder = 'flux_sampling/'
+
+
     
     # load custom model
-    model = load_custom_model(
-        utils.FilePath.fromStrPath(ARGS.input_model), utils.FilePath.fromStrPath(ARGS.model_name).ext)
+    model1 = load_custom_model(
+        utils.FilePath.fromStrPath(ARGS.input_model[0]), utils.FilePath.fromStrPath(ARGS.model_name[0]).ext)
+    model2 = load_custom_model(
+        utils.FilePath.fromStrPath(ARGS.input_model[1]), utils.FilePath.fromStrPath(ARGS.model_name[1]).ext)
     
-    ARGS.model_name = os.path.splitext(os.path.basename(ARGS.input_model))[0] # extract model filename only
+    #ARGS.model_name = os.path.splitext(os.path.basename(ARGS.input_model))[0] # extract model filename only
+
+
+    with open(ARGS.output_folder, 'w') as f:
+                f.write([model1, model2])
+
+    '''
     
     if ARGS.sampling_algorithm == 'OPTGP':
         OPTGP_sampler(model, ARGS.model_name, ARGS.n_samples, ARGS.thinning, ARGS.n_batches, ARGS.seed, ARGS.out_dir)
@@ -181,7 +204,7 @@
         CBS_sampler(model, ARGS.model_name, ARGS.n_samples, ARGS.n_batches, ARGS.seed, ARGS.out_dir)
     else:
         raise utils.ValueErr(ARGS.sampling_algorithm,
-        f"Algorithm \"{ARGS.sampling_algorithm}\" is not recognized, only OPTGP and CBS are supported.")
+        f"Algorithm \"{ARGS.sampling_algorithm}\" is not recognized, only OPTGP and CBS are supported.")'''
         
 ##############################################################################
 if __name__ == "__main__":