Skip to content

Commit

Permalink
Use weakref.proxy where parent=self.
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Nickel <lukas.nickel@tu-dortmund.de>
Co-authored-by: Rune Dominik <rune.dominik@tu-dortmund.de>
  • Loading branch information
3 people committed Jan 18, 2023
1 parent 8a7a367 commit 2ddb4fc
Show file tree
Hide file tree
Showing 31 changed files with 160 additions and 99 deletions.
10 changes: 6 additions & 4 deletions ctapipe/calib/camera/calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Definition of the `CameraCalibrator` class, providing all steps needed to apply
calibration and image extraction, as well as supporting algorithms.
"""

import warnings
import weakref

import astropy.units as u
import numpy as np
Expand Down Expand Up @@ -131,7 +131,7 @@ def __init__(
if image_extractor is None:
for (_, _, name) in self.image_extractor_type:
self.image_extractors[name] = ImageExtractor.from_name(
name, subarray=self.subarray, parent=self
name, subarray=self.subarray, parent=weakref.proxy(self)
)
else:
name = image_extractor.__class__.__name__
Expand All @@ -140,7 +140,9 @@ def __init__(

if data_volume_reducer is None:
self.data_volume_reducer = DataVolumeReducer.from_name(
self.data_volume_reducer_type, subarray=self.subarray, parent=self
self.data_volume_reducer_type,
subarray=self.subarray,
parent=weakref.proxy(self),
)
else:
self.data_volume_reducer = data_volume_reducer
Expand All @@ -150,7 +152,7 @@ def __init__(
self.invalid_pixel_handler = InvalidPixelHandler.from_name(
self.invalid_pixel_handler_type,
subarray=self.subarray,
parent=self,
parent=weakref.proxy(self),
)

def _check_r1_empty(self, waveforms):
Expand Down
4 changes: 2 additions & 2 deletions ctapipe/calib/camera/flatfield.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Factory for the estimation of the flat field coefficients
"""

import weakref
from abc import abstractmethod

import numpy as np
Expand Down Expand Up @@ -95,7 +95,7 @@ def __init__(self, subarray, **kwargs):
super().__init__(**kwargs)
# load the waveform charge extractor
self.extractor = ImageExtractor.from_name(
self.charge_product, parent=self, subarray=subarray
self.charge_product, parent=weakref.proxy(self), subarray=subarray
)

self.log.info(f"extractor {self.extractor}")
Expand Down
4 changes: 2 additions & 2 deletions ctapipe/calib/camera/pedestals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Factory for the estimation of the flat field coefficients
"""

import weakref
from abc import abstractmethod

import numpy as np
Expand Down Expand Up @@ -122,7 +122,7 @@ def __init__(self, subarray, **kwargs):

# load the waveform charge extractor
self.extractor = ImageExtractor.from_name(
self.charge_product, parent=self, subarray=subarray
self.charge_product, parent=weakref.proxy(self), subarray=subarray
)
self.log.info(f"extractor {self.extractor}")

Expand Down
4 changes: 2 additions & 2 deletions ctapipe/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(self, config=None, parent=None, **kwargs):
parent: Tool or Component
If a Component is created by another Component or Tool,
you need to pass the creating Component as parent, e.g.
`parent=self`. This makes sure the config is correctly
`parent=weakref.proxy(self)`. This makes sure the config is correctly
handed down to the child components.
Do not pass config in this case.
kwargs
Expand All @@ -136,7 +136,7 @@ def __init__(self, config=None, parent=None, **kwargs):
if parent is not None and config is not None:
raise ValueError(
"Only one of `config` or `parent` allowed"
" If you create a Component as part of another, give `parent=self`"
" If you create a Component as part of another, give `parent=weakref.proxy(self)`"
" and not `config`"
)

Expand Down
13 changes: 7 additions & 6 deletions ctapipe/core/tests/test_component.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import warnings
import weakref
from abc import ABC, abstractmethod

import pytest
Expand Down Expand Up @@ -48,12 +49,12 @@ class Bottom(Component):
class Middle(Component):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.bottom = Bottom(parent=self)
self.bottom = Bottom(parent=weakref.proxy(self))

class Top(Component):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.middle = Middle(parent=self)
self.middle = Middle(parent=weakref.proxy(self))

# test with root present
c = Config({"Top": {"Middle": {"Bottom": {"val": 5}}}})
Expand Down Expand Up @@ -83,7 +84,7 @@ class ExampleComponent(Component):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sub = SubComponent(parent=self)
self.sub = SubComponent(parent=weakref.proxy(self))


class ExampleSubclass1(ExampleComponent):
Expand Down Expand Up @@ -409,7 +410,7 @@ class MyComponent(Component):

def __init__(self, config=None, parent=None):
super().__init__(config=config, parent=parent)
self.sub = SubComponent(parent=self)
self.sub = SubComponent(parent=weakref.proxy(self))

comp = MyComponent()
assert comp.get_current_config() == {
Expand All @@ -436,13 +437,13 @@ def __init__(self, **kwargs):
class Bar(Component):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.foo = Foo(parent=self)
self.foo = Foo(parent=weakref.proxy(self))

class Baz(Tool):
name = "baz"

def setup(self):
self.bar = Bar(parent=self)
self.bar = Bar(parent=weakref.proxy(self))

def start(self):
pass
Expand Down
5 changes: 4 additions & 1 deletion ctapipe/core/tests/test_telescope_component.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tempfile
import weakref
from unittest import mock

import pytest
Expand Down Expand Up @@ -303,7 +304,9 @@ class TelescopeTool(Tool):
classes = tool_classes

def setup(self):
self.comp = SomeComponent(subarray=mock_subarray, parent=self)
self.comp = SomeComponent(
subarray=mock_subarray, parent=weakref.proxy(self)
)

tool = TelescopeTool()
assert run_tool(tool) == 0
Expand Down
9 changes: 5 additions & 4 deletions ctapipe/core/tests/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import tempfile
import weakref
from pathlib import Path

import pytest
Expand Down Expand Up @@ -113,7 +114,7 @@ class MyTool2(Tool):
classes = [MyComponent]

def setup(self):
self.comp = MyComponent(parent=self)
self.comp = MyComponent(parent=weakref.proxy(self))

def start(self):
pass
Expand Down Expand Up @@ -154,14 +155,14 @@ class MyComponent(Component):

def __init__(self, config=None, parent=None):
super().__init__(config=config, parent=parent)
self.sub = SubComponent(parent=self)
self.sub = SubComponent(parent=weakref.proxy(self))

class MyTool(Tool):
description = "test"
userparam = Float(5.0, help="parameter").tag(config=True)

def setup(self):
self.my_comp = MyComponent(parent=self)
self.my_comp = MyComponent(parent=weakref.proxy(self))

config = Config()
config.MyTool.userparam = 2.0
Expand Down Expand Up @@ -217,7 +218,7 @@ class MyTool(Tool):
aliases = Dict({"component_param": "SubComponent.component_param"})

def setup(self):
self.sub = SubComponent(parent=self)
self.sub = SubComponent(parent=weakref.proxy(self))

config = Config(
{"MyTool": {"userparam": 12.0}, "SubComponent": {"component_param": 15.0}}
Expand Down
6 changes: 4 additions & 2 deletions ctapipe/core/tests/test_traits.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pathlib
import tempfile
import weakref
from abc import ABCMeta, abstractmethod
from unittest import mock

Expand Down Expand Up @@ -303,7 +304,7 @@ class MyComponent(Component):

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.base = Base.from_name(self.base_name, parent=self)
self.base = Base.from_name(self.base_name, parent=weakref.proxy(self))
self.base.stuff()

class MyListComponent(Component):
Expand All @@ -319,7 +320,8 @@ def __init__(self, **kwargs):

if self.base_names is not None:
self.bases = [
Base.from_name(name, parent=self) for name in self.base_names
Base.from_name(name, parent=weakref.proxy(self))
for name in self.base_names
]

for base in self.bases:
Expand Down
10 changes: 5 additions & 5 deletions ctapipe/core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ class MyTool(Tool):
allow_none=False).tag(config=True)
def setup(self):
self.comp = MyComponent(self, parent=self)
self.comp2 = SecondaryMyComponent(self, parent=self)
self.comp = MyComponent(self, parent=weakref.proxy(self))
self.comp2 = SecondaryMyComponent(self, parent=weakref.proxy(self))
# correct use of component that is a context manager
# using it like this makes sure __exit__ will be called
# at the end of Tool.run, even in case of exceptions
self.comp3 = self.enter_context(MyComponent(parent=self))
self.comp3 = self.enter_context(MyComponent(parent=weakref.proxy(self)))
self.advanced = AdvancedComponent(parent=self)
self.advanced = AdvancedComponent(parent=weakref.proxy(self))
def start(self):
self.log.info("Performing {} iterations..."\
Expand Down Expand Up @@ -318,7 +318,7 @@ def add_component(self, component_instance):
--------
.. code-block:: python3
self.mycomp = self.add_component(MyComponent(parent=self))
self.mycomp = self.add_component(MyComponent(parent=weakref.proxy(self)))
"""
self._registered_components.append(component_instance)
Expand Down
3 changes: 2 additions & 1 deletion ctapipe/image/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
]


import weakref
from abc import abstractmethod
from functools import lru_cache
from typing import Tuple
Expand Down Expand Up @@ -891,7 +892,7 @@ def __init__(self, subarray, **kwargs):
self.invalid_pixel_handler = InvalidPixelHandler.from_name(
self.invalid_pixel_handler_type,
self.subarray,
parent=self,
parent=weakref.proxy(self),
)

@lru_cache(maxsize=4096)
Expand Down
7 changes: 4 additions & 3 deletions ctapipe/image/image_processor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
High level image processing (ImageProcessor Component)
"""
import weakref
from copy import deepcopy

import numpy as np
Expand Down Expand Up @@ -99,11 +100,11 @@ def __init__(
super().__init__(subarray=subarray, config=config, parent=parent, **kwargs)
self.subarray = subarray
self.clean = ImageCleaner.from_name(
self.image_cleaner_type, subarray=subarray, parent=self
self.image_cleaner_type, subarray=subarray, parent=weakref.proxy(self)
)
self.modify = ImageModifier(subarray=subarray, parent=self)
self.modify = ImageModifier(subarray=subarray, parent=weakref.proxy(self))

self.check_image = ImageQualityQuery(parent=self)
self.check_image = ImageQualityQuery(parent=weakref.proxy(self))

self.default_image_container = DEFAULT_IMAGE_PARAMETERS_CAMFRAME
if self.use_telescope_frame:
Expand Down
7 changes: 5 additions & 2 deletions ctapipe/image/reducer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Algorithms for the data volume reduction.
"""
import weakref
from abc import abstractmethod

import numpy as np
Expand Down Expand Up @@ -162,15 +163,17 @@ def __init__(
super().__init__(config=config, parent=parent, subarray=subarray, **kwargs)

if cleaner is None:
self.cleaner = TailcutsImageCleaner(parent=self, subarray=self.subarray)
self.cleaner = TailcutsImageCleaner(
parent=weakref.proxy(self), subarray=self.subarray
)
else:
self.cleaner = cleaner

self.image_extractors = {}
if image_extractor is None:
for (_, _, name) in self.image_extractor_type:
self.image_extractors[name] = ImageExtractor.from_name(
name, subarray=self.subarray, parent=self
name, subarray=self.subarray, parent=weakref.proxy(self)
)
else:
name = image_extractor.__class__.__name__
Expand Down
10 changes: 5 additions & 5 deletions ctapipe/io/datawriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Class to write DL1 (a,b) and DL2 (a) data from an event stream
"""


import pathlib
import weakref
from collections import defaultdict
from typing import List

Expand Down Expand Up @@ -149,7 +149,7 @@ class DataWriter(Component):
.. code-block:: python
with DataWriter(parent=self) as write_data:
with DataWriter(parent=weakref.proxy(self)) as write_data:
for event in source:
calibrate(event)
process_images(event)
Expand Down Expand Up @@ -251,8 +251,8 @@ def __init__(self, event_source: EventSource, config=None, parent=None, **kwargs
super().__init__(config=config, parent=parent, **kwargs)

self.event_source = event_source
self.contact_info = meta.Contact(parent=self)
self.instrument_info = meta.Instrument(parent=self)
self.contact_info = meta.Contact(parent=weakref.proxy(self))
self.instrument_info = meta.Instrument(parent=weakref.proxy(self))

self._at_least_one_event = False
self._is_simulation = event_source.is_simulation
Expand Down Expand Up @@ -411,7 +411,7 @@ def _setup_writer(self):
"""
writer = HDF5TableWriter(
str(self.output_path),
parent=self,
parent=weakref.proxy(self),
mode="a",
add_prefix=True,
filters=self._hdf5_filters,
Expand Down
2 changes: 1 addition & 1 deletion ctapipe/io/eventsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class EventSource(Component):
An ``EventSource`` can also be created through the configuration system,
by passing ``config`` or ``parent`` as appropriate.
E.g. if using ``EventSource`` inside of a ``Tool``, you would do:
>>> self.source = EventSource(parent=self) # doctest: +SKIP
>>> self.source = EventSource(parent=weakref.proxy(self)) # doctest: +SKIP
To loop through the events in a file:
>>> source = EventSource(input_url="dataset://gamma_prod5.simtel.zst", max_events=2)
Expand Down
3 changes: 2 additions & 1 deletion ctapipe/io/simteleventsource.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import enum
import warnings
import weakref
from contextlib import nullcontext
from enum import Enum, auto, unique
from gzip import GzipFile
Expand Down Expand Up @@ -520,7 +521,7 @@ def __init__(self, input_url=Undefined, config=None, parent=None, **kwargs):
self.start_pos = self.file_.tell()

self.gain_selector = GainSelector.from_name(
self.gain_selector_type, parent=self
self.gain_selector_type, parent=weakref.proxy(self)
)

self._atmosphere_density_profile = read_atmosphere_profile_from_simtel(
Expand Down
Loading

0 comments on commit 2ddb4fc

Please sign in to comment.