Skip to content

Commit

Permalink
ENH: Do not try/except on non-optional imports
Browse files Browse the repository at this point in the history
Do not `try`/`except` on non-optional imports.

Also, raise an exception if the corresponding option is not found.
  • Loading branch information
jhlegarreta committed Oct 21, 2023
1 parent 4c93d41 commit 7738fa0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 50 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ whitematteranalysis
[![Documentation Status](https://readthedocs.org/projects/whitematteranalysis/badge/?version=latest)](https://whitematteranalysis.readthedocs.io/en/latest/?badge=latest)
[![code format](https://github.com/SlicerDMRI/whitematteranalysis/actions/workflows/check_format.yml/badge.svg?branch=master)](https://github.com/SlicerDMRI/whitematteranalysis/actions/workflows/check_format.yml?query=branch%3Amaster)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)


# Synopsis
Expand Down
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
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 '<cluster.py> 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
import scipy.optimize
import vtk

try:
Expand Down Expand Up @@ -270,7 +262,8 @@ def compute(self):
print("FLAG:", warnflag)

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

self.final_transform = numpy.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
import scipy.optimize
import vtk

try:
Expand Down Expand Up @@ -322,7 +314,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
import scipy.optimize
import vtk
import vtk.util.numpy_support

Expand Down Expand Up @@ -335,7 +326,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 7738fa0

Please sign in to comment.