Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions sample_plot_config.ini
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
[General]
title = nuspacesim_run
rows = 1
columns = 1
figsize = (8,7)
pop_up = yes
save_to_file = no
save_as = pdf
default_color = 0
# as in 'C0'
default_markersize = 3
default_colormap = viridis
# output_path = ~/Documents/Pictures

[Spectra]
spectra_histogram = yes

[Geometry]
geom_beta_tr_hist = no
#interaction_height = no (to be implemented)
geom_beta_tr_hist = yes
path_length_to_detector = yes
# interaction_height = no (to be implemented)
# geometry_overview = no (to be implemented)

[Taus]
taus_scatter = no
taus_pexit = no
taus_histogram = yes
taus_overview = yes
energy_hists = yes
beta_hist = yes
tau_beta_hist = yes
tau_lorentz_hist = yes
tau_exit_prob_hist = yes
tau_beta_hex = yes
tau_lorentz_hex = yes
tau_exit_prob_hex = yes

[EASOpitcal]
eas_optical_scatter = no
eas_optical_histogram = yes
numpes_vs_beta = yes
altdec_vs_numpes = yes
showerEnergy_vs_numPEs = yes
costhetacheff_vs_beta = yes
altDec_vs_costhetacheff = yes
showerEnergy_vs_costhetacheff = yes
numpes_hist = yes
costhetacheff_hist = yes

[Detector]
#event_count_at_detector = no (to be implemented)
#detection_prob_vs_beta = no (to be implemented)
# event_count_at_detector = no (to be implemented)
# detection_prob_vs_beta = no (to be implemented)
# detector_overview = no (to be implemented)

[Overview]
dashboard = yes
taus_density_beta_overview = yes
taus_histograms_overview = yes
taus_pexit_overview = yes
taus_overview = yes
eas_optical_density_overview = yes
eas_optical_histogram_overview = yes
281 changes: 4 additions & 277 deletions src/nuspacesim/apps/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,9 @@
"""


import configparser

import click

from .. import NssConfig, SimulationParameters, simulation
from ..compute import compute
from ..config import FileSpectrum, MonoSpectrum, PowerSpectrum
from ..results_table import ResultsTable
from ..utils import plots
from ..utils.plot_function_registry import registry
from ..xml_config import config_from_xml, create_xml

__all__ = ["create_config", "run", "show_plot"]
from .commands import create_config, run, show_plot


@click.group()
Expand All @@ -72,272 +62,9 @@ def cli():
# # ctx.obj["DEBUG"] = debug


@cli.command()
@click.option(
"-o", "--output", type=click.Path(exists=False), default=None, help="Output file."
)
@click.option(
"-p",
"--plot",
type=click.Choice(list(registry), case_sensitive=False),
multiple=True,
default=[],
help="Available plotting functions. Select multiple plots with multiple uses of -p",
)
@click.option(
"-P",
"--plotconfig",
type=click.Path(
exists=True,
dir_okay=False,
readable=True,
),
help="Read selected plotting functions and options from the specified INI file",
)
@click.option("--plotall", is_flag=True, help="Show all result plots.")
@click.option(
"-w",
"--write-stages",
is_flag=True,
help="Write intermediate values after each simulation stage.",
)
@click.option(
"-n",
"--no-result-file",
is_flag=True,
help="Do not save the results to an output file.",
)
@click.option(
"--monospectrum",
type=float,
default=None,
help="Mono Energetic Spectrum Log Energy.",
)
@click.option(
"--powerspectrum",
nargs=3,
type=click.Tuple([float, float, float]),
default=None,
help="Power Spectrum index, lower_bound, upper_bound.",
)
@click.argument(
"config_file",
default=None,
type=click.Path(exists=True, dir_okay=False, readable=True),
)
@click.argument("count", type=float, default=0.0)
def run(
config_file: str,
count: float,
monospectrum,
powerspectrum,
no_result_file: bool,
output: str,
plot: list,
plotconfig: str,
plotall: bool,
write_stages: bool,
) -> None:
"""Perform the full nuspacesim simulation.

Main Simulator for nuspacesim. Given a XML configuration file, and
optionally a count of simulated nutrinos, runs nutrino simulation.

\f

Parameters
----------
config_file: str
XML configuration file for particular simulation particular.
count : int, optional
Number of thrown trajectories. Optionally override value in config_file.
spectrum_type : str, optional
Type of
output: str, optional
Name of the output file holding the simulation results.
plot: list, optional
Plot the simulation results.
plotconfig: str, optional
INI file to select plots for each module, as well as to specifiy global plot settings.
plotall: bool, optional
Plot all the available the simulation results plots.
no_result_file: bool, optional
Disables writing results to output files.


Examples
--------
Command line usage of the run command may work with the following invocation.

`nuspacesim run sample_input_file.xml 1e5 8 -o my_sim_results.fits`
"""

# User Inputs
config = config_from_xml(config_file)

config.simulation.N = int(config.simulation.N if count == 0.0 else count)

if monospectrum is not None and powerspectrum is not None:
raise RuntimeError("Only one of --monospectrum or --powerspectrum may be used.")
if monospectrum is not None:
config.simulation.spectrum = MonoSpectrum(monospectrum)
if powerspectrum is not None:
config.simulation.spectrum = PowerSpectrum(*powerspectrum)

plot = (
list(registry)
if plotall
else read_plot_config(plotconfig)
if plotconfig
else plot
)
simulation = compute(
config,
verbose=True,
to_plot=plot,
output_file=output,
write_stages=write_stages,
)

if not no_result_file:
simulation.write(output, overwrite=True)


@cli.command()
@click.option(
"-n", "--numtrajs", type=float, default=100, help="number of trajectories."
)
@click.option(
"--monospectrum",
type=float,
default=None,
help="Mono Energetic Spectrum Log Energy.",
)
@click.option(
"--powerspectrum",
nargs=3,
type=click.Tuple([float, float, float]),
default=None,
help="Power Spectrum index, lower_bound, upper_bound.",
)
@click.argument("filename")
def create_config(filename: str, numtrajs: float, monospectrum, powerspectrum) -> None:
"""Generate a configuration file from the given parameters.

\f

Parameters
----------
filename: str
Name of output xml configuration file.
numtrajs: float, optional
Number of thrown trajectories. Optionally override value in config_file.

Examples
--------
Command line usage of the create_config command may work with the following invocation.

`nuspacesim create_config -n 1e5 sample_input_file.xml`
"""
if monospectrum is not None and powerspectrum is not None:
raise RuntimeError("Only one of --monospectrum or --powerspectrum may be used.")

spec = MonoSpectrum()

if monospectrum is not None:
spec = MonoSpectrum(monospectrum)
if powerspectrum is not None:
spec = PowerSpectrum(*powerspectrum)
# spec = FileSpectrum()

simulation = SimulationParameters(N=int(numtrajs), spectrum=spec)

create_xml(filename, NssConfig(simulation=simulation))


@cli.command()
@click.option(
"-p",
"--plot",
type=click.Choice(list(registry), case_sensitive=False),
multiple=True,
default=[],
help="Available plotting functions. Select multiple plots with multiple uses of -p",
)
@click.option(
"-P",
"--plotconfig",
type=click.Path(
exists=True,
dir_okay=False,
readable=True,
),
help="Read selected plotting functions and options from the specified INI file",
)
@click.option("--plotall", is_flag=True, help="Show all result plots.")
@click.argument(
"simulation_file",
default=None,
type=click.Path(exists=True, dir_okay=False, readable=True),
)
def show_plot(
simulation_file: str,
plot: list,
plotconfig: str,
plotall: bool,
) -> None:
"""Show predefined plots of results in simulation file.

\f

Parameters
----------
simulation_file: str
input ResultsTable fits file.
plot: list, optional
Plot the simulation results.
plotconfig: str, optional
INI file to select plots for each module, as well as to specifiy global plot settings.
plotall: bool, optional
Plot all the available the simulation results plots.


Examples
--------
Command line usage of the run command may work with the following invocation.

`nuspacesim show_plot my_sim_results.fits -p taus_overview`
"""

sim = ResultsTable.read(simulation_file)

plot = (
list(registry)
if plotall
else read_plot_config(plotconfig)
if plotconfig
else plot
)

simulation.geometry.region_geometry.show_plot(sim, plot)
simulation.taus.taus.show_plot(sim, plot)
simulation.eas_optical.eas.show_plot(sim, plot)
plots.show_plot(sim, plot)


def read_plot_config(filename):
plot_list = []
cfg = configparser.ConfigParser()
cfg.read(filename)
for sec in cfg.sections()[1:]:
for key in cfg[sec]:
try:
if cfg[sec].getboolean(key):
plot_list.append(key)
except Exception as e:
print(e, "Config file contains non-valid option")
return plot_list

cli.add_command(run.run)
cli.add_command(create_config.create_config)
cli.add_command(show_plot.show_plot)

if __name__ == "__main__":
cli()
Loading