@@ -475,7 +475,8 @@ def setup(self,
475475 scan : Scan ,
476476 xgm : XGM ,
477477 tof_response : Dict [int , TOFAnalogResponse ]= None ,
478- parallel = None
478+ parallel = False ,
479+ parallel_over_tofs = None ,
479480 ):
480481 """
481482 Derive calibrations.
@@ -493,6 +494,7 @@ def setup(self,
493494 For example: `XGM(run, "SQS_DIAG1_XGMD/XGM/DOOCS")`
494495 tof_response: The response function object for deconvolution if that is desired.
495496 parallel: Whether to paralellize data reading.
497+ parallel_over_tofs: Whether to parallelize over eTOFs.
496498 """
497499 # base properties
498500 self ._run = run
@@ -524,7 +526,7 @@ def setup(self,
524526 self .update_metadata ()
525527
526528 # find RoI if needed
527- self .update_roi (parallel )
529+ self .update_roi (parallel , parallel_over_tofs )
528530
529531 # find where the peaks are per energy in each Tof
530532 self .update_fit_result ()
@@ -688,7 +690,7 @@ def update_tof_settings(self):
688690 self .kwargs_adq [tof_id ]["name" ] = self ._tof [tof_id ].name
689691 self .mask = {tof_id : True for tof_id in self .kwargs_adq .keys ()}
690692
691- def update_roi (self , parallel = None ):
693+ def update_roi (self , parallel = False , parallel_over_tofs = 16 ):
692694 """
693695 Given calibrated data, apply a selection and find RoI if needed.
694696
@@ -697,7 +699,7 @@ def update_roi(self, parallel=None):
697699 """
698700 # average data for each energy slice
699701 logging .info ("Reading calibration data ... (this takes a while)" )
700- self .select_calibration_data (parallel )
702+ self .select_calibration_data (parallel , parallel_over_tofs = parallel_over_tofs )
701703 # find RoI if needed
702704 for tof_id in self .kwargs_adq .keys ():
703705 if (self .auger_start_roi [tof_id ] is None
@@ -728,7 +730,7 @@ def update_calibration(self):
728730 def fast_response_correction (self , x , tof_id ):
729731 return self ._tof_response [tof_id ].apply (x .fillna (0.0 ), method = "nn_matrix" , n_iter = 100 , nonneg = True )
730732
731- def select_calibration_data (self , parallel = None ):
733+ def select_calibration_data (self , parallel = False , parallel_over_tofs = None ):
732734 """
733735 Select data for calibration.
734736 """
@@ -751,15 +753,27 @@ def select_calibration_data(self, parallel=None):
751753 correction_fn = correction_fn ,
752754 parallel = parallel ,
753755 )
754- itr_gen = list (itertools .product (tof_ids , energy_ids ))
755- data_gen = map (fn , itr_gen )
756- # organize it all in a numpy array
757- for (d , x ), (tof_id , energy_id ) in zip (data_gen , itr_gen ):
758- data [tof_id ] += [d ]
759- mean_xgm [tof_id ] += [x ]
760- for tof_id in tof_ids :
761- data [tof_id ] = np .stack (data [tof_id ], axis = 0 )
762- mean_xgm [tof_id ] = np .stack (mean_xgm [tof_id ], axis = 0 )
756+ if parallel_over_tofs is not None :
757+ with ProcessPoolExecutor (max_workers = parallel_over_tofs ) as p :
758+ itr_gen = list (itertools .product (tof_ids , energy_ids ))
759+ data_gen = p .map (fn , itr_gen )
760+ # organize it all in a numpy array
761+ for (d , x ), (tof_id , energy_id ) in zip (data_gen , itr_gen ):
762+ data [tof_id ] += [d ]
763+ mean_xgm [tof_id ] += [x ]
764+ for tof_id in tof_ids :
765+ data [tof_id ] = np .stack (data [tof_id ], axis = 0 )
766+ mean_xgm [tof_id ] = np .stack (mean_xgm [tof_id ], axis = 0 )
767+ else :
768+ itr_gen = list (itertools .product (tof_ids , energy_ids ))
769+ data_gen = map (fn , itr_gen )
770+ # organize it all in a numpy array
771+ for (d , x ), (tof_id , energy_id ) in zip (data_gen , itr_gen ):
772+ data [tof_id ] += [d ]
773+ mean_xgm [tof_id ] += [x ]
774+ for tof_id in tof_ids :
775+ data [tof_id ] = np .stack (data [tof_id ], axis = 0 )
776+ mean_xgm [tof_id ] = np .stack (mean_xgm [tof_id ], axis = 0 )
763777
764778 self .calibration_data = data
765779 self .calibration_mean_xgm = mean_xgm
0 commit comments