|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import itk |
| 4 | +from itk import RTK as rtk |
| 5 | + |
| 6 | + |
| 7 | +def build_parser(): |
| 8 | + parser = rtk.RTKArgumentParser(description="Estimate I0 from projections") |
| 9 | + |
| 10 | + parser.add_argument( |
| 11 | + "--verbose", "-v", action="store_true", help="Verbose execution" |
| 12 | + ) |
| 13 | + parser.add_argument("--debug", "-d", help="Debug CSV output file name") |
| 14 | + parser.add_argument( |
| 15 | + "--range", |
| 16 | + nargs=3, |
| 17 | + type=int, |
| 18 | + help="Range of projection to analyse min step max", |
| 19 | + metavar=("MIN", "STEP", "MAX"), |
| 20 | + ) |
| 21 | + |
| 22 | + parser.add_argument( |
| 23 | + "--lambda", |
| 24 | + "-l", |
| 25 | + dest="lambda_", |
| 26 | + type=float, |
| 27 | + default=0.8, |
| 28 | + help="RLS estimate coefficient", |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "--expected", |
| 32 | + "-e", |
| 33 | + dest="expected", |
| 34 | + type=int, |
| 35 | + default=65535, |
| 36 | + help="Expected I0 value", |
| 37 | + ) |
| 38 | + |
| 39 | + rtk.add_rtkinputprojections_group(parser) |
| 40 | + |
| 41 | + return parser |
| 42 | + |
| 43 | + |
| 44 | +def process(args_info: argparse.Namespace): |
| 45 | + Dimension = 3 |
| 46 | + InputImageType = itk.Image[itk.F, Dimension] |
| 47 | + USImageType = itk.Image[itk.US, Dimension] |
| 48 | + |
| 49 | + reader = rtk.ProjectionsReader[InputImageType].New() |
| 50 | + rtk.SetProjectionsReaderFromArgParse(reader, args_info) |
| 51 | + reader.SetFileNames(rtk.GetProjectionsFileNamesFromArgParse(args_info)) |
| 52 | + reader.UpdateOutputInformation() |
| 53 | + |
| 54 | + subsetRegion = reader.GetOutput().GetLargestPossibleRegion() |
| 55 | + |
| 56 | + ExtractFilterType = itk.ExtractImageFilter[InputImageType, InputImageType] |
| 57 | + extract = ExtractFilterType.New() |
| 58 | + extract.InPlaceOff() |
| 59 | + extract.SetDirectionCollapseToSubmatrix() |
| 60 | + extract.SetInput(reader.GetOutput()) |
| 61 | + |
| 62 | + extractSize = list(subsetRegion.GetSize()) |
| 63 | + extractSize[2] = 1 |
| 64 | + |
| 65 | + istep = 1 |
| 66 | + imin = 1 |
| 67 | + imax = subsetRegion.GetSize()[2] |
| 68 | + if args_info.range is not None: |
| 69 | + rmin, rstep, rmax = args_info.range |
| 70 | + if (rmin <= rmax) and (istep <= (rmax - rmin)): |
| 71 | + imin = rmin |
| 72 | + istep = rstep |
| 73 | + imax = min(rmax, imax) |
| 74 | + |
| 75 | + i0est = rtk.I0EstimationProjectionFilter[USImageType, USImageType, 2].New() |
| 76 | + if args_info.lambda_ is not None: |
| 77 | + i0est.SetLambda(args_info.lambda_) |
| 78 | + if args_info.expected is not None and args_info.expected != 65535: |
| 79 | + i0est.SetExpectedI0(args_info.expected) |
| 80 | + i0est.SaveHistogramsOn() |
| 81 | + |
| 82 | + I0buffer = [] |
| 83 | + |
| 84 | + CastFilterType = itk.CastImageFilter[InputImageType, USImageType] |
| 85 | + cast = CastFilterType.New() |
| 86 | + cast.SetInput(extract.GetOutput()) |
| 87 | + |
| 88 | + start = list(subsetRegion.GetIndex()) |
| 89 | + for i in range(imin, imax, istep): |
| 90 | + start[2] = i |
| 91 | + desiredRegion = itk.ImageRegion[Dimension]() |
| 92 | + desiredRegion.SetIndex(start) |
| 93 | + desiredRegion.SetSize(extractSize) |
| 94 | + |
| 95 | + extract.SetExtractionRegion(desiredRegion) |
| 96 | + |
| 97 | + i0est.SetInput(cast.GetOutput()) |
| 98 | + i0est.UpdateLargestPossibleRegion() |
| 99 | + |
| 100 | + I0buffer.append(i0est.GetI0()) |
| 101 | + I0buffer.append(i0est.GetI0rls()) |
| 102 | + I0buffer.append(i0est.GetI0fwhm()) |
| 103 | + |
| 104 | + if args_info.debug: |
| 105 | + with open(args_info.debug, "w") as f: |
| 106 | + f.write(",".join(str(v) for v in I0buffer)) |
| 107 | + |
| 108 | + return 0 |
| 109 | + |
| 110 | + |
| 111 | +def main(argv=None): |
| 112 | + parser = build_parser() |
| 113 | + args_info = parser.parse_args(argv) |
| 114 | + return process(args_info) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments