Пример #1
0
def test_from_raven_dir():
    """ Test number of annotations when importing full folder (Raven). """
    paths = get_paths()
    total_annotations = 0
    for file in paths['raven_annot_files']:
        total_annotations += file['annot_number'] - file['duplicates']
    annot = Annotation()
    annot.from_raven(paths['raven_annot_dir'], verbose=False)
    assert len(annot) == total_annotations
    return None
Пример #2
0
def test_from_raven_singlefile_with_duplicates(fileidx=1):
    """ Test that Raven annotation duplicates are removed correctly. """
    paths = get_paths()
    annot = Annotation()
    annot.from_raven(os.path.join(
        paths['raven_annot_dir'],
        paths['raven_annot_files'][fileidx]['filename']),
                     verbose=False)
    assert len(annot) == paths['raven_annot_files'][fileidx][
        'annot_number'] - paths['raven_annot_files'][fileidx]['duplicates']
    return None
Пример #3
0
def test_from_raven_singlefile(fileidx=0):
    """ Test number of annotations when importing a single Raven file.

    Only use one file for this test defined by fileidx=0
    """
    paths = get_paths()
    annot = Annotation()
    annot.from_raven(os.path.join(
        paths['raven_annot_dir'],
        paths['raven_annot_files'][fileidx]['filename']),
                     verbose=False)
    assert len(annot) == paths['raven_annot_files'][fileidx][
        'annot_number'] - paths['raven_annot_files'][fileidx]['duplicates']
    return None
Пример #4
0
 def run(self, spectro, debug=False):
     
     self._prerun_check(self, spectrogram)
     
     detec = Annotation()
     detec.data['from_detector'] = True
     detec.data['software_name'] = self.name
     detec.data['software_version'] = self.version
     detec.data['entry_date'] = datetime.now()
     detec.data['uuid'] = detec.data.apply(lambda _: str(uuid.uuid4()), axis=1)
     return detec
Пример #5
0
def test_len_is_0():
    """ Test len of annot is 0 upon instantiation. """
    annot = Annotation()
    assert len(annot) == 0
    return None
#_, index = np.unique(f['time'], return_index=True)

indir = r'F:\results\Run_3_large_dataset_balanced\RCA_In_Dec3_2018_Jan30_2019_67391491'
outfile = 'hourly_summary.nc'
confidence_step = 0.05
#confidence_step = 0.24

# Start timer
tic = time.perf_counter()

# load data
print('')
print('Loading dataset')
tic1 = time.perf_counter()
annot = Annotation()
annot.from_raven(indir)
toc1 = time.perf_counter()
print(f"Executed in {toc1 - tic1:0.4f} seconds")

#ds = xarray.open_dataset(r'C:\Users\xavier.mouy\Documents\PhD\Projects\Dectector\DFO_RCA_run\Out_Jan_April2019.nc')

# Extract hourly summaries for each class and threshold
print('')
print('Calculating hourly detections')
tic2 = time.perf_counter()
class_groups = ds.groupby('label_class')
confidence_thresholds = np.arange(1 / len(class_groups), 1, confidence_step)
class_names = []
Xarrays = []
for name, group in class_groups:
Пример #7
0
from ecosound.core.annotation import Annotation
from ecosound.core.measurement import Measurement
from ecosound.visualization.grapher_builder import GrapherFactory
from ecosound.measurements.measurer_builder import MeasurerFactory
import time
import pandas as pd


## Input paraneters ##########################################################

annotation_file = r"C:\Users\xavier.mouy\Documents\PhD\Projects\Dectector\datasets\Master_annotations_dataset.nc"
detection_file = r"C:\Users\xavier.mouy\Documents\PhD\Projects\Dectector\results\Full_dataset_with_metadata2"
outfile=r'C:\Users\xavier.mouy\Documents\PhD\Projects\Dectector\results\dataset_annotations_only2.nc'

# load annotations
annot = Annotation()
annot.from_netcdf(annotation_file)

# load detections
detec = Measurement()
detec.from_netcdf(detection_file)
print(detec)

freq_ovp = True # default True
dur_factor_max = None # default None
dur_factor_min = 0.1 # default None
ovlp_ratio_min = 0.3 # defaulkt None
remove_duplicates = True # dfault - False
inherit_metadata = True # default False
filter_deploymentID = False # default True
Пример #8
0
t1 = 0#24
t2 = 200#40
## ###########################################################################
tic = time.perf_counter()

# load audio data
sound = Sound(audio_file)
sound.read(channel=0, chunk=[t1, t2], unit='sec', detrend=True)

# Calculates  spectrogram
spectro = Spectrogram(frame, window_type, nfft, step, sound.waveform_sampling_frequency, unit='samp')
spectro.compute(sound, dB=True, use_dask=True, dask_chunks=40)
spectro.crop(frequency_min=fmin, frequency_max=fmax, inplace=True)

# load annotations
annot = Annotation()
annot.from_raven(annotation_file)

# load detections
detec = Measurement()
detec.from_netcdf(detection_file)

freq_ovp = True # default True
dur_factor_max = None # default None
dur_factor_min = 0.1 # default None
ovlp_ratio_min = 0.3 # defaulkt None
remove_duplicates = True # dfault - False
inherit_metadata = True # default False
filter_deploymentID = False # default True
# here <-= filter per filename and deployment ID
Пример #9
0
    def run(self,
            spectro,
            start_time=None,
            use_dask=False,
            dask_chunks=(1000, 1000),
            debug=False):
        """Run detector.

        Runs the detector on the spectrogram object.

        Parameters
        ----------
        spectro : Spectrogram
            Spectrogram object to detect from.
        debug : bool, optional
            Displays binarization results for debugging purpused.The default
            is False.
        start_time : datetime.datetime, optional
            Start time/date of the signal being processed. If defined, the
            fields 'time_min_date' and 'time_max_date' of the detection
            annotation object are populated. The default is None.
        use_dask, bool, optional
            If True, runs the detector in parallel using Dask. The default is
            False.
        dask_chunks, tuple -> (int, int), optional
            Tuple of two int defining the size of the spectrogram chunks to use
            for the parallel processing: dask_chunks=(number of frequency bins,
             number of time bbins). Only used in use_dask is True. The default
            is (1000, 1000).

        Returns
        -------
        detec : Annotation
            Annotation object with the detection results.

        """
        # Pre-run verifications
        self._prerun_check(spectro)
        # Convert units to spectrogram bins
        kernel_duration = max(
            round(self.kernel_duration / spectro.time_resolution), 1)
        kernel_bandwidth = max(
            round(self.kernel_bandwidth / spectro.frequency_resolution), 1)
        duration_min = max(round(self.duration_min / spectro.time_resolution),
                           2)
        bandwidth_min = max(
            round(self.bandwidth_min / spectro.frequency_resolution), 2)
        if debug:
            self._plot_matrix(spectro.spectrogram, 'Spectrogram matrix')
        # # Apply filter
        if use_dask:
            dask_spectro = dask.array.from_array(spectro.spectrogram,
                                                 chunks=dask_chunks)
            Svar = dask_image.ndfilters.generic_filter(dask_spectro,
                                                       calcVariance2D,
                                                       size=(kernel_bandwidth,
                                                             kernel_duration),
                                                       mode='mirror')
            Svar = Svar.compute()
        else:
            Svar = ndimage.generic_filter(spectro.spectrogram,
                                          calcVariance2D,
                                          (kernel_bandwidth, kernel_duration),
                                          mode='mirror')

        if debug:
            self._plot_matrix(np.log(Svar), 'Local variance matrix')
        # binarization
        Svar[Svar < self.threshold] = 0
        Svar[Svar > 0] = 1
        if debug:
            self._plot_matrix(Svar, 'Binarized spectrogram matrix')

        #new
        #Svar = cv2.cvtColor(cv2.UMat(Svar), cv2.COLOR_RGB2GRAY)
        # Define contours
        Svar_gray = cv2.normalize(src=Svar,
                                  dst=None,
                                  alpha=0,
                                  beta=255,
                                  norm_type=cv2.NORM_MINMAX,
                                  dtype=cv2.CV_8UC1)
        (cnts, hierarchy) = cv2.findContours(Svar_gray.copy(),
                                             cv2.RETR_EXTERNAL,
                                             cv2.CHAIN_APPROX_SIMPLE)
        # loop over the contours
        isdetec = False
        t1 = []
        t2 = []
        fmin = []
        fmax = []
        for c in cnts:
            # Compute the bounding box for the contour
            (x, y, w, h) = cv2.boundingRect(c)
            # if the contour is too small, ignore it
            if w < duration_min or h < bandwidth_min:
                continue
            else:
                isdetec = True
                # box coord
                t1.append(x)
                t2.append(x + w - 1)
                fmin.append(y)
                fmax.append(y + h - 1)
        # Insert results in an Annotation object
        detec = Annotation()
        detec.data['time_min_offset'] = [
            t * spectro.time_resolution for t in t1
        ]
        detec.data['time_max_offset'] = [
            t * spectro.time_resolution for t in t2
        ]
        #detec.data['frequency_min'] = [f*spectro.frequency_resolution for f in fmin]
        #detec.data['frequency_max'] = [f*spectro.frequency_resolution for f in fmax]
        #detec.data['frequency_min'] = [(f*spectro.frequency_resolution)+spectro.axis_frequencies[0] for f in fmin]
        #detec.data['frequency_max'] = [(f*spectro.frequency_resolution)+spectro.axis_frequencies[0] for f in fmax]
        detec.data['frequency_min'] = [
            spectro.axis_frequencies[f] for f in fmin
        ]
        detec.data['frequency_max'] = [
            spectro.axis_frequencies[f] for f in fmax
        ]
        detec.data['duration'] = detec.data['time_max_offset'] - detec.data[
            'time_min_offset']
        detec.data['from_detector'] = True
        detec.data['software_name'] = self.name
        detec.data['software_version'] = self.version
        detec.data['entry_date'] = datetime.now()
        detec.data['uuid'] = detec.data.apply(lambda _: str(uuid.uuid4()),
                                              axis=1)
        if start_time:
            detec.data['time_min_date'] = pd.to_datetime(
                start_time +
                pd.to_timedelta(detec.data['time_min_offset'], unit='s'))
            detec.data['time_max_date'] = pd.to_datetime(
                start_time +
                pd.to_timedelta(detec.data['time_max_offset'], unit='s'))
        # sort by ascending order
        detec.data.sort_values(
            'time_min_offset',
            ascending=True,
            inplace=True,
            ignore_index=True,
        )
        return detec