Hi,
I major in astronomy and using your package for spectral fitting. Thans for you excellent works.
I recently try to use multi-processing to accelerate the fitting calculation but encountered some problem. I have narrowed down the problem to this script. It is clear that the problem will only raise when I try to fit with CompositeModel.
Please see the attachment for the demo.
While perform the fitting with emcee, the code works fine for simple model under any number of workers. It also compatible for CompositeModel with workers=1. The Error raise for workers>2 as follow:
----------------------------------------------------------------------------------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/wenke/data/20230803DESI/20250306CaseStudy/code/Bug_Explore.py", line 57, in <module>
fit_result = combined_model.fit(
^^^^^^^^^^^^^^^^^^^
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/site-packages/lmfit/model.py", line 1177, in fit
output.fit(data=data, weights=weights)
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/site-packages/lmfit/model.py", line 1570, in fit
_ret = self.minimize(method=self.method, params=self.init_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/site-packages/lmfit/minimizer.py", line 2330, in minimize
return function(**kwargs)
^^^^^^^^^^^^^^^^^^
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/site-packages/lmfit/minimizer.py", line 1405, in emcee
output = self.sampler.run_mcmc(p0, steps, progress=progress, **run_mcmc_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/site-packages/emcee/ensemble.py", line 443, in run_mcmc
for results in self.sample(initial_state, iterations=nsteps, **kwargs):
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/site-packages/emcee/ensemble.py", line 344, in sample
state.log_prob, state.blobs = self.compute_log_prob(state.coords)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/site-packages/emcee/ensemble.py", line 489, in compute_log_prob
results = list(map_func(self.log_prob_fn, p))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/multiprocessing/pool.py", line 367, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/multiprocessing/pool.py", line 774, in get
raise self._value
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/multiprocessing/pool.py", line 540, in _handle_tasks
put(task)
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/multiprocessing/connection.py", line 205, in send
self._send_bytes(_ForkingPickler.dumps(obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wenke/opt/anaconda3/envs/DESI/lib/python3.11/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
AttributeError: Can't pickle local object 'CompositeModel.__init__.<locals>._tmp'
----------------------------------------------------------------------------------------------------------------------------------------------
I quickly went through the conversations here and searched relavent key words in your documents. I didn't notice any relavent discussion or warning. Please let me know if this is a known problem and is there any hope to fix this.
Best wishes,
Wenke