From 307f27dcf5aab391d5d2a457e1caddac25c0f973 Mon Sep 17 00:00:00 2001 From: gvarnavi Date: Thu, 4 Jul 2024 09:33:21 -0700 Subject: [PATCH] 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, }