PKIOI9 pzfx_parser.py"""Package to load and parse tables in a Prism pzfx file.""" import xml.etree.ElementTree as ET import pandas as pd from itertools import count, chain, cycle import numpy as np __version__ = '0.2' class PrismFileLoadError(Exception): pass def _get_all_text(element): s = '' for c in element.iter(): if c.text is not None: s += c.text return s def _subcolumn_to_numpy(subcolumn): data = [float(_get_all_text(d)) if not (('Excluded' in d.attrib) and (d.attrib['Excluded'] == '1')) else np.nan for d in subcolumn.findall('d')] return np.array(data) def _parse_xy_table(table): xformat = table.attrib['XFormat'] try: yformat = table.attrib['YFormat'] except KeyError: yformat = None evformat = table.attrib['EVFormat'] xscounter = count() xsubcolumn_names = lambda: str(next(xscounter)) if yformat == 'SEN': yslist = cycle(['Mean', 'SEM', 'N']) ysubcolumn_names = lambda: next(yslist) elif yformat == 'upper-lower-limits': yslist = cycle(['Mean', 'Lower', 'Upper']) ysubcolumn_names = lambda: next(yslist) else: yscounter = count() ysubcolumn_names = lambda: str(next(yscounter)) columns = {} for xcolumn in chain(table.findall('XColumn'), table.findall('XAdvancedColumn')): xcolumn_name = _get_all_text(xcolumn.find('Title')) for subcolumn in xcolumn.findall('Subcolumn'): subcolumn_name = xcolumn_name + '_' + xsubcolumn_names() columns[subcolumn_name] = _subcolumn_to_numpy(subcolumn) for ycolumn in chain(table.findall('YColumn'), table.findall('YAdvancedColumn')): ycolumn_name = _get_all_text(ycolumn.find('Title')) for subcolumn in ycolumn.findall('Subcolumn'): subcolumn_name = ycolumn_name + '_' + ysubcolumn_names() columns[subcolumn_name] = _subcolumn_to_numpy(subcolumn) maxlength = max([v.shape[0] for v in columns.values()]) for k, v in columns.items(): if v.shape[0] < maxlength: columns[k] = np.pad(v, ((0, maxlength-v.shape[0])), mode='constant', constant_values=np.nan) return pd.DataFrame(columns) def _parse_table_to_dataframe(table): table_id = table.attrib['ID'] tabletype = table.attrib['TableType'] if tabletype == 'XY' or tabletype == 'TwoWay' or tabletype == 'OneWay': df = _parse_xy_table(table) else: raise PrismFileLoadError('Cannot parse %s tables for now!' % tabletype) return df def read_pzfx(filename): """Open and parse the Prism pzfx file given in `filename`. Returns a dictionary containing table names as keys and pandas DataFrames as values.""" tree = ET.parse(filename) root = tree.getroot() if root.tag != 'GraphPadPrismFile': raise PrismFileLoadError('Not a Prism file!') if root.attrib['PrismXMLVersion'] != '5.00': raise PrismFileLoadError('Can only load Prism files with XML version 5.00!') tables = {_get_all_text(table.find('Title')): _parse_table_to_dataframe(table) for table in root.findall('Table')} return tables PKv1NZ;;!pzfx_parser-0.2.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2019 Ulrich Dobramysl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!Hd BUcpzfx_parser-0.2.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,rzd&Y)r$[)T&UD"PK!H^l"pzfx_parser-0.2.dist-info/METADATA]KOA+rDL$B4;< JzE5fl^)&NL^-)=`L/3-b<(`#A`Mot̐GtMvx*1o *o(P?$#Y