Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use n_ prefix consistently, (rename all num_ to n_), fixes #986 #2046

Merged
merged 1 commit into from
Jul 28, 2022
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
25 changes: 11 additions & 14 deletions ctapipe/calib/camera/flatfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def __init__(self, **kwargs):
self.log.info("Used events statistics : %d", self.sample_size)

# members to keep state in calculate_relative_gain()
self.num_events_seen = 0
self.n_events_seen = 0
self.time_start = None # trigger time of first event in sample
self.charge_medians = None # med. charge in camera per event in sample
self.charges = None # charge per event in sample
Expand Down Expand Up @@ -213,8 +213,8 @@ def calculate_relative_gain(self, event):
container = event.mon.tel[self.tel_id].flatfield

# re-initialize counter
if self.num_events_seen == self.sample_size:
self.num_events_seen = 0
if self.n_events_seen == self.sample_size:
self.n_events_seen = 0

# real data
trigger_time = event.trigger.time
Expand All @@ -231,7 +231,7 @@ def calculate_relative_gain(self, event):
else: # patches for MC data
pixel_mask = np.zeros(waveform.shape[1], dtype=bool)

if self.num_events_seen == 0:
if self.n_events_seen == 0:
self.time_start = trigger_time
self.setup_sample_buffers(waveform, self.sample_size)

Expand All @@ -247,10 +247,7 @@ def calculate_relative_gain(self, event):
sample_age = (trigger_time - self.time_start).to_value(u.s)

# check if to create a calibration event
if (
sample_age > self.sample_duration
or self.num_events_seen == self.sample_size
):
if sample_age > self.sample_duration or self.n_events_seen == self.sample_size:
relative_gain_results = self.calculate_relative_gain_results(
self.charge_medians, self.charges, self.sample_masked_pixels
)
Expand All @@ -262,7 +259,7 @@ def calculate_relative_gain(self, event):
)

result = {
"n_events": self.num_events_seen,
"n_events": self.n_events_seen,
**relative_gain_results,
**time_results,
}
Expand Down Expand Up @@ -296,11 +293,11 @@ def collect_sample(self, charge, pixel_mask, arrival_time):
good_charge = np.ma.array(charge, mask=pixel_mask)
charge_median = np.ma.median(good_charge, axis=1)

self.charges[self.num_events_seen] = charge
self.arrival_times[self.num_events_seen] = arrival_time
self.sample_masked_pixels[self.num_events_seen] = pixel_mask
self.charge_medians[self.num_events_seen] = charge_median
self.num_events_seen += 1
self.charges[self.n_events_seen] = charge
self.arrival_times[self.n_events_seen] = arrival_time
self.sample_masked_pixels[self.n_events_seen] = pixel_mask
self.charge_medians[self.n_events_seen] = charge_median
self.n_events_seen += 1

def calculate_time_results(
self, trace_time, masked_pixels_of_sample, time_start, trigger_time
Expand Down
23 changes: 10 additions & 13 deletions ctapipe/calib/camera/pedestals.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def __init__(self, **kwargs):
self.log.info("Used events statistics : %d", self.sample_size)

# members to keep state in calculate_relative_gain()
self.num_events_seen = 0
self.n_events_seen = 0
self.time_start = None # trigger time of first event in sample
self.charge_medians = None # med. charge in camera per event in sample
self.charges = None # charge per event in sample
Expand Down Expand Up @@ -242,8 +242,8 @@ def calculate_pedestals(self, event):
container = event.mon.tel[self.tel_id].pedestal

# re-initialize counter
if self.num_events_seen == self.sample_size:
self.num_events_seen = 0
if self.n_events_seen == self.sample_size:
self.n_events_seen = 0

# real data
trigger_time = event.trigger.time
Expand All @@ -252,7 +252,7 @@ def calculate_pedestals(self, event):
else: # patches for MC data
pixel_mask = np.zeros(waveform.shape[1], dtype=bool)

if self.num_events_seen == 0:
if self.n_events_seen == 0:
self.time_start = trigger_time
self.setup_sample_buffers(waveform, self.sample_size)

Expand All @@ -268,17 +268,14 @@ def calculate_pedestals(self, event):
sample_age = (trigger_time - self.time_start).to_value(u.s)

# check if to create a calibration event
if (
sample_age > self.sample_duration
or self.num_events_seen == self.sample_size
):
if sample_age > self.sample_duration or self.n_events_seen == self.sample_size:
pedestal_results = calculate_pedestal_results(
self, self.charges, self.sample_masked_pixels
)
time_results = calculate_time_results(self.time_start, trigger_time)

result = {
"n_events": self.num_events_seen,
"n_events": self.n_events_seen,
**pedestal_results,
**time_results,
}
Expand Down Expand Up @@ -308,10 +305,10 @@ def collect_sample(self, charge, pixel_mask):
good_charge = np.ma.array(charge, mask=pixel_mask)
charge_median = np.ma.median(good_charge, axis=1)

self.charges[self.num_events_seen] = charge
self.sample_masked_pixels[self.num_events_seen] = pixel_mask
self.charge_medians[self.num_events_seen] = charge_median
self.num_events_seen += 1
self.charges[self.n_events_seen] = charge
self.sample_masked_pixels[self.n_events_seen] = pixel_mask
self.charge_medians[self.n_events_seen] = charge_median
self.n_events_seen += 1


def calculate_time_results(time_start, trigger_time):
Expand Down
4 changes: 2 additions & 2 deletions ctapipe/calib/camera/tests/test_flatfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_flasherflatfieldcalculator(prod5_sst):
print(data.r1.tel[tel_id].waveform[0, 0, 20])

# First test: good event
while ff_calculator.num_events_seen < n_events:
while ff_calculator.n_events_seen < n_events:
if ff_calculator.calculate_relative_gain(data):
assert data.mon.tel[tel_id].flatfield

Expand All @@ -75,7 +75,7 @@ def test_flasherflatfieldcalculator(prod5_sst):
:, failing_pixels_id
] = True

while ff_calculator.num_events_seen < n_events:
while ff_calculator.n_events_seen < n_events:
if ff_calculator.calculate_relative_gain(data):

# working pixel have good gain
Expand Down
2 changes: 1 addition & 1 deletion ctapipe/calib/camera/tests/test_pedestals.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_pedestal_integrator(prod5_sst):
data.r1.tel[tel_id].waveform = np.full((2, n_pixels, 40), ped_level)
data.r1.tel[tel_id].selected_gain_channel = np.zeros(n_pixels, dtype=np.uint8)

while ped_calculator.num_events_seen < n_events:
while ped_calculator.n_events_seen < n_events:
if ped_calculator.calculate_pedestals(data):
assert data.mon.tel[tel_id].pedestal
assert np.mean(data.mon.tel[tel_id].pedestal.charge_median) == (
Expand Down
16 changes: 8 additions & 8 deletions ctapipe/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import enum
from functools import partial

import numpy as np
from astropy import units as u
from astropy.time import Time
from numpy import nan
import numpy as np

from .core import Container, Field, Map

Expand Down Expand Up @@ -254,11 +254,11 @@ class TimingParametersContainer(BaseTimingParametersContainer):
class MorphologyContainer(Container):
"""Parameters related to pixels surviving image cleaning"""

num_pixels = Field(-1, "Number of usable pixels")
num_islands = Field(-1, "Number of distinct islands in the image")
num_small_islands = Field(-1, "Number of <= 2 pixel islands")
num_medium_islands = Field(-1, "Number of 2-50 pixel islands")
num_large_islands = Field(-1, "Number of > 50 pixel islands")
n_pixels = Field(-1, "Number of usable pixels")
n_islands = Field(-1, "Number of distinct islands in the image")
n_small_islands = Field(-1, "Number of <= 2 pixel islands")
n_medium_islands = Field(-1, "Number of 2-50 pixel islands")
n_large_islands = Field(-1, "Number of > 50 pixel islands")


class StatisticsContainer(Container):
Expand Down Expand Up @@ -601,7 +601,7 @@ class SimulationConfigContainer(Container):
nan, description="Time when detector simulation started"
)
detector_prog_id = Field(nan, description="simtelarray=1")
num_showers = Field(nan, description="Number of showers simulated")
n_showers = Field(nan, description="Number of showers simulated")
shower_reuse = Field(nan, description="Numbers of uses of each shower")
max_alt = Field(nan * u.rad, description="Maximimum shower altitude", unit=u.rad)
min_alt = Field(nan * u.rad, description="Minimum shower altitude", unit=u.rad)
Expand Down Expand Up @@ -1095,7 +1095,7 @@ class SimulatedShowerDistribution(Container):

obs_id = Field(-1, description="links to which events this corresponds to")
hist_id = Field(-1, description="Histogram ID")
num_entries = Field(-1, description="Number of entries in the histogram")
n_entries = Field(-1, description="Number of entries in the histogram")
bins_energy = Field(
None,
description="array of energy bin lower edges, as in np.histogram",
Expand Down
25 changes: 12 additions & 13 deletions ctapipe/core/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@
import platform
import sys
import uuid
from collections import UserList
from contextlib import contextmanager
from importlib import import_module
from os.path import abspath
from pathlib import Path

import pkg_resources

import psutil
from astropy.time import Time
from pkg_resources import get_distribution

import ctapipe
from collections import UserList
from pathlib import Path

from .support import Singleton

Expand Down Expand Up @@ -54,7 +53,7 @@ def get_module_version(name):
except AttributeError:
try:
return get_distribution(name).version
except:
except Exception:
return "unknown"
except ImportError:
return "not installed"
Expand All @@ -78,7 +77,7 @@ def __init__(self):
self._finished_activities = []

def start_activity(self, activity_name=sys.executable):
""" push activity onto the stack"""
"""push activity onto the stack"""
activity = _ActivityProvenance(activity_name)
activity.start()
self._activities.append(activity)
Expand Down Expand Up @@ -132,7 +131,7 @@ def add_config(self, config):
self.current_activity.register_config(config)

def finish_activity(self, status="completed", activity_name=None):
""" end the current activity """
"""end the current activity"""
activity = self._activities.pop()
if activity_name is not None and activity_name != activity.name:
raise ValueError(
Expand All @@ -146,7 +145,7 @@ def finish_activity(self, status="completed", activity_name=None):

@contextmanager
def activity(self, name):
""" context manager for activities """
"""context manager for activities"""
self.start_activity(name)
yield
self.finish_activity(name)
Expand All @@ -164,15 +163,15 @@ def finished_activities(self):

@property
def provenance(self):
""" returns provenence for full list of activities """
"""returns provenence for full list of activities"""
return [x.provenance for x in self._finished_activities]

def as_json(self, **kwargs):
"""return all finished provenance as JSON. Kwargs for `json.dumps`
may be included, e.g. ``indent=4``"""

def set_default(obj):
""" handle sets (not part of JSON) by converting to list"""
"""handle sets (not part of JSON) by converting to list"""
if isinstance(obj, set):
return list(obj)
if isinstance(obj, UserList):
Expand All @@ -191,7 +190,7 @@ def finished_activity_names(self):
return [x.name for x in self._finished_activities]

def clear(self):
""" remove all tracked activities """
"""remove all tracked activities"""
self._activities = []
self._finished_activities = []

Expand Down Expand Up @@ -251,11 +250,11 @@ def register_output(self, url, role=None):
self._prov["output"].append(dict(url=url, role=role))

def register_config(self, config):
""" add a dictionary of configuration parameters to this activity"""
"""add a dictionary of configuration parameters to this activity"""
self._prov["config"] = config

def finish(self, status="completed"):
""" record final provenance information, normally called at shutdown."""
"""record final provenance information, normally called at shutdown."""
self._prov["stop"].update(_sample_cpu_and_memory())

# record the duration (wall-clock) for this activity
Expand Down Expand Up @@ -314,7 +313,7 @@ def _get_system_provenance():
system=platform.system(),
release=platform.release(),
libcver=platform.libc_ver(),
num_cpus=psutil.cpu_count(),
n_cpus=psutil.cpu_count(),
boot_time=Time(psutil.boot_time(), format="unix").isot,
),
python=dict(
Expand Down
8 changes: 4 additions & 4 deletions ctapipe/image/cleaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

from ..core.component import TelescopeComponent
from ..core.traits import (
BoolTelescopeParameter,
FloatTelescopeParameter,
IntTelescopeParameter,
BoolTelescopeParameter,
)
from .morphology import number_of_islands, brightest_island
from .morphology import brightest_island, number_of_islands


def tailcuts_clean(
Expand Down Expand Up @@ -275,8 +275,8 @@ def apply_time_average_cleaning(
if np.count_nonzero(mask) > 0:

# use main island (maximum charge) for time average calculation
num_islands, island_labels = number_of_islands(geom, mask)
mask_main = brightest_island(num_islands, island_labels, image)
n_islands, island_labels = number_of_islands(geom, mask)
mask_main = brightest_island(n_islands, island_labels, image)
time_ave = np.average(arrival_times[mask_main], weights=image[mask_main] ** 2)

time_diffs = np.abs(arrival_times[mask] - time_ave)
Expand Down
8 changes: 3 additions & 5 deletions ctapipe/image/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,13 +1077,11 @@ def _apply_second_pass(
# STEP 3

# find all islands using this cleaning
num_islands, labels = number_of_islands(camera_geometry, mask_clean)
n_islands, labels = number_of_islands(camera_geometry, mask_clean)

if num_islands > 0:
if n_islands > 0:
# ...find the brightest one
mask_brightest_island = brightest_island(
num_islands, labels, charge_1stpass
)
mask_brightest_island = brightest_island(n_islands, labels, charge_1stpass)
else:
mask_brightest_island = mask_clean

Expand Down
Loading