Skip to content

Commit

Permalink
Merge afd5c75 into 4ad6e0e
Browse files Browse the repository at this point in the history
  • Loading branch information
po09i committed Jun 25, 2021
2 parents 4ad6e0e + afd5c75 commit 150eb49
Show file tree
Hide file tree
Showing 7 changed files with 724 additions and 271 deletions.
6 changes: 3 additions & 3 deletions docs/source/api_reference/api.rst
Expand Up @@ -56,9 +56,9 @@ Shim
.. Error while importing sklearn and skimage
.. automodule:: shimmingtoolbox.shim.realtime_shim
:members:
.. automodule:: shimmingtoolbox.optimizer.sequential
:members:
.. Error while importing sklearn (Linear Regression)
.. automodule:: shimmingtoolbox.shim.sequencer
:members:
Optimizer
---------
Expand Down
65 changes: 57 additions & 8 deletions shimmingtoolbox/optimizer/basic_optimizer.py
Expand Up @@ -43,20 +43,43 @@ def __init__(self, coils: ListCoil, unshimmed, affine):
logging.basicConfig(filename='test_optimizer.log', filemode='w', level=logging.DEBUG)

self.coils = coils
self.unshimmed = np.array([])
self.unshimmed_affine = []
self.merged_coils = []
self.merged_bounds = []
self.set_unshimmed(unshimmed, affine)

def set_unshimmed(self, unshimmed, affine):
""" Set the unshimmed array to a new array. Resamples coil profiles accordingly.
Args:
unshimmed (numpy.ndarray): 3d array of unshimmed volume
affine: (numpy.ndarray): 4x4 array containing the qform affine transformation for the unshimmed array
"""
# Check dimensions of unshimmed map
if unshimmed.ndim != 3:
raise ValueError(f"Unshimmed profile has {unshimmed.ndim} dimensions, expected 3 (dim1, dim2, dim3)")
self.unshimmed = unshimmed

# Check dimensions of affine
if affine.shape != (4, 4):
raise ValueError("Shape of affine matrix should be 4x4")

# Define coil profiles if unshimmed or affine is different than previously
if (self.unshimmed.shape != unshimmed.shape) or not np.all(self.unshimmed_affine == affine):
self.merged_coils, self.merged_bounds = self.merge_coils(unshimmed, affine)

self.unshimmed = unshimmed
self.unshimmed_affine = affine

# Define coil profiles
merged_coils, merged_bounds = self.merge_coils(unshimmed, affine)
self.merged_coils = merged_coils
def set_merged_bounds(self, merged_bounds):
"""
Changes the default bounds set in the coil profile
Args:
merged_bounds: Concatenated coil profile bounds
"""
if len(self.merged_bounds) != len(merged_bounds):
raise ValueError(f"Size of merged bounds: must match the number of total channel: {len(self.merged_bounds)}")
self.merged_bounds = merged_bounds

def optimize(self, mask):
Expand Down Expand Up @@ -99,7 +122,6 @@ def merge_coils(self, unshimmed, affine):
"""

coil_profiles_list = []
bounds = []

# Define the nibabel unshimmed array
nii_unshimmed = nib.Nifti1Image(unshimmed, affine)
Expand All @@ -109,15 +131,42 @@ def merge_coils(self, unshimmed, affine):

# Resample a coil on the unshimmed image
resampled_coil = resample_from_to(nii_coil, nii_unshimmed).get_fdata()
coil_profiles_list.append(resampled_coil)

coil_profiles = np.concatenate(coil_profiles_list, axis=3)

bounds = self.merge_bounds(self.coils)

return coil_profiles, bounds

def merge_bounds(self, coils):

bounds = []
for coil in coils:
# Concat coils and bounds
coil_profiles_list.append(resampled_coil)
for a_bound in coil.coef_channel_minmax:
bounds.append(a_bound)

coil_profiles = np.concatenate(coil_profiles_list, axis=3)
return bounds

return coil_profiles, bounds
def initial_guess_half_bounds(self):
"""
Calculates the initial guess from the bounds, sets it to half of the bounds
Returns:
np.ndarray: 1d array (n_channels) of coefficient representing the initial guess
"""
current_0 = []
for bounds in self.merged_bounds:
avg = np.mean(bounds)

if np.isnan(avg):
current_0.append(0)
else:
current_0.append(avg)

return np.array(current_0)

def _check_sizing(self, mask):
"""
Expand Down
2 changes: 1 addition & 1 deletion shimmingtoolbox/optimizer/lsq_optimizer.py
Expand Up @@ -68,7 +68,7 @@ def _apply_sum_constraint(inputs, indexes, coef_sum_max):
start_index = end_index

# Set up output currents
currents_0 = np.zeros(n_channels)
currents_0 = self.initial_guess_half_bounds()

# Optimize
currents_sp = opt.minimize(self._residuals, currents_0,
Expand Down
89 changes: 0 additions & 89 deletions shimmingtoolbox/optimizer/sequential.py

This file was deleted.

0 comments on commit 150eb49

Please sign in to comment.