Merge pull request #9 from logos-co/plotter

Plotter
This commit is contained in:
Alberto Soutullo 2022-11-29 13:16:21 +01:00 committed by GitHub
commit 4051ed3c98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 155 additions and 58 deletions

Binary file not shown.

View File

@ -1,5 +1,8 @@
{
"arguments": {"output-format": "csv", "output-file": "test"},
"arguments": {
"output-format": "csv",
"output-file": "test"
},
"simulation": {
"consensus_settings": {
"snow_ball": {
@ -38,7 +41,23 @@
],
"seed": 18042022
},
"plotting": {
"test": "test"
"plotter": {
"countplot": {
"plot_options": {
"x": "vote"
},
"save_options": {
"name": "test.png"
}
},
"scatterplot": {
"plot_options": {
"x": "id",
"y": "vote"
},
"save_options": {
"name": "test2.png"
}
}
}
}

View File

@ -2,6 +2,7 @@
import typer
# Project Imports
from src.plotter.run_plotter import run_plotter
from src.simulation_runner.runner import run_simulation
from src.utilities.files.json.simulation_config_parser import SimulationConfigParser
@ -16,11 +17,10 @@ def main(
if run_type == "simulation":
run_simulation(arguments_config, simulation_config)
elif run_type == "plotter":
# run_plotter()
pass
run_plotter(arguments_config, plotter_config)
else:
run_simulation()
# run_plotter()
run_plotter()
if __name__ == '__main__':

View File

@ -1,24 +1,24 @@
# Python Imports
import typer
import seaborn as sns
import matplotlib.pyplot as plt
# Project Imports
from src.utilities.env_variables import SHARED_FOLDER
from src.utilities.files.simulation_data_parser import SimulationDataParser
def main(
input_data: str = typer.Option(..., "--input-data", "-i"),
plot_type: str = typer.Option(..., "--plot-type", "-p"),
plot_settings: str = typer.Option(..., "--plot-settings", "-s"),
):
# Parse data file
print("")
# Accept plot type
def run_plotter(arguments_config: dict, plotter_config: dict):
file_name = f"{arguments_config['output-file']}.{arguments_config['output-format']}"
# Read file given in arguments config
parser = SimulationDataParser()
polars_df = parser.read_content(SHARED_FOLDER + file_name)
# Handle Plot arguments
pandas_df = polars_df.to_pandas()
'''
import foo
bar = getattr(foo, 'bar')
result = bar()
'''
if __name__ == '__main__':
typer.run(main)
# Loop through all plots given in plotting section
for plot, options in plotter_config.items():
print(plot)
print(options)
method = getattr(sns, plot)
method(pandas_df, **options["plot_options"])
plt.savefig(SHARED_FOLDER + options["save_options"]["name"])

View File

@ -6,6 +6,12 @@ CONFIGURATION_SETTINGS = "shared/configuration_settings.json"
# Schemas
ARGUMENTS_SCHEMA_PATH = "src/utilities/files/schemas/arguments_schema.json"
CONFIGURATION_SCHEMA_PATH = "src/utilities/files/schemas/snow_family_configuration_schema.json"
PLOTTER_SCHEMA_PATH = "src/utilities/files/schemas/plotter_schema.json"
# Shared folder
SHARED_FOLDER = "shared/"
# Configuration
ARGUMENTS_BLOCK = "arguments"
SIMULATION_BLOCK = "simulation"
PLOTTER_BLOCK = "plotter"

View File

@ -16,17 +16,16 @@ class SimulationConfigParser:
# Split two parts
arguments_config = json_configuration["arguments"]
simulation_config = json_configuration["simulation"]
# plotter_config = json_configuration["plotter"]
plotter_config = json_configuration["plotter"]
# Retrieve valid schemas
arguments_json_schema = read_json(env_variables.ARGUMENTS_SCHEMA_PATH)
config_json_schema = read_json(env_variables.CONFIGURATION_SCHEMA_PATH)
# plotter_json_schema = read_json(env_variables.plotter_schema_path)
# plotter_json_schema = read_json(env_variables.plotter_schema_path) todo uncomment
# Validate
validate_json(arguments_config, arguments_json_schema)
validate_json(simulation_config, config_json_schema)
# validate_json(plotter_config, plotter_json_schema)
# validate_json(plotter_config, plotter_json_schema) todo uncomment
# return simulation_config, plotter_config
return arguments_config, simulation_config, {}
return arguments_config, simulation_config, plotter_config

View File

@ -0,0 +1,66 @@
{
"type": "object",
"properties": {
"scatterplot": {
"type": "object",
"properties": {
"plot_options": {
"type": "object"
},
"save_options": {
"type": "object"
}
},
"required": [
"plot_options",
"save_options"
]
},
"lineplot": {
"type": "object",
"properties": {
"plot_options": {
"type": "object"
},
"save_options": {
"type": "object"
}
},
"required": [
"plot_options",
"save_options"
]
},
"histplot": {
"type": "object",
"properties": {
"plot_options": {
"type": "object"
},
"save_options": {
"type": "object"
}
},
"required": [
"plot_options",
"save_options"
]
},
"countplot": {
"type": "object",
"properties": {
"plot_options": {
"type": "object"
},
"save_options": {
"type": "object"
}
},
"required": [
"plot_options",
"save_options"
]
}
},
"aditionalProperties": false
}

View File

@ -1,6 +1,8 @@
# Python Imports
import os.path
import polars
# Project Imports
from src.utilities.files.simulation_data_types.json_data_simulation_handler import JsonDataSimulationHandler
from src.utilities.files.simulation_data_types.csv_data_simulation_handler import CsvDataSimulationHandler
@ -8,17 +10,18 @@ from src.utilities.files.simulation_data_types.parquet_simulation_data_handler i
handlers = {'.csv': CsvDataSimulationHandler,
'.json': JsonDataSimulationHandler,
'parquet': ParquetDataSimulationHandler
'.parquet': ParquetDataSimulationHandler
}
class SimulationDataParser:
def __init__(self, file_path):
self._file_path = file_path
def __init__(self):
self._file_path = None
self._file_extension = None
def read_content(self):
def read_content(self, file_path: str) -> polars.DataFrame:
self._file_path = file_path
# Check file exists
self._check_file()
# Check extension
@ -26,10 +29,9 @@ class SimulationDataParser:
# Get correct handler
handler = self._get_data_handler()
# Load content
handler.load_data()
# Convert it to dataframe
dataframe = handler.convert_into_dataframe()
polars_df = handler.load_data(self._file_path)
# Return
return polars_df
def _check_file(self):
exists = os.path.exists(self._file_path)
@ -39,7 +41,7 @@ class SimulationDataParser:
exit(1)
def _check_extension(self):
return os.path.splitext(self._file_path)[1]
self._file_extension = os.path.splitext(self._file_path)[1]
def _get_data_handler(self):
handler = handlers.get(self._file_extension)

View File

@ -1,16 +1,11 @@
# Python Imports
import abc
from abc import ABC
import polars as pl
class BaseDataSimulationHandler(ABC):
def __init__(self):
self._data = None
@abc.abstractmethod
def load_data(self, path):
pass
@abc.abstractmethod
def convert_into_dataframe(self):
def load_data(self, path: str) -> pl.DataFrame:
pass

View File

@ -1,10 +1,14 @@
# Python Imports
import polars as pl
# Project Imports
from src.utilities.files.simulation_data_types.base_data_simulation_handler import BaseDataSimulationHandler
class CsvDataSimulationHandler(BaseDataSimulationHandler):
def load_data(self, path):
pass
def load_data(self, path: str) -> pl.DataFrame:
polars_df = pl.read_csv(path)
return polars_df
def convert_into_dataframe(self):
pass

View File

@ -1,10 +1,13 @@
# Python Imports
import polars as pl
# Project Imports
from src.utilities.files.simulation_data_types.base_data_simulation_handler import BaseDataSimulationHandler
class JsonDataSimulationHandler(BaseDataSimulationHandler):
def load_data(self, path):
pass
def load_data(self, path: str) -> pl.DataFrame:
polars_df = pl.read_json(path)
def convert_into_dataframe(self):
pass
return polars_df

View File

@ -1,10 +1,13 @@
# Python Imports
import polars as pl
# Project Imports
from src.utilities.files.simulation_data_types.base_data_simulation_handler import BaseDataSimulationHandler
class ParquetDataSimulationHandler(BaseDataSimulationHandler):
def load_data(self, path):
pass
def load_data(self, path: str) -> pl.DataFrame:
polars_df = pl.read_parquet(path)
def convert_into_dataframe(self):
pass
return polars_df