optiland.fileio.optiland_handler

Optiland File Handler

This module provides functions for loading and saving Optiland objects and Optic instances to and from JSON files. The functions load_obj_from_json and save_obj_to_json can be used to load and save any object that has a class method from_dict and a method to_dict, respectively. In Optiland, this includes most core classes, such as Optic, BaseGeometry, BaseCoating, BaseMaterial, Aperture, FieldGroup, WavelengthGroup, etc.

Kramer Harrison, 2024

Functions

load_obj_from_json(cls, filepath)

Load an object from a JSON file.

load_optiland_file(filepath)

Load an Optiland Optic from a JSON file.

save_obj_to_json(obj, filepath)

Save an object to a JSON file.

save_optiland_file(obj, filepath)

Save an Optiland Optic to a JSON file.

Classes

OptilandEncoder(*[, skipkeys, ensure_ascii, ...])

Custom JSON encoder to handle tensor/array serialization across backends.

class OptilandEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Custom JSON encoder to handle tensor/array serialization across backends.

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)
encode(o)

Return a JSON string representation of a Python data structure.

>>> from json.encoder import JSONEncoder
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
item_separator = ', '
iterencode(o, _one_shot=False)

Encode the given object and yield each string representation as available.

For example:

for chunk in JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)
key_separator = ': '
load_obj_from_json(cls, filepath)[source]

Load an object from a JSON file.

Note that this function can be used to load any object that has a class method from_dict that can be used to create an instance of the class from a dictionary. In Optiland, this includes most core classes, such as Optic, BaseGeometry, BaseCoating, BaseMaterial, Aperture, FieldGroup, WavelengthGroup, etc.

Parameters:
  • cls – The class of the object to load.

  • filepath – The path to the JSON file.

Returns:

An instance of the class

load_optiland_file(filepath)[source]

Load an Optiland Optic from a JSON file.

Parameters:

filepath – The path to the JSON file.

Returns:

An Optic instance.

save_obj_to_json(obj, filepath)[source]

Save an object to a JSON file.

Note that this function can be used to save any object that has a method to_dict that returns a dictionary representation of the object. In Optiland, this includes most core classes, such as Optic, BaseGeometry, BaseCoating, BaseMaterial, Aperture, FieldGroup, WavelengthGroup, etc.

Parameters:
  • obj – The object to save.

  • filepath – The path to the JSON file.

save_optiland_file(obj, filepath)[source]

Save an Optiland Optic to a JSON file.

Parameters:
  • obj – The Optic to save.

  • filepath – The path to the JSON file.