Skip to content

Commit

Permalink
Make ctaplot optional
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Jul 11, 2023
1 parent 6791cbd commit d2b7ce7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
5 changes: 4 additions & 1 deletion lstchain/mc/plot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from ctaplot.plots import plot_sensitivity_magic_performance
from matplotlib.colors import LogNorm
from pyirf.spectral import CRAB_MAGIC_JHEAP2015
from astropy.visualization import quantity_support
Expand Down Expand Up @@ -304,6 +303,10 @@ def sensitivity_plot_comparison(energy, sensitivity, ax=None):
-------
fig_sens: `matplotlib.pyplot.figure` Figure containing sensitivity plot
"""
try:
from ctaplot.plots import plot_sensitivity_magic_performance
except ModuleNotFoundError:
raise ModuleNotFoundError("Please install ctaplot: pip install ctaplot")

Check warning on line 309 in lstchain/mc/plot_utils.py

View check run for this annotation

Codecov / codecov/patch

lstchain/mc/plot_utils.py#L308-L309

Added lines #L308 - L309 were not covered by tests

# Final sensitivity plot
ax = plt.gca() if ax is None else ax
Expand Down
18 changes: 12 additions & 6 deletions lstchain/scripts/benchmarks/charge_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
import sys
from pathlib import Path

import ctaplot
import matplotlib.pyplot as plt
import tables
from astropy.table import Table
from ctaplot.plots.calib import (
plot_charge_resolution,
plot_photoelectron_true_reco,
plot_pixels_pe_spectrum,
)
from matplotlib.backends.backend_pdf import PdfPages

from lstchain.io.config import (
Expand Down Expand Up @@ -49,6 +43,18 @@


def main():
try:
import ctaplot
except ModuleNotFoundError:
print("ctaplot is needed for this script, please install using `pip install ctaplot`", file=sys.stderr)
sys.exit(1)

from ctaplot.plots.calib import (
plot_charge_resolution,
plot_photoelectron_true_reco,
plot_pixels_pe_spectrum,
)

ctaplot.set_style()

output_dir = args.output_dir.absolute()
Expand Down
14 changes: 10 additions & 4 deletions lstchain/scripts/lstchain_mc_sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
--o /output/path
"""

import sys
import argparse
import os
import warnings

import astropy.units as u
import ctaplot
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Expand All @@ -37,8 +36,6 @@
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=RuntimeWarning)

ctaplot.set_style()

parser = argparse.ArgumentParser(description="Compute MC sensitivity curve.")

parser.add_argument('--input-file-gamma-dl2', '--gdl2', type=str,
Expand All @@ -57,6 +54,15 @@

def main():
args = parser.parse_args()

try:
import ctaplot
except ModuleNotFoundError:
print("ctaplot is needed for this script, please install using `pip install ctaplot`", file=sys.stderr)
sys.exit(1)

Check warning on line 62 in lstchain/scripts/lstchain_mc_sensitivity.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_mc_sensitivity.py#L58-L62

Added lines #L58 - L62 were not covered by tests

ctaplot.set_style()

Check warning on line 64 in lstchain/scripts/lstchain_mc_sensitivity.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_mc_sensitivity.py#L64

Added line #L64 was not covered by tests


ntelescopes_gamma = 1
n_bins_energy = 20 # Number of energy bins
Expand Down
24 changes: 22 additions & 2 deletions lstchain/visualization/plot_dl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os

import astropy.units as u
import ctaplot
import joblib
import matplotlib
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -176,8 +175,12 @@ def energy_results(dl2_data, points_outfile=None, plot_outfile=None):
-------
fig, axes: `matplotlib.pyplot.figure`, `matplotlib.pyplot.axes`
"""
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
try:
import ctaplot
except ModuleNotFoundError:
raise ModuleNotFoundError("This function needs ctaplot. Please install ctaplot: pip install ctaplot")

Check warning on line 181 in lstchain/visualization/plot_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/visualization/plot_dl2.py#L180-L181

Added lines #L180 - L181 were not covered by tests

fig, axes = plt.subplots(2, 2, figsize=(12, 8))
ctaplot.plot_energy_resolution(dl2_data.mc_energy.values * u.TeV,
dl2_data.reco_energy.values * u.TeV,
ax=axes[0, 0], bias_correction=False)
Expand Down Expand Up @@ -562,6 +565,11 @@ def plot_roc_gamma(dl2_data, energy_bins=None, ax=None, **kwargs):
-------
ax: `matplotlib.pyplot.axis`
"""
try:
import ctaplot
except ModuleNotFoundError:
raise ModuleNotFoundError("This function needs ctaplot. Please install ctaplot: pip install ctaplot")

Check warning on line 571 in lstchain/visualization/plot_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/visualization/plot_dl2.py#L568-L571

Added lines #L568 - L571 were not covered by tests

if energy_bins is None:
ax = ctaplot.plot_roc_curve_gammaness(dl2_data.mc_type, dl2_data.gammaness,
ax=ax,
Expand Down Expand Up @@ -595,6 +603,10 @@ def plot_energy_resolution(dl2_data, ax=None, bias_correction=False, cta_req_nor
-------
ax: `matplotlib.pyplot.axes`
"""
try:
import ctaplot
except ModuleNotFoundError:
raise ModuleNotFoundError("This function needs ctaplot. Please install ctaplot: pip install ctaplot")

Check warning on line 609 in lstchain/visualization/plot_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/visualization/plot_dl2.py#L606-L609

Added lines #L606 - L609 were not covered by tests

ax = ctaplot.plot_energy_resolution(dl2_data.mc_energy.values * u.TeV,
dl2_data.reco_energy.values * u.TeV,
Expand Down Expand Up @@ -628,6 +640,10 @@ def plot_angular_resolution(dl2_data, ax=None, bias_correction=False, cta_req_no
-------
ax: `matplotlib.pyplot.axes`
"""
try:
import ctaplot
except ModuleNotFoundError:
raise ModuleNotFoundError("This function needs ctaplot. Please install ctaplot: pip install ctaplot")

Check warning on line 646 in lstchain/visualization/plot_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/visualization/plot_dl2.py#L643-L646

Added lines #L643 - L646 were not covered by tests

ax = ctaplot.plot_angular_resolution_per_energy(dl2_data.mc_alt.values * u.rad,
dl2_data.reco_alt.values * u.rad,
Expand Down Expand Up @@ -660,6 +676,10 @@ def direction_results(dl2_data, points_outfile=None, plot_outfile=None):
-------
fig, axes: `matplotlib.pyplot.figure`, `matplotlib.pyplot.axes`
"""
try:
import ctaplot
except ModuleNotFoundError:
raise ModuleNotFoundError("This function needs ctaplot. Please install ctaplot: pip install ctaplot")

Check warning on line 682 in lstchain/visualization/plot_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/visualization/plot_dl2.py#L681-L682

Added lines #L681 - L682 were not covered by tests

fig, axes = plt.subplots(2, 2, figsize=(15, 12))

Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def find_scripts(script_dir, prefix):
'bokeh~=2.0',
'ctapipe~=0.19.2',
'ctapipe_io_lst~=0.22.0',
'ctaplot~=0.6.2',
'eventio>=1.9.1,<2.0.0a0', # at least 1.1.1, but not 2
'gammapy~=1.1',
'h5py',
Expand All @@ -66,7 +65,7 @@ def find_scripts(script_dir, prefix):
'jinja2~=3.0.2', # pinned for bokeh 1.0 compatibility
],
extras_require={
"all": tests_require + docs_require,
"all": tests_require + docs_require + ["ctaplot~=0.6.2"],
"tests": tests_require,
"docs": docs_require,
},
Expand Down

0 comments on commit d2b7ce7

Please sign in to comment.