Merge pull request #66 from pim-n/exporting-data

Exporting data
This commit is contained in:
Pim Nelissen
2026-04-15 09:31:32 +02:00
committed by GitHub
4 changed files with 25 additions and 3 deletions

View File

@ -98,7 +98,7 @@ def main():
if args.showplots: if args.showplots:
plotter.plot() plotter.plot()
if not (args.save and args.showplots): if not (args.save or args.showplots):
logger.warning( logger.warning(
"No output produced. Use --save flag to save outputs and/or " "No output produced. Use --save flag to save outputs and/or "
"--showplots to display interactive plots." "--showplots to display interactive plots."

View File

@ -34,6 +34,7 @@ class SimulationEngine:
return SimulationOutput( return SimulationOutput(
name=self.landscape.name, name=self.landscape.name,
size=self.landscape.size,
count_rate=count_rate_results, count_rate=count_rate_results,
sources=source_results sources=source_results
) )

View File

@ -27,5 +27,6 @@ class SourceOutput:
@dataclass @dataclass
class SimulationOutput: class SimulationOutput:
name: str name: str
size: tuple
count_rate: CountRateOutput count_rate: CountRateOutput
sources: List[SourceOutput] sources: List[SourceOutput]

View File

@ -1,9 +1,11 @@
from dataclasses import asdict
from datetime import datetime as dt from datetime import datetime as dt
import json
import os import os
import logging import logging
import re import re
from numpy import array, full_like from numpy import array, full_like, ndarray
from pandas import DataFrame from pandas import DataFrame
from pg_rad.simulator.outputs import SimulationOutput from pg_rad.simulator.outputs import SimulationOutput
@ -12,6 +14,13 @@ from pg_rad.simulator.outputs import SimulationOutput
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, ndarray):
return obj.tolist()
return super().default(obj)
def generate_folder_name(sim: SimulationOutput) -> str: def generate_folder_name(sim: SimulationOutput) -> str:
formatted_sim_name = re.sub(r"\s+", '_', sim.name.lower()) formatted_sim_name = re.sub(r"\s+", '_', sim.name.lower())
folder_name = ( folder_name = (
@ -35,9 +44,18 @@ def save_results(sim: SimulationOutput, folder_name: str) -> None:
df = generate_df(sim) df = generate_df(sim)
csv_name = generate_csv_name(sim) csv_name = generate_csv_name(sim)
df.to_csv(f"{folder_name}/{csv_name}.csv", index=False) df.to_csv(f"{folder_name}/{csv_name}.csv", index=False)
with open(f"{folder_name}/parameters.json", 'w') as f:
json.dump(generate_sim_param_dict(sim), f, cls=NumpyEncoder)
logger.info(f"Simulation output saved to {folder_name}!") logger.info(f"Simulation output saved to {folder_name}!")
def generate_sim_param_dict(sim: SimulationOutput) -> dict:
"""Parse simulation parameters and hyperparameters to dictionary."""
d = asdict(sim)
d.pop('count_rate')
return d
def generate_df(sim: SimulationOutput) -> DataFrame: def generate_df(sim: SimulationOutput) -> DataFrame:
"""Parse simulation output to CSV format and the name of CSV.""" """Parse simulation output to CSV format and the name of CSV."""
@ -62,6 +80,7 @@ def generate_df(sim: SimulationOutput) -> DataFrame:
def generate_csv_name(sim: SimulationOutput) -> str: def generate_csv_name(sim: SimulationOutput) -> str:
"""Generate CSV name according to Alex' specification""" """Generate CSV name according to Alex' specification"""
num_src = len(sim.sources) num_src = len(sim.sources)
src_ids = [str(i+1) for i in range(num_src)]
bkg_cps = round(sim.count_rate.mean_bkg_cps) bkg_cps = round(sim.count_rate.mean_bkg_cps)
source_param_strings = [ source_param_strings = [
[ [
@ -81,5 +100,6 @@ def generate_csv_name(sim: SimulationOutput) -> str:
src_str = "_".join(src_str_array.flat) src_str = "_".join(src_str_array.flat)
csv_name = f"{num_src}_src_{bkg_cps}_cps_bkg_{src_str}" src_ids_str = "_".join(src_ids)
csv_name = f"{src_ids_str}_src_{bkg_cps}_cps_bkg_{src_str}"
return csv_name return csv_name