Skip to content

Commit 6d2397b

Browse files
committed
Make image filepath metadata PV configurable
1 parent c1e0d50 commit 6d2397b

7 files changed

Lines changed: 91 additions & 30 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ usage limit.
8787
The post processing agent handles cataloging raw and reduced data files in ONCat https://oncat.ornl.gov/ by
8888
calling scripts hosted on the analysis cluster.
8989

90+
##### Image File Cataloging
91+
92+
For instruments that produce image files (e.g., FITS or TIFF format), the agent can automatically discover
93+
and catalog these files along with the main data file. The metadata path where image file locations are
94+
stored can be configured:
95+
96+
"image_filepath_metadata_paths": ["metadata.entry.daslogs.bl10:exp:im:imagefilepath.value"]
97+
98+
If not specified, this defaults to the VENUS instrument metadata path. For other instruments, configure
99+
this parameter to match the appropriate metadata path(s) in your NeXus files. Multiple paths can be specified
100+
as an array.
101+
90102

91103
Installation
92104
------------

‎postprocessing/Configuration.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ def __init__(self, config_file):
9191
self.oncat_url = config.get("oncat_url", "")
9292
self.oncat_api_token = config.get("oncat_api_token", "")
9393

94+
# Image filepath metadata paths for cataloging image files
95+
# Default is for VENUS instrument, but can be configured per instrument
96+
default_image_metadata_paths = ["metadata.entry.daslogs.bl10:exp:im:imagefilepath.value"]
97+
self.image_filepath_metadata_paths = config.get("image_filepath_metadata_paths", default_image_metadata_paths)
98+
9499
sys.path.insert(0, self.sw_dir)
95100
# Configure processor plugins
96101
default_processors = [

‎postprocessing/processors/oncat_processor.py‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
import pyoncat
1212

1313

14-
# Metadata paths where image file paths may be stored
15-
IMAGE_FILEPATH_METADATA_PATHS = [
16-
"metadata.entry.daslogs.bl10:exp:im:imagefilepath.value",
17-
]
18-
1914
# Batch size for image ingestion (must be less than max of 100)
2015
IMAGE_BATCH_SIZE = 50
2116

@@ -70,7 +65,7 @@ def ingest(self, location):
7065
oncat.Datafile.ingest(related_file)
7166

7267
# Catalog image files using batch API for efficiency
73-
images = image_files(datafile)
68+
images = image_files(datafile, self.configuration.image_filepath_metadata_paths)
7469
for batch in batches(images, IMAGE_BATCH_SIZE):
7570
logging.info("Batch ingesting %d image files", len(batch))
7671
oncat.Datafile.batch(batch)
@@ -120,15 +115,16 @@ def related_files(datafile):
120115
]
121116

122117

123-
def image_files(datafile):
118+
def image_files(datafile, metadata_paths):
124119
"""Find image files from metadata paths.
125120
126-
Iterates through the known metadata paths, retrieves values from
121+
Iterates through the configured metadata paths, retrieves values from
127122
the datafile metadata, and globs for image files in the discovered
128123
subdirectories.
129124
130125
Args:
131126
datafile: ONCat datafile object with metadata
127+
metadata_paths: List of metadata paths to check for image directory locations
132128
133129
Returns:
134130
List of absolute paths to image files (FITS and TIFF)
@@ -138,7 +134,7 @@ def image_files(datafile):
138134
experiment = datafile.experiment
139135
image_file_paths = []
140136

141-
for metadata_path in IMAGE_FILEPATH_METADATA_PATHS:
137+
for metadata_path in metadata_paths:
142138
value = datafile.get(metadata_path)
143139
if value is None:
144140
continue

‎tests/integration/Dockerfile.oncat‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,13 @@ RUN mkdir -p /SNS/CORELLI/IPTS-15526/nexus && \
55
mkdir -p /SNS/CORELLI/IPTS-15526/images/det_main && \
66
touch /SNS/CORELLI/IPTS-15526/images/det_main/CORELLI_29666_det_main_000001.tiff && \
77
mkdir -p /SNS/CORELLI/IPTS-15526/shared/autoreduce && \
8-
echo '{"output_files":[], "input_files": []}' > /SNS/CORELLI/IPTS-15526/shared/autoreduce/CORELLI_29666.json
8+
echo '{"output_files":[], "input_files": []}' > /SNS/CORELLI/IPTS-15526/shared/autoreduce/CORELLI_29666.json && \
9+
\
10+
mkdir -p /SNS/VENUS/IPTS-99999/nexus && \
11+
mkdir -p /SNS/VENUS/IPTS-99999/images && \
12+
mkdir -p /SNS/VENUS/IPTS-99999/shared/autoreduce && \
13+
touch /SNS/VENUS/IPTS-99999/nexus/VENUS_12345.nxs.h5 && \
14+
touch /SNS/VENUS/IPTS-99999/images/image_001.fits && \
15+
touch /SNS/VENUS/IPTS-99999/images/image_002.fits && \
16+
touch /SNS/VENUS/IPTS-99999/images/image_003.tiff
917
CMD ["python", "oncat_server.py"]

‎tests/integration/test_cataloging.py‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ def test_oncat_catalog():
3131
# send data ready
3232
conn.send("/queue/CATALOG.ONCAT.DATA_READY", json.dumps(message).encode())
3333

34-
listener.wait_for_message()
34+
# Wait for messages until we get the one for this run
35+
max_attempts = 10
36+
for _ in range(max_attempts):
37+
listener.wait_for_message()
38+
header, body = listener.get_latest_message()
39+
msg = json.loads(body)
40+
if msg["run_number"] == message["run_number"]:
41+
break
42+
else:
43+
pytest.fail(f"Did not receive COMPLETE message for CORELLI run {message['run_number']}")
3544

3645
conn.disconnect()
3746

38-
header, body = listener.get_latest_message()
39-
40-
msg = json.loads(body)
4147
assert msg["run_number"] == message["run_number"]
4248
assert msg["instrument"] == message["instrument"]
4349
assert msg["ipts"] == message["ipts"]
@@ -113,7 +119,7 @@ def test_oncat_catalog_venus_images():
113119

114120
conn = stomp.Connection(host_and_ports=[("localhost", 61613)])
115121

116-
listener = stomp.listener.TestListener()
122+
listener = stomp.listener.TestListener(10) # 10 second timeout
117123
conn.set_listener("", listener)
118124

119125
try:
@@ -122,26 +128,21 @@ def test_oncat_catalog_venus_images():
122128
pytest.skip("Requires activemq running")
123129

124130
# expect a message on CATALOG.ONCAT.COMPLETE
125-
conn.subscribe("/queue/CATALOG.ONCAT.COMPLETE", id="123", ack="auto")
131+
conn.subscribe("/queue/CATALOG.ONCAT.COMPLETE", id="venus123", ack="auto")
126132

127133
# send data ready
128134
conn.send("/queue/CATALOG.ONCAT.DATA_READY", json.dumps(message).encode())
129135

130-
# Wait for the correct message, skipping any stale messages from previous tests
136+
# Wait for messages until we get the one for this run
131137
max_attempts = 10
132-
for attempt in range(max_attempts):
133-
time.sleep(0.5) # Wait briefly for message
138+
for _ in range(max_attempts):
134139
listener.wait_for_message()
135140
header, body = listener.get_latest_message()
136141
msg = json.loads(body)
137-
138-
# Check if this is our VENUS message
139-
if msg.get("instrument") == "VENUS" and msg.get("run_number") == "12345":
142+
if msg["run_number"] == message["run_number"]:
140143
break
141-
142-
# If not our message, keep waiting for the next one
143-
if attempt == max_attempts - 1:
144-
pytest.fail(f"Did not receive VENUS message after {max_attempts} attempts. Last message: {msg}")
144+
else:
145+
pytest.fail(f"Did not receive COMPLETE message for VENUS run {message['run_number']}")
145146

146147
conn.disconnect()
147148

‎tests/unit/postprocessing/processors/test_oncat_processor.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def test_image_files_no_metadata():
8080
mock_datafile.experiment = "IPTS-99999"
8181
mock_datafile.get.return_value = None # No metadata found
8282

83-
result = image_files(mock_datafile)
83+
metadata_paths = ["metadata.entry.daslogs.bl10:exp:im:imagefilepath.value"]
84+
result = image_files(mock_datafile, metadata_paths)
8485
assert result == []
8586

8687

@@ -95,7 +96,8 @@ def test_image_files_metadata_not_a_directory():
9596
with patch("os.path.isdir") as mock_isdir:
9697
mock_isdir.return_value = False
9798

98-
result = image_files(mock_datafile)
99+
metadata_paths = ["metadata.entry.daslogs.bl10:exp:im:imagefilepath.value"]
100+
result = image_files(mock_datafile, metadata_paths)
99101
assert result == []
100102

101103

@@ -122,7 +124,8 @@ def glob_side_effect(pattern):
122124

123125
mock_glob.side_effect = glob_side_effect
124126

125-
result = image_files(mock_datafile)
127+
metadata_paths = ["metadata.entry.daslogs.bl10:exp:im:imagefilepath.value"]
128+
result = image_files(mock_datafile, metadata_paths)
126129

127130
assert len(result) == 3
128131
assert "/SNS/VENUS/IPTS-99999/images/image_001.fits" in result
@@ -150,7 +153,8 @@ def glob_side_effect(pattern):
150153

151154
mock_glob.side_effect = glob_side_effect
152155

153-
result = image_files(mock_datafile)
156+
metadata_paths = ["metadata.entry.daslogs.bl10:exp:im:imagefilepath.value"]
157+
result = image_files(mock_datafile, metadata_paths)
154158

155159
assert len(result) == 2
156160
assert "/SNS/VENUS/IPTS-99999/images/batch1/image_001.fits" in result
@@ -170,6 +174,7 @@ def test_oncat_processor_ingest_with_images():
170174
mock_conf = Mock()
171175
mock_conf.oncat_url = "http://oncat:8000"
172176
mock_conf.oncat_api_token = "test-token"
177+
mock_conf.image_filepath_metadata_paths = ["metadata.entry.daslogs.bl10:exp:im:imagefilepath.value"]
173178

174179
mock_send_function = Mock()
175180

@@ -228,6 +233,7 @@ def test_oncat_processor_ingest_with_many_images():
228233
mock_conf = Mock()
229234
mock_conf.oncat_url = "http://oncat:8000"
230235
mock_conf.oncat_api_token = "test-token"
236+
mock_conf.image_filepath_metadata_paths = ["metadata.entry.daslogs.bl10:exp:im:imagefilepath.value"]
231237

232238
mock_send_function = Mock()
233239

‎tests/unit/postprocessing/test_Configuration.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,39 @@ def test_log_configuration(self, data_server, test_logger):
3636
log_contents = open(test_logger.log_file).read()
3737
assert "LOCAL execution" in log_contents
3838

39+
def test_image_filepath_metadata_paths_default(self, data_server):
40+
"""Test that image_filepath_metadata_paths has correct default value"""
41+
conf = Configuration(data_server.path_to("post_processing.conf"))
42+
# Should use default VENUS metadata path
43+
assert conf.image_filepath_metadata_paths == ["metadata.entry.daslogs.bl10:exp:im:imagefilepath.value"]
44+
45+
def test_image_filepath_metadata_paths_custom(self, tmp_path):
46+
"""Test that image_filepath_metadata_paths can be configured"""
47+
custom_paths = [
48+
"metadata.entry.daslogs.custom:path:1.value",
49+
"metadata.entry.daslogs.custom:path:2.value",
50+
]
51+
config_data = {
52+
"failover_uri": "failover:(tcp://localhost:61613)",
53+
"brokers": [["localhost", 61613]],
54+
"amq_user": "test",
55+
"amq_pwd": "test",
56+
"sw_dir": "/tmp",
57+
"log_file": "/tmp/test.log",
58+
"postprocess_error": "ERROR",
59+
"reduction_started": "STARTED",
60+
"reduction_complete": "COMPLETE",
61+
"reduction_error": "ERROR",
62+
"reduction_disabled": "DISABLED",
63+
"heart_beat": "/topic/HEARTBEAT",
64+
"image_filepath_metadata_paths": custom_paths,
65+
}
66+
tmp_conf_file = tmp_path / "test_config.conf"
67+
tmp_conf_file.write_text(json.dumps(config_data))
68+
69+
conf = Configuration(tmp_conf_file.as_posix())
70+
assert conf.image_filepath_metadata_paths == custom_paths
71+
3972

4073
def test_read_configuration(data_server, caplog):
4174
caplog.set_level(logging.INFO)

0 commit comments

Comments
 (0)