Skip to content

MAINT: Delayed imports to reduce import time #2809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions nipype/algorithms/confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import nibabel as nb
import numpy as np
from numpy.polynomial import Legendre
from scipy import linalg

from .. import config, logging
from ..external.due import BibTeX
Expand Down Expand Up @@ -1186,7 +1185,7 @@ def compute_noise_components(imgseries, mask_images, num_components,

# "The covariance matrix C = MMT was constructed and decomposed into its
# principal components using a singular value decomposition."
u, _, _ = linalg.svd(M, full_matrices=False)
u, _, _ = np.linalg.svd(M, full_matrices=False)
if components is None:
components = u[:, :num_components]
else:
Expand Down
2 changes: 1 addition & 1 deletion nipype/algorithms/icc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import os
import numpy as np
from numpy import ones, kron, mean, eye, hstack, dot, tile
from numpy.linalg import pinv
import nibabel as nb
from scipy.linalg import pinv
from ..interfaces.base import BaseInterfaceInputSpec, TraitedSpec, \
BaseInterface, traits, File
from ..utils import NUMPY_MMAP
Expand Down
10 changes: 7 additions & 3 deletions nipype/algorithms/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

import nibabel as nb
import numpy as np
from scipy.ndimage.morphology import binary_erosion
from scipy.spatial.distance import cdist, euclidean, dice, jaccard
from scipy.ndimage.measurements import center_of_mass, label

from .. import config, logging

Expand Down Expand Up @@ -74,6 +71,7 @@ class Distance(BaseInterface):
_hist_filename = "hist.pdf"

def _find_border(self, data):
from scipy.ndimage.morphology import binary_erosion
eroded = binary_erosion(data)
border = np.logical_and(data, np.logical_not(eroded))
return border
Expand All @@ -87,6 +85,7 @@ def _get_coordinates(self, data, affine):
return coordinates[:3, :]

def _eucl_min(self, nii1, nii2):
from scipy.spatial.distance import cdist, euclidean
origdata1 = nii1.get_data().astype(np.bool)
border1 = self._find_border(origdata1)

Expand All @@ -105,6 +104,8 @@ def _eucl_min(self, nii1, nii2):
set1_coordinates.T[point1, :], set2_coordinates.T[point2, :])

def _eucl_cog(self, nii1, nii2):
from scipy.spatial.distance import cdist
from scipy.ndimage.measurements import center_of_mass, label
origdata1 = np.logical_and(nii1.get_data() != 0,
np.logical_not(np.isnan(nii1.get_data())))
cog_t = np.array(center_of_mass(origdata1.copy())).reshape(-1, 1)
Expand All @@ -128,6 +129,7 @@ def _eucl_cog(self, nii1, nii2):
return np.mean(dist_matrix)

def _eucl_mean(self, nii1, nii2, weighted=False):
from scipy.spatial.distance import cdist
origdata1 = nii1.get_data().astype(np.bool)
border1 = self._find_border(origdata1)

Expand All @@ -154,6 +156,7 @@ def _eucl_mean(self, nii1, nii2, weighted=False):
return np.mean(min_dist_matrix)

def _eucl_max(self, nii1, nii2):
from scipy.spatial.distance import cdist
origdata1 = nii1.get_data()
origdata1 = np.logical_not(
np.logical_or(origdata1 == 0, np.isnan(origdata1)))
Expand Down Expand Up @@ -287,6 +290,7 @@ class Overlap(BaseInterface):
output_spec = OverlapOutputSpec

def _bool_vec_dissimilarity(self, booldata1, booldata2, method):
from scipy.spatial.distance import dice, jaccard
methods = {'dice': dice, 'jaccard': jaccard}
if not (np.any(booldata1) or np.any(booldata2)):
return 0
Expand Down
7 changes: 4 additions & 3 deletions nipype/algorithms/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import nibabel as nb
import numpy as np
from math import floor, ceil
from scipy.ndimage.morphology import grey_dilation
import scipy.io as sio
import itertools
import scipy.stats as stats
import warnings

from .. import logging
Expand Down Expand Up @@ -103,6 +100,7 @@ def _get_brodmann_area(self):
newdata[:int(ceil(float(origdata.shape[0]) / 2)), :, :] = 0

if self.inputs.dilation_size != 0:
from scipy.ndimage.morphology import grey_dilation
newdata = grey_dilation(newdata,
(2 * self.inputs.dilation_size + 1,
2 * self.inputs.dilation_size + 1,
Expand Down Expand Up @@ -356,6 +354,7 @@ class Matlab2CSV(BaseInterface):
output_spec = Matlab2CSVOutputSpec

def _run_interface(self, runtime):
import scipy.io as sio
in_dict = sio.loadmat(op.abspath(self.inputs.in_file))

# Check if the file has multiple variables in it. If it does, loop
Expand Down Expand Up @@ -393,6 +392,7 @@ def _run_interface(self, runtime):
return runtime

def _list_outputs(self):
import scipy.io as sio
outputs = self.output_spec().get()
in_dict = sio.loadmat(op.abspath(self.inputs.in_file))
saved_variables = list()
Expand Down Expand Up @@ -909,6 +909,7 @@ def calc_moments(timeseries_file, moment):
timeseries_file -- text file with white space separated timepoints in rows

"""
import scipy.stats as stats
timeseries = np.genfromtxt(timeseries_file)

m2 = stats.moment(timeseries, 2, axis=0)
Expand Down
2 changes: 1 addition & 1 deletion nipype/algorithms/modelgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from nibabel import load
import numpy as np
from scipy.special import gammaln

from ..utils import NUMPY_MMAP
from ..interfaces.base import (BaseInterface, TraitedSpec, InputMultiPath,
Expand Down Expand Up @@ -84,6 +83,7 @@ def spm_hrf(RT, P=None, fMRI_T=16):
-1.46257507e-04]

"""
from scipy.special import gammaln
p = np.array([6, 16, 1, 1, 6, 0, 32], dtype=float)
if P is not None:
p[0:len(P)] = P
Expand Down
7 changes: 4 additions & 3 deletions nipype/algorithms/rapidart.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

from nibabel import load, funcs, Nifti1Image
import numpy as np
from scipy import signal
import scipy.io as sio

from ..utils import NUMPY_MMAP
from ..interfaces.base import (BaseInterface, traits, InputMultiPath,
Expand Down Expand Up @@ -151,7 +149,8 @@ def _calc_norm_affine(affines, use_differences, brain_pts=None):
(3, all_pts.shape[1])),
axis=0)))
else:
newpos = np.abs(signal.detrend(newpos, axis=0, type='constant'))
from scipy.signal import detrend
newpos = np.abs(detrend(newpos, axis=0, type='constant'))
normdata = np.sqrt(np.mean(np.power(newpos, 2), axis=1))
return normdata, displacement

Expand Down Expand Up @@ -411,6 +410,7 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None):
"""
Core routine for detecting outliers
"""
from scipy import signal
if not cwd:
cwd = os.getcwd()

Expand Down Expand Up @@ -750,6 +750,7 @@ def _get_spm_submatrix(self, spmmat, sessidx, rows=None):
def _run_interface(self, runtime):
"""Execute this module.
"""
import scipy.io as sio
motparamlist = self.inputs.realignment_parameters
intensityfiles = self.inputs.intensity_values
spmmat = sio.loadmat(self.inputs.spm_mat_file, struct_as_record=False)
Expand Down
6 changes: 2 additions & 4 deletions nipype/interfaces/base/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,12 +1164,10 @@ class LibraryBaseInterface(BaseInterface):
def __init__(self, check_import=True, *args, **kwargs):
super(LibraryBaseInterface, self).__init__(*args, **kwargs)
if check_import:
import importlib
import pkgutil
failed_imports = []
for pkg in (self._pkg,) + tuple(self.imports):
try:
importlib.import_module(pkg)
except ImportError:
if pkgutil.find_loader(pkg) is None:
failed_imports.append(pkg)
if failed_imports:
iflogger.warning('Unable to import %s; %s interface may fail to '
Expand Down
2 changes: 1 addition & 1 deletion nipype/interfaces/cmtk/cmtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import numpy as np
import nibabel as nb
import networkx as nx
import scipy.io as sio

from ... import logging
from ...utils.filemanip import split_filename
Expand Down Expand Up @@ -178,6 +177,7 @@ def cmat(track_file,
endpoint_name,
intersections=False):
""" Create the connection matrix for each resolution using fibers and ROIs. """
import scipy.io as sio

stats = {}
iflogger.info('Running cmat function')
Expand Down
3 changes: 2 additions & 1 deletion nipype/interfaces/cmtk/nx.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import numpy as np
import networkx as nx
import scipy.io as sio

from ... import logging
from ...utils.filemanip import split_filename
Expand Down Expand Up @@ -94,6 +93,7 @@ def average_networks(in_files, ntwk_res_file, group_id):
"""
import networkx as nx
import os.path as op
import scipy.io as sio
iflogger.info('Creating average network for group: %s', group_id)
matlab_network_list = []
if len(in_files) == 1:
Expand Down Expand Up @@ -442,6 +442,7 @@ class NetworkXMetrics(BaseInterface):
output_spec = NetworkXMetricsOutputSpec

def _run_interface(self, runtime):
import scipy.io as sio
global gpickled, nodentwks, edgentwks, kntwks, matlab
gpickled = list()
nodentwks = list()
Expand Down
10 changes: 7 additions & 3 deletions nipype/interfaces/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:

import numpy as np
import nibabel as nb

from ..utils.filemanip import fname_presuffix
from .base import (SimpleInterface, TraitedSpec, BaseInterfaceInputSpec,
traits, File)
Expand Down Expand Up @@ -63,6 +60,9 @@ class Rescale(SimpleInterface):
output_spec = RescaleOutputSpec

def _run_interface(self, runtime):
import numpy as np
import nibabel as nb

img = nb.load(self.inputs.in_file)
data = img.get_data()
ref_data = nb.load(self.inputs.ref_file).get_data()
Expand Down Expand Up @@ -171,6 +171,8 @@ class Reorient(SimpleInterface):
output_spec = ReorientOutputSpec

def _run_interface(self, runtime):
import numpy as np
import nibabel as nb
from nibabel.orientations import (
axcodes2ornt, ornt_transform, inv_ornt_aff)

Expand Down Expand Up @@ -211,6 +213,8 @@ def _run_interface(self, runtime):

def _as_reoriented_backport(img, ornt):
"""Backport of img.as_reoriented as of nibabel 2.2.0"""
import numpy as np
import nibabel as nb
from nibabel.orientations import inv_ornt_aff
if np.array_equal(ornt, [[0, 1], [1, 1], [2, 1]]):
return img
Expand Down
Loading