Created
March 18, 2022 18:59
-
-
Save caiofcm/1088c9b62f0968b665a91f15ff23d447 to your computer and use it in GitHub Desktop.
Web Plot Digitizer to csv and pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import typing | |
from pathlib import Path | |
import click | |
import numpy as np | |
import pandas as pd | |
def get_dataset_as_dict(data_loaded: dict) -> dict: | |
datasetColl = data_loaded['datasetColl'] | |
dict_of_data_sets = {} | |
for data_set in datasetColl: | |
data = data_set['data'] | |
xy_par = np.array([datum['value'] for datum in data]) | |
dict_of_data_sets[data_set['name']] = xy_par | |
return dict_of_data_sets | |
def get_dataset_as_dataframe_from_dict(dict_of_data_sets: dict) -> pd.DataFrame: | |
list_of_dfs = [] | |
for name, xy_matrix in dict_of_data_sets.items(): | |
df = pd.DataFrame({ | |
'x': xy_matrix[:,0], | |
'y': xy_matrix[:,1], | |
'name': name, | |
}) | |
list_of_dfs += [df] | |
df_dataset = pd.concat(list_of_dfs, ignore_index=True) | |
return df_dataset | |
def get_dataset_as_daframe_from_path(fpath: typing.Union[str, Path]) -> pd.DataFrame: | |
with open(fpath, 'r') as fp: | |
data_loaded = json.load(fp) | |
dict_of_data_sets = get_dataset_as_dict(data_loaded) | |
df_dataset = get_dataset_as_dataframe_from_dict(dict_of_data_sets) | |
return df_dataset | |
def main(fpath_json, fpath_out): | |
df_dataset = get_dataset_as_daframe_from_path('wpd_project (2).json') | |
print(df_dataset.head()) | |
return df_dataset | |
@click.command() | |
@click.argument('fpath_json', type=click.Path(exists=True)) | |
@click.argument('fpath_out', type=click.Path(exists=False)) | |
def main_click(fpath_json, fpath_out): | |
df_dataset = get_dataset_as_daframe_from_path(fpath_json) | |
print(df_dataset.head()) | |
return df_dataset | |
if __name__ == "__main__": | |
# main() | |
main_click() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment