Skip to content

Commit

Permalink
ENH: additional sanity checks for the event_number column
Browse files Browse the repository at this point in the history
  • Loading branch information
laborleben committed Sep 24, 2019
1 parent 2ee55b3 commit 70f42a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pixel_clusterizer/clusterizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,14 @@ def cluster_hits(self, hits, noisy_pixels=None, disabled_pixels=None):
if cluster_hits_field_name in self._cluster_hits.dtype.fields:
self._cluster_hits[cluster_hits_field_name][:n_hits] = hits[field_name]

# Check if event number is increasing. Otherwise give a warning message.
if self._last_event_number is not None and hits.shape[0] != 0 and self._cluster_hits["event_number"][0] <= self._last_event_number:
logging.warning('The event number does not increase with successive chunks.')
if hits.shape[0] > 1 and not np.all((self._cluster_hits["event_number"][1:n_hits] - self._cluster_hits["event_number"][:n_hits - 1]) >= 0):
raise RuntimeError('Some values in column "%s" decrease.' % (self._hit_fields_mapping["event_number"],))
if self._cluster_hits.shape[0] != 0:
self._last_event_number = self._cluster_hits[-1]["event_number"]

noisy_pixels_array = np.array([]) if noisy_pixels is None else np.array(noisy_pixels)
if noisy_pixels_array.shape[0] != 0:
noisy_pixels_max_range = np.array([max(0, np.max(noisy_pixels_array[:, 0])), max(0, np.max(noisy_pixels_array[:, 1]))])
Expand All @@ -386,12 +394,6 @@ def cluster_hits(self, hits, noisy_pixels=None, disabled_pixels=None):
# disabled_pixels = np.recarray(disabled_pixels_array.shape[0], dtype=mask_dtype)
# disabled_pixels[:] = [(item[0], item[1]) for item in disabled_pixels_array]

# Check if event number is increasing. Otherwise give a warning message.
if self._last_event_number is not None and self._cluster_hits.shape[0] != 0 and self._cluster_hits[0]["event_number"] == self._last_event_number:
logging.warning('Event number not increasing.')
if self._cluster_hits.shape[0] != 0:
self._last_event_number = self._cluster_hits[-1]["event_number"]

n_clusters = self.cluster_functions._cluster_hits( # Set n_clusters to new size
hits=self._cluster_hits[:n_hits],
clusters=self._clusters[:n_hits],
Expand Down Expand Up @@ -474,4 +476,4 @@ def _check_struct_compatibility(self, hits):
raise TypeError('The dtype for hit data field "%s" does not match. Got/expected: %s/%s.' % (key, hits.dtype[key], self._cluster_hits.dtype[mapped_key]))
additional_hit_fields = set(hits.dtype.names) - set([key for key, val in self._cluster_hits_descr])
if additional_hit_fields:
logging.warning('Found additional hit fields: %s' % ", ".join(additional_hit_fields))
logging.warning('Found additional column: %s' % ", ".join(additional_hit_fields))
6 changes: 6 additions & 0 deletions pixel_clusterizer/testing/test_clusterizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def test_exceptions(self):
'frame': 'frame'}
clusterizer = HitClusterizer(hit_fields=hit_mapping, hit_dtype=hit_dtype_new, pure_python=self.pure_python)
_, _ = clusterizer.cluster_hits(np.array([], dtype=hit_dtype_new))
# TEST 4 Set custom and correct hit mapping, decrease event_number
hits = np.ones(shape=(2, ), dtype=hit_dtype_new)
hits[0]['column'], hits[0]['row'], hits[0]['charge'], hits[0]['not_defined'] = 17, 36, 30, 19
hits[1]['column'], hits[1]['row'], hits[1]['charge'], hits[1]['not_defined'] = 18, 36, 6, 18
with self.assertRaises(RuntimeError):
_, _ = clusterizer.cluster_hits(hits)

def test_cluster_algorithm(self): # Basic functionality checks
# Initialize Clusterizer with default arguments
Expand Down

0 comments on commit 70f42a7

Please sign in to comment.