Skip to content

Commit

Permalink
Fix HDF format string to be reflected in dataset table
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Jun 8, 2017
1 parent 4627240 commit f107bf4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
17 changes: 7 additions & 10 deletions malcolm/parts/ADCore/datasetrunnablechildpart.py
Expand Up @@ -13,34 +13,31 @@ def update_configure_validate_args(self):
# Decorate validate and configure with the sum of its parts
method_metas = [self.child["configure"],
DatasetRunnableChildPart.configure.MethodMeta]
without = ["filePath"]
without = ["formatName"]
self.method_metas["validate"].recreate_from_others(
method_metas, without)
self.method_metas["configure"].recreate_from_others(
method_metas, without)

def _params_with_file_path(self, params):
file_path = os.path.join(params.fileDir, self.name + ".h5")
filtered_params = {k: v for k, v in params.items() if k != "fileDir"}
def _params_with_format_name(self, params):
params = self.child["configure"].prepare_input_map(
filePath=file_path, **filtered_params)
formatName=self.name, **params)
return params

# MethodMeta will be filled in by _update_configure_args
@RunnableController.Validate
@method_takes()
def validate(self, task, part_info, params):
params = self._params_with_file_path(params)
params = self._params_with_format_name(params)
return super(DatasetRunnableChildPart, self).validate(
task, part_info, params)

# MethodMeta will be filled in at reset()
@RunnableController.Configure
@method_takes(
"fileDir", StringMeta("File dir to write HDF files into"), REQUIRED)
@method_takes()
def configure(self, task, completed_steps, steps_to_do, part_info, params):
if "filePath" in self.child["configure"].takes.elements:
params = self._params_with_file_path(params)
if "formatName" in self.child["configure"].takes.elements:
params = self._params_with_format_name(params)
task.post(self.child["configure"], params)
datasets_table = self.child.datasets
info_list = []
Expand Down
20 changes: 11 additions & 9 deletions malcolm/parts/ADCore/hdfwriterpart.py
Expand Up @@ -119,21 +119,23 @@ def reset(self, task):
@RunnableController.Configure
@method_takes(
"generator", PointGeneratorMeta("Generator instance"), REQUIRED,
"filePath", StringMeta("Full file path to write data to"), REQUIRED,
"fileDir", StringMeta("Directory to write hdf file to"), REQUIRED,
"formatName", StringMeta(
"Name argument for fileTemplate, normally filename without extension"),
REQUIRED,
"fileTemplate", StringMeta(
"""Printf style template to generate full path from. Arguments are:
1) %s: directory name including trailing slash from filePath
2) %s: file name from filePath
"""), "%s%s")
"""Printf style template to generate filename relative to fileDir.
Arguments are:
1) %s: the value of formatName"""), "%s.h5")
def configure(self, task, completed_steps, steps_to_do, part_info, params):
self.done_when_reaches = completed_steps + steps_to_do
# For first run then open the file
# Enable position mode before setting any position related things
task.put(self.child["positionMode"], True)
# Setup our required settings
# TODO: this should be different for windows detectors
file_path = params.filePath.rstrip(os.sep)
file_dir, filename = file_path.rsplit(os.sep, 1)
file_dir = params.fileDir.rstrip(os.sep)
filename = params.fileTemplate % params.formatName
assert "." in filename, \
"File extension for %r should be supplied" % filename
futures = task.put_many_async(self.child, dict(
Expand All @@ -145,8 +147,8 @@ def configure(self, task, completed_steps, steps_to_do, part_info, params):
lazyOpen=True,
arrayCounter=0,
filePath=file_dir + os.sep,
fileName=filename,
fileTemplate=params.fileTemplate))
fileName=params.formatName,
fileTemplate="%s" + params.fileTemplate))
futures += self._set_dimensions(task, params.generator)
xml = self._make_layout_xml(params.generator, part_info)
self.layout_filename = os.path.join(
Expand Down
5 changes: 2 additions & 3 deletions malcolm/parts/ADCore/statspluginpart.py
Expand Up @@ -30,15 +30,14 @@ def _make_attributes_xml(self):

@RunnableController.Configure
@method_takes(
"filePath", StringMeta("File path to write data to"), REQUIRED)
"fileDir", StringMeta("File directory to write data to"), REQUIRED)
def configure(self, task, completed_steps, steps_to_do, part_info, params):
file_dir, filename = params.filePath.rsplit(os.sep, 1)
fs = task.put_many_async(self.child, dict(
enableCallbacks=True,
computeStatistics=True))
xml = self._make_attributes_xml()
self.attributes_filename = os.path.join(
file_dir, "%s-attributes.xml" % self.params.mri)
params.fileDir, "%s-attributes.xml" % self.params.mri)
open(self.attributes_filename, "w").write(xml)
fs += task.put_async(self.child["attributesFile"],
self.attributes_filename)
Expand Down
19 changes: 10 additions & 9 deletions tests/test_parts/test_ADCore/test_hdfwriterpart.py
Expand Up @@ -37,8 +37,9 @@ def test_configure(self):
spiral = SpiralGenerator(
["x", "y"], ["mm", "mm"], [0., 0.], 5., scale=2.0)
params.generator = CompoundGenerator([energy, spiral], [], [], 0.1)
params.filePath = "/tmp/file.h5"
params.fileTemplate = "%sthing-%s"
params.fileDir = "/tmp"
params.formatName = "file"
params.fileTemplate = "thing-%s.h5"
params.generator.prepare()
completed_steps = 0
steps_to_do = 38
Expand All @@ -50,33 +51,33 @@ def test_configure(self):
task, completed_steps, steps_to_do, part_info, params)
self.assertEqual(len(infos), 5)
self.assertEquals(infos[0].name, "xspress3.data")
self.assertEquals(infos[0].filename, "file.h5")
self.assertEquals(infos[0].filename, "thing-file.h5")
self.assertEquals(infos[0].type, "primary")
self.assertEquals(infos[0].rank, 4)
self.assertEquals(infos[0].path, "/entry/detector/detector")
self.assertEquals(infos[0].uniqueid,
"/entry/NDAttributes/NDArrayUniqueId")
self.assertEquals(infos[1].name, "xspress3.sum")
self.assertEquals(infos[1].filename, "file.h5")
self.assertEquals(infos[1].filename, "thing-file.h5")
self.assertEquals(infos[1].type, "secondary")
self.assertEquals(infos[1].rank, 4)
self.assertEquals(infos[1].path, "/entry/sum/sum")
self.assertEquals(infos[1].uniqueid,
"/entry/NDAttributes/NDArrayUniqueId")
self.assertEquals(infos[2].name, "energy.value_set")
self.assertEquals(infos[2].filename, "file.h5")
self.assertEquals(infos[2].filename, "thing-file.h5")
self.assertEquals(infos[2].type, "position_set")
self.assertEquals(infos[2].rank, 1)
self.assertEquals(infos[2].path, "/entry/detector/energy_set")
self.assertEquals(infos[2].uniqueid, "")
self.assertEquals(infos[3].name, "x.value_set")
self.assertEquals(infos[3].filename, "file.h5")
self.assertEquals(infos[3].filename, "thing-file.h5")
self.assertEquals(infos[3].type, "position_set")
self.assertEquals(infos[3].rank, 1)
self.assertEquals(infos[3].path, "/entry/detector/x_set")
self.assertEquals(infos[3].uniqueid, "")
self.assertEquals(infos[4].name, "y.value_set")
self.assertEquals(infos[4].filename, "file.h5")
self.assertEquals(infos[4].filename, "thing-file.h5")
self.assertEquals(infos[4].type, "position_set")
self.assertEquals(infos[4].rank, 1)
self.assertEquals(infos[4].path, "/entry/detector/y_set")
Expand All @@ -97,8 +98,8 @@ def test_configure(self):
lazyOpen=True,
arrayCounter=0,
filePath="/tmp/",
fileName="file.h5",
fileTemplate="%sthing-%s")))
fileName="file",
fileTemplate="%sthing-%s.h5")))
self.assertEqual(task.put_many_async.call_args_list[1],
call(self.child, dict(
numExtraDims=1,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parts/test_ADCore/test_statspluginpart.py
Expand Up @@ -37,7 +37,7 @@ def test_configure(self):
steps_to_do = ANY
part_info = ANY
params = MagicMock()
params.filePath = "/tmp/anything.h5"
params.fileDir = "/tmp"
infos = self.o.configure(
task, completed_steps, steps_to_do, part_info, params)
self.assertIsNone(infos)
Expand Down

0 comments on commit f107bf4

Please sign in to comment.