@@ -221,12 +221,17 @@ def __run_in_serial(self, append, light_mode):
221
221
-------
222
222
None
223
223
"""
224
-
225
224
# Create data files for inputs, outputs and error logging
226
225
open_mode = "a" if append else "w"
227
226
228
227
# Open files
229
228
if light_mode :
229
+ # get initial simulation index
230
+ idx_i , idx_o = self .__get_light_indexes (
231
+ self ._input_file , self ._output_file , append = append
232
+ )
233
+
234
+ # open files in write/append mode
230
235
input_file = open (self ._input_file , open_mode , encoding = "utf-8" )
231
236
output_file = open (self ._output_file , open_mode , encoding = "utf-8" )
232
237
error_file = open (self ._error_file , open_mode , encoding = "utf-8" )
@@ -237,8 +242,8 @@ def __run_in_serial(self, append, light_mode):
237
242
)
238
243
error_file = open (self ._error_file , open_mode , encoding = "utf-8" )
239
244
240
- idx_i = self .__get_initial_sim_idx (input_file )
241
- idx_o = self .__get_initial_sim_idx (output_file )
245
+ idx_i = self .__get_initial_sim_idx (input_file , light_mode = light_mode )
246
+ idx_o = self .__get_initial_sim_idx (output_file , light_mode = light_mode )
242
247
243
248
if idx_i != idx_o :
244
249
raise ValueError (
@@ -303,14 +308,6 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
303
308
inputs_lock = manager .Lock ()
304
309
outputs_lock = manager .Lock ()
305
310
errors_lock = manager .Lock ()
306
- queue = manager .JoinableQueue ()
307
-
308
- # Initialize queue
309
- for _ in range (self .number_of_simulations ):
310
- queue .put ("RUN" )
311
-
312
- for _ in range (n_workers ):
313
- queue .put ("STOP" )
314
311
315
312
# Initialize write file
316
313
open_mode = "a" if append else "w"
@@ -324,10 +321,15 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
324
321
325
322
# Initialize files
326
323
if light_mode :
327
- with open (self ._input_file , mode = open_mode ) as _ :
328
- pass # initialize file
329
- with open (self ._output_file , mode = open_mode ) as _ :
330
- pass # initialize file
324
+ # get initial simulation index
325
+ idx_i , idx_o = self .__get_light_indexes (
326
+ self ._input_file , self ._output_file , append = append
327
+ )
328
+ # open files in write/append mode
329
+ with open (self ._input_file , mode = open_mode ) as f :
330
+ pass # initialize file
331
+ with open (self ._output_file , mode = open_mode ) as f :
332
+ pass # initialize file
331
333
332
334
else :
333
335
# Change file extensions to .h5
@@ -337,9 +339,9 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
337
339
338
340
# Initialize files and get initial simulation index
339
341
with h5py .File (file_paths ["input_file" ], open_mode ) as f :
340
- idx_i = self .__get_initial_sim_idx (f )
342
+ idx_i = self .__get_initial_sim_idx (f , light_mode = light_mode )
341
343
with h5py .File (file_paths ["output_file" ], open_mode ) as f :
342
- idx_o = self .__get_initial_sim_idx (f )
344
+ idx_o = self .__get_initial_sim_idx (f , light_mode = light_mode )
343
345
344
346
if idx_i != idx_o :
345
347
raise ValueError (
@@ -352,6 +354,15 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
352
354
353
355
# Initialize simulation counter
354
356
sim_counter = manager .SimCounter (idx_i )
357
+
358
+ queue = manager .JoinableQueue ()
359
+
360
+ # Initialize queue
361
+ for _ in range (self .number_of_simulations - sim_counter .get_count ()):
362
+ queue .put ("RUN" )
363
+
364
+ for _ in range (n_workers ):
365
+ queue .put ("STOP" )
355
366
356
367
print ("\n Starting monte carlo analysis" , end = "\r " )
357
368
print (f"Number of simulations: { self .number_of_simulations } " )
@@ -1065,7 +1076,7 @@ def inspect_object_attributes(obj):
1065
1076
return result
1066
1077
1067
1078
@staticmethod
1068
- def __get_initial_sim_idx (file ):
1079
+ def __get_initial_sim_idx (file , light_mode ):
1069
1080
"""
1070
1081
Get the initial simulation index from the filename.
1071
1082
@@ -1079,12 +1090,51 @@ def __get_initial_sim_idx(file):
1079
1090
int
1080
1091
Initial simulation index.
1081
1092
"""
1093
+ if light_mode :
1094
+ lines = file .readlines ()
1095
+ return len (lines )
1096
+
1082
1097
if len (file .keys ()) == 0 :
1083
- return 0
1098
+ return 0 # light mode not using the simulation index
1084
1099
1085
1100
keys = [int (key ) for key in file .keys ()]
1086
1101
return max (keys ) + 1
1087
1102
1103
+ @staticmethod
1104
+ def __get_light_indexes (input_file , output_file , append ):
1105
+ """
1106
+ Get the initial simulation index from the filename.
1107
+
1108
+ Parameters
1109
+ ----------
1110
+ input_file : str
1111
+ Name of the input file to be analyzed.
1112
+ output_file : str
1113
+ Name of the output file to be analyzed.
1114
+ append : bool
1115
+ If True, the results will be appended to the existing files. If
1116
+ False, the files will be overwritten.
1117
+
1118
+ Returns
1119
+ -------
1120
+ int
1121
+ Initial simulation index.
1122
+ """
1123
+ if append :
1124
+ try :
1125
+ with open (input_file , 'r' , encoding = "utf-8" ) as f :
1126
+ idx_i = MonteCarlo .__get_initial_sim_idx (f , light_mode = True )
1127
+ with open (output_file , 'r' , encoding = "utf-8" ) as f :
1128
+ idx_o = MonteCarlo .__get_initial_sim_idx (f , light_mode = True )
1129
+ except OSError : # File not found, return 0
1130
+ idx_i = 0
1131
+ idx_o = 0
1132
+ else :
1133
+ idx_i = 0
1134
+ idx_o = 0
1135
+
1136
+ return idx_i , idx_o
1137
+
1088
1138
@staticmethod
1089
1139
def __dict_to_h5 (h5_file , path , dic ):
1090
1140
"""
0 commit comments