-
Notifications
You must be signed in to change notification settings - Fork 59
/
zscore.py
85 lines (71 loc) · 2.44 KB
/
zscore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Z-score normalize image (voxel-wise subtract mean, divide by standard deviation)
Author: Jacob Reinhold <[email protected]>
Created on: 01 Jun 2021
"""
from __future__ import annotations
__all__ = ["ZScoreNormalize"]
import argparse
import typing
import intensity_normalization.errors as intnorme
import intensity_normalization.normalize.base as intnormb
import intensity_normalization.typing as intnormt
class ZScoreNormalize(intnormb.LocationScaleCLIMixin, intnormb.SingleImageNormalizeCLI):
def __init__(self, *, norm_value: float = 1.0, **kwargs: typing.Any):
"""Voxel-wise subtract the mean and divide by the standard deviation."""
super().__init__(norm_value=norm_value, **kwargs)
self.voi: intnormt.ImageLike | None = None
def calculate_location(
self,
image: intnormt.ImageLike,
/,
mask: intnormt.ImageLike | None = None,
*,
modality: intnormt.Modality = intnormt.Modality.T1,
) -> float:
if self.voi is None:
raise intnorme.NormalizationError("'voi' needs to be set.")
loc: float = float(self.voi.mean())
return loc
def calculate_scale(
self,
image: intnormt.ImageLike,
/,
mask: intnormt.ImageLike | None = None,
*,
modality: intnormt.Modality = intnormt.Modality.T1,
) -> float:
if self.voi is None:
raise intnorme.NormalizationError("'voi' needs to be set.")
scale: float = float(self.voi.std())
return scale
def setup(
self,
image: intnormt.ImageLike,
/,
mask: intnormt.ImageLike | None = None,
*,
modality: intnormt.Modality = intnormt.Modality.T1,
) -> None:
self.voi = self._get_voi(image, mask, modality=modality)
def teardown(self) -> None:
del self.voi
self.voi = None
@staticmethod
def name() -> str:
return "zscore"
@staticmethod
def fullname() -> str:
return "Z-Score"
@staticmethod
def description() -> str:
return "Standardize an MR image by the foreground intensities."
def plot_histogram_from_args(
self,
args: argparse.Namespace,
/,
normalized: intnormt.ImageLike,
mask: intnormt.ImageLike | None = None,
) -> None:
if mask is None:
mask = self.estimate_foreground(normalized)
super().plot_histogram_from_args(args, normalized, mask)