Commit 70f58d32 authored by payno's avatar payno
Browse files

[patch-nx] add an option to convert automatically all frame of a given type...

[patch-nx] add an option to convert automatically all frame of a given type (dark, flat, projection...)

close #82
parent 89bedd41
Pipeline #63102 passed with stages
in 3 minutes and 2 seconds
......@@ -66,6 +66,8 @@ Those are some examples of usage:
# force frames 26 and 27 to be dark
nxtomomill patch-nx --update-to-dark 26:28 simple_case.h5 entry
# force all projections to be dark
nxtomomill patch-nx --update-to-dark projection simple_case.h5 entry
# force frames 10, 13, 16 and 19 to be flatfield
nxtomomill patch-nx --update-to-flat 10:20:3 simple_case.h5 entry
# force frame 0, 1 and to 4 to be projections
......@@ -74,6 +76,8 @@ Those are some examples of usage:
nxtomomill patch-nx --update-to-alignment 0 simple_case.h5 entry
# force all frames to be invalid
nxtomomill patch-nx --invalid-frames : simple_case.h5 entry
# force all "projection" frame to be invalid
nxtomomill patch-nx --invalid-frames projection simple_case.h5 entry
.. note:: If you try to modify several frame type at the same type you should know that the order of resolution is:
......
......@@ -73,6 +73,8 @@ __date__ = "12/10/2020"
import logging
from nxtomomill import utils
from silx.io.url import DataUrl
from silx.utils.enum import Enum as _Enum
from tomoscan.esrf.hdf5scan import HDF5TomoScan
from nxtomomill.utils import ImageKey
import argparse
import os
......@@ -92,10 +94,36 @@ _INFO_URL = (
"of just by giving dataset_path@file_path".format(_SILX_DATA_URL)
)
class _ImageKeyName(_Enum):
ALIGNMENT = "alignment"
PROJECTION = "projection"
FLAT_FIELD = "flat"
DARK_FIELD = "dark"
INVALID = "invalid"
@staticmethod
def to_image_key(image_key) -> ImageKey:
image_key = _ImageKeyName.from_value(image_key.lower())
if image_key is _ImageKeyName.ALIGNMENT:
return ImageKey.ALIGNMENT
elif image_key is _ImageKeyName.PROJECTION:
return ImageKey.PROJECTION
elif image_key is _ImageKeyName.DARK_FIELD:
return ImageKey.DARK_FIELD
elif image_key is _ImageKeyName.FLAT_FIELD:
return ImageKey.FLAT_FIELD
elif image_key is _ImageKeyName.INVALID:
return ImageKey.INVALID
else:
raise ValueError(f"{image_key} not handled")
_INFO_FRAME_INPUT = (
"Frames can be provided two ways: \n"
"Frames can be provided three ways: \n"
"- has a list: frame_index_1,frame_index_2\n"
"- has a python slice: from:to:steps\n"
f"- has an image key value. Valid values are {_ImageKeyName.values()}\n"
)
......@@ -131,7 +159,7 @@ def _extract_data_url(url_as_a_str):
return url
def _get_slice_to_modify(slice_as_str):
def _get_slice_to_modify(slice_as_str, master_file, entry):
"""
Return a list of int or a `slice` from slice_as_str
:param slice_as_str:
......@@ -140,6 +168,15 @@ def _get_slice_to_modify(slice_as_str):
"""
if slice_as_str is None:
return None
elif slice_as_str.lower() in _ImageKeyName.values():
image_key = _ImageKeyName.to_image_key(slice_as_str)
scan = HDF5TomoScan(master_file, entry)
frames = scan.frames
slices = []
for frame in frames:
if frame.image_key is image_key:
slices.append(frame.index)
return slices
elif ":" in slice_as_str:
elmts = slice_as_str.split(":")
......@@ -204,7 +241,7 @@ def main(argv):
parser.add_argument(
"--invalid-frames",
default=None,
help="Define the set of frames to be mark as invalid. " "" + _INFO_FRAME_INPUT,
help="Define the set of frames to be mark as invalid. " + _INFO_FRAME_INPUT,
)
parser.add_argument(
"--update-to-projection",
......@@ -216,12 +253,12 @@ def main(argv):
parser.add_argument(
"--update-to-dark",
default=None,
help="Define the set of frames to be mark as dark. " "" + _INFO_FRAME_INPUT,
help="Define the set of frames to be mark as dark. " + _INFO_FRAME_INPUT,
)
parser.add_argument(
"--update-to-flat",
default=None,
help="Define the set of frames to be mark as flat. " "" + _INFO_FRAME_INPUT,
help="Define the set of frames to be mark as flat. " + _INFO_FRAME_INPUT,
)
parser.add_argument(
"--update-to-alignment",
......@@ -247,11 +284,21 @@ def main(argv):
patch_det_data = darks_start_url or flat_start_url or flat_end_url or darks_end_url
# get information for modifying image_key
slice_to_update_to_dark = _get_slice_to_modify(options.update_to_dark)
slice_to_update_to_flat = _get_slice_to_modify(options.update_to_flat)
slice_to_update_to_projection = _get_slice_to_modify(options.update_to_projection)
slice_to_update_to_alignment = _get_slice_to_modify(options.update_to_alignment)
slice_to_invalid = _get_slice_to_modify(options.invalid_frames)
slice_to_update_to_dark = _get_slice_to_modify(
options.update_to_dark, master_file=options.file_path, entry=options.entry
)
slice_to_update_to_flat = _get_slice_to_modify(
options.update_to_flat, master_file=options.file_path, entry=options.entry
)
slice_to_update_to_projection = _get_slice_to_modify(
options.update_to_projection, master_file=options.file_path, entry=options.entry
)
slice_to_update_to_alignment = _get_slice_to_modify(
options.update_to_alignment, master_file=options.file_path, entry=options.entry
)
slice_to_invalid = _get_slice_to_modify(
options.invalid_frames, master_file=options.file_path, entry=options.entry
)
patch_image_key = (
slice_to_update_to_dark
or slice_to_update_to_flat
......
......@@ -55,18 +55,7 @@ import uuid
from silx.io.utils import h5py_read_dataset
import h5py._hl.selections as selection
from h5py import h5s as h5py_h5s
class ImageKey(_Enum):
"""
Possible values of image_key_control
"""
ALIGNMENT = -1
PROJECTION = 0
FLAT_FIELD = 1
DARK_FIELD = 2
INVALID = 3
from tomoscan.esrf.hdf5scan import ImageKey
class FieldOfView(_Enum):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment