Skip to content

Commit

Permalink
Added codes
Browse files Browse the repository at this point in the history
  • Loading branch information
chaithyagr committed Apr 30, 2024
1 parent 6eb2619 commit 6efe296
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
38 changes: 34 additions & 4 deletions src/mri/cli/dc_adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from mrinufft.io import read_trajectory
from mri.operators import NonCartesianFFT
from mri.operators.fourier.utils import estimate_density_compensation
from mri.cli.utils import save_data
from mrinufft.io.nsp import read_arbgrad_rawdat
from mrinufft.io.utils import add_phase_to_kspace_with_shifts
from mrinufft.extras.utils import get_smaps
from pymrt.recipes.coils import compress_svd

import numpy as np
import pickle as pkl
import logging
import os

Expand Down Expand Up @@ -51,13 +51,23 @@
implementation="gpuNUFFT",
name="gpu",
)
fourier_store(
fourier_op_config,
upsampfac=1,
implementation="gpuNUFFT",
name="gpu_lowmem",
)

smaps_store = store(group="fourier/smaps")
smaps_store(smaps_config, name="low_frequency")
density_store = store(group="fourier/density_comp")
density_store(density_est_config, implementation="pipe", name="pipe")
density_store(density_est_config, implementation="pipe", osf=1, name="pipe_lowmem")


def recon(obs_file: str, traj_file: str, obs_reader, traj_reader, fourier, coil_compress: str|int = -1):

def recon(obs_file: str, traj_file: str, obs_reader, traj_reader, fourier, coil_compress: str|int = -1,
output_filename: str = "dc_adjoint.pkl"):
"""
Reconstructs an image using the adjoint operator.
Expand All @@ -78,7 +88,13 @@ def recon(obs_file: str, traj_file: str, obs_reader, traj_reader, fourier, coil_
coil_compress : str|int, optional default -1
The number of singular values to keep in the coil compression.
If -1, coil compression is not applied
output_filename: str, optional default 'dc_adjoint.pkl'
The output file name with the right extension.
It can be:
1) *.pkl / *.mat: Holds the reconstructed results saved in dictionary as `recon`.
#TODO: Add scope for debug by saving intermediate results also in output.
2) *.nii : NIFTI file holding the reconstructed images.
Returns
-------
None
Expand Down Expand Up @@ -117,7 +133,7 @@ def recon(obs_file: str, traj_file: str, obs_reader, traj_reader, fourier, coil_
recon = fourier_op.adj_op(kspace_data)
if not fourier_op.uses_sense:
recon = np.linalg.norm(recon, axis=-1)
pkl.dump(recon, open("dc_adjoint.pkl", "wb"))
save_data(output_filename, recon, data_header)

store(
recon,
Expand All @@ -131,6 +147,20 @@ def recon(obs_file: str, traj_file: str, obs_reader, traj_reader, fourier, coil_
],
name="dc_adjoint",
)
store(
recon,
obs_reader=raw_config,
traj_reader=traj_config,
hydra_defaults=[
"_self_",
{"fourier": "gpu_lowmem"},
{"fourier/density_comp": "pipe_lowmem"},
{"fourier/smaps": "low_frequency"},
],
name="dc_adjoint_lowmem",
)



store.add_to_hydra_store()
zen(recon).hydra_main(
Expand Down
5 changes: 4 additions & 1 deletion src/mri/operators/fourier/non_cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class NonCartesianFFT(OperatorBase):
"""This class wraps around different implementation algorithms for NFFT"""
def __init__(self, samples, shape, implementation='finufft', n_coils=1,
density_comp=None, smaps=None, **kwargs):
density_comp=None, smaps=None, upsampfac: float|int = 2, **kwargs):
""" A small wrapper around mri-nufft package
This is mostly maintained just for legacy reasons (all legacy reconstruction
uses this codes)
Expand All @@ -44,6 +44,8 @@ def __init__(self, samples, shape, implementation='finufft', n_coils=1,
density_comp: np.ndarray (M,) default None
smaps: np.ndarray, optional, default None
The sensitivity maps.
upsampfac: float|int, optional, default 2
The oversampling factor to be used
kwargs: extra keyword args
these arguments are passed to underlying operator. Check the docs of
mrinufft : https://mind-inria.github.io/mri-nufft/
Expand All @@ -60,6 +62,7 @@ def __init__(self, samples, shape, implementation='finufft', n_coils=1,
density=self.density_comp,
n_coils=self.n_coils,
smaps=smaps,
upsampfac=upsampfac,
**self.kwargs
)

Expand Down
6 changes: 5 additions & 1 deletion src/mri/operators/fourier/utils/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ def check_if_fourier_op_uses_sense(fourier_op):
return False


def estimate_density_compensation(kspace_loc, volume_shape, implementation='pipe', **kwargs):
def estimate_density_compensation(kspace_loc, volume_shape, implementation='pipe',
osf: float|int = 2, **kwargs):
""" Utils function to obtain the density compensator for a
given set of kspace locations.
Expand All @@ -277,13 +278,16 @@ def estimate_density_compensation(kspace_loc, volume_shape, implementation='pipe
implementation: str default 'pipe'
the implementation of the non-cartesian operator
can be 'pipe' which needs gpuNUFFT or 'cell_count'
osf: float|int default 2
the oversampling factor used in estimation
kwargs: dict
extra keyword arguments to be passed to the density
compensation estimation
"""
density_comp = get_density(implementation)(
kspace_loc,
volume_shape,
osf=osf,
**kwargs
)
return np.squeeze(density_comp)

0 comments on commit 6efe296

Please sign in to comment.