LatticeJSON¶
- class latticejson.CompactJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)¶
A JSON Encoder which only indents the first two levels.
Taken from https://github.com/nobeam/latticejson/blob/main/latticejson/format.py
- encode(obj, level=0)¶
Return a JSON string representation of a Python data structure.
>>> from json.encoder import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
- latticejson.convert_element(element: Element, elements_dict: dict | None = None) tuple[str, str, dict]¶
Deconstruct an element into its name, class and parameters for saving to JSON. If a property of the element is another element, it is recursively converted and added to elements_dict.
- Parameters:
element – Cheetah element
elements_dict – Optional dictionary to accumulate sub-elements.
- Returns:
Tuple of element name, element class, and element parameters.
- latticejson.convert_segment(segment: Segment) tuple[dict, dict]¶
Deconstruct a segment into its name, a list of its elements and a dictionary of its element parameters for saving to JSON.
- Parameters:
segment – Cheetah segment.
- Returns:
Tuple of elments and lattices dictionaries found in segment, including the segment itself.
- latticejson.feature2nontorch(value: Any) Any¶
If necessary, convert an the value of a feature of a cheetah.Element to a non-torch type that can be saved to LatticeJSON.
- Parameters:
value – Value of the feature that might be in some kind of PyTorch format, such as torch.Tensor or torch.nn.Parameter.
- Returns:
Value of the feature if it is not in a PyTorch format, otherwise the value converted to a non-PyTorch format.
- latticejson.load_cheetah_model(filename: str, device: device | None = None, dtype: dtype | None = None) Segment¶
Load a Cheetah model from a JSON file.
- Parameters:
filename – Name/path of the file to load the lattice from.
device – Device to place the lattice elements on.
dtype – Data type to use for the lattice elements.
- Returns:
Loaded Cheetah Segment.
- latticejson.nontorch2feature(value: Any, device: device | None = None, dtype: dtype | None = None) Any¶
Convert a value like a float, int, etc. to a torch.Tensor if necessary. Values of type str and bool are not converted, because all currently existing cheetah.Element subclasses expect these values to not be of type torch.Tensor. (dict values are used for the free-form metadata field.)
- Parameters:
value – Value to convert to a torch.Tensor if necessary.
device – Device to place the lattice elements on.
dtype – Data type to use for the lattice elements.
- Returns:
Value converted to a torch.Tensor if necessary.
- latticejson.parse_element(name: str, lattice_dict: dict, device: device | None = None, dtype: dtype | None = None) Element¶
Parse an Element named name from a lattice_dict.
- Parameters:
name – Name of the Element to parse.
lattice_dict – Dictionary containing the lattice information.
device – Device to place the lattice elements on.
dtype – Data type to use for the lattice elements.
- latticejson.parse_segment(name: str, lattice_dict: dict, device: device | None = None, dtype: dtype | None = None) Segment¶
Parse a Segment named name from a lattice_dict.
- Parameters:
name – Name of the Segment to parse.
lattice_dict – Dictionary containing the lattice information.
device – Device to place the lattice elements on.
dtype – Data type to use for the lattice elements.
- latticejson.save_cheetah_model(segment: Segment, filename: str, title: str | None = None, info: str = 'This is a placeholder lattice description') None¶
Save a cheetah model to json file accoding to the lattice-json convention c.f. https://github.com/nobeam/latticejson
- Parameters:
segment – Cheetah Segment to save.
filename – Name/path of the file to save the lattice to.
title – Title of the lattice. If not provided, defaults to the name of the Segment object. If that also does not have a name, defaults to “Unnamed Lattice”.
info – Information about the lattice. Defaults to “This is a placeholder lattice description”.