Skip to content

Commit

Permalink
Use lazy imports for dask and pandas (#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgrote authored May 23, 2023
1 parent 0687580 commit f875c10
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
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

0 comments on commit f875c10

Please sign in to comment.