Commit 11f8e235 authored by Pierre Paleo's avatar Pierre Paleo
Browse files

Replace text file with iterable

Ignored projections will be provided directly with an iterable rather than in a file to read
parent bac27908
Pipeline #36711 passed with stages
in 10 minutes and 12 seconds
...@@ -37,7 +37,7 @@ import copy ...@@ -37,7 +37,7 @@ import copy
from lxml import etree from lxml import etree
import json import json
import io import io
from typing import Union from typing import Union, Iterable
from ..scanbase import TomoScanBase from ..scanbase import TomoScanBase
from .utils import get_parameters_frm_par_or_info, extract_urls_from_edf from .utils import get_parameters_frm_par_or_info, extract_urls_from_edf
from ..unitsystem.metricsystem import MetricSystem from ..unitsystem.metricsystem import MetricSystem
...@@ -75,7 +75,7 @@ class EDFTomoScan(TomoScanBase): ...@@ -75,7 +75,7 @@ class EDFTomoScan(TomoScanBase):
self, self,
scan: Union[str, None], scan: Union[str, None],
n_frames: Union[int, None] = None, n_frames: Union[int, None] = None,
ignore_projections: Union[None, str] = None, ignore_projections: Union[None, Iterable] = None,
): ):
TomoScanBase.__init__( TomoScanBase.__init__(
self, scan=scan, type_=self._TYPE, ignore_projections=ignore_projections self, scan=scan, type_=self._TYPE, ignore_projections=ignore_projections
...@@ -269,8 +269,8 @@ class EDFTomoScan(TomoScanBase): ...@@ -269,8 +269,8 @@ class EDFTomoScan(TomoScanBase):
else: else:
self._projections = all_projections self._projections = all_projections
self._alignment_projections = {} self._alignment_projections = {}
if self._ignored_projections is not None: if self.ignore_projections is not None:
for idx in self._ignored_projections: for idx in self.ignore_projections:
self._projections.pop(idx, None) self._projections.pop(idx, None)
self._darks = EDFTomoScan.get_darks_url(self.path) self._darks = EDFTomoScan.get_darks_url(self.path)
......
...@@ -118,7 +118,7 @@ class HDF5TomoScan(TomoScanBase): ...@@ -118,7 +118,7 @@ class HDF5TomoScan(TomoScanBase):
scan: str, scan: str,
entry: str = None, entry: str = None,
index: typing.Union[int, None] = 0, index: typing.Union[int, None] = 0,
ignore_projections: typing.Union[None, str] = None, ignore_projections: typing.Union[None, typing.Iterable] = None,
): ):
if entry is not None: if entry is not None:
index = None index = None
...@@ -336,8 +336,8 @@ class HDF5TomoScan(TomoScanBase): ...@@ -336,8 +336,8 @@ class HDF5TomoScan(TomoScanBase):
if self._projections is None: if self._projections is None:
if self.frames: if self.frames:
ignored_projs = [] ignored_projs = []
if self._ignored_projections is not None: if self.ignore_projections is not None:
ignored_projs = self._ignored_projections ignored_projs = self.ignore_projections
proj_frames = tuple( proj_frames = tuple(
filter( filter(
lambda x: ( lambda x: (
......
...@@ -607,13 +607,6 @@ class TestIgnoredProjections(unittest.TestCase): ...@@ -607,13 +607,6 @@ class TestIgnoredProjections(unittest.TestCase):
def setUp(self) -> None: def setUp(self) -> None:
self.folder = tempfile.mkdtemp() self.folder = tempfile.mkdtemp()
self.ignored_projs = [5, 6, 7] self.ignored_projs = [5, 6, 7]
self.ignored_projs_fname = os.path.join(self.folder, "ignored_projections.txt")
numpy.savetxt(
self.ignored_projs_fname,
self.ignored_projs,
header="Indices of ignored projections",
comments="#",
)
def tearDown(self) -> None: def tearDown(self) -> None:
shutil.rmtree(self.folder) shutil.rmtree(self.folder)
...@@ -623,10 +616,7 @@ class TestIgnoredProjections(unittest.TestCase): ...@@ -623,10 +616,7 @@ class TestIgnoredProjections(unittest.TestCase):
scan_path=self.folder, n_radio=15, n_ini_radio=10, n_extra_radio=0, dim=10 scan_path=self.folder, n_radio=15, n_ini_radio=10, n_extra_radio=0, dim=10
) )
mock.end_acquisition() mock.end_acquisition()
scan = EDFTomoScan( scan = EDFTomoScan(scan=self.folder, ignore_projections=self.ignored_projs)
scan=self.folder, ignore_projections=self.ignored_projs_fname
)
self.assertTrue(numpy.allclose(scan._ignored_projections, self.ignored_projs))
for idx in self.ignored_projs: for idx in self.ignored_projs:
self.assertFalse( self.assertFalse(
idx in scan.projections, idx in scan.projections,
......
...@@ -318,24 +318,12 @@ class TestIgnoredProjections(HDF5TestBaseClass): ...@@ -318,24 +318,12 @@ class TestIgnoredProjections(HDF5TestBaseClass):
super(TestIgnoredProjections, self).setUp() super(TestIgnoredProjections, self).setUp()
self.dataset_file = self.get_dataset("frm_edftomomill_oneentry.nx") self.dataset_file = self.get_dataset("frm_edftomomill_oneentry.nx")
self.ignored_projs = [387, 388, 389, 390, 391, 392, 393, 394, 395, 396] self.ignored_projs = [387, 388, 389, 390, 391, 392, 393, 394, 395, 396]
self.ignored_projs_fname = os.path.join(
self.test_dir, "ignored_projections.txt"
)
numpy.savetxt(
self.ignored_projs_fname,
self.ignored_projs,
header="Indices of ignored projections",
comments="#",
)
def testIgnoreProjections(self): def testIgnoreProjections(self):
self.scan = HDF5TomoScan( self.scan = HDF5TomoScan(
scan=self.dataset_file, ignore_projections=self.ignored_projs_fname scan=self.dataset_file, ignore_projections=self.ignored_projs
) )
self.scan._projections = None self.scan._projections = None
self.assertTrue(
numpy.allclose(self.scan._ignored_projections, self.ignored_projs)
)
for idx in self.ignored_projs: for idx in self.ignored_projs:
self.assertFalse( self.assertFalse(
idx in self.scan.projections, idx in self.scan.projections,
......
...@@ -32,7 +32,7 @@ import os ...@@ -32,7 +32,7 @@ import os
import typing import typing
import logging import logging
import numpy import numpy
from typing import Union from typing import Union, Iterable
from collections import OrderedDict from collections import OrderedDict
from .unitsystem.metricsystem import MetricSystem from .unitsystem.metricsystem import MetricSystem
from silx.utils.enum import Enum as _Enum from silx.utils.enum import Enum as _Enum
...@@ -74,7 +74,7 @@ class TomoScanBase: ...@@ -74,7 +74,7 @@ class TomoScanBase:
self, self,
scan: Union[None, str], scan: Union[None, str],
type_: str, type_: str,
ignore_projections: Union[None, str] = None, ignore_projections: Union[None, Iterable] = None,
): ):
self.path = scan self.path = scan
self._type = type_ self._type = type_
...@@ -92,7 +92,7 @@ class TomoScanBase: ...@@ -92,7 +92,7 @@ class TomoScanBase:
self._flats_weights = None self._flats_weights = None
"""list flats indexes to use for flat field correction and associate """list flats indexes to use for flat field correction and associate
weights""" weights"""
self._get_ignored_projections(ignore_projections) self.ignore_projections = ignore_projections
def clear_caches(self): def clear_caches(self):
"""clear caches. Might be call if some data changed after """clear caches. Might be call if some data changed after
...@@ -634,15 +634,3 @@ class TomoScanBase: ...@@ -634,15 +634,3 @@ class TomoScanBase:
for proj_index in self.projections: for proj_index in self.projections:
res[proj_index] = get_weights(proj_index=proj_index) res[proj_index] = get_weights(proj_index=proj_index)
return res return res
def _get_ignored_projections(self, ignore_projections):
self.ignore_projections = ignore_projections
self._ignored_projections = None
if ignore_projections is not None:
self._ignored_projections = numpy.loadtxt(ignore_projections, comments="#")
if self._ignored_projections.ndim > 1:
raise ValueError(
"Expected exactly one row or one column in file %s"
% ignore_projections
)
self._ignored_projections = self._ignored_projections.astype(numpy.uint32)
Supports Markdown
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