From c3408efda30c4924ee7ae45f9311bb4bac808d13 Mon Sep 17 00:00:00 2001 From: Kirsi-Marja Zitting Date: Mon, 6 Jan 2025 21:01:49 -0500 Subject: [PATCH] Make the accelerometer dependency optional This dependency currently causes tricky conflicts. Importing it dynamically replaces the hard dependency with an optional one that only users with BBA files need to install. --- pyActigraphy/io/bba/bba.py | 24 ++++++++++++++++++++---- setup.py | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pyActigraphy/io/bba/bba.py b/pyActigraphy/io/bba/bba.py index c1a0b464..a6881ee1 100644 --- a/pyActigraphy/io/bba/bba.py +++ b/pyActigraphy/io/bba/bba.py @@ -1,11 +1,10 @@ +import importlib import json import os import pandas as pd import re from ..base import BaseRaw -from accelerometer.utils import date_parser -from accelerometer.summarisation import imputeMissing class RawBBA(BaseRaw): @@ -69,6 +68,18 @@ def __init__( use_metadata_json=True, metadata_fname=None ): + # The accelerometer package causes some dependency conflicts, + # so instead of always including it as a hard dependency, we + # try to import it as an optional one. Users with BBA files + # likely already have accelerometer installed, or should be + # able to sort out the dependency conflicts as needed. + try: + utils = importlib.import_module('accelerometer.utils') + except ModuleNotFoundError as e: + raise Exception( + 'Unable to load the required accelerometer.utils module.' + ' Install the package with "pip install accelerometer".' + ) from e # get absolute file path input_fname = os.path.abspath(input_fname) @@ -79,7 +90,7 @@ def __init__( engine=engine, index_col=['time'], parse_dates=['time'], - date_parser=date_parser + date_parser=utils.date_parser ) # read meta-data file (if found): @@ -139,7 +150,8 @@ def __init__( # Impute missing data (if required) if impute_missing: - data = imputeMissing(data) + s11n = importlib.import_module('accelerometer.summarization') + data = s11n.imputeMissing(data) # LIGHT self.__white_light = self.__extract_baa_data( @@ -343,6 +355,10 @@ def read_raw_bba( r"""Reader function for files produced by the biobankAccelerometerAnalysis package. + Note that the required 'accelerometer' package is not automatically + included as a dependency of pyActigraphy. You need to explicitly install + it with something like 'pip install accelerometer' to use this function. + Parameters ---------- input_fname: str diff --git a/setup.py b/setup.py index 34c0e067..0701c3a6 100644 --- a/setup.py +++ b/setup.py @@ -108,7 +108,7 @@ def find_version(*file_paths): install_requires=[ 'joblib', 'lmfit', 'pandas', 'plotly', 'numba', 'numpy', 'pyexcel', 'pyexcel-ods3', 'pyexcel-xlsx', 'scipy', 'spm1d', 'statsmodels>=0.10', - 'stochastic>=0.6.0', 'accelerometer>=6.2.2' + 'stochastic>=0.6.0' ], # Optional # Data files included in your packages that need to be installed.