Source code for pylimer_tools.io.read_pylimer_tools_output_file
"""This module provides a few functions to read output from pylimer_tools_cpp's simulators."""importpandasaspdfrompylimer_tools.utils.cache_utilityimportdo_cache,load_cache
[docs]defread_avg_file(filename:str)->pd.DataFrame:""" Read an averages-output file from one of the simulators shipped with pylimer_tools. This function parses the output file format used by pylimer_tools_cpp simulators, handling multiple data sections and converting them to a pandas DataFrame. The function also caches results to improve performance on subsequent reads. :param filename: Path to the averages file to read :type filename: str :return: DataFrame containing the parsed averages data, grouped by OutputStep :rtype: pd.DataFrame :note: The function automatically filters out lines containing "-nan" values, null characters, or fewer than 3 columns. :note: The returned DataFrame is grouped by OutputStep, keeping only the last entry for each step. """cache=load_cache(filename,"my-avg")ifcacheisnotNone:returncachedata_frames=[]withopen(filename,"r")asf:first_line_split=f.readline().removeprefix("#").strip().split()data=[]forlineinf:if"-nan"inlineor"\x00"inlineorlen(line.split())<3:continuestripped_line=line.removeprefix("#").strip()ifstripped_line.startswith(first_line_split[0]):data_frames.append(pd.DataFrame(data,columns=first_line_split))first_line_split=stripped_line.split()data=[]elifstripped_line!="":data.append(stripped_line.split())ifnotlen(data)==0:data_frames.append(pd.DataFrame(data,columns=first_line_split))df=pd.concat(data_frames,ignore_index=True)result=df.apply(pd.to_numeric,errors="ignore")result=result.groupby("OutputStep",as_index=False).last()assertnotresult["OutputStep"].duplicated().any()do_cache(result,filename,"my-avg")returnresult