Skip to content

Commit

Permalink
Merge pull request #192 from jhlegarreta/DoNotTryExceptNonOptionalImp…
Browse files Browse the repository at this point in the history
…orts

ENH: Do not `try`/`except` on non-optional imports
  • Loading branch information
ljod authored Dec 7, 2023
2 parents 780e905 + 87e935d commit b814dc7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 50 deletions.
25 changes: 6 additions & 19 deletions whitematteranalysis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,13 @@
import colorsys
import os
import pickle
from pprint import pprint

import numpy as np
import scipy.cluster.hierarchy
import scipy.cluster.vq
import vtk

try:
import scipy.cluster.hierarchy
import scipy.cluster.vq
USE_SCIPY = 1
except ImportError:
USE_SCIPY = 0
print(f"<{os.path.basename(__file__)}> Failed to import scipy.cluster, cannot cluster.")
print(f"<{os.path.basename(__file__)}> Please install scipy for this functionality.")
try:
from joblib import Parallel, delayed
USE_PARALLEL = 1
except ImportError:
USE_PARALLEL = 0
print(f"<{os.path.basename(__file__)}> Failed to import joblib, cannot multiprocess.")
print(f"<{os.path.basename(__file__)}> Please install joblib for this functionality.")

from pprint import pprint
from joblib import Parallel, delayed

from . import fibers, filter, io, mrml, render, similarity

Expand Down Expand Up @@ -510,7 +496,8 @@ def spectral(input_polydata, number_of_clusters=200,
print("Silhouette Coefficient: %0.3f" % cluster_metric)

else:
print("ERROR: Unknown centroid finder", centroid_finder)
raise NotImplementedError(
f"Workflow not implemented for centroid finder: {centroid_finder}.")
## # This found fewer clusters than we need to represent the anatomy well
## # Leave code here in case wanted in future for more testing.
## print(f'<{os.path.basename(__file__)}> Affinity Propagation clustering in embedding space.')
Expand Down
13 changes: 3 additions & 10 deletions whitematteranalysis/register_two_subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@ class RegisterTractography
"""

import os

try:
import scipy.optimize
USE_SCIPY = 1
except ImportError:
USE_SCIPY = 0
print(f"<{os.path.basename(__file__)}> Failed to import scipy.optimize, cannot align or register.")
print(f"<{os.path.basename(__file__)}> Please install scipy.optimize for this functionality.")

import sys
import time

import numpy as np
import scipy.optimize
import vtk

import whitematteranalysis as wma
Expand Down Expand Up @@ -262,7 +254,8 @@ def compute(self):
print("FLAG:", warnflag)

else:
print("Unknown optimizer.")
raise NotImplementedError(
f"Workflow not implemented for optimizer: {self.optimizer}.")

self.final_transform = np.divide(self.final_transform, self.transform_scaling)

Expand Down
13 changes: 3 additions & 10 deletions whitematteranalysis/register_two_subjects_nonrigid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@ class RegisterTractographyNonrigid
"""

import os

try:
import scipy.optimize
USE_SCIPY = 1
except ImportError:
USE_SCIPY = 0
print(f"<{os.path.basename(__file__)}> Failed to import scipy.optimize, cannot align or register.")
print(f"<{os.path.basename(__file__)}> Please install scipy.optimize for this functionality.")

import sys
import time

import numpy as np
import scipy.optimize
import vtk

import whitematteranalysis as wma
Expand Down Expand Up @@ -314,7 +306,8 @@ def compute(self):
print("TRANS:", self.final_transform, "FLAG:", warnflag)

else:
print("Unknown optimizer.")
raise NotImplementedError(
f"Workflow not implemented for optimizer: {self.optimizer}.")

if self.verbose:
print("O:", self.objective_function_values)
Expand Down
14 changes: 3 additions & 11 deletions whitematteranalysis/register_two_subjects_nonrigid_bsplines.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,12 @@ class RegisterTractographyNonrigid
"""

import os

try:
import scipy.optimize
USE_SCIPY = 1
except ImportError:
USE_SCIPY = 0
print(f"<{os.path.basename(__file__)}> Failed to import scipy.optimize, cannot align or register.")
print(f"<{os.path.basename(__file__)}> Please install scipy.optimize for this functionality.")

import os
import sys
import time

import numpy as np
import scipy.optimize
import vtk
import vtk.util.numpy_support

Expand Down Expand Up @@ -327,7 +318,8 @@ def compute(self):
print("TRANS:", self.final_transform, "FLAG:", warnflag)

else:
print("Unknown optimizer.")
raise NotImplementedError(
f"Workflow not implemented for optimizer: {self.optimizer}.")

progress_file = open(self.progress_filename, 'a')
self.total_time = time.time() - self.start_time
Expand Down

0 comments on commit b814dc7

Please sign in to comment.