Skip to content
Merged
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
52 changes: 49 additions & 3 deletions dl1_data_handler/image_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from collections import Counter, namedtuple

from ctapipe.instrument.camera import PixelShape
from ctapipe.core import TelescopeComponent
from ctapipe.core.traits import Bool, Int
from ctapipe.core import Component
from ctapipe.core.traits import Bool, Int, Float

__all__ = [
"ImageMapper",
Expand All @@ -23,7 +23,7 @@
"SquareMapper",
]

class ImageMapper(TelescopeComponent):
class ImageMapper(Component):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would need to remove the passing of the subarray in line reader-L386 and reader-L393.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are somo FIXMEs and TODOs for setting up the configuration of the ImageMapper. I can fix this in a follow-up PR. Probably the ImageMapper trait in the reader should just be a ComponentName.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will let you handle any more todos :)

"""
Base component for mapping raw 1D vectors into 2D mapped images.

Expand Down Expand Up @@ -84,8 +84,24 @@ def __init__(
parent : ctapipe.core.Component or ctapipe.core.Tool
Parent of this component in the configuration hierarchy,
this is mutually exclusive with passing ``config``
**kwargs
Additional keyword arguments for traitlets. Non-traitlet kwargs
(like 'subarray') are filtered out for compatibility.
"""

# Filter out non-traitlet kwargs before passing to Component
# This allows compatibility with ctapipe's reader which may pass extra kwargs
component_kwargs = {
key: value for key, value in kwargs.items()
if self.class_own_traits().get(key) is not None
}

super().__init__(
config=config,
parent=parent,
**component_kwargs,
)

# Camera types
self.geometry = geometry
self.camera_type = self.geometry.name
Expand Down Expand Up @@ -1166,6 +1182,16 @@ class RebinMapper(ImageMapper):
),
).tag(config=True)

max_memory_gb = Float(
default_value=10,
allow_none=True,
help=(
"Maximum memory in GB that RebinMapper is allowed to allocate. "
"Set to None to disable memory checks. Default is 10 GB. "
"Note: RebinMapper uses approximately (image_shape * 10)^2 * image_shape^2 * 4 bytes."
),
).tag(config=True)

def __init__(
self,
geometry,
Expand Down Expand Up @@ -1205,6 +1231,26 @@ def __init__(
self.image_shape = self.interpolation_image_shape
self.internal_shape = self.image_shape + self.internal_pad * 2
self.rebinning_mult_factor = 10

# Validate memory requirements before proceeding (if max_memory_gb is set)
if self.max_memory_gb is not None:
# RebinMapper uses a fine grid (internal_shape * rebinning_mult_factor)^2
# and creates a mapping matrix of shape (fine_grid_size, internal_shape, internal_shape)
fine_grid_size = (self.internal_shape * self.rebinning_mult_factor) ** 2
estimated_memory_gb = (
fine_grid_size * self.internal_shape * self.internal_shape * 4
) / (1024**3) # 4 bytes per float32

if estimated_memory_gb > self.max_memory_gb:
raise ValueError(
f"RebinMapper with image_shape={self.image_shape} would require "
f"approximately {estimated_memory_gb:.1f} GB of memory, which exceeds "
f"the limit of {self.max_memory_gb:.1f} GB. "
f"To allow this allocation, set max_memory_gb to a higher value or None. "
f"Alternatively, consider using a smaller interpolation_image_shape (recommended < 60) "
f"or use BilinearMapper or BicubicMapper instead, which are more memory-efficient."
)

# Creating the hexagonal and the output grid for the conversion methods.
input_grid, output_grid = super()._get_grids_for_interpolation()
# Calculate the mapping table
Expand Down
Loading