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)

Deconstruct an element into its name, class and parameters for saving to JSON.

Parameters:

element – Cheetah element

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 necesary, 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) Segment

Load a Cheetah model from a JSON file.

Parameters:

filename – Name/path of the file to load the lattice from.

Returns:

Loaded Cheetah Segment.

latticejson.nontorch2feature(value: Any) 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.

Parameters:

value – Value to convert to a torch.Tensor if necessary.

Returns:

Value converted to a torch.Tensor if necessary.

latticejson.parse_element(name: str, lattice_dict: dict) 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.

latticejson.parse_segment(name: str, lattice_dict: dict) 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.

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”.