44
55import numpy as np
66import pandas as pd
7- import warnings , scipy , multiprocessing
7+ import warnings , scipy
88
99if not hasattr (np , 'trapezoid' ): # numpy < 2.0
1010 np .trapezoid = np .trapz
1818
1919from corems .chroma_peak .factory .chroma_peak_classes import LCMSMassFeature
2020from corems .encapsulation .plot_utils import _finalize_plot
21+ from corems .encapsulation .spawn_pool import spawn_pool
2122from corems .mass_spectra .calc import SignalProcessing as sp
2223from corems .mass_spectra .factory .chromat_data import EIC_Data
2324from corems .mass_spectrum .input .numpyArray import ms_from_array_profile
@@ -4478,16 +4479,13 @@ def reload_representative_mass_features(self, add_ms2=False, auto_process_ms2=Tr
44784479 ms2_scan_filter = ms2_scan_filter )
44794480 else :
44804481 # Parallel processing
4481- import multiprocessing
44824482 from tqdm import tqdm
44834483
44844484 if self .parameters .lcms_collection .cores > len (sample_mf_map ):
44854485 ncores = len (sample_mf_map )
44864486 else :
44874487 ncores = self .parameters .lcms_collection .cores
44884488
4489- pool = multiprocessing .Pool (ncores )
4490-
44914489 # Build arguments list for starmap
44924490 args_list = [
44934491 (sample_id , sample_mf_map [sample_id ], add_ms2 , auto_process_ms2 ,
@@ -4496,9 +4494,8 @@ def reload_representative_mass_features(self, add_ms2=False, auto_process_ms2=Tr
44964494 ]
44974495
44984496 # Execute in parallel
4499- mp_result = pool .starmap (self ._reload_sample_mass_features , args_list )
4500- pool .close ()
4501- pool .join ()
4497+ with spawn_pool (ncores ) as pool :
4498+ mp_result = pool .starmap (self ._reload_sample_mass_features , args_list )
45024499
45034500 # Collect results back into samples
45044501 for i , sample_id in enumerate (tqdm (sample_mf_map .keys (), desc = "Collecting reloaded mass features" , unit = "sample" )):
@@ -5412,11 +5409,11 @@ def fill_missing_cluster_features(self):
54125409 ncores = len (self )
54135410 else :
54145411 ncores = self .parameters .lcms_collection .cores
5415- pool = multiprocessing . Pool (ncores )
5416- mp_result = pool .starmap (
5417- self ._search_for_targeted_mass_features_in_sample ,
5418- [(x , missingdf , cluster_dict , expand_on_miss , False ) for x in range (sample_ct )]
5419- )
5412+ with spawn_pool (ncores ) as pool :
5413+ mp_result = pool .starmap (
5414+ self ._search_for_targeted_mass_features_in_sample ,
5415+ [(x , missingdf , cluster_dict , expand_on_miss , False ) for x in range (sample_ct )]
5416+ )
54205417
54215418 for i in tqdm (range (sample_ct ), desc = "Collecting gap-filled features" , unit = "sample" ):
54225419 self [i ].induced_mass_features = mp_result [i ]
@@ -5536,15 +5533,11 @@ def process_samples_pipeline(self, operations, description=None, keep_raw_data=F
55365533 results_by_operation [op_name ][sample_id ] = result
55375534 else :
55385535 # Parallel processing
5539- import multiprocessing
5540-
55415536 if self .parameters .lcms_collection .cores > sample_ct :
55425537 ncores = sample_ct
55435538 else :
55445539 ncores = self .parameters .lcms_collection .cores
55455540
5546- pool = multiprocessing .Pool (ncores )
5547-
55485541 # Build arguments for each sample
55495542 args_list = [
55505543 (sample_id , operations , runtime_params , False )
@@ -5554,45 +5547,43 @@ def process_samples_pipeline(self, operations, description=None, keep_raw_data=F
55545547 # Execute in parallel with progress tracking
55555548 results_by_operation = {op .name : {} for op in operations }
55565549
5557- if show_progress :
5558- from tqdm import tqdm
5559- import time
5560-
5561- # Use starmap_async for parallel execution with progress tracking
5562- async_result = pool .starmap_async (self ._execute_sample_pipeline , args_list )
5563-
5564- # Poll for completion and update progress bar
5565- print (description )
5566- pbar = tqdm (
5567- total = sample_ct ,
5568- desc = "" ,
5569- unit = "sample" ,
5570- position = 0 ,
5571- leave = True ,
5572- dynamic_ncols = True
5573- )
5574- prev_completed = 0
5575- while not async_result .ready ():
5576- # Get number of completed tasks by checking remaining
5577- completed = sample_ct - async_result ._number_left
5578- if completed > prev_completed :
5579- pbar .update (completed - prev_completed )
5580- prev_completed = completed
5581- time .sleep (0.5 ) # Poll every 500ms to avoid spam
5582-
5583- # Final update to 100%
5584- if prev_completed < sample_ct :
5585- pbar .update (sample_ct - prev_completed )
5586- pbar .close ()
5587-
5588- # Get all results
5589- mp_results = async_result .get ()
5590- else :
5591- # Execute without progress
5592- mp_results = pool .starmap (self ._execute_sample_pipeline , args_list )
5593-
5594- pool .close ()
5595- pool .join ()
5550+ with spawn_pool (ncores ) as pool :
5551+ if show_progress :
5552+ from tqdm import tqdm
5553+ import time
5554+
5555+ # Use starmap_async for parallel execution with progress tracking
5556+ async_result = pool .starmap_async (self ._execute_sample_pipeline , args_list )
5557+
5558+ # Poll for completion and update progress bar
5559+ print (description )
5560+ pbar = tqdm (
5561+ total = sample_ct ,
5562+ desc = "" ,
5563+ unit = "sample" ,
5564+ position = 0 ,
5565+ leave = True ,
5566+ dynamic_ncols = True
5567+ )
5568+ prev_completed = 0
5569+ while not async_result .ready ():
5570+ # Get number of completed tasks by checking remaining
5571+ completed = sample_ct - async_result ._number_left
5572+ if completed > prev_completed :
5573+ pbar .update (completed - prev_completed )
5574+ prev_completed = completed
5575+ time .sleep (0.5 ) # Poll every 500ms to avoid spam
5576+
5577+ # Final update to 100%
5578+ if prev_completed < sample_ct :
5579+ pbar .update (sample_ct - prev_completed )
5580+ pbar .close ()
5581+
5582+ # Get all results
5583+ mp_results = async_result .get ()
5584+ else :
5585+ # Execute without progress
5586+ mp_results = pool .starmap (self ._execute_sample_pipeline , args_list )
55965587
55975588 # Collect results back into collection
55985589 for result in mp_results :
0 commit comments