mapstp package

Subpackages

Submodules

mapstp.exceptions module

Utility module to specify exception hierarchy for all tee package.

exception mapstp.exceptions.FileError[source]

Bases: MyError

STP parser file format error.

exception mapstp.exceptions.MyError[source]

Bases: ValueError

Base class for exceptions in the mapstp package.

exception mapstp.exceptions.PathInfoError[source]

Bases: MyError

Error on extracting information for labels specified in STP paths.

__init__(message, row, path_info)[source]

Create exception object with information on location in path_info caused the error.

Parameters:
  • message (str) – explanation, what happened

  • row (int) – row in path_info table

  • path_info (DataFrame) – the path_info table

  • self (PathInfoError)

Return type:

None

exception mapstp.exceptions.STPParserError[source]

Bases: MyError

STP parser syntax error.

__init__(message='The STP is invalid')[source]

Create STP parsing specific exception.

Parameters:
Return type:

None

mapstp.extract_info module

Extract meta information from paths given in an STP file.

class mapstp.extract_info.MetaInfoCollector[source]

Bases: object

Helper to store meta information from a path.

__init__(mnemonic=None, factor=None, rwcl=None)
Parameters:
  • mnemonic (str | None)

  • factor (float | None)

  • rwcl (str | None)

Return type:

None

factor: float | None = None
mnemonic: str | None = None
rwcl: str | None = None
update(pars)[source]

Revise meta information collected on traversing along an STP branch.

Parameters:
Return type:

None

mapstp.extract_info.define_material_number_and_density(material_index, meta_info, path)[source]

Define material number and density from a material index for given meta info.

Parameters:
  • material_index (DataFrame) – table mapping material mnemonics to material number and density

  • meta_info (MetaInfoCollector) – … collected from the path

  • path (str) – … for diagnostics

Return type:

tuple[float | None, int | None]

Returns:

density and material

mapstp.extract_info.extract_meta_info_from_path(path)[source]

Extract meta information from an STP path.

Parameters:

path (str) – … to body with [m-…] tags

Return type:

MetaInfoCollector

Returns:

Collected meta info map.

mapstp.extract_info.extract_path_info(paths, material_index)[source]

Extract meta information from paths and associate corresponding data with each path.

Parameters:
  • paths (list[str]) – STP paths

  • material_index (DataFrame) – mnemonic-material-density lookup table

Return type:

DataFrame

Returns:

Table with material number, density, applied correction factor, and rwcl label corresponding to every path in paths

mapstp.materials module

Code to load materials map.

The map associates material number to its MCNP specification text.

mapstp.materials.MaterialsDict

Mapping material number -> material MCNP text.

alias of dict[int, str]

mapstp.materials.drop_material_cards(lines)[source]

Drop lines belonging to material cards.

Used on replacing materials in the model with ones actually used.

Parameters:

lines (Iterable[str]) – mcnp file split to lines

Yields:

all the lines of the model without material cards

Return type:

Generator[str]

mapstp.materials.get_used_materials(materials_map, path_info)[source]

Collect text of used materials specifications.

Parameters:
  • materials_map (dict[int, str]) – map material number -> spec.

  • path_info (DataFrame) – dataframe containing column with used material numbers.

Return type:

str

Returns:

All the used materials specs to be used as part of MCNP model text.

mapstp.materials.get_used_materials_sql(con, materials_map)[source]

Collect text of used materials specifications.

Parameters:
  • con (Connection) – database connection

  • materials_map (dict[int, str]) – map material number -> spec.

Return type:

str

mapstp.materials.load_materials_map(materials)[source]

Read materials from MCNP file.

Parameters:

materials (str | Path) – name of MCNP file, containing materials to read

Returns:

mapping material number -> material text

Return type:

MaterialsDict

mapstp.materials.load_materials_map_from_stream(stream)[source]

Read materials from opened MCNP file.

Parameters:

stream (TextIO) – stream to read from

Returns:

mapping material number -> material text

Return type:

MaterialsDict

mapstp.materials.materials_spec_mapper(materials_map)[source]

Create method to extract a material specification by its number.

Parameters:

materials_map (dict[int, str]) – map number -> spec

Return type:

Callable[[int], str]

Returns:

method to be used in map extracting material specification.

mapstp.materials_index module

Code to load materials index.

mapstp.materials_index.load_materials_index(materials_index=None)[source]

Load material index from file.

Parameters:

materials_index (str | None) – file name of index to load, if not provided, uses data/default-material-index.xlsx

Return type:

DataFrame

Note

Validation of material index input values is postponed to usage of defined mnemonics. The input file may contain ‘missed’ data for mnemonics in design phase, until the mnemonics are actually used.

Return type:

DataFrame

Returns:

DataFrame with columns mnemonic, number of material, density with omitted rows, where mnemonic is not specified.

Raises:

FileNotFoundError – if the file materials_index doesn’t exist.

Parameters:

materials_index (str | None)

mapstp.merge module

The module implements algorithms to transform MCNP model.

Inserts end comments with information about path in STP corresponding to a cell and sets materials and densities, if specified in STP paths.

mapstp.merge.extract_number_and_density(cell, path_info)[source]

Extract material number and density from a path_info for a given cell.

Validate the values: number, if provided, is to be positive, density - not negative.

Parameters:
  • cell (int) – index in path_info

  • path_info (DataFrame) – table of data extracted from materials index for a given STP path.

Return type:

tuple[int, float] | None

Returns:

number and density or None, if not available

mapstp.merge.is_defined(number)[source]

Check if number coming from a DataFrame object cell is not None or NaN.

Parameters:

number (float | None) – value to

Return type:

bool

Returns:

true - if number is a valid number, false - otherwise

mapstp.merge.merge_paths(output, path_info, mcnp, used_materials_text=None)[source]

Print to output the updated MCNP code.

The material numbers and densities are inserted instead of zeroes. The STP path is inserted as end of line comment below each corresponding cell.

Parameters:
  • output (TextIO) – stream to print to

  • path_info (DataFrame) – table with other information on cells: material number, density, density correction factor.

  • mcnp (Path) – The input MCNP file name.

  • used_materials_text (str | None) – The specification of materials to add to model.

Return type:

None

mapstp.stp_parser module

The module defines methods and classes to parse STP file and represent parsing results.

class mapstp.stp_parser.Body[source]

Bases: Numbered

Body (MCNP cell) definition.

__init__(number, name)
Parameters:
Return type:

None

classmethod from_string(text)[source]

Parse MANIFOLD_SOLID_BREP line.

Parameters:

text (str) – input text

Return type:

Body

Returns:

The new Body object.

Raises:

STPParserError – on invalid input

name: str
class mapstp.stp_parser.LeafProduct[source]

Bases: Product

The class to append bodies to “Product definitions”.

__init__(number, name, bodies=<factory>)
Parameters:
Return type:

None

append(body)[source]

Append body to this product.

Only applicable to LeafProduct.

Parameters:
Return type:

None

bodies: list[Body]
property is_leaf: bool

Tell if this product is the last in an STP path.

The Leaf products may have bodies and can be shared between multiple paths in STP.

Returns:

True always for LeafProducts

Bases: Numbered

Linkage between products.

__init__(number, name, src, dst)
Parameters:
Return type:

None

dst: int
classmethod from_string(text)[source]

Parse STP line with NEXT_OCCURRENCE_USAGE.

The line specifies a source->destination link between products.

Parameters:

text (str) – line from STP file being parsed.

Return type:

Link

Returns:

new Link object.

Raises:

STPParserError – on invalid input

name: str
src: int
class mapstp.stp_parser.Numbered[source]

Bases: object

The class shares common property of STP objects: number.

__init__(number)
Parameters:

number (int)

Return type:

None

number: int
class mapstp.stp_parser.Product[source]

Bases: Numbered

The class to store “Product definitions”.

__init__(number, name)
Parameters:
Return type:

None

classmethod from_string(text)[source]

Create Product from a text string.

Parameters:

text (str) – Line with ‘PRODUCT_DEFINITION’ statement from an STP file.

Return type:

Product

Returns:

New product with given number and name.

Raises:

STPParserError – if text doesn’t match ‘PRODUCT_DEFINITION’ statement format.

property is_leaf: bool

Tell if this product is the last in an STP path.

The Leaf products may have bodies and can be shared between multiple paths in STP.

Returns:

False always for non LeafProducts.

name: str
mapstp.stp_parser.check_header(inp)[source]

Check if the inp is a valid STP file.

Parameters:

inp (TextIO) – input text stream

Raises:

FileError – if header is invalid or protocol is not AP214.

Return type:

None

mapstp.stp_parser.make_index(products)[source]

Collect dictionary from a list of Product objects.

Parameters:

products (Iterable[Product]) – list of Product objects

Return type:

dict[int, Product]

Returns:

Dictionary product id -> product.

mapstp.stp_parser.parse(inp)[source]

Collect products and their links defined in an STP file.

Parameters:

inp (TextIO) – text of STP model.

Return type:

tuple[list[Product], list[Link]]

Returns:

Tuple containing list of products and list of links between them.

Raises:

FileError – with line number where parsing failed

mapstp.stp_parser.parse_path(inp)[source]

Collect products and their links defined in an STP file given by path.

Prepares and delegates the work to parse() method.

Parameters:

inp (Path) – path to STP model.

Return type:

tuple[list[Product], list[Link]]

Returns:

Tuple containing list of products and list of links between them.

mapstp.tree module

Data structures and algorithms to store and process STP nodes and their links.

class mapstp.tree.Node[source]

Bases: object

Node for the following Tree class.

Stores the references to a Product corresponding to a number presented in a Link as source or destination. Also stores reference to its parent node. Only upward search is necessary in this application, so, there’s no references to childes.

__init__(product, parent=None)
Parameters:
Return type:

None

collect_parents()[source]

Iterate through the parents of the node from root parent to this node.

Yields:

Chain of products starting from the topmost node.

Return type:

Iterator[Product]

Parameters:

self (Node)

parent: Node | None = None
product: Product
class mapstp.tree.Tree[source]

Bases: object

Upward directed tree: it is used to find only the parents from a given node.

Indexes and stores results of STP file parsing.

__init__(products, links)[source]

Create tree from objects found in an STP file.

Parameters:
  • products (Iterable[Product]) – list of product found on parsing STP

  • links (list[Link]) – pairs denoting links between the products.

  • self (Tree)

Return type:

None

create_bodies_paths()[source]

Create list of paths for each body in STP file.

Return type:

list[str]

Returns:

The list of paths.

Parameters:

self (Tree)

mapstp.tree.create_bodies_paths(products, links)[source]

Create list of paths for each body in STP file.

Parameters:
  • products (Iterable[Product]) – list of product found on parsing STP

  • links (list[Link]) – pairs denoting links between the products.

Return type:

list[str]

Returns:

The list of paths.

Raises:

ValueError – if more than one product is found in STP without components

mapstp.workflow module

The module contains typical workflow methods and base methods for them.

Use imported methods to organize other workflows in notebooks or dependant packages. Check the methods defined here as templates for other workflows.

mapstp.workflow.create_path_info(materials_index, stp)[source]

Join information from materials index and stp paths to table.

Parameters:
  • materials_index (str) – file name of materials index file.

  • stp (str) – file name of stp file.

Return type:

tuple[list[str], DataFrame]

Returns:

collected paths from the stp file table with joined information

Module contents

The mapstp package.

Provides functionality to transfer meta information inserted to STP file component names as special tags to MCNP files generated from the STP with SuperMC.

mapstp.init_logger(*, stderr_format='<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <level>{message}</level>', log_path=PosixPath('mapstp.log'))[source]

Configure logger with given parameters.

Parameters:
  • stderr_format (str | None) – log message format for stderr handler, if None, no stderr logging.

  • log_path (Path | None) – path to file for logging, if None, no file logging.

Return type:

None