Skip to content

Commit

Permalink
Add pre-processing flag and functions applicable to fMOST datastes (#61)
Browse files Browse the repository at this point in the history
* Add pre-processing functions for fMOST datasets

* Add command-line option for pre-processing

* Add opencv to dependencies

* Corrections for CI/CD

* Docstrings, refactoring

* Save pre-processed image in brainreg output directory

* Replace opencv connected components by skimage label and regionprops

* Add test to check background removal

* Corrections for PR review

* Format code with black

* remove fmost preprocessing

Co-authored-by: Adam Tyson <[email protected]>
  • Loading branch information
noisysky and adamltyson authored Nov 16, 2022
1 parent c9ef993 commit 2b9474a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion brainreg/backend/niftyreg/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def run_niftyreg(
DATA_ORIENTATION,
ATLAS_ORIENTATION,
niftyreg_args,
preprocessing_args,
scaling,
load_parallel,
sort_input_file,
Expand Down Expand Up @@ -78,7 +79,7 @@ def run_niftyreg(
paths.downsampled_brain_path,
)

target_brain = preprocess.filter_image(target_brain)
target_brain = preprocess.filter_image(target_brain, preprocessing_args)
save_nii(
target_brain, atlas.resolution, niftyreg_paths.downsampled_filtered
)
Expand Down
20 changes: 20 additions & 0 deletions brainreg/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def register_cli_parser():
parser = pixel_parser(parser)
parser = geometry_parser(parser)
parser = misc_parse(parser)
parser = preprocessing_parser(parser)

return parser

Expand Down Expand Up @@ -177,6 +178,24 @@ def geometry_parser(parser):
return parser


def preprocessing_parser(parser):
preprocessing_opt_parser = parser.add_argument_group(
"Pre-processing options"
)

preprocessing_opt_parser.add_argument(
"--pre-processing",
dest="preprocessing",
type=str,
default="default",
required=False,
help="Pre-processing method to be applied before registration. "
"Possible values: skip, default.",
)

return parser


def prep_registration(args):
logging.debug("Making registration directory")
ensure_directory_exists(args.brainreg_directory)
Expand Down Expand Up @@ -219,6 +238,7 @@ def main():
paths,
args.voxel_sizes,
arg_groups["NiftyReg registration backend options"],
arg_groups["Pre-processing options"],
sort_input_file=args.sort_input_file,
n_free_cpus=args.n_free_cpus,
additional_images_downsample=additional_images_downsample,
Expand Down
2 changes: 2 additions & 0 deletions brainreg/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def main(
paths,
voxel_sizes,
niftyreg_args,
preprocessing_args,
n_free_cpus=2,
sort_input_file=False,
additional_images_downsample=None,
Expand Down Expand Up @@ -70,6 +71,7 @@ def main(
data_orientation,
atlas.metadata["orientation"],
niftyreg_args,
preprocessing_args,
scaling,
load_parallel,
sort_input_file,
Expand Down
9 changes: 6 additions & 3 deletions brainreg/utils/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
from tqdm import trange


def filter_image(brain):
def filter_image(brain, preprocessing_args=None):
"""
Filter a 3D image to allow registration
:return: The filtered brain
:rtype: np.array
"""
brain = brain.astype(np.float64, copy=False)
for i in trange(brain.shape[-1], desc="filtering", unit="plane"):
brain[..., i] = filter_plane(brain[..., i])
if preprocessing_args and preprocessing_args.preprocessing == "skip":
pass
else: # default pre-processing
for i in trange(brain.shape[-1], desc="filtering", unit="plane"):
brain[..., i] = filter_plane(brain[..., i])
brain = scale_and_convert_to_16_bits(brain)
return brain

Expand Down

0 comments on commit 2b9474a

Please sign in to comment.