From fcd09ca1763f4344dffcb63f289f780ac92ae247 Mon Sep 17 00:00:00 2001 From: "Adam M. Krajewski" <54290107+amkrajewski@users.noreply.github.com> Date: Sat, 17 Feb 2024 20:02:41 +0000 Subject: [PATCH] (ME) distributed class-specific imports to limit displaying of the extra-dependency requirement to only when needed --- pysipfenn/core/modelExporters.py | 33 +++++++++++++------ .../KS2022_randomSolutions.py | 2 +- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pysipfenn/core/modelExporters.py b/pysipfenn/core/modelExporters.py index c1feb10..fa90f2a 100644 --- a/pysipfenn/core/modelExporters.py +++ b/pysipfenn/core/modelExporters.py @@ -4,18 +4,12 @@ import io from tqdm import tqdm -try: - import coremltools as ct - from onnxconverter_common import float16 - from onnxsim import simplify -except ModuleNotFoundError as e: - print('Note: Export Dependencies are not installed by default. If you need them, you have to install pySIPFENN in ' - '"dev" mode like: pip install -e "pysipfenn[dev]", or like pip install -e ".[dev]" (see pysipfenn.org)') - - class ONNXExporter: """Export models to the ONNX format (what they ship in by default) to allow (1) exporting modified pySIPFENN models, (2) simplify the models using ONNX optimizer, and (3) convert them to `FP16` precision, cutting the size in half. + + Note: Some of the dependencies (``onnxconverter_common`` and ``onnxsim``) are not installed by default. If you need them, + you have to install pySIPFENN in `dev` mode like: ``pip install "pysipfenn[dev]"``, or like ``pip install -e ".[dev]"``. Args: calculator: A ``Calculator`` object with loaded models that has loaded PyTorch models (happens automatically @@ -28,9 +22,16 @@ class ONNXExporter: simplifiedDict: A boolean dictionary of models that have been simplified. fp16Dict: A boolean dictionary of models that have been converted to FP16. """ - + def __init__(self, calculator: Calculator): """Initialize the ``ONNXExporter`` using a calculator object.""" + try: + from onnxconverter_common import float16 + from onnxsim import simplify + except ModuleNotFoundError as e: + raise Exception(str(e) + '\n\nNote: Export Dependencies are not installed by default. If you need them, you have to install ' + 'pySIPFENN in `dev` mode like: `pip install "pysipfenn[dev]"`, or like `pip install -e ".[dev]"` (see pysipfenn.org)') + self.simplifiedDict = {model: False for model in calculator.loadedModels.keys()} self.fp16Dict = {model: False for model in calculator.loadedModels.keys()} self.calculator = calculator @@ -167,6 +168,7 @@ class TorchExporter: Attributes: calculator: A ``Calculator`` object with loaded models. """ + def __init__(self, calculator: Calculator): """Initialize the TorchExporter with a calculator object that has loaded models.""" self.calculator = calculator @@ -225,6 +227,9 @@ class CoreMLExporter: """Export models to the ``CoreML`` format to allow for easy loading and inference in ``CoreML`` in other projects, particularly valuable for Apple devices, as pySIPFENN models can be run using the Neural Engine accelerator with minimal power consumption and neat optimizations. + + Note: Some of the dependencies (``coremltools``) are not installed by default. If you need them, + you have to install pySIPFENN in `dev` mode like: ``pip install "pysipfenn[dev]"``, or like ``pip install -e ".[dev]"``. Args: calculator: A ``Calculator`` object with loaded models. @@ -232,7 +237,15 @@ class CoreMLExporter: Attributes: calculator: A ``Calculator`` object with loaded models. """ + def __init__(self, calculator: Calculator): + try: + import coremltools as ct + except ModuleNotFoundError as e: + raise Exception(str(e) + '\n\nNote: Export Dependencies are not installed by default. If you need them, you have to install ' + 'pySIPFENN in `dev` mode like: `pip install "pysipfenn[dev]"`, or like `pip install -e ".[dev]"` (see pysipfenn.org)') + + self.calculator = calculator assert len(self.calculator.loadedModels)>0, 'No models loaded in calculator. Nothing to export.' print(f'Initialized CoreMLExporter with models: {list(self.calculator.loadedModels.keys())}') diff --git a/pysipfenn/descriptorDefinitions/KS2022_randomSolutions.py b/pysipfenn/descriptorDefinitions/KS2022_randomSolutions.py index d60662e..53b6d5a 100755 --- a/pysipfenn/descriptorDefinitions/KS2022_randomSolutions.py +++ b/pysipfenn/descriptorDefinitions/KS2022_randomSolutions.py @@ -410,7 +410,7 @@ def generate_descriptor(struct: Structure, diffArray = np.array(diffHistory) # Plot the parameters as lines. Add hover text to show the parameter name based on the labels_KS2022.csv file. - with resources.files('pysipfenn').joinpath('descriptorDefinitions/labels_KS2022.csv').open() as f: + with resources.files('pysipfenn.descriptorDefinitions').joinpath('labels_KS2022.csv').open() as f: labels = f.readlines() fig = px.line(pd.DataFrame(diffArray, columns=labels), title='KS2022 Descriptor Parameters', range_y=[-0.5, 0.5])