MeasurementManager
Description
Manages saving and loading measurement data collected by the SpectroscopyTool. Handles structured text-based export of both metadata and raw measurement values (wavelength, intensity).
Constructor
python
MeasurementManager(metadata, raw_data)| Parameter | Type | Description |
|---|---|---|
metadata | dict | Metadata dictionary (e.g., tool info, collection settings). |
raw_data | dict | Measurement dictionary with "wavelength" and "intensity" keys. |
Key Attributes
| Attribute | Type | Description |
|---|---|---|
raw_data | dict | Dictionary containing raw measurement data. |
wavelengths | list[float] | List of measured wavelengths. |
intensities | list[float] | List of measured intensities. |
metadata | dict | Metadata associated with the measurement. |
Important Methods
| Method | Returns | Description |
|---|---|---|
pretty_print() | None | Display the metadata and raw data in a readable format. |
generate_file(filename, path) | None | Save the measurement and metadata into a structured .txt file. |
read_file(filepath) | tuple | Static method to load metadata and measurements from a saved file. |
Method Details
Pretty Print
python
pretty_print()Prints out both metadata and raw data in an easy-to-read format for quick inspection.
Generate File
python
generate_file(filename, path)Saves the current metadata and measurement data into a text file with the following structure:
plaintext
---- METADATA ----
key1: value1
key2: value2
...
---- RAW DATA ----
wavelength1 intensity1
wavelength2 intensity2
...- Automatically adds
.txtif the filename does not already have it. - Creates both metadata and measurement sections in a human-readable style.
Read File (Static Method)
python
MeasurementManager.read_file(filepath)Reads a previously saved .txt file and parses:
- Metadata into a dictionary.
- Measurements into a dictionary of
"Wavelength"and"Intensity"lists.
Returns a tuple: (metadata, measurements)
Usage Example
python
# Initialize a MeasurementManager with collected spectrum
mm = MeasurementManager(metadata, raw_data)
# Save to a file
mm.generate_file(filename="spectrum_20250426", path="./data")
# Read from a file
metadata, measurements = MeasurementManager.read_file("./data/spectrum_20250426.txt")
# Pretty print
mm.pretty_print()Important Notes
- Saved text files are easy to inspect manually and compatible with basic parsers (e.g., Python, Excel).
- The structure is simple but order-sensitive — first
METADATA, thenRAW DATA. - When reading, expects metadata fields separated by
": "and measurement values separated by a tab (\t).