grapa.datatypes.graphXML.GraphXML

class grapa.datatypes.graphXML.GraphXML(filename: str | List[str] | List | ndarray = '', complement: dict | str | List[dict | str] = '', silent: bool = True, config: str | None = 'auto', log_active: bool = False)

Bases: Graph

Can read xml files. Can be used from outside grapa with class methods:

content = GraphXML.parsexml_to_dict(filename, preset)
datasets = GraphXML.split_format_datasets(content, filename, preset)

Intepret xml hierarchy as keys of a dictionary A fundamental problem is that xml format accepts several children to have same names etc., while python dictionary only accept uique keys. The following behaviors are implemented:

  • increment key values: <text>value 1</text><text>value 2</text> –> {"text#0": "value1", "text#1": "value2"}

  • attribs_append_to_tag: <length unit="m">123</length> with [["unit", "()", "delete"]] –> {"length(m)": 123}

  • attribs_as_metadata: process attrib as if it were a subelement. <item id="abc"><sub>content</sub></item> with [["id", "delete"]] –> {"item_id": "abc", "item_sub": "content"}

See file XML_fitparameters.txt for more details.

__init__(filename: str | List[str] | List | ndarray = '', complement: dict | str | List[dict | str] = '', silent: bool = True, config: str | None = 'auto', log_active: bool = False)

default constructor. Calls method reset.

Methods

convert_file(filename[, preset])

# parse an XML file and reformat the content into a grapa-style output file # Maybe useful to debug and try converg to suitable values to input in # XML_dataformats.txt input # not tested actually, more to store this bit of code somewhere

isFileReadable(_fname, fileext[, line1, ...])

parsexml_to_dict(filename, preset[, silent])

in principle, a generic xml parser, agnostic of file content.

readDataFromFile(attributes[, line1, line2, ...])

split_format_datasets(datainput, filename, ...)

retrieve data from within metadata Splits content according to possibly several datasets.

Attributes

DATAFORMATS

DATAFORMATS_FILE

FILEIO_GRAPHTYPE

PATHXMLFILE

classmethod convert_file(filename, preset='auto')

# parse an XML file and reformat the content into a grapa-style output file # Maybe useful to debug and try converg to suitable values to input in # XML_dataformats.txt input # not tested actually, more to store this bit of code somewhere

classmethod parsexml_to_dict(filename, preset, silent=False)

in principle, a generic xml parser, agnostic of file content. As few as possible file-format-specific modifications Some custom behavios configured in preset

  • “attribs_append_to_tag”: []

    [["unit", "()", "delete"]]: parses <length unit="m">123</length> –> {"length(m)": 123}

    [["axis", "-", "delete"]]: parses <position axis="AXIS_1">123</position> –> {"position-AXIS_1": 123}

    Special value[2]: “delete” will delete the attrib[key] after append. Purposes: clear output, compact and self-sufficient key-value pairs

  • “attribs_as_metadata”: to extract attrb as if it were a subelement.

    With [["id", "delete"]]: <item id="abc"><sub>content</sub></item> –> "item_id": "abc", "item_sub": "content"

  • “separator”: “_”, # could be “_” or “/”

  • “key_replace”: format metadata key to shorten metadata keys, e.g. [["xrdMeasurement", "xrd", "replace"], ["xrds_xrd_", "xrd_", "replace"], ["xrds_xrd", "xrd", "equal"]]

    list element [2] can be “equal” (replace only if str are equal), or “replace” (apply str.replace() function)

  • “roottag_strip”: to remove excess information from the tag. “{}” to remove ‘{url}’ found in root tag

classmethod split_format_datasets(datainput, filename, preset)

retrieve data from within metadata Splits content according to possibly several datasets. Datasets consist in metadata, and 2 1-D vectors of data: datax, datay Returns a dict {idx1: {“data”: [datax, datay], “metadata”: {…}}, idx2: …}