Source code for aerosandbox.tools.webplotdigitizer_reader
"""A series of utilities for working with CSV data extracted from WebPlotDigitizer.https://automeris.io/WebPlotDigitizer/https://github.com/ankitrohatgi/WebPlotDigitizer"""importnumpyasnpfromtypingimportDictfrompathlibimportPathfromtypingimportUnion
[docs]defstring_to_float(s:str)->float:"""Converts a string input to a float. If not possible, returns NaN."""try:returnfloat(s)exceptValueError:returnnp.nan
[docs]defremove_nan_rows(a:np.ndarray)->np.ndarray:"""Removes any rows in a 2D ndarray where any of the entries are NaN."""nan_rows=np.any(np.isnan(a),axis=1)returna[~nan_rows,:]
[docs]defread_webplotdigitizer_csv(filename:Union[Path,str],)->Dict[str,np.ndarray]:""" Reads a CSV file produced by WebPlotDigitizer (https://automeris.io/WebPlotDigitizer/). If there's only one data series, produces a Dict with key "data" and value 2D ndarray. If there are multiple data series, produces a Dict with keys of the names and values of 2D ndarrays. 2D ndarrays are sorted by their X-values before being returned. Args: filename: Filename, as a string or pathlib Path, or equivalent. Returns: A dictionary where keys are series names and values are data points. """delimiter=","withopen(filename,"r")asf:lines=f.readlines()has_titles=np.any([np.isnan(string_to_float(s))forsinlines[0].split(delimiter)])ifhas_titles:titles=lines[0].split(delimiter)[::2]first_data_row=2else:titles=["data"]first_data_row=0all_data=np.array([[string_to_float(item)foriteminline.split(delimiter)]forlineinlines[first_data_row:]],dtype=float,)output={}fori,titleinenumerate(titles):series=all_data[:,2*i:2*i+2]all_nan_rows=np.all(np.isnan(series),axis=1)series=series[~all_nan_rows,:]sort_order=np.argsort(series[:,0])output[title]=series[sort_order,:]returnoutput