Skip to content

Commit

Permalink
Merge 76948ab into 4ad6e0e
Browse files Browse the repository at this point in the history
  • Loading branch information
po09i committed Jul 7, 2021
2 parents 4ad6e0e + 76948ab commit de91ba6
Show file tree
Hide file tree
Showing 8 changed files with 784 additions and 272 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
72 changes: 64 additions & 8 deletions shimmingtoolbox/optimizer/basic_optimizer.py
Expand Up @@ -43,20 +43,44 @@ 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 +123,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 +132,48 @@ 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()

return coil_profiles, bounds

def merge_bounds(self):
"""
Merge the coil profile bounds into a single array.
Returns:
list: list of bounds corresponding to each merged coils
"""

bounds = []
for coil in self.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_mean_bounds(self):
"""
Calculates the initial guess from the bounds, sets it to the mean 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_mean_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.

6 changes: 5 additions & 1 deletion shimmingtoolbox/pmu.py
Expand Up @@ -37,6 +37,8 @@ def __init__(self, fname_pmu):
self.stop_time_mdh = attributes['stop_time_mdh']
self.start_time_mpcu = attributes['start_time_mpcu']
self.stop_time_mpcu = attributes['stop_time_mpcu']
self.max = attributes['max']
self.min = attributes['min']

def read_resp(self, fname_pmu):
"""
Expand Down Expand Up @@ -114,7 +116,9 @@ def read_resp(self, fname_pmu):
'start_time_mdh': start_time_mdh,
'stop_time_mdh': stop_time_mdh,
'start_time_mpcu': start_time_mpcu,
'stop_time_mpcu': stop_time_mpcu
'stop_time_mpcu': stop_time_mpcu,
'max': 4095,
'min': 0
}

return attributes
Expand Down

0 comments on commit de91ba6

Please sign in to comment.