Skip to content

Commit

Permalink
Merge pull request #682 from gammasim/separate_ctapipe_imports
Browse files Browse the repository at this point in the history
Improve startup time of applications
  • Loading branch information
GernotMaier authored Nov 17, 2023
2 parents d1234dc + 550c286 commit c779140
Show file tree
Hide file tree
Showing 40 changed files with 217 additions and 190 deletions.
14 changes: 0 additions & 14 deletions docs/source/io_handler.rst

This file was deleted.

22 changes: 22 additions & 0 deletions docs/source/io_operations.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.. _IO:

I/O
---

This module include I/O related functionality.

.. _iomodule:

io_handler
**********

.. automodule:: io_operations.io_handler
:members:

.. _iohdf5:

hdf5_handler
************

.. automodule:: io_operations.hdf5_handler
:members:
2 changes: 1 addition & 1 deletion docs/source/library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ simtools is composed of the following modules and module groups:
corsika_simtel
data_model
db_handler
io_handler
io_operations
job_execution
layout_array
mc_model
Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/compare_cumulative_psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
import yaml

import simtools.utils.general as gen
from simtools import io_handler
from simtools.configuration import configurator
from simtools.io_operations import io_handler
from simtools.model.telescope_model import TelescopeModel
from simtools.ray_tracing import RayTracing
from simtools.visualization import visualize
Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/generate_corsika_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@
import numpy as np

import simtools.utils.general as gen
from simtools import io_handler
from simtools.configuration import configurator
from simtools.corsika import corsika_histograms_visualize
from simtools.corsika.corsika_histograms import CorsikaHistograms
from simtools.io_operations import io_handler

logger = logging.getLogger()

Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/generate_simtel_array_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
from matplotlib.backends.backend_pdf import PdfPages

import simtools.utils.general as gen
from simtools import io_handler
from simtools.configuration import configurator
from simtools.io_operations import io_handler
from simtools.simtel.simtel_histograms import SimtelHistograms


Expand Down
3 changes: 2 additions & 1 deletion simtools/applications/make_regular_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
import astropy.units as u

import simtools.utils.general as gen
from simtools import db_handler, io_handler
from simtools import db_handler
from simtools.configuration import configurator
from simtools.io_operations import io_handler
from simtools.layout.layout_array import LayoutArray


Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/plot_layout_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
from astropy import units as u

import simtools.utils.general as gen
from simtools import io_handler
from simtools.configuration import configurator
from simtools.io_operations import io_handler
from simtools.layout.layout_array import LayoutArray
from simtools.visualization.visualize import plot_array

Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/sim_showers_for_trigger_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@

import astropy.units as u

from simtools import io_handler
from simtools.configuration import configurator
from simtools.io_operations import io_handler
from simtools.simulator import Simulator
from simtools.utils import general as gen

Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/tune_psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
from matplotlib.backends.backend_pdf import PdfPages

import simtools.utils.general as gen
from simtools import io_handler
from simtools.configuration import configurator
from simtools.io_operations import io_handler
from simtools.model.model_utils import split_simtel_parameter
from simtools.model.telescope_model import TelescopeModel
from simtools.ray_tracing import RayTracing
Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/validate_camera_efficiency.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
from pathlib import Path

import simtools.utils.general as gen
from simtools import io_handler
from simtools.camera_efficiency import CameraEfficiency
from simtools.configuration import configurator
from simtools.io_operations import io_handler
from simtools.model.telescope_model import TelescopeModel


Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/validate_camera_fov.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
from pathlib import Path

import simtools.utils.general as gen
from simtools import io_handler
from simtools.configuration import configurator
from simtools.io_operations import io_handler
from simtools.model.telescope_model import TelescopeModel


Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/validate_optics.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
from matplotlib.backends.backend_pdf import PdfPages

import simtools.utils.general as gen
from simtools import io_handler
from simtools.configuration import configurator
from simtools.io_operations import io_handler
from simtools.model.telescope_model import TelescopeModel
from simtools.ray_tracing import RayTracing

Expand Down
2 changes: 1 addition & 1 deletion simtools/camera_efficiency.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from astropy.table import Table

import simtools.utils.general as gen
from simtools import io_handler
from simtools.io_operations import io_handler
from simtools.model.telescope_model import TelescopeModel
from simtools.simtel.simtel_runner_camera_efficiency import SimtelRunnerCameraEfficiency
from simtools.utils import names
Expand Down
2 changes: 1 addition & 1 deletion simtools/configuration/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from dotenv import load_dotenv

import simtools.configuration.commandline_parser as argparser
from simtools import io_handler
from simtools.io_operations import io_handler

__all__ = [
"Configurator",
Expand Down
2 changes: 1 addition & 1 deletion simtools/corsika/corsika_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from astropy.io.misc import yaml

import simtools.utils.general as gen
from simtools import io_handler
from simtools.io_operations import io_handler
from simtools.layout.layout_array import LayoutArray
from simtools.utils import names
from simtools.utils.general import collect_data_from_yaml_or_dict
Expand Down
5 changes: 3 additions & 2 deletions simtools/corsika/corsika_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
from ctapipe.io import write_table
from eventio import IACTFile

from simtools import io_handler, version
from simtools import version
from simtools.io_operations import io_handler
from simtools.io_operations.hdf5_handler import fill_hdf5_table
from simtools.utils.general import (
collect_data_from_yaml_or_dict,
convert_2D_to_radial_distr,
fill_hdf5_table,
rotate,
save_dict_to_file,
)
Expand Down
2 changes: 1 addition & 1 deletion simtools/corsika/corsika_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from copy import copy
from pathlib import Path

from simtools import io_handler
from simtools.corsika.corsika_config import (
CorsikaConfig,
MissingRequiredInputInCorsikaConfigData,
)
from simtools.io_operations import io_handler
from simtools.utils import names
from simtools.utils.general import collect_data_from_yaml_or_dict

Expand Down
2 changes: 1 addition & 1 deletion simtools/data_model/metadata_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import simtools.utils.general as gen
import simtools.version
from simtools import io_handler
from simtools.data_model import metadata_model, validate_schema
from simtools.io_operations import io_handler
from simtools.utils import names

__all__ = ["MetadataCollector"]
Expand Down
2 changes: 1 addition & 1 deletion simtools/data_model/model_data_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import yaml

import simtools.utils.general as gen
from simtools import io_handler
from simtools.io_operations import io_handler

__all__ = ["ModelDataWriter"]

Expand Down
2 changes: 1 addition & 1 deletion simtools/db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pymongo.errors import BulkWriteError

import simtools.utils.general as gen
from simtools import io_handler
from simtools.io_operations import io_handler
from simtools.model.model_utils import get_telescope_class
from simtools.utils import names

Expand Down
Empty file.
112 changes: 112 additions & 0 deletions simtools/io_operations/hdf5_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import logging
from pathlib import PosixPath

import astropy.units as u
import tables
from astropy.table import Table
from ctapipe.io import read_table

from simtools.utils.names import sanitize_name

__all__ = [
"fill_hdf5_table",
"read_hdf5",
]

_logger = logging.getLogger(__name__)


def fill_hdf5_table(hist, x_bin_edges, y_bin_edges, x_label, y_label, meta_data):
"""
Create and fill an hdf5 table with the histogram information.
It works for both 1D and 2D distributions.
Parameters
----------
hist: numpy.ndarray
The counts of the histograms.
x_bin_edges: numpy.array
The x bin edges of the histograms.
y_bin_edges: numpy.array
The y bin edges of the histograms.
Use None for 1D histograms.
x_label: str
X bin edges label.
y_label: str
Y bin edges label.
Use None for 1D histograms.
meta_data: dict
Dictionary with the histogram metadata.
"""

# Complement metadata
if x_label is not None:
meta_data["x bin edges"] = sanitize_name(x_label)
meta_data["x bin edges unit"] = (
x_bin_edges.unit if isinstance(x_bin_edges, u.Quantity) else u.dimensionless_unscaled
)

if y_bin_edges is not None:
if y_label is not None:
meta_data["y bin edges"] = sanitize_name(y_label)
names = [
f"{meta_data['y bin edges'].split('__')[0]}_{i}"
for i in range(len(y_bin_edges[:-1]))
]
else:
names = [
f"{meta_data['Title'].split('__')[0]}_{i}" for i in range(len(y_bin_edges[:-1]))
]
meta_data["y bin edges unit"] = (
y_bin_edges.unit if isinstance(y_bin_edges, u.Quantity) else u.dimensionless_unscaled
)

table = Table(
[hist[i, :] for i in range(len(y_bin_edges[:-1]))],
names=names,
meta=meta_data,
)

else:
if x_label is not None:
meta_data["x bin edges"] = sanitize_name(x_label)
names = meta_data["x bin edges"]
else:
names = meta_data["Title"]
table = Table(
[
x_bin_edges[:-1],
hist,
],
names=(names, sanitize_name("Values")),
meta=meta_data,
)
return table


def read_hdf5(hdf5_file_name):
"""
Read a hdf5 output file.
Parameters
----------
hdf5_file_name: str or Path
Name or Path of the hdf5 file to read from.
Returns
-------
list
The list with the astropy.Table instances for the various 1D and 2D histograms saved
in the hdf5 file.
"""
if isinstance(hdf5_file_name, PosixPath):
hdf5_file_name = hdf5_file_name.absolute().as_posix()

tables_list = []

with tables.open_file(hdf5_file_name, mode="r") as file:
for node in file.walk_nodes("/", "Table"):
table_path = node._v_pathname
table = read_table(hdf5_file_name, table_path)
tables_list.append(table)
return tables_list
File renamed without changes.
3 changes: 2 additions & 1 deletion simtools/layout/layout_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import numpy as np
from astropy.table import QTable

from simtools import db_handler, io_handler
from simtools import db_handler
from simtools.io_operations import io_handler
from simtools.layout.geo_coordinates import GeoCoordinates
from simtools.layout.telescope_position import TelescopePosition
from simtools.utils import names
Expand Down
3 changes: 2 additions & 1 deletion simtools/model/array_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
from copy import copy

from simtools import db_handler, io_handler
from simtools import db_handler
from simtools.io_operations import io_handler
from simtools.layout.layout_array import LayoutArray
from simtools.model.telescope_model import TelescopeModel
from simtools.simtel.simtel_config_writer import SimtelConfigWriter
Expand Down
3 changes: 2 additions & 1 deletion simtools/model/telescope_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from astropy.table import Table

import simtools.utils.general as gen
from simtools import db_handler, io_handler
from simtools import db_handler
from simtools.io_operations import io_handler
from simtools.model.camera import Camera
from simtools.model.mirrors import Mirrors
from simtools.simtel.simtel_config_writer import SimtelConfigWriter
Expand Down
2 changes: 1 addition & 1 deletion simtools/ray_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from astropy.table import QTable

import simtools.utils.general as gen
from simtools import io_handler
from simtools.io_operations import io_handler
from simtools.model.model_utils import compute_telescope_transmission
from simtools.model.telescope_model import TelescopeModel
from simtools.psf_analysis import PSFImage
Expand Down
2 changes: 1 addition & 1 deletion simtools/simtel/simtel_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from matplotlib.backends.backend_pdf import PdfPages

from simtools import version
from simtools.utils.general import fill_hdf5_table
from simtools.io_operations.hdf5_handler import fill_hdf5_table
from simtools.utils.names import sanitize_name

__all__ = ["BadHistogramFormat", "SimtelHistograms"]
Expand Down
Loading

0 comments on commit c779140

Please sign in to comment.