theislab / scvelo

RNA Velocity generalized through dynamical modeling
https://scvelo.org
BSD 3-Clause "New" or "Revised" License
408 stars 103 forks source link

recover_dynamics opens a new thread #1188

Closed AlessiaLeclercq closed 6 months ago

AlessiaLeclercq commented 6 months ago

Hello, I'm trying to use scVelo to compute velocities for genes. I'm using windows 11 to run the following python code on Visual Studio

import argparse
import scanpy as sc
import scvelo as scv

seed = 52
parser= argparse.ArgumentParser()
parser.add_argument('-k', type=int, help='Integer describing the size of the local neighborhood')
parser.add_argument('-pc', type=int, help='Integer describing the number of principal components')
parser.add_argument('-res', type=float, help='Float indicating the resolution of the Louvain community detection algorithm')
args = parser.parse_args()
print(args.k, args.pc, args.res)

CWD = os.getcwd()
DATA_FOLDER = os.path.join(CWD, "scRNA_adata")
DATA_PATH = os.path.join(DATA_FOLDER, '10xMouse_loom_preprocessed.h5ad')

adata = sc.read_h5ad(DATA_PATH)

scv.pp.moments(adata, n_neighbors=args.k, n_pcs=args.pc)
sc.pp.neighbors(adata, n_neighbors=args.k, n_pcs=args.pc, random_state = seed)
sc.tl.louvain(adata, resolution=args.res, random_state=seed)
sc.tl.umap(adata, random_state=seed)

# Recovers full splicing kinetics 
scv.tl.recover_dynamics(adata, n_jobs=-1, backend='threading')
print("STWMPW")
scv.tl.velocity(adata, mode='dynamical')
scv.tl.velocity_graph(adata, n_jobs=-1)

title = f"Dynamical model K={args.k} PC={args.pc} resolution={args.res}"
path= f"dynamicalModel_{args.k}K{args.pc}PC{args.res}resolution.png"
scv.pl.velocity_embedding_stream(adata, basis='umap', color=['louvain', 'celltype'], title=title, save= path)

# Compute latent time (cell's internal clock)
title = f"Cell specific latent time K={args.k} PC={args.pc} resolution={args.res}"
path= f"latentTime_{args.k}K{args.pc}PC{args.res}resolution.png"
scv.tl.latent_time(adata)
scv.pl.scatter(adata, color='latent_time', color_map='gnuplot', size=40, save=path, title=title)

# Saving file
path = os.path.join(DATA_FOLDER, f"scVeloDynamical_{args.k}K{args.pc}PC{args.res}res.h5ad")
adata.write_h5ad(path)

This is the output I'm getting: scvelo From the picture one can see that after recover_dynamics is called, a new thread is created and runs the same script from the beginning (it outputs the args parameters a second time, recomputes neighbors and moments and calls recover_dynamics - highlighted). Then, at the second call, I get the following error:

recovering dynamics (using 8/8 cores)
  0%|          | 0/1614 [00:00<?, ?gene/s]
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\aless\Desktop\scVEMO\10XMouse_scVelo_velocities.py", line 26, in <module>
    scv.tl.recover_dynamics(adata, n_jobs=-1, backend='threading')
  File "C:\Users\aless\Desktop\scVEMO\lib\site-packages\scvelo\tools\_em_model_core.py", line 553, in recover_dynamics
    res = parallelize(
  File "C:\Users\aless\Desktop\scVEMO\lib\site-packages\scvelo\core\_parallelize.py", line 121, in wrapper
    queue = Manager().Queue()
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py", line 57, in Manager
    m.start()
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\managers.py", line 553, in start
    self._process.start()
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\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.
Traceback (most recent call last):

    queue = Manager().Queue()
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py", line 57, in Manager
    m.start()
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\managers.py", line 557, in start
    self._address = reader.recv()
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\connection.py", line 255, in recv
    buf = self._recv_bytes()
  File "C:\Users\aless\AppData\Local\Programs\Python\Python39\lib\multiprocessing\connection.py", line 310, in _recv_bytes
    waitres = _winapi.WaitForMultipleObjects(

I'm using the following packages:

aiohttp==3.9.0
aiosignal==1.3.1
anndata==0.10.3
annotated-types==0.6.0
ansicon==1.89.0
anyio==3.7.1
array-api-compat==1.4
arrow==1.3.0
asttokens==2.4.1
async-timeout==4.0.3
attrs==23.1.0
backoff==2.2.1
bamnostic==1.1.10
beautifulsoup4==4.12.2
blessed==1.20.0
boto3==1.29.3
botocore==1.32.3
cellrank==2.0.1
certifi==2023.11.17
cffi==1.16.0
charset-normalizer==3.3.2
chex==0.1.7
click==8.1.7
colorama==0.4.6
comm==0.2.0
contextlib2==21.6.0
contourpy==1.2.0
croniter==1.4.1
cycler==0.12.1
dateutils==0.6.12
debugpy==1.8.0
decorator==5.1.1
deepdiff==6.7.1
dm-tree==0.1.8
docrep==0.3.2
episcanpy==0.4.0+20.g0e845ef
etils==1.5.2
exceptiongroup==1.1.3
executing==2.0.1
fastapi==0.104.1
filelock==3.13.1
flax==0.7.5
fonttools==4.45.0
frozenlist==1.4.0
fsspec==2023.10.0
get-annotations==0.1.2
h11==0.14.0
h5py==3.10.0
idna==3.4
igraph==0.10.8
importlib-metadata==6.8.0
importlib-resources==6.1.1
inquirer==3.1.3
intervaltree==3.1.0
ipykernel==6.26.0
ipython==8.17.2
ipywidgets==8.1.2
itsdangerous==2.1.2
jax==0.4.20
jaxlib==0.4.20
jedi==0.19.1
Jinja2==3.0.3
jinxed==1.2.0
jmespath==1.0.1
joblib==1.3.2
jupyter_client==8.6.0
jupyter_core==5.5.0
jupyterlab_widgets==3.0.10
kiwisolver==1.4.5
kneed==0.8.5
legacy-api-wrap==1.4
leidenalg==0.10.1
lightning==2.0.9.post0
lightning-cloud==0.5.55
lightning-utilities==0.10.0
llvmlite==0.41.1
loompy==3.0.7
louvain==0.8.1
markdown-it-py==3.0.0
MarkupSafe==2.1.3
matplotlib==3.7.1
matplotlib-inline==0.1.6
mdurl==0.1.2
ml-collections==0.1.1
ml-dtypes==0.3.1
mpmath==1.3.0
msgpack==1.0.7
mudata==0.2.3
multidict==6.0.4
multipledispatch==1.0.0
natsort==8.4.0
nest-asyncio==1.5.8
networkx==3.2.1
numba==0.58.1
numpy==1.24.2
numpy-groupies==0.10.2
numpyro==0.13.2
opt-einsum==3.3.0
optax==0.1.7
orbax-checkpoint==0.4.3
ordered-set==4.1.0
packaging==23.2
pandas==1.5.3
parso==0.8.3
patsy==0.5.3
Pillow==10.1.0
platformdirs==4.0.0
progressbar2==4.2.0
prompt-toolkit==3.0.41
protobuf==4.25.1
psutil==5.9.6
pure-eval==0.2.2
pycparser==2.21
pydantic==2.1.1
pydantic_core==2.4.0
pygam==0.9.0
Pygments==2.17.1
pygpcca==1.0.4
PyJWT==2.8.0
pynndescent==0.5.10
pyparsing==3.1.1
pyro-api==0.1.2
pyro-ppl==1.8.6
python-dateutil==2.8.2
python-editor==1.0.4
python-multipart==0.0.6
python-utils==3.8.1
pytorch-lightning==2.1.2
pytz==2023.3.post1
pywin32==306
PyYAML==6.0.1
pyzmq==25.1.1
readchar==4.0.5
requests==2.31.0
rich==13.7.0
rpy2==3.5.14
s3transfer==0.7.0
scanpy==1.9.6
scikit-learn==1.1.3
scipy==1.11.4
scvelo @ git+https://github.com/theislab/scvelo@d89ca6aecbe93256fbcdd8a521fdee2b9f2a673a
scvi-tools==1.0.4
seaborn==0.12.2
session-info==1.0.0
setuptools-scm==8.0.4
six==1.16.0
sniffio==1.3.0
sortedcontainers==2.4.0
soupsieve==2.5
sparse==0.14.0
stack-data==0.6.3
starlette==0.32.0.post1
starsessions==1.3.0
statsmodels==0.14.0
stdlib-list==0.10.0
sympy==1.12
tbb==2021.11.0
tensorstore==0.1.50
texttable==1.7.0
threadpoolctl==3.2.0
tomli==2.0.1
toolz==0.12.0
torch==2.1.1
torchmetrics==1.2.0
tornado==6.3.3
tqdm==4.66.1
traitlets==5.13.0
types-python-dateutil==2.8.19.14
typing_extensions==4.8.0
tzdata==2023.3
tzlocal==5.2
umap-learn==0.5.5
urllib3==2.1.0
uvicorn==0.24.0.post1
wcwidth==0.2.10
websocket-client==1.6.4
websockets==12.0
widgetsnbextension==4.0.10
wrapt==1.16.0
xarray==2023.11.0
yarl==1.9.2
zipp==3.17.0

Any suggestion? I do not have the same issue when I run the same code on a jupyter notebook.

AlessiaLeclercq commented 6 months ago

Sorry. apparently it's a problem with my Windows.