mahautils.multics.PolygonFile#
- class mahautils.multics.PolygonFile(path: str | Path | None = None, unit_converter: UnitConverter | None = None)#
Bases:
MahaMulticsConfigFileAn object representing a Maha Multics polygon file
The Maha Multics software uses polygon files to store geometry used for setting boundary conditions when running fluid simulations. This class is capable of parsing such files and providing an interface through which users can interact with and manipulate data in the file (through the
polygon_dataattribute).Note
The data structure in
PolygonFileobjects largely mirrors the content of Maha Multics polygon files, although there are a few differences (for which descriptive error messages are displayed). For more information about the format of these files, refer to the Polygon File Format page.Attributes
The number of time steps in the polygon file
A reference to the dictionary containing the polygon data in the file
A copy of the dictionary containing the polygon data in the file
The method used to combine disjoint polygons
The method used to handle time values outside the defined time range
The units in which "time" values are stored in the polygon file
The values of "time" defined in the polygon file
Methods
__init__([path, unit_converter])Creates an object to represent Maha Multics configuration files
filter_times(interval[, tolerance])Reduces the number of time steps in a polygon file
parse()Parses the file content in the
contentslist and populates attributes (such as the dictionary returned bypolygon_data) with extracted dataplot([delay, show, return_fig])Generates an animated plot showing the geometry in the polygon file
time_step([units])Returns the time step for the polygon file
Updates the
contentslist based on object attributesInherited Attributes
A tuple of all characters considered to denote comments
A reference to a list containing the (potentially modified) file content of each line of the file
A copy of the dictionary containing any file hashes previously computed for the file specified by the
pathattributeThe character(s) used to denote the end of lines in the text file
Path describing the location of the file on the disk
A copy of the raw file content
Whether the original file had a newline at the end of the file
The unit converter used to convert the units of quantities stored in the file
Inherited Methods
clean_contents([remove_comments, ...])Clean
contentsin-placeClears any stored file hashes
compute_file_hashes([hash_functions, store])Computes hashes of the file specified by the
pathattributeextract_section_by_keyword(section_label, ...)Extracts a section from the
contentslist of file linesReturns whether the file specified by the
pathattribute has changed since the last time file hashes were computedoverwrite([prologue, epilogue, line_ending])read([path, parse])Read file from disk
set_contents(contents, trailing_newline[, ...])Add data to the
contentslistset_read_metadata([path])Configures metadata related to file to be read from disk
store_file_hashes([hash_functions])Computes and stores hashes of the file specified by the
pathattributetrack_new_file(path[, hash_functions])Shortcut for simultaneously modifying the
pathattribute and storing file hasheswrite(output_file[, write_mode, ...])Write file to disk
- __init__(path: str | Path | None = None, unit_converter: UnitConverter | None = None) None#
Creates an object to represent Maha Multics configuration files
Creates an instance of the
MahaMulticsConfigFileclass, including configuring file comments to be represented by the#character.- Parameters:
path (str or pathlib.Path, optional) – Location of the text file in the file system (default is
None)unit_converter (pyxx.units.UnitConverter, optional) – A
pyxx.units.UnitConverterinstance which will be used to convert units of quantities stored in the configuration file (default isNone, which uses theMahaMulticsUnitConverterunit converter to perform unit conversions)
- property trailing_newline: bool#
Whether the original file had a newline at the end of the file
- property num_time_steps: int#
The number of time steps in the polygon file
- property polygon_data: Dictionary[float, Layer]#
A reference to the dictionary containing the polygon data in the file
This method returns a
mahautils.utils.Dictionaryinstance containing the polygon data in the file. The keys are the time values defined in the file (with units given bytime_units) and the values areLayerinstances containing the polygons corresponding to each time step. This dictionary can be directly edited to modify the polygon file (adding new time steps, removing time steps, changing polygon properties, etc.).Important
The dictionary is returned by reference, so editing the returned dictionary will directly edit data in the
PolygonFileinstance.Notes
This property can only be accessed if
polygon_merge_method,time_extrap_method, andtime_unitsare all defined (even for polygon files with a single time step, since additional time steps may be added by modifying the polygon data dictionary). This ensures consistency of the data stored in thePolygonFileobject. To access the polygon data before setting these properties, usepolygon_data_readonly.Any of the methods for adding or removing data provided by
mahautils.utils.Dictionarycan be used to modify polygon data. For instance, to insert a new time step prior to an existing time, simply usemahautils.utils.Dictionary.insert_before(). This is why there is no “add polygon” method – accessing the dictionary methods provides greater flexibility.
- property polygon_data_readonly: Dictionary[float, Layer]#
A copy of the dictionary containing the polygon data in the file
This method returns a
mahautils.utils.Dictionaryinstance containing the polygon data in the file. The keys are the time values defined in the file (with units given bytime_units) and the values areLayerinstances containing the polygons corresponding to each time step.Important
The dictionary is returned by copy, so editing the returned dictionary will not directly edit data in the
PolygonFileinstance.Notes
Unlike
polygon_data, this method does not require thepolygon_merge_method,time_extrap_method, andtime_unitsproperties to be defined defined.
- property polygon_merge_method: int#
The method used to combine disjoint polygons
This attribute represents the method for combining multiple disjoint polygons and is applied for all time steps (a limitation hard-coded in the Maha Multics software).
For more information and a list of permissible values for the,
polygon_merge_methodproperty refer to the Polygon File Format page.
- property time_extrap_method: int#
The method used to handle time values outside the defined time range
This property defines how the Maha Multics software will handle time values outside the range given by
time_values.For more information and a list of permissible values for the,
time_extrap_methodproperty refer to the Polygon File Format page.
- property time_units: str#
The units in which “time” values are stored in the polygon file
Note that the term “time” is used loosely for polygon files, and “time” may also be given in terms of quantities such as shaft rotation angle. For more information, refer to the Polygon File Format page.
- property time_values: List[float]#
The values of “time” defined in the polygon file
This method returns a copy of the list of time values, so modifying the returned list will not affect the time values stored in the polygon file. The time values are returned with units given by
time_units.
- filter_times(interval: float, tolerance: float = 1e-12) None#
Reduces the number of time steps in a polygon file
Prunes time steps from a polygon file at user-specified intervals. This can be useful, for instance, if you have a polygon file with a time step of 0.01 seconds and you want to increase the time step to 0.1 seconds.
- Parameters:
interval (float) – The spacing at which to keep time steps, in units of
time_unitstolerance (float, optional) – The allowable difference between retained time steps and intervals of size
interval(default is1e-12)
Notes
Time steps are kept if they fall within a distance of
toleranceof{..., -2*interval, -interval, 0, interval, 2*interval, ...}
- parse() None#
Parses the file content in the
contentslist and populates attributes (such as the dictionary returned bypolygon_data) with extracted dataThis method parses the data in
contents, checking for polygon file format errors and extracting data on individual polygons for easier reading and modification.
- clean_contents(remove_comments: bool = False, skip_full_line_comments: bool = False, strip: bool = False, concat_lines: bool = False, remove_blank_lines: bool = False) None#
Clean
contentsin-placeCleans
contents(removing comments, blank lines, etc.) based on user-defined rules. Modifications are made in-place (i.e., the resulting content is stored incontents).- Parameters:
remove_comments (bool, optional) – Whether to remove comments from file (default is
True)skip_full_line_comments (bool, optional) – Whether to skip removing comments where the comment is the only text on a line. Only applies if
remove_commentsisTrue(default isFalse)strip (bool, optional) – Whether to strip leading and trailing whitespace from each line (default is
True)concat_lines (bool, optional) – Whether to concatenate lines ending with a backslash with the following line (default is
True)remove_blank_lines (bool, optional) – Whether to remove lines that contain no content after other cleaning operations have completed (default is
True)
- clear_file_hashes() None#
Clears any stored file hashes
- property comment_chars: Tuple[str, ...] | None#
A tuple of all characters considered to denote comments
- compute_file_hashes(hash_functions: tuple | str = ('md5', 'sha256'), store: bool = False) Dict[str, str]#
Computes hashes of the file specified by the
pathattributeComputes and returns the hashes of the file specified by the
pathattribute, with the option to populate thehashesdictionary with their values.- Parameters:
hash_functions (tuple or str, optional) – Tuple of strings (or individual string) specifying which hash(es) to compute. Any hash functions supported by
hashlibcan be used. Default is('md5', 'sha256')store (bool, optional) – Whether to store the computed hashes in the
hashesdictionary (default isFalse)
- Returns:
A dictionary containing the file hashes specified by
hash_functions- Return type:
dict
See also
pyxx.files.compute_file_hashFunction used to compute file hashes
Notes
Prior to calling this method, the
pathattribute must be defined. To simultaneously set thepathattribute and store file hashes, usetrack_new_file().
- property contents: List[str]#
A reference to a list containing the (potentially modified) file content of each line of the file
Warning
This attribute returns the list by reference. This means that if you set a variable equal to this reference, then editing this variable will edit the
contentsattribute (e.g., if you setmy_content = MyTextFile.contents, then editingmy_contentwill change the content stored inMyTextFile).Notes
If trying to set the
contentsattribute, do not try to set this attribute directly (i.e., don’t use code similar toMyTextFile.contents = ['line1', 'line2', 'line3']). Instead, use theset_contents()method, as it offers greater control over whether the contents are passed by reference or value.
- extract_section_by_keyword(section_label: str, begin_regex: str, end_regex: str, section_line_regex: str = '(.*)', max_sections: int | None = None, begin_idx: int = 0, allow_comment_lines: bool = True) Tuple[List[Match], List[Tuple[str, ...]], int, int]#
Extracts a section from the
contentslist of file linesMany Maha Multics configuration files contain sections with certain types of data, where the section begins following a formatted section marker and ends at another marker (both with unique, identifiable regex patterns). This method extracts the data from such a section. If multiple sections are found, the data in all sections is merged, unless specified otherwise by setting
max_sections.- Parameters:
section_label (str) – A descriptive name identifying the section. This is not used in parsing the file; it is only used to customize error messages and make them more descriptive
begin_regex (str) – The regex pattern which marks the beginning of the section
end_regex (str) – The regex pattern which marks the end of the section
section_line_regex (str, optional) – If provided, this regex pattern must be matched by all lines inside the section (default is
'(.*)', which matches any text)max_sections (int, optional) – The maximum number of sections to extract; that is, only the first
max_sectionsencountered will be extracted and returned (default isNone, which extracts all sections)begin_idx (int, optional) – The index (in the
contentslist) at which to begin to search for and extract data from sections (default is0)allow_comment_lines (bool, optional) – If
True, any lines within the section that do not matchsection_line_regexbut begin with any of the characters incomment_charswill be outputted (part of the second output of the method); ifFalse, any lines within the section that do not matchsection_line_regexwill result in an error being thrown (default isTrue)
- Returns:
list – A list of
re.Matchobjects containing the matches for the regex patternsection_line_regexfor all lines in the section(s)list – A list (of the same length as the first argument returned) of tuples of strings. For each
re.Matchobject, the corresponding item in this list contains a tuple with any full-line comments preceding the matched lineint – The index of
contentsof the next line immediately following the line on whichend_regexwas foundint – The number of sections that were extracted from the
contentslist
- has_changed() bool#
Returns whether the file specified by the
pathattribute has changed since the last time file hashes were computed- Returns:
Whether file has changed since the last time file hashes were computed
- Return type:
bool
- property hashes: Dict[str, str]#
A copy of the dictionary containing any file hashes previously computed for the file specified by the
pathattribute
- property line_ending: str | Tuple[str, ...]#
The character(s) used to denote the end of lines in the text file
This property only applies to files that were read using the
read()method. After reading a file, this property stores the line ending(s) used in the file. Lines in text files can be terminated with'\n'(LF),'\r\n'(CRLF),'\r', or a combination of these characters (potentially with different line endings on different lines).After reading a file, this property stores either a string containing the line endings on every line of the file, or a tuple containing all line endings encountered throughout the file.
- overwrite(prologue: str = '', epilogue: str | None = None, line_ending: str = '\n') None#
Write data in
contentsto the file specified bypathWrites the lines of content in the
contentsattribute to the (previously-defined) file specified by thepathattribute, suppressing warnings before overwriting the file. This is useful for cases when the file contents are manually populated and it is desired to “dump” them to a file. This method is also useful if a file’s contents need to be updated periodically based on the results of another process.- Parameters:
prologue (str, optional) – Content written at beginning of file (default is
'')epilogue (str, optional) – Content written at end of file (default is to use the value of the
line_endingargument iftrailing_newlineisTrueand''otherwise)line_ending (str, optional) – String written at the end of each line when writing file content (default is
'\n')
- property path: Path | None#
Path describing the location of the file on the disk
Assigning a value to this attribute (regardless whether it matches the current value or is a different path) will save the value as a
pathlib.Pathand will automatically clear any saved file hashes.
- plot(delay: float = 100, show: bool = True, return_fig: bool = False) Figure | None#
Generates an animated plot showing the geometry in the polygon file
Polygons in the file are illustrated as filled, solid shapes, and construction geometry is illustrated as dotted outlines.
- Parameters:
delay (float, optional) – The delay (in milliseconds) between each frame when animating the plot (default is
100)show (bool, optional) – Whether to open the figure in a browser (default is
True)return_fig (bool, optional) – Whether to return the figure (default is
False)
- Returns:
An animated Plotly figure depicting the polygon file. Returned if and only if
return_figisTrue- Return type:
go.Figure
- property raw_contents: List[str] | None#
A copy of the raw file content
If the file was read using the
read()method, this attribute stores the original, unaltered contents of each line of the input file, and it returns a copy of this list of lines. If the file was not read with theread()method, this attribute stores a value ofNone.
- read(path: str | Path | None = None, parse: bool = True) None#
Read file from disk
Calling this method reads the file specified by the
pathattribute from the disk, populatingcontentsandraw_contents. Additionally, the file hashes stored in thehashesattribute are updated (to make it easier to check if the file has been modified later).- Parameters:
path (str or pathlib.Path, optional) – Location of the text file in the file system (default is
None)parse (bool, optional) – Whether to call the
parse()method after reading the file (default isTrue)
- set_contents(contents: List[str], trailing_newline: bool, pass_by_reference: bool = False) None#
Add data to the
contentslistAllows users to manually fill the
contentslist with user-defined content. The input list must be a list of strings, and the user can optionally choose whether to pass the input by reference or value.- Parameters:
contents (list) – List of strings which are to be assigned to the
contentslisttrailing_newline (bool) – Whether the contents being added represent a file with a trailing newline (because the file wasn’t read, the object has no way to determine whether the file has a trailing newline, so users must provide this information)
pass_by_reference (bool, optional) – Whether to pass the
contentsargument by reference (default isFalse)
Notes
If passing
contentsby reference, this means that if subsequent changes are made to the originalcontentsobject, they will be reflected in thecontentsattribute. If passing by value, then a copy of thecontentsargument will be made, so changing the object outside the class instance will not affect thecontentsattribute.
- set_read_metadata(path: str | Path | None = None) None#
Configures metadata related to file to be read from disk
This method performs several pre-processing steps to prepare to read a file from the disk:
Sets the
pathattribute. If thepathargument was provided, the attribute is set to this value; otherwise, the existing value stored in thepathattribute is used (or an error is thrown if not defined).Verifies that the file specified by the
pathattribute exists.Stores the hashes for the file.
It is advised that this method be called prior to reading any file.
- Parameters:
path (str or pathlib.Path, optional) – Location of the file in the file system (default is
None)- Raises:
- store_file_hashes(hash_functions: tuple | str = ('md5', 'sha256')) None#
Computes and stores hashes of the file specified by the
pathattributeComputes given hashes of the file specified by the
pathattribute and populates thehashesdictionary with their values.- Parameters:
hash_functions (tuple or str, optional) – Tuple of strings (or individual string) specifying which hash(es) to compute. Any hash functions supported by
hashlibcan be used. Default is('md5', 'sha256')
See also
pyxx.files.compute_file_hashFunction used to compute file hashes
track_new_fileUse this method if you want to store file hashes but the
pathattribute isn’t yet defined
Notes
Prior to calling this method, the
pathattribute must be defined. To simultaneously set thepathattribute and store file hashes, usetrack_new_file().
- track_new_file(path: str | Path, hash_functions: tuple | str = ('md5', 'sha256')) None#
Shortcut for simultaneously modifying the
pathattribute and storing file hashesThis method functions as a “shortcut,” both modifying the
pathattribute and storing an optionally user-specified list of file hashes in thehashesattribute. The intention of this method is that if aFileinstance is tracking a given file, and user wants to switch to tracking another file, this provides a convenient way to do so with a single line of code.- Parameters:
file (str or pathlib.Path) – File that the object is to represent
hash_functions (tuple or str, optional) – Tuple of strings (or individual string) specifying which hash(es) to compute. Any hash functions supported by
hashlibcan be used. Default is('md5', 'sha256')
See also
pyxx.files.compute_file_hashFunction used to compute file hashes
- property unit_converter: UnitConverter#
The unit converter used to convert the units of quantities stored in the file
- write(output_file: str | Path, write_mode: str = 'w', warn_before_overwrite: bool = True, prologue: str = '', epilogue: str | None = None, line_ending: str = '\n', update_contents: bool = True) None#
Write file to disk
Calling this method writes the file contents stored in
contentsto the disk.- Parameters:
output_file (str or pathlib.Path) – Output file to which to write content
write_mode (str, optional) – Any mode (such as
'w'or'a') for the built-inopen()function for writing files (default is'w')warn_before_overwrite (bool, optional) – Whether to throw an error if
output_filealready exists (default isTrue)prologue (str, optional) – Content written at beginning of file (default is
'')epilogue (str, optional) – Content written at end of file (default is to use the value of the
line_endingargument iftrailing_newlineisTrueand''otherwise)line_ending (str, optional) – String written at the end of each line when writing file content (default is
'\n')update_contents (bool, optional) – Whether to call the
update_contents()method before writing the file (default isTrue)
- time_step(units: str | None = None) float#
Returns the time step for the polygon file
Verifies that the polygon file has a constant, positive time step between successive values in
time_valuesand returns this time step. If the polygon file has only a single time step,0is returned.- Parameters:
units (str, optional) – The units in which the time step should be returned. Required to be provided if the polygon file has more than one time step, but optional otherwise (default is
None)- Returns:
The time step between successive values in
time_values, or0for polygon files with a single time step- Return type:
float
- Raises:
PolygonFileFormatError – If the polygon file has multiple time values but the time step between them is not consistent
Notes
The term “time” is used loosely for polygon files, and “time” may also be given in terms of quantities such as shaft rotation angle. For more information, refer to the Polygon File Format page.
- update_contents() None#
Updates the
contentslist based on object attributesThis method synchronizes the
contentslist with the data stored in thePolygonFileattributes. This step should generally be performed prior to callingwrite(), as writing a file directly saves the data incontentsto disk.Construction polygons are not added to the
contentslist.