diff --git a/mu_map/recon/filter.py b/mu_map/recon/filter.py new file mode 100644 index 0000000000000000000000000000000000000000..ffcc956f4331cb9c00e541d0be6655ce05ce436f --- /dev/null +++ b/mu_map/recon/filter.py @@ -0,0 +1,44 @@ +from mu_map.file.interfile import Interfile, InterfileKeys + +TEMPLATE_FILTER_GAUSSIAN_PARAMS = """ +post-filter type := Separable Gaussian +separable gaussian filter parameters := + x-dir filter fwhm (in mm) := {FWHM_X} + y-dir filter fwhm (in mm) := {FWHM_Y} + z-dir filter fwhm (in mm) := {FWHM_Z} + x-dir maximum kernel size := {SIZE_X} + y-dir maximum kernel size := {SIZE_Y} + z-dir maximum kernel size := {SIZE_Z} + Normalise filter to 1 := 1 +end separable gaussian filter parameters := +""" + +class GaussianFilter: + + def __init__(self, projection: Interfile, width_scale: int = 1.0): + header, _ = projection + + self.params = TEMPLATE_FILTER_GAUSSIAN_PARAMS + + spacing_x = float(header[InterfileKeys.spacing(1)]) * width_scale + spacing_y = float(header[InterfileKeys.spacing(1)]) * width_scale + spacing_z = float(header[InterfileKeys.spacing(2)]) * width_scale + self.params = self.params.replace("{FWHM_X}", f"{spacing_x:.4f}") + self.params = self.params.replace("{FWHM_Y}", f"{spacing_y:.4f}") + self.params = self.params.replace("{FWHM_Z}", f"{spacing_z:.4f}") + + self.params = self.params.replace("{SIZE_X}", header[InterfileKeys.dim(1)]) + self.params = self.params.replace("{SIZE_Y}", header[InterfileKeys.dim(1)]) + self.params = self.params.replace("{SIZE_Z}", header[InterfileKeys.dim(2)]) + + def insert(self, osem_params: str): + lines = self.params.strip().split("\n") + lines = list(map(lambda line: " " + line, lines)) + lines.append("\n") + + osem_lines = osem_params.strip().split("\n") + res = osem_lines[:-1] + lines + osem_lines[-1:] + return "\n".join(res) + + def __repr__(self): + return self.params