Пример #1
0
def test_concat_overlapping_hits(hits0, hits1, le, re):
    # combining fake hits of the two channels:
    hits1['channel'] = 1
    hits = np.concatenate([hits0, hits1])

    if not len(hits):
        # In case there are no hitlets there is not much to do:
        concat_hits = strax.concat_overlapping_hits(hits, (le, re), (0, 1), 0,
                                                    float('inf'))
        assert not len(
            concat_hits), 'Concatenated hits not empty although hits are empty'

    else:
        hits = strax.sort_by_time(hits)

        # Additional offset to time since le > hits['time'].min() does not
        # make sense:
        hits['time'] += 100

        # Now we are ready for the tests:
        # Creating for each channel a dummy array.
        tmax = strax.endtime(
            hits).max()  # Since dt is one this is the last sample
        tmax += re

        dummy_array = np.zeros((2, tmax), np.int64)
        for h in hits:
            # Filling samples with 1 if inside a hit:
            st = h['time'] - le
            et = strax.endtime(h) + re
            dummy_array[h['channel'], st:et] = 1

        # Now we concatenate the hits and check whether their length matches
        # with the total sum of our dummy arrays.
        concat_hits = strax.concat_overlapping_hits(hits, (le, re), (0, 1), 0,
                                                    float('inf'))

        assert len(concat_hits) <= len(
            hits), 'Somehow we have more hits than before ?!?'

        for ch in [0, 1]:
            dummy_sum = np.sum(dummy_array[ch])

            # Computing total length of concatenated hits:
            diff = strax.endtime(concat_hits) - concat_hits['time']
            m = concat_hits['channel'] == ch
            concat_sum = np.sum(diff[m])

            assert concat_sum == dummy_sum, f'Total length of concatenated hits deviates from hits for channel {ch}'

            if len(concat_hits[m]) > 1:
                # Checking if new hits do not overlapp or touch anymore:
                mask = strax.endtime(
                    concat_hits[m])[:-1] - concat_hits[m]['time'][1:]
                assert np.all(
                    mask < 0
                ), f'Found two hits within {ch} which are touching or overlapping'
Пример #2
0
def create_hitlets_from_hits(
    hits,
    save_outside_hits,
    channel_range,
    chunk_start=0,
    chunk_end=np.inf,
):
    """
    Function which creates hitlets from a bunch of hits.

    :param hits: Hits found in records.
    :param save_outside_hits: Tuple with left and right hit extension.
    :param channel_range: Detectors change range from channel map.
    :param chunk_start: (optional) start time of a chunk. Ensures that
        no hitlet is earlier than this timestamp.
    :param chunk_end: (optional) end time of a chunk. Ensures that
        no hitlet ends later than this timestamp.

    :return: Hitlets with temporary fields (data, max_goodness_of_split...)
    """
    # Merge concatenate overlapping  within a channel. This is important
    # in case hits were split by record boundaries. In case we
    # accidentally concatenate two PMT signals we split them later again.
    hits = strax.concat_overlapping_hits(
        hits,
        save_outside_hits,
        channel_range,
        chunk_start,
        chunk_end,
    )
    hits = strax.sort_by_time(hits)

    hitlets = np.zeros(len(hits), strax.hitlet_dtype())
    strax.copy_to_buffer(hits, hitlets, '_refresh_hit_to_hitlets')
    return hitlets
Пример #3
0
    def compute(self, records_nv, start, end):
        # Search again for hits in records:
        hits = strax.find_hits(
            records_nv, min_amplitude=self.config['hit_min_amplitude_nv'])

        # Merge concatenate overlapping  within a channel. This is important
        # in case hits were split by record boundaries. In case we
        # accidentally concatenate two PMT signals we split them later again.
        hits = strax.concat_overlapping_hits(
            hits, self.config['save_outside_hits_nv'],
            self.config['channel_map']['nveto'], start, end)
        hits = strax.sort_by_time(hits)

        # Now convert hits into temp_hitlets including the data field:
        if len(hits):
            nsamples = hits['length'].max()
        else:
            nsamples = 0
        temp_hitlets = np.zeros(
            len(hits), strax.hitlet_with_data_dtype(n_samples=nsamples))

        # Generating hitlets and copying relevant information from hits to hitlets.
        # These hitlets are not stored in the end since this array also contains a data
        # field which we will drop later.
        strax.refresh_hit_to_hitlets(hits, temp_hitlets)
        del hits

        # Get hitlet data and split hitlets:
        strax.get_hitlets_data(temp_hitlets, records_nv, to_pe=self.to_pe)

        temp_hitlets = strax.split_peaks(
            temp_hitlets,
            records_nv,
            self.to_pe,
            data_type='hitlets',
            algorithm='local_minimum',
            min_height=self.config['min_split_nv'],
            min_ratio=self.config['min_split_ratio_nv'])

        # Compute other hitlet properties:
        # We have to loop here 3 times over all hitlets...
        strax.hitlet_properties(temp_hitlets)
        entropy = strax.conditional_entropy(temp_hitlets,
                                            template='flat',
                                            square_data=False)
        temp_hitlets['entropy'][:] = entropy
        strax.compute_widths(temp_hitlets)

        # Remove data field:
        hitlets = np.zeros(len(temp_hitlets), dtype=strax.hitlet_dtype())
        drop_data_field(temp_hitlets, hitlets)

        return hitlets
Пример #4
0
def test_get_hitlets_data():
    dummy_records = [  # Contains Hitlet #:
        [
            [1, 3, 2, 1, 0, 0],
        ],  # 0
        [
            [0, 0, 0, 0, 1, 3],  # 1
            [2, 1, 0, 0, 0, 0]
        ],  #
        [
            [0, 0, 0, 0, 1, 3],  # 2
            [2, 1, 0, 1, 3, 2],
        ],  # 3
        [
            [0, 0, 0, 0, 1, 2],  # 4
            [2, 2, 2, 2, 2, 2],
            [2, 1, 0, 0, 0, 0]
        ],
        [[2, 1, 0, 1, 3, 2]],  # 5, 6
        [[2, 2, 2, 2, 2, 2]]  # 7
    ]

    # Defining the true parameters of the hitlets:
    true_area = [7, 7, 7, 6, 18, 3, 6, 12]
    true_time = [10, 28, 46, 51, 68, 88, 91, 104]
    true_waveform = [[1, 3, 2, 1], [1, 3, 2, 1], [1, 3, 2, 1], [1, 3, 2],
                     [1, 2, 2, 2, 2, 2, 2, 2, 2, 1], [2, 1], [1, 3, 2],
                     [2, 2, 2, 2, 2, 2]]

    records = _make_fake_records(dummy_records)
    hits = strax.find_hits(records, min_amplitude=2)
    hits = strax.concat_overlapping_hits(hits, (1, 1), (0, 1), 0, float('inf'))
    hitlets = np.zeros(
        len(hits),
        strax.hitlet_with_data_dtype(n_samples=np.max(hits['length'])))
    strax.refresh_hit_to_hitlets(hits, hitlets)
    strax.get_hitlets_data(hitlets, records, np.array([1, 1]))

    for i, (a, wf, t) in enumerate(zip(true_area, true_waveform, true_time)):
        h = hitlets[i]
        assert h['area'] == a, f'Hitlet {i} has the wrong area'
        assert np.all(h['data'][:h['length']] ==
                      wf), f'Hitlet {i} has the wrong waveform'
        assert h['time'] == t, f'Hitlet {i} has the wrong starttime'