kearnz / autoimpute

Python package for Imputation Methods
MIT License
237 stars 19 forks source link

SingleImputer just hangs on dataset. #88

Closed svaningelgem closed 1 year ago

svaningelgem commented 1 year ago

I was trying the latest commit from git (6ef8266) to auto-impute a dataset, but it just hangs (with no CPU/GPU usage).

From the docs:

import pandas as pd
from autoimpute.imputations import SingleImputer

data = pd.read_csv('data.csv').set_index('id')
data_imputed_once = SingleImputer().fit_transform(data)

The only thing that is shown is this:

Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [alpha, beta, σ]

And then... Nothing...

My dataset is this one: data.zip

data.isna().sum()

name     5024
gem      5500
MPa      4452
mf       4791
x_e     10415
D_e      5488
D_h      4589
len      4759
ce          0

Versions:

svaningelgem commented 1 year ago

When I run it in a notebook, the cell keeps running forever. When I run it in a script, I get a RuntimeError:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "[..]\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "[..]\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "[..]\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "[..]\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "[..]\lib\runpy.py", line 289, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "[..]\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "[..]\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\Steven.DESKTOP-AGV9U99\AppData\Roaming\JetBrains\IntelliJIdea2023.1\scratches\scratch_3.py", line 5, in <module>
    data_imputed_once = SingleImputer().fit_transform(data)
  File "[..]\lib\site-packages\sklearn\utils\_set_output.py", line 140, in wrapped
    data_to_wrap = f(self, X, *args, **kwargs)
  File "[..]\lib\site-packages\autoimpute\imputations\dataframe\single_imputer.py", line 316, in fit_transform
    return self.fit(X, y).transform(X, **trans_kwargs)
  File "[..]\lib\site-packages\sklearn\utils\_set_output.py", line 140, in wrapped
    data_to_wrap = f(self, X, *args, **kwargs)
  File "[..]\lib\site-packages\autoimpute\utils\checks.py", line 61, in wrapper
    return func(d, *args, **kwargs)
  File "[..]\lib\site-packages\autoimpute\utils\checks.py", line 126, in wrapper
    return func(d, *args, **kwargs)
  File "[..]\lib\site-packages\autoimpute\utils\checks.py", line 173, in wrapper
    return func(d, *args, **kwargs)
  File "[..]\lib\site-packages\autoimpute\imputations\dataframe\single_imputer.py", line 301, in transform
    X.loc[imp_ix, column] = imputer.impute(x_)
  File "[..]\lib\site-packages\autoimpute\imputations\series\default.py", line 400, in impute
    X_ = super().impute(X)
  File "[..]\lib\site-packages\autoimpute\imputations\series\default.py", line 214, in impute
    X_ = imp.impute(X)
  File "[..]\lib\site-packages\autoimpute\imputations\series\pmm.py", line 165, in impute
    tr = pm.sample(
  File "[..]\lib\site-packages\pymc\sampling\mcmc.py", line 747, in sample
    _mp_sample(**sample_args, **parallel_args)
  File "[..]\lib\site-packages\pymc\sampling\mcmc.py", line 1122, in _mp_sample
    sampler = ps.ParallelSampler(
  File "[..]\lib\site-packages\pymc\sampling\parallel.py", line 402, in __init__
    self._samplers = [
  File "[..]\lib\site-packages\pymc\sampling\parallel.py", line 403, in <listcomp>
    ProcessAdapter(
  File "[..]\lib\site-packages\pymc\sampling\parallel.py", line 259, in __init__
    self._process.start()
  File "[..]\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "[..]\lib\multiprocessing\context.py", line 336, in _Popen
    return Popen(process_obj)
  File "[..]\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "[..]\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "[..]\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

Maybe this runtime error is hidden within the notebook, I don't know. But still

svaningelgem commented 1 year ago

I think it's related to this one issue: https://github.com/pymc-devs/pymc/issues/3403#issuecomment-471546082

In imputations/series/pmm.py on line 168, I added now this:

                cores=1 if sys.platform == 'win32' else None,

Will update with the results later on.

kearnz commented 1 year ago

Hi @svaningelgem,

As you discovered - using pymc on Windows has it's own special considerations. Because autoimpute heavily relies on pymc for sampling procedures, the issues on Windows extend to autoimpute too. I wrote a little bit about it in the autoimpute README here.

Unfortunately I have not had much time to test the platform on Windows, and I don't see myself having that bandwidth in the near future. If you're interested in collaborating (or know someone who is), please let me know!

I'll leave this issue open for another day before closing it.

svaningelgem commented 1 year ago

Thanks for your reaction @kearnz . As mentioned above, if you add that line of code in the pmm file everything works on Windows. You'll not get parallelism, but at least it works.