From 307f27dcf5aab391d5d2a457e1caddac25c0f973 Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Thu, 4 Jul 2024 09:33:21 -0700 Subject: [PATCH 01/10] lite refactor commits from dev --- py4DSTEM/__init__.py | 10 ++++++++-- py4DSTEM/braggvectors/diskdetection.py | 5 ++++- py4DSTEM/datacube/virtualimage.py | 5 +++-- py4DSTEM/io/filereaders/__init__.py | 5 ++++- py4DSTEM/io/importfile.py | 2 +- py4DSTEM/process/__init__.py | 10 ++++++++-- py4DSTEM/process/diffraction/crystal_viz.py | 4 +++- py4DSTEM/process/phase/__init__.py | 5 ++++- py4DSTEM/process/phase/utils.py | 2 +- py4DSTEM/process/polar/polar_analysis.py | 3 ++- py4DSTEM/process/polar/polar_peaks.py | 3 ++- py4DSTEM/utils/configuration_checker.py | 8 ++------ 12 files changed, 42 insertions(+), 20 deletions(-) diff --git a/py4DSTEM/__init__.py b/py4DSTEM/__init__.py index d5df63f5e..712095abd 100644 --- a/py4DSTEM/__init__.py +++ b/py4DSTEM/__init__.py @@ -52,7 +52,10 @@ BraggVectorMap, ) -from py4DSTEM.process import classification +try: + from py4DSTEM.process import classification +except (ImportError, ModuleNotFoundError): + pass # diffraction @@ -70,7 +73,10 @@ # strain from py4DSTEM.process.strain.strain import StrainMap -from py4DSTEM.process import wholepatternfit +try: + from py4DSTEM.process import wholepatternfit +except (ImportError, ModuleNotFoundError): + pass ### more submodules diff --git a/py4DSTEM/braggvectors/diskdetection.py b/py4DSTEM/braggvectors/diskdetection.py index 99818b75e..e329d193e 100644 --- a/py4DSTEM/braggvectors/diskdetection.py +++ b/py4DSTEM/braggvectors/diskdetection.py @@ -10,7 +10,10 @@ from py4DSTEM.datacube import DataCube from py4DSTEM.preprocess.utils import get_maxima_2D from py4DSTEM.process.utils.cross_correlate import get_cross_correlation_FT -from py4DSTEM.braggvectors.diskdetection_aiml import find_Bragg_disks_aiml +try: + from py4DSTEM.braggvectors.diskdetection_aiml import find_Bragg_disks_aiml +except (ImportError, ModuleNotFoundError): + pass def find_Bragg_disks( diff --git a/py4DSTEM/datacube/virtualimage.py b/py4DSTEM/datacube/virtualimage.py index 627223d23..d4fe15241 100644 --- a/py4DSTEM/datacube/virtualimage.py +++ b/py4DSTEM/datacube/virtualimage.py @@ -5,7 +5,6 @@ # for bragg virtual imaging methods, goto diskdetection.virtualimage.py import numpy as np -import dask.array as da from typing import Optional import inspect @@ -220,7 +219,9 @@ def get_virtual_image( virtual_image[rx, ry] = np.sum(self.data[rx, ry] * mask) # dask - if dask is True: + if dask: + import dask.array as da + # set up a generalized universal function for dask distribution def _apply_mask_dask(self, mask): virtual_image = np.sum( diff --git a/py4DSTEM/io/filereaders/__init__.py b/py4DSTEM/io/filereaders/__init__.py index b6f4eb0a2..eb5cd02e1 100644 --- a/py4DSTEM/io/filereaders/__init__.py +++ b/py4DSTEM/io/filereaders/__init__.py @@ -2,5 +2,8 @@ from py4DSTEM.io.filereaders.read_K2 import read_gatan_K2_bin from py4DSTEM.io.filereaders.empad import read_empad from py4DSTEM.io.filereaders.read_mib import load_mib -from py4DSTEM.io.filereaders.read_arina import read_arina +try: + from py4DSTEM.io.filereaders.read_arina import read_arina +except (ImportError, ModuleNotFoundError): + pass from py4DSTEM.io.filereaders.read_abTEM import read_abTEM diff --git a/py4DSTEM/io/importfile.py b/py4DSTEM/io/importfile.py index ff3d1c37c..73e402de0 100644 --- a/py4DSTEM/io/importfile.py +++ b/py4DSTEM/io/importfile.py @@ -7,7 +7,6 @@ from py4DSTEM.io.filereaders import ( load_mib, read_abTEM, - read_arina, read_dm, read_empad, read_gatan_K2_bin, @@ -90,6 +89,7 @@ def import_file( elif filetype == "mib": data = load_mib(filepath, mem=mem, binfactor=binfactor, **kwargs) elif filetype == "arina": + from py4DSTEM.io.filereaders import read_arina data = read_arina(filepath, mem=mem, binfactor=binfactor, **kwargs) elif filetype == "abTEM": data = read_abTEM(filepath, mem=mem, binfactor=binfactor, **kwargs) diff --git a/py4DSTEM/process/__init__.py b/py4DSTEM/process/__init__.py index 0509d181e..8acee4e3c 100644 --- a/py4DSTEM/process/__init__.py +++ b/py4DSTEM/process/__init__.py @@ -4,6 +4,12 @@ from py4DSTEM.process import phase from py4DSTEM.process import calibration from py4DSTEM.process import utils -from py4DSTEM.process import classification +try: + from py4DSTEM.process import classification +except (ImportError, ModuleNotFoundError): + pass from py4DSTEM.process import diffraction -from py4DSTEM.process import wholepatternfit +try: + from py4DSTEM.process import wholepatternfit +except (ImportError, ModuleNotFoundError): + pass diff --git a/py4DSTEM/process/diffraction/crystal_viz.py b/py4DSTEM/process/diffraction/crystal_viz.py index 47df2e6ca..d3e4b682a 100644 --- a/py4DSTEM/process/diffraction/crystal_viz.py +++ b/py4DSTEM/process/diffraction/crystal_viz.py @@ -8,7 +8,6 @@ from scipy.signal import medfilt from scipy.ndimage import gaussian_filter from scipy.ndimage import distance_transform_edt -from skimage.morphology import dilation, erosion import warnings import numpy as np @@ -1884,7 +1883,10 @@ def plot_clusters( for a0 in range(self.cluster_sizes.shape[0]): if self.cluster_sizes[a0] >= area_min: + if outline_grains: + from skimage.morphology import erosion + im_grain[:] = False im_grain[ self.cluster_inds[a0][0, :], diff --git a/py4DSTEM/process/phase/__init__.py b/py4DSTEM/process/phase/__init__.py index ecfeaa1d2..927e5a08a 100644 --- a/py4DSTEM/process/phase/__init__.py +++ b/py4DSTEM/process/phase/__init__.py @@ -11,6 +11,9 @@ from py4DSTEM.process.phase.parallax import Parallax from py4DSTEM.process.phase.ptychographic_tomography import PtychographicTomography from py4DSTEM.process.phase.singleslice_ptychography import SingleslicePtychography -from py4DSTEM.process.phase.parameter_optimize import OptimizationParameter, PtychographyOptimizer +try: + from py4DSTEM.process.phase.parameter_optimize import OptimizationParameter, PtychographyOptimizer +except (ImportError, ModuleNotFoundError): + pass # fmt: on diff --git a/py4DSTEM/process/phase/utils.py b/py4DSTEM/process/phase/utils.py index 5742ff7e7..2834e8c2e 100644 --- a/py4DSTEM/process/phase/utils.py +++ b/py4DSTEM/process/phase/utils.py @@ -22,7 +22,6 @@ def get_array_module(*args): from py4DSTEM.process.utils import get_CoM from py4DSTEM.process.utils.cross_correlate import align_and_shift_images from py4DSTEM.process.utils.utils import electron_wavelength_angstrom -from skimage.restoration import unwrap_phase # fmt: off @@ -1755,6 +1754,7 @@ def unwrap_phase_2d(array, weights=None, gauge=None, corner_centered=True, xp=np def unwrap_phase_2d_skimage(array, corner_centered=True, xp=np): + from skimage.restoration import unwrap_phase if xp is np: array = array.astype(np.float64) unwrapped_array = unwrap_phase(array, wrap_around=corner_centered).astype( diff --git a/py4DSTEM/process/polar/polar_analysis.py b/py4DSTEM/process/polar/polar_analysis.py index 78a95c24a..dbf16ff97 100644 --- a/py4DSTEM/process/polar/polar_analysis.py +++ b/py4DSTEM/process/polar/polar_analysis.py @@ -5,7 +5,6 @@ import matplotlib.pyplot as plt from scipy.optimize import curve_fit from scipy.ndimage import gaussian_filter -from sklearn.decomposition import PCA from emdfile import tqdmnd @@ -980,6 +979,8 @@ def background_pca( radial PCA component selected """ + from sklearn.decomposition import PCA + # PCA decomposition shape = self.radial_all.shape A = np.reshape(self.radial_all, (shape[0] * shape[1], shape[2])) diff --git a/py4DSTEM/process/polar/polar_peaks.py b/py4DSTEM/process/polar/polar_peaks.py index 535ae7143..6e18e753c 100644 --- a/py4DSTEM/process/polar/polar_peaks.py +++ b/py4DSTEM/process/polar/polar_peaks.py @@ -3,7 +3,6 @@ from scipy.ndimage import gaussian_filter, gaussian_filter1d from scipy.signal import peak_prominences -from skimage.feature import peak_local_max from scipy.optimize import curve_fit, leastsq import warnings @@ -105,6 +104,8 @@ def find_peaks_single_pattern( """ + from skimage.feature import peak_local_max + # if needed, generate mask from Bragg peaks if bragg_peaks is not None: mask_bragg = self._datacube.get_braggmask( diff --git a/py4DSTEM/utils/configuration_checker.py b/py4DSTEM/utils/configuration_checker.py index b50a21de2..6edf44ffc 100644 --- a/py4DSTEM/utils/configuration_checker.py +++ b/py4DSTEM/utils/configuration_checker.py @@ -7,10 +7,6 @@ # need a mapping of pypi/conda names to import names import_mapping_dict = { - "scikit-image": "skimage", - "scikit-learn": "sklearn", - "scikit-optimize": "skopt", - "mp-api": "mp_api", } @@ -88,7 +84,8 @@ def get_modules_dict(): # module_depenencies = get_modules_dict() -modules = get_modules_list() +# modules = get_modules_list() +modules = [] #### Class and Functions to Create Coloured Strings #### @@ -527,7 +524,6 @@ def print_no_extra_checks(m: str): # dict of extra check functions funcs_dict = { - "cupy": check_cupy_gpu, } From fdce7a920a1c33c1fcdd55f5d580bdde7d8c9de3 Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Thu, 4 Jul 2024 09:54:26 -0700 Subject: [PATCH 02/10] black --- py4DSTEM/braggvectors/diskdetection.py | 1 + py4DSTEM/io/filereaders/__init__.py | 1 + py4DSTEM/io/importfile.py | 1 + py4DSTEM/process/__init__.py | 2 ++ py4DSTEM/process/diffraction/crystal_viz.py | 4 ++-- py4DSTEM/process/phase/utils.py | 5 ++++- py4DSTEM/utils/configuration_checker.py | 6 ++---- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/py4DSTEM/braggvectors/diskdetection.py b/py4DSTEM/braggvectors/diskdetection.py index e329d193e..a66ff62ea 100644 --- a/py4DSTEM/braggvectors/diskdetection.py +++ b/py4DSTEM/braggvectors/diskdetection.py @@ -10,6 +10,7 @@ from py4DSTEM.datacube import DataCube from py4DSTEM.preprocess.utils import get_maxima_2D from py4DSTEM.process.utils.cross_correlate import get_cross_correlation_FT + try: from py4DSTEM.braggvectors.diskdetection_aiml import find_Bragg_disks_aiml except (ImportError, ModuleNotFoundError): diff --git a/py4DSTEM/io/filereaders/__init__.py b/py4DSTEM/io/filereaders/__init__.py index eb5cd02e1..372275f11 100644 --- a/py4DSTEM/io/filereaders/__init__.py +++ b/py4DSTEM/io/filereaders/__init__.py @@ -2,6 +2,7 @@ from py4DSTEM.io.filereaders.read_K2 import read_gatan_K2_bin from py4DSTEM.io.filereaders.empad import read_empad from py4DSTEM.io.filereaders.read_mib import load_mib + try: from py4DSTEM.io.filereaders.read_arina import read_arina except (ImportError, ModuleNotFoundError): diff --git a/py4DSTEM/io/importfile.py b/py4DSTEM/io/importfile.py index 73e402de0..b3002c77e 100644 --- a/py4DSTEM/io/importfile.py +++ b/py4DSTEM/io/importfile.py @@ -90,6 +90,7 @@ def import_file( data = load_mib(filepath, mem=mem, binfactor=binfactor, **kwargs) elif filetype == "arina": from py4DSTEM.io.filereaders import read_arina + data = read_arina(filepath, mem=mem, binfactor=binfactor, **kwargs) elif filetype == "abTEM": data = read_abTEM(filepath, mem=mem, binfactor=binfactor, **kwargs) diff --git a/py4DSTEM/process/__init__.py b/py4DSTEM/process/__init__.py index 8acee4e3c..b04451913 100644 --- a/py4DSTEM/process/__init__.py +++ b/py4DSTEM/process/__init__.py @@ -4,11 +4,13 @@ from py4DSTEM.process import phase from py4DSTEM.process import calibration from py4DSTEM.process import utils + try: from py4DSTEM.process import classification except (ImportError, ModuleNotFoundError): pass from py4DSTEM.process import diffraction + try: from py4DSTEM.process import wholepatternfit except (ImportError, ModuleNotFoundError): diff --git a/py4DSTEM/process/diffraction/crystal_viz.py b/py4DSTEM/process/diffraction/crystal_viz.py index d3e4b682a..b5a22d609 100644 --- a/py4DSTEM/process/diffraction/crystal_viz.py +++ b/py4DSTEM/process/diffraction/crystal_viz.py @@ -453,7 +453,8 @@ def plot_scattering_intensity( int_sf_plot = calc_1D_profile( k, self.g_vec_leng, - (self.struct_factors_int**int_power_scale) * (self.g_vec_leng**k_power_scale), + (self.struct_factors_int**int_power_scale) + * (self.g_vec_leng**k_power_scale), remove_origin=True, k_broadening=k_broadening, int_scale=int_scale, @@ -1883,7 +1884,6 @@ def plot_clusters( for a0 in range(self.cluster_sizes.shape[0]): if self.cluster_sizes[a0] >= area_min: - if outline_grains: from skimage.morphology import erosion diff --git a/py4DSTEM/process/phase/utils.py b/py4DSTEM/process/phase/utils.py index 2834e8c2e..0ed82f703 100644 --- a/py4DSTEM/process/phase/utils.py +++ b/py4DSTEM/process/phase/utils.py @@ -202,7 +202,9 @@ def evaluate_gaussian_envelope( self, alpha: Union[float, np.ndarray] ) -> Union[float, np.ndarray]: xp = self._xp - return xp.exp(-0.5 * self._gaussian_spread**2 * alpha**2 / self._wavelength**2) + return xp.exp( + -0.5 * self._gaussian_spread**2 * alpha**2 / self._wavelength**2 + ) def evaluate_spatial_envelope( self, alpha: Union[float, np.ndarray], phi: Union[float, np.ndarray] @@ -1755,6 +1757,7 @@ def unwrap_phase_2d(array, weights=None, gauge=None, corner_centered=True, xp=np def unwrap_phase_2d_skimage(array, corner_centered=True, xp=np): from skimage.restoration import unwrap_phase + if xp is np: array = array.astype(np.float64) unwrapped_array = unwrap_phase(array, wrap_around=corner_centered).astype( diff --git a/py4DSTEM/utils/configuration_checker.py b/py4DSTEM/utils/configuration_checker.py index 6edf44ffc..cbdbd2881 100644 --- a/py4DSTEM/utils/configuration_checker.py +++ b/py4DSTEM/utils/configuration_checker.py @@ -6,8 +6,7 @@ from importlib.util import find_spec # need a mapping of pypi/conda names to import names -import_mapping_dict = { -} +import_mapping_dict = {} # programatically get all possible requirements in the import name style @@ -523,8 +522,7 @@ def print_no_extra_checks(m: str): # dict of extra check functions -funcs_dict = { -} +funcs_dict = {} #### main function used to check the configuration of the installation From b84847cc0969aef74d3bb258103ea85c0aac58af Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Thu, 4 Jul 2024 10:03:46 -0700 Subject: [PATCH 03/10] one more try, new black --- 24 | 16 ++++++++++++++++ py4DSTEM/process/diffraction/crystal_viz.py | 3 +-- py4DSTEM/process/phase/utils.py | 4 +--- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 24 diff --git a/24 b/24 new file mode 100644 index 000000000..16bdba3c0 --- /dev/null +++ b/24 @@ -0,0 +1,16 @@ +conda-forge/linux-64 Using cache +conda-forge/noarch Using cache +Transaction + + Prefix: /home/george/mambaforge/envs/py4dstem-dev + + All requested packages already installed + + +Looking for: ['black'] + + +Pinned packages: + - python 3.12.* + + diff --git a/py4DSTEM/process/diffraction/crystal_viz.py b/py4DSTEM/process/diffraction/crystal_viz.py index b5a22d609..ecc6fed0e 100644 --- a/py4DSTEM/process/diffraction/crystal_viz.py +++ b/py4DSTEM/process/diffraction/crystal_viz.py @@ -453,8 +453,7 @@ def plot_scattering_intensity( int_sf_plot = calc_1D_profile( k, self.g_vec_leng, - (self.struct_factors_int**int_power_scale) - * (self.g_vec_leng**k_power_scale), + (self.struct_factors_int**int_power_scale) * (self.g_vec_leng**k_power_scale), remove_origin=True, k_broadening=k_broadening, int_scale=int_scale, diff --git a/py4DSTEM/process/phase/utils.py b/py4DSTEM/process/phase/utils.py index 0ed82f703..f932b40b5 100644 --- a/py4DSTEM/process/phase/utils.py +++ b/py4DSTEM/process/phase/utils.py @@ -202,9 +202,7 @@ def evaluate_gaussian_envelope( self, alpha: Union[float, np.ndarray] ) -> Union[float, np.ndarray]: xp = self._xp - return xp.exp( - -0.5 * self._gaussian_spread**2 * alpha**2 / self._wavelength**2 - ) + return xp.exp(-0.5 * self._gaussian_spread**2 * alpha**2 / self._wavelength**2) def evaluate_spatial_envelope( self, alpha: Union[float, np.ndarray], phi: Union[float, np.ndarray] From 7b60b13f8e33cfc9e0a17b10275fd20811a67021 Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Thu, 4 Jul 2024 10:07:40 -0700 Subject: [PATCH 04/10] odd mamba file made its way in --- 24 | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 24 diff --git a/24 b/24 deleted file mode 100644 index 16bdba3c0..000000000 --- a/24 +++ /dev/null @@ -1,16 +0,0 @@ -conda-forge/linux-64 Using cache -conda-forge/noarch Using cache -Transaction - - Prefix: /home/george/mambaforge/envs/py4dstem-dev - - All requested packages already installed - - -Looking for: ['black'] - - -Pinned packages: - - python 3.12.* - - From 1e8278f8c53cc39a4e6a5fa82a0a3be8a587df46 Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Wed, 10 Jul 2024 01:59:00 -0700 Subject: [PATCH 05/10] is_package_lite demo --- py4DSTEM/__init__.py | 14 +++++++++----- setup.py | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/py4DSTEM/__init__.py b/py4DSTEM/__init__.py index 712095abd..695d2b55d 100644 --- a/py4DSTEM/__init__.py +++ b/py4DSTEM/__init__.py @@ -1,6 +1,9 @@ from py4DSTEM.version import __version__ from emdfile import tqdmnd +from importlib.metadata import metadata + +is_package_lite = "lite" in metadata("py4DSTEM")["Keywords"].lower().split(",") ### io @@ -54,9 +57,9 @@ try: from py4DSTEM.process import classification -except (ImportError, ModuleNotFoundError): - pass - +except (ImportError, ModuleNotFoundError) as exc: + if not is_package_lite: + raise exc # diffraction from py4DSTEM.process.diffraction import Crystal, Orientation @@ -75,8 +78,9 @@ try: from py4DSTEM.process import wholepatternfit -except (ImportError, ModuleNotFoundError): - pass +except (ImportError, ModuleNotFoundError) as exc: + if not is_package_lite: + raise exc ### more submodules diff --git a/setup.py b/setup.py index 3a853cc9d..28b16692a 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ author="Benjamin H. Savitzky", author_email="ben.savitzky@gmail.com", license="GNU GPLv3", - keywords="STEM 4DSTEM", + keywords="STEM,4DSTEM", python_requires=">=3.10", install_requires=[ "numpy >= 1.19", From e872c79f98e2c7c557d510cb4b35e70298b79264 Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Wed, 10 Jul 2024 02:06:52 -0700 Subject: [PATCH 06/10] switching to __package__ calls instead --- py4DSTEM/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py4DSTEM/__init__.py b/py4DSTEM/__init__.py index 695d2b55d..2fd84dc0a 100644 --- a/py4DSTEM/__init__.py +++ b/py4DSTEM/__init__.py @@ -3,7 +3,7 @@ from importlib.metadata import metadata -is_package_lite = "lite" in metadata("py4DSTEM")["Keywords"].lower().split(",") +is_package_lite = "lite" in metadata(__package__)["Keywords"].lower().split(",") ### io From 44c5e1ac5d602eed9d6023e2cefa47cb26b0ff2d Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Wed, 10 Jul 2024 02:20:50 -0700 Subject: [PATCH 07/10] I clearly dont understand __package__ --- py4DSTEM/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/py4DSTEM/__init__.py b/py4DSTEM/__init__.py index 2fd84dc0a..0189cb2be 100644 --- a/py4DSTEM/__init__.py +++ b/py4DSTEM/__init__.py @@ -3,6 +3,9 @@ from importlib.metadata import metadata +package_spec = __spec__ +package_package = __package__ +package_name = __name__ is_package_lite = "lite" in metadata(__package__)["Keywords"].lower().split(",") ### io From 33d20b6fed62f5b5057cdcd4469e67835878fae9 Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Wed, 10 Jul 2024 02:30:35 -0700 Subject: [PATCH 08/10] reverting, doesnt seem like metadata would work - will continue on plane --- py4DSTEM/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/py4DSTEM/__init__.py b/py4DSTEM/__init__.py index 0189cb2be..2fd84dc0a 100644 --- a/py4DSTEM/__init__.py +++ b/py4DSTEM/__init__.py @@ -3,9 +3,6 @@ from importlib.metadata import metadata -package_spec = __spec__ -package_package = __package__ -package_name = __name__ is_package_lite = "lite" in metadata(__package__)["Keywords"].lower().split(",") ### io From 1693c18805eb4122b9569bd0a2596d5a7d26fb73 Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Wed, 10 Jul 2024 19:09:57 -0700 Subject: [PATCH 09/10] alright - this hack ought to work --- py4DSTEM/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py4DSTEM/__init__.py b/py4DSTEM/__init__.py index 2fd84dc0a..0f4490eb2 100644 --- a/py4DSTEM/__init__.py +++ b/py4DSTEM/__init__.py @@ -1,9 +1,9 @@ from py4DSTEM.version import __version__ from emdfile import tqdmnd -from importlib.metadata import metadata +from importlib.metadata import packages_distributions -is_package_lite = "lite" in metadata(__package__)["Keywords"].lower().split(",") +is_package_lite = "py4DSTEM-lite" in packages_distributions()["py4DSTEM"] ### io From f75992cecde8923347640cc4ed5e8ff1ff9dcd39 Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Wed, 10 Jul 2024 19:27:16 -0700 Subject: [PATCH 10/10] updating all __init__ files to be lite_aware --- py4DSTEM/braggvectors/diskdetection.py | 6 ++++-- py4DSTEM/io/filereaders/__init__.py | 8 +++++--- py4DSTEM/process/__init__.py | 12 ++++++++---- py4DSTEM/process/phase/__init__.py | 6 ++++-- py4DSTEM/utils/configuration_checker.py | 23 ++++++++++++++++++++--- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/py4DSTEM/braggvectors/diskdetection.py b/py4DSTEM/braggvectors/diskdetection.py index a66ff62ea..500dbd2e9 100644 --- a/py4DSTEM/braggvectors/diskdetection.py +++ b/py4DSTEM/braggvectors/diskdetection.py @@ -5,6 +5,7 @@ from scipy.ndimage import gaussian_filter from emdfile import tqdmnd +from py4DSTEM import is_package_lite from py4DSTEM.braggvectors.braggvectors import BraggVectors from py4DSTEM.data import QPoints from py4DSTEM.datacube import DataCube @@ -13,8 +14,9 @@ try: from py4DSTEM.braggvectors.diskdetection_aiml import find_Bragg_disks_aiml -except (ImportError, ModuleNotFoundError): - pass +except (ImportError, ModuleNotFoundError) as exc: + if not is_package_lite: + raise exc def find_Bragg_disks( diff --git a/py4DSTEM/io/filereaders/__init__.py b/py4DSTEM/io/filereaders/__init__.py index 372275f11..4b89175a7 100644 --- a/py4DSTEM/io/filereaders/__init__.py +++ b/py4DSTEM/io/filereaders/__init__.py @@ -1,10 +1,12 @@ +from py4DSTEM import is_package_lite +from py4DSTEM.io.filereaders.empad import read_empad from py4DSTEM.io.filereaders.read_dm import read_dm from py4DSTEM.io.filereaders.read_K2 import read_gatan_K2_bin -from py4DSTEM.io.filereaders.empad import read_empad from py4DSTEM.io.filereaders.read_mib import load_mib try: from py4DSTEM.io.filereaders.read_arina import read_arina -except (ImportError, ModuleNotFoundError): - pass +except (ImportError, ModuleNotFoundError) as exc: + if not is_package_lite: + raise exc from py4DSTEM.io.filereaders.read_abTEM import read_abTEM diff --git a/py4DSTEM/process/__init__.py b/py4DSTEM/process/__init__.py index b04451913..6d7d36b28 100644 --- a/py4DSTEM/process/__init__.py +++ b/py4DSTEM/process/__init__.py @@ -1,3 +1,4 @@ +from py4DSTEM import is_package_lite from py4DSTEM.process.polar import PolarDatacube from py4DSTEM.process.strain.strain import StrainMap @@ -7,11 +8,14 @@ try: from py4DSTEM.process import classification -except (ImportError, ModuleNotFoundError): - pass +except (ImportError, ModuleNotFoundError) as exc: + if not is_package_lite: + raise exc + from py4DSTEM.process import diffraction try: from py4DSTEM.process import wholepatternfit -except (ImportError, ModuleNotFoundError): - pass +except (ImportError, ModuleNotFoundError) as exc: + if not is_package_lite: + raise exc diff --git a/py4DSTEM/process/phase/__init__.py b/py4DSTEM/process/phase/__init__.py index 927e5a08a..68dac8989 100644 --- a/py4DSTEM/process/phase/__init__.py +++ b/py4DSTEM/process/phase/__init__.py @@ -2,6 +2,7 @@ _emd_hook = True +from py4DSTEM import is_package_lite from py4DSTEM.process.phase.dpc import DPC from py4DSTEM.process.phase.magnetic_ptychographic_tomography import MagneticPtychographicTomography from py4DSTEM.process.phase.magnetic_ptychography import MagneticPtychography @@ -13,7 +14,8 @@ from py4DSTEM.process.phase.singleslice_ptychography import SingleslicePtychography try: from py4DSTEM.process.phase.parameter_optimize import OptimizationParameter, PtychographyOptimizer -except (ImportError, ModuleNotFoundError): - pass +except (ImportError, ModuleNotFoundError) as exc: + if not is_package_lite: + raise exc # fmt: on diff --git a/py4DSTEM/utils/configuration_checker.py b/py4DSTEM/utils/configuration_checker.py index cbdbd2881..2d292618e 100644 --- a/py4DSTEM/utils/configuration_checker.py +++ b/py4DSTEM/utils/configuration_checker.py @@ -5,8 +5,19 @@ import re from importlib.util import find_spec +from py4DSTEM import is_package_lite + # need a mapping of pypi/conda names to import names -import_mapping_dict = {} +import_mapping_dict = ( + {} + if is_package_lite + else { + "scikit-image": "skimage", + "scikit-learn": "sklearn", + "scikit-optimize": "skopt", + "mp-api": "mp_api", + } +) # programatically get all possible requirements in the import name style @@ -84,7 +95,7 @@ def get_modules_dict(): # module_depenencies = get_modules_dict() # modules = get_modules_list() -modules = [] +modules = [] if is_package_lite else get_modules_list() #### Class and Functions to Create Coloured Strings #### @@ -522,7 +533,13 @@ def print_no_extra_checks(m: str): # dict of extra check functions -funcs_dict = {} +funcs_dict = ( + {} + if is_package_lite + else { + "cupy": check_cupy_gpu, + } +) #### main function used to check the configuration of the installation