python-object-serialization
Summary
Serialize structured Python objects (dictionaries, data structures) to persistent storage formats (pickle, JSON) for reuse in downstream computational workflows. This skill ensures that in-memory data structures from SMITER's simulation pipeline can be reliably stored and retrieved across analysis sessions.
When to use
After constructing a peak properties dictionary via csv_to_peak_properties or other data transformation steps, and before passing the dictionary to subsequent SMITER simulation functions (fragmentor selection, noise injection, mzML generation). Use this skill when you need to checkpoint intermediate results, share data between pipeline stages, or avoid recomputing expensive transformations.
When NOT to use
- Input is already serialized (file on disk); deserialize instead of re-serializing.
- Peak properties dictionary has not been validated for required keys/schema; validate before serialization to catch structural errors early.
- Workflow does not require intermediate checkpointing or data sharing between pipeline stages; serialization adds I/O overhead.
Inputs
- peak properties dictionary (Python dict object with molecule definitions and properties)
- file path (string) specifying output location
Outputs
- serialized pickle file (.pkl)
- serialized JSON file (.json)
How to apply
After the peak properties dictionary is validated (contains required keys and structure expected by SMITER simulation functions), select a serialization format based on downstream tool compatibility: pickle for Python-native workflows with no interoperability requirement, or JSON for human-readability and cross-language compatibility. Serialize the dictionary using Python's built-in pickle.dump() or json.dumps() functions to write to disk. Verify serialization succeeded by reading the file back and performing a schema or key-presence check to ensure all required peak properties fields are intact.
Related tools
- Python pickle module (built-in serialization to binary format for checkpoint storage)
- Python json module (built-in serialization to JSON text format for human inspection and cross-language compatibility)
- SMITER (consumes deserialized peak properties dictionary in simulation pipeline (csv_to_peak_properties, fragmentation, noise, mzML writing)) — https://github.com/LeidelLab/SMITER
Examples
import pickle; from smiter.lib import csv_to_peak_properties; peak_props = csv_to_peak_properties('example_data/molecules.csv'); pickle.dump(peak_props, open('peak_properties.pkl', 'wb'))
Evaluation signals
- Serialized file exists at specified output path and has non-zero file size.
- Deserialization round-trip succeeds: read the file back and verify the reconstructed dictionary is type
dict with all keys present from the original.
- Schema validation: reconstructed dictionary contains expected top-level keys (e.g., molecule identifiers, peak properties, chemical formulas) matching the SMITER pipeline requirements.
- Downstream consumption: deserialized dictionary is successfully passed to SMITER simulation functions (e.g., fragmentation, noise injection) without key-not-found or type mismatch errors.
Limitations
- Pickle format is Python-specific; not suitable for cross-language pipelines or long-term archival without version pinning.
- JSON serialization requires all dictionary values to be JSON-serializable (strings, numbers, lists, nested dicts); complex Python objects (custom classes, functions) require custom encoder.
- No built-in schema enforcement during serialization; structural validation must be performed before or after serialization to detect malformed dictionaries.
- Large dictionaries (many molecules or high-dimensional peak properties) may result in large file sizes; compression (gzip) may be needed for storage efficiency.
Evidence
- [other] Serialize the peak properties dictionary to a Python pickle or JSON file for use in subsequent simulation steps.: "Serialize the peak properties dictionary to a Python pickle or JSON file for use in subsequent simulation steps."
- [other] Validate that the resulting dictionary contains the required keys and structure expected by SMITER's simulation functions.: "Validate that the resulting dictionary contains the required keys and structure expected by SMITER's simulation functions."
- [other] Run the simulation and write the resulting mzML using
smiter.synthetic_mzml.write_mzml: "Run the simulation and write the resulting mzML using smiter.synthetic_mzml.write_mzml"
1---2name: python-object-serialization3description: Use when after constructing a peak properties dictionary via csv_to_peak_properties or other data transformation steps, and before passing the dictionary to subsequent SMITER simulation functions (fragmentor selection, noise injection, mzML generation).4license: CC-BY-4.05---67# python-object-serialization89## Summary1011Serialize structured Python objects (dictionaries, data structures) to persistent storage formats (pickle, JSON) for reuse in downstream computational workflows. This skill ensures that in-memory data structures from SMITER's simulation pipeline can be reliably stored and retrieved across analysis sessions.1213## When to use1415After constructing a peak properties dictionary via csv_to_peak_properties or other data transformation steps, and before passing the dictionary to subsequent SMITER simulation functions (fragmentor selection, noise injection, mzML generation). Use this skill when you need to checkpoint intermediate results, share data between pipeline stages, or avoid recomputing expensive transformations.1617## When NOT to use1819- Input is already serialized (file on disk); deserialize instead of re-serializing.20- Peak properties dictionary has not been validated for required keys/schema; validate before serialization to catch structural errors early.21- Workflow does not require intermediate checkpointing or data sharing between pipeline stages; serialization adds I/O overhead.2223## Inputs2425- peak properties dictionary (Python dict object with molecule definitions and properties)26- file path (string) specifying output location2728## Outputs2930- serialized pickle file (.pkl)31- serialized JSON file (.json)3233## How to apply3435After the peak properties dictionary is validated (contains required keys and structure expected by SMITER simulation functions), select a serialization format based on downstream tool compatibility: pickle for Python-native workflows with no interoperability requirement, or JSON for human-readability and cross-language compatibility. Serialize the dictionary using Python's built-in `pickle.dump()` or `json.dumps()` functions to write to disk. Verify serialization succeeded by reading the file back and performing a schema or key-presence check to ensure all required peak properties fields are intact.3637## Related tools3839- **Python pickle module** (built-in serialization to binary format for checkpoint storage)40- **Python json module** (built-in serialization to JSON text format for human inspection and cross-language compatibility)41- **SMITER** (consumes deserialized peak properties dictionary in simulation pipeline (csv_to_peak_properties, fragmentation, noise, mzML writing)) — https://github.com/LeidelLab/SMITER4243## Examples4445```46import pickle; from smiter.lib import csv_to_peak_properties; peak_props = csv_to_peak_properties('example_data/molecules.csv'); pickle.dump(peak_props, open('peak_properties.pkl', 'wb'))47```4849## Evaluation signals5051- Serialized file exists at specified output path and has non-zero file size.52- Deserialization round-trip succeeds: read the file back and verify the reconstructed dictionary is type `dict` with all keys present from the original.53- Schema validation: reconstructed dictionary contains expected top-level keys (e.g., molecule identifiers, peak properties, chemical formulas) matching the SMITER pipeline requirements.54- Downstream consumption: deserialized dictionary is successfully passed to SMITER simulation functions (e.g., fragmentation, noise injection) without key-not-found or type mismatch errors.5556## Limitations5758- Pickle format is Python-specific; not suitable for cross-language pipelines or long-term archival without version pinning.59- JSON serialization requires all dictionary values to be JSON-serializable (strings, numbers, lists, nested dicts); complex Python objects (custom classes, functions) require custom encoder.60- No built-in schema enforcement during serialization; structural validation must be performed before or after serialization to detect malformed dictionaries.61- Large dictionaries (many molecules or high-dimensional peak properties) may result in large file sizes; compression (gzip) may be needed for storage efficiency.6263## Evidence6465- [other] Serialize the peak properties dictionary to a Python pickle or JSON file for use in subsequent simulation steps.: "Serialize the peak properties dictionary to a Python pickle or JSON file for use in subsequent simulation steps."66- [other] Validate that the resulting dictionary contains the required keys and structure expected by SMITER's simulation functions.: "Validate that the resulting dictionary contains the required keys and structure expected by SMITER's simulation functions."67- [other] Run the simulation and write the resulting mzML using `smiter.synthetic_mzml.write_mzml`: "Run the simulation and write the resulting mzML using `smiter.synthetic_mzml.write_mzml`"