Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lazy imports for dask and pandas #1442

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/binding/python/openpmd_api/DaskArray.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

import numpy as np

try:
from dask.array import from_array
found_dask = True
except ImportError:
found_dask = False


class DaskRecordComponent:
# shape, .ndim, .dtype and support numpy-style slicing
Expand Down Expand Up @@ -80,6 +74,13 @@ def record_component_to_daskarray(record_component):
are used internally to parallelize reading
dask.array : the (potentially distributed) array object created here
"""
# Import dask here for a lazy import
try:
from dask.array import from_array
found_dask = True
except ImportError:
found_dask = False

if not found_dask:
raise ImportError("dask NOT found. Install dask for Dask DataFrame "
"support.")
Expand Down
25 changes: 13 additions & 12 deletions src/binding/python/openpmd_api/DaskDataFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@
"""
import numpy as np

try:
import dask.dataframe as dd
from dask.delayed import delayed
found_dask = True
except ImportError:
found_dask = False
try:
import pandas # noqa
found_pandas = True
except ImportError:
found_pandas = False


def read_chunk_to_df(species, chunk):
stride = np.s_[chunk.offset[0]:chunk.offset[0]+chunk.extent[0]]
Expand Down Expand Up @@ -51,6 +39,19 @@ def particles_to_daskdataframe(particle_species):
are used internally to parallelize particle processing
dask.dataframe : the central dataframe object created here
"""
# import here for lazy imports
try:
import dask.dataframe as dd
from dask.delayed import delayed
found_dask = True
except ImportError:
found_dask = False
try:
import pandas # noqa
found_pandas = True
except ImportError:
found_pandas = False

if not found_dask:
raise ImportError("dask NOT found. Install dask for Dask DataFrame "
"support.")
Expand Down
13 changes: 7 additions & 6 deletions src/binding/python/openpmd_api/DataFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

import numpy as np

try:
import pandas as pd
found_pandas = True
except ImportError:
found_pandas = False


def particles_to_dataframe(particle_species, slice=None):
"""
Expand Down Expand Up @@ -46,6 +40,13 @@ def particles_to_dataframe(particle_species, slice=None):
are optimal arguments for the slice parameter
pandas.DataFrame : the central dataframe object created here
"""
# import pandas here for a lazy import
try:
import pandas as pd
found_pandas = True
except ImportError:
found_pandas = False

if not found_pandas:
raise ImportError("pandas NOT found. Install pandas for DataFrame "
"support.")
Expand Down