Skip to Content

Specs

Спецификации параметров для экспорта и сериализации

from svetlanna.specs import ...

Классы

ParameterSaveContext

Generates different context managers that can be used to write a parameter data to output stream or file.

Методы

__init__ constructor

__init__(self, parameter_name: str, directory: Path)

📥 Параметры

ПараметрТипОписание
parameter_namestrthe human-readable name for the parameter
directorystrthe directory where the generated file will be saved, if any
stream TextIOstream where the generated text will be written, if any

get_new_filepath

get_new_filepath(self, extension: str) -> Path

Create a new filepath for a specific extension. The generated filename of a specific extension will have a unique name ending with _<n>.<extension>, where <n> is auto-incrementing index.

📥 Параметры

ПараметрТипОписание
extensionstrfilename extension

📤 Возвращает

Path

relative path to the file

rel_filepath

rel_filepath(self, filepath: Path) -> Path

Get relative to specs file filepath

📥 Параметры

ПараметрТипОписание
filepathPathabsolute path

📤 Возвращает

Path

relative path

file

file(self, filepath: Path) -> Generator[BufferedWriter, Any, None]

Context manager for the output file

📥 Параметры

ПараметрТипОписание
filepathPathfilepath

Yields

  • Generator[BufferedWriter, Any, None] — Buffer

Representation

Наследует: Generic[ParameterSaveContext_]

Base class for a parameter representation

MarkdownRepresentation

Наследует: Representation[ParameterSaveContext_]

Representation that can be exported to markdown file

Методы

to_markdown

to_markdown(self, out: TextIO, context: ParameterSaveContext_) -> None

Write the parameter related data to be shown in a markdown file. The text should be written to the out stream.

📥 Параметры

ПараметрТипОписание
outTextIOoutput text stream
contextParameterSaveContext_the parameter save context

StrRepresentation

Наследует: Representation[ParameterSaveContext_]

Representation that can be exported in the text format

Методы

to_str

to_str(self, out: TextIO, context: ParameterSaveContext_) -> None

Write the parameter related data to be shown as a plain text. The text should be written to the out stream.

📥 Параметры

ПараметрТипОписание
outTextIOoutput text stream
contextParameterSaveContext_the parameter save context

HTMLRepresentation

Наследует: Representation[ParameterSaveContext_]

Representation that can be exported to the HTML

Методы

to_html

to_html(self, out: TextIO, context: ParameterSaveContext_) -> None

Write the parameter related data to be shown in a HTML file. The text should be written to the out stream.

📥 Параметры

ПараметрТипОписание
outTextIOoutput text stream
contextParameterSaveContext_the parameter save context

ImageRepr

Наследует: StrRepresentation MarkdownRepresentation HTMLRepresentation

Representation of the parameter as an image. Image generation is based on the pillow package.

Методы

__init__ constructor

__init__(self, value: Any, mode: Literal['1', 'L', 'LA', 'I', 'P', 'RGB', 'RGBA'] = 'L', format: str = 'png', show_image: bool = True)

📥 Параметры

ПараметрТипОписание
valueAnyThe image data. See matplotlib.pyplot.imshow docs.
modeLiteral['1', 'L', 'LA', 'I', 'P', 'RGB', 'RGBA'], optionalthe mode of the image, see https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-modes . By default L
formatstr, optionalthe image format, by default ‘png’

draw_image

draw_image(self, context: ParameterSaveContext, filepath: Path) -> Image.Image

Draw image into the file, using pillow package.

📥 Параметры

ПараметрТипОписание
contextParameterSaveContextthe parameter save context
filepathPathpath to the image file to be created

to_str

to_str(self, out: TextIO, context: ParameterSaveContext)

to_markdown

to_markdown(self, out: TextIO, context: ParameterSaveContext)

to_html

to_html(self, out: TextIO, context: ParameterSaveContext)

ReprRepr

Наследует: StrRepresentation MarkdownRepresentation HTMLRepresentation

Representation of the parameter as a plain text. The __repr__ method is used to generate the text.

Методы

__init__ constructor

__init__(self, value: Any)

📥 Параметры

ПараметрТипОписание
valueAnyobject with defined __repr__ method that will be used to generate plain text.

to_str

to_str(self, out: TextIO, context: ParameterSaveContext)

to_markdown

to_markdown(self, out: TextIO, context: ParameterSaveContext)

to_html

to_html(self, out: TextIO, context: Any)

NpyFileRepr

Наследует: StrRepresentation MarkdownRepresentation

Representation of the parameter as a .npy file.

Методы

__init__ constructor

__init__(self, value: ArrayLike)

📥 Параметры

ПараметрТипОписание
valueArrayLikeparameter data.

save_to_file

save_to_file(self, context: ParameterSaveContext, filepath: Path)

Save the parameter related data to npy file.

📥 Параметры

ПараметрТипОписание
contextParameterSaveContextthe parameter save context
filepathPathpath to the file to be created

to_str

to_str(self, out: TextIO, context: ParameterSaveContext)

to_markdown

to_markdown(self, out: TextIO, context: ParameterSaveContext)

PrettyReprRepr

Наследует: ReprRepr HTMLRepresentation

Same as ReprRepr but with better handling of Parameters and BoundedParameter

Методы

__init__ constructor

__init__(self, value: Any, units: str | None = None)

Representation of the parameter as a plain text. The __repr__ method is used to generate the text if the value is not torch.Tensor or Parameter.

📥 Параметры

ПараметрТипОписание
valueAnyobject to generate plain text of.
units`strNone, optional`

to_str

to_str(self, out: TextIO, context: ParameterSaveContext)

to_markdown

to_markdown(self, out: TextIO, context: ParameterSaveContext)

to_html

to_html(self, out: TextIO, context: ParameterSaveContext)

ParameterSpecs

Container with all representations for the parameter.

Методы

__init__ constructor

__init__(self, parameter_name: str, representations: Iterable[Representation]) -> None

📥 Параметры

ПараметрТипОписание
namestrthe parameter’s name.
representationsIterable[ParameterRepr]all representations of the parameter.

SubelementSpecs

Container for named subelement

Методы

__init__ constructor

__init__(self, subelement_type: str, subelement: 'Specsable')

📥 Параметры

ПараметрТипОписание
subelement_typestrhuman-readable type of the subelement.
subelementSpecsablethe subelement.

Specsable

Наследует: Protocol

Represents any specsable object

Методы

to_specs

to_specs(self) -> Iterable[ParameterSpecs | SubelementSpecs]

_IndexedObject

Наследует: Generic[_T]

_WriterContext

Storage for additional info within ParameterSaveContext

_ElementInTree

Методы

create_copy

create_copy(self, subelement_type: str | None) -> '_ElementInTree'

_ElementsIterator

Свойства

tree

Get a tree of all elements iterated

Методы

__init__ constructor

__init__(self, *iterables: Specsable) -> None

Функции

context_generator(element: Specsable, element_index: int, directory: str | Path, subelements: list[SubelementSpecs]) -> _WriterContextGenerator

Generate _WriterContext for the element

📥 Параметры

ПараметрТипОписание
elementSpecsableElement
element_indexintIndex of the element. It is used to create unique directory for element specs.
directory`strPath`

Yields

  • _WriterContext — context

write_specs_to_str(element: Specsable, element_index: int, writer_context_generator: _WriterContextGenerator, stream: TextIO)

write_specs_to_markdown(element: Specsable, element_index: int, writer_context_generator: _WriterContextGenerator, stream: TextIO)

write_specs_to_html(element: Specsable, element_index: int, writer_context_generator: _WriterContextGenerator, stream: TextIO)

write_elements_tree_to_str(tree: list[_ElementInTree], stream: TextIO)

write_elements_tree_to_markdown(tree: list[_ElementInTree], stream: TextIO)

write_specs(*iterables: Specsable)