Exemple #1
0
def load_spectrograms(filename):
    train_data = []
    spectrogram, sp_time, sp_freq, fs = ap.get_spectrogram(
        filename, ST_WIN, ST_STEP)
    # These should change depending on the signal's size
    spec_resize_ratio_freq = 4
    spec_resize_ratio_time = 4

    f_low = F1 if F1 < fs / 2.0 else fs / 2.0
    f_high = F2 if F2 < fs / 2.0 else fs / 2.0

    # define feature sequence for vocalization detection
    f1 = np.argmin(np.abs(sp_freq - f_low))
    f2 = np.argmin(np.abs(sp_freq - f_high))

    spectral_energy_1 = spectrogram.sum(axis=1)
    spectral_energy_2 = spectrogram[:, f1:f2].sum(axis=1)
    seg_limits, thres_sm, _ = ap.get_syllables(spectral_energy_2,
                                               spectral_energy_1,
                                               ST_STEP,
                                               threshold_per=thres * 100,
                                               min_duration=MIN_VOC_DUR)

    train_data += (ar.cluster_syllables(seg_limits,
                                        spectrogram,
                                        sp_freq,
                                        f_low,
                                        f_high,
                                        ST_STEP,
                                        train=True))
    return train_data
Exemple #2
0
def signal_handler(signal, frame):
    """
    This function is called when Ctr + C is pressed and is used to output the
    final buffer into a WAV file
    """
    # write final buffer to wav file
    global fs
    if len(all_data) > 1:
        wavfile.write(outstr + ".wav", fs, np.int16(all_data))

    spectrogram, sp_time, sp_freq, fs = ap.get_spectrogram(
        outstr + ".wav", ST_WIN, ST_STEP)

    f_low = F1 if F1 < fs / 2.0 else fs / 2.0
    f_high = F2 if F2 < fs / 2.0 else fs / 2.0

    # define feature sequence for vocalization detection
    f1 = np.argmin(np.abs(sp_freq - f_low))
    f2 = np.argmin(np.abs(sp_freq - f_high))

    spectral_energy, means, max_values = ap.prepare_features(
        spectrogram[:, f1:f2])

    time_sec = 100
    seg_limits, thres_sm = ap.get_syllables(
        spectral_energy,
        means,
        max_values,
        ST_STEP,
        threshold_per=thres * 100,
        min_duration=MIN_VOC_DUR,
        threshold_buf=means,
    )

    for s in seg_limits:
        with open("debug_offline.csv", "a") as fp:
            fp.write(f'{count_mid_bufs * mid_buffer_size + s[0]},'
                     f'{count_mid_bufs * mid_buffer_size + s[1]}\n')

    sys.exit(0)
Exemple #3
0
        args.input_file, args.win, args.step)
    duration = spectrogram.shape[0] * args.step

    f_low = F1 if F1 < fs / 2.0 else fs / 2.0
    f_high = F2 if F2 < fs / 2.0 else fs / 2.0

    # define feature sequence for vocalization detection
    f1 = np.argmin(np.abs(sp_freq - f_low))
    f2 = np.argmin(np.abs(sp_freq - f_high))

    spectral_energy_1 = spectrogram.sum(axis=1)
    spectral_energy_2 = spectrogram[:, f1:f2].sum(axis=1)

    segs, thres_sm, spectral_ratio = ap.get_syllables(spectral_energy_2,
                                                      spectral_energy_1,
                                                      args.step,
                                                      threshold_per=thres *
                                                      100,
                                                      min_duration=MIN_VOC_DUR)

    segs_gt, f_gt = read_ground_truth(args.ground_truth_file)

    shapes, shapes2, shapes_gt, shapes_gt2 = [], [], [], []

    for s in segs:
        s1 = {
            'type': 'rect',
            'x0': s[0],
            'y0': f_low,
            'x1': s[1],
            'y1': f_high,
            'line': {
Exemple #4
0
            # define feature sequence for vocalization detection
            f1 = np.argmin(np.abs(sp_freq - f_low))
            f2 = np.argmin(np.abs(sp_freq - f_high))

            spectral_energy, mean_values, max_values = \
                ap.prepare_features(spectrogram[:,f1:f2])

            means.append(spectral_energy.mean())

            time_sec = 100
            seg_limits, thres_sm = ap.get_syllables(
                spectral_energy,
                mean_values,
                max_values,
                ST_STEP,
                threshold_per=thres * 100,
                factor=factor,
                min_duration=MIN_VOC_DUR,
                threshold_buf=means,
            )

            win = ST_STEP
            # the following lines save the detected
            # vocalizations in a .csv file and correct the split ones
            for s in seg_limits:
                if cnt > 0:
                    real_start = count_mid_bufs * mid_buffer_size - 0.1 + s[0]
                    real_end = count_mid_bufs * mid_buffer_size - 0.1 + s[1]
                    if s[0] <= 0.1 and s[1] >= 0.1:
                        # last vocalization should be changed
                        syllables_csv1 = []
Exemple #5
0
def get_layout(spec=False):

    global list_contour, segments, images, f1, f2, feats_simple, feats_deep, feats_2d_s, feats_2d_d, seg_limits, syllables
    seg_limits, thres_sm = ap.get_syllables(spectral_energy,
                                            means,
                                            max_values,
                                            ST_STEP,
                                            threshold_per=thres * 100,
                                            factor=factor,
                                            min_duration=MIN_VOC_DUR)
    time_end = time.time()
    print("Time needed for vocalizations detection: {} s".format(
        round(time_end - time_start, 1)))
    continue_ = args.continue_
    with open('offline_vocalizations.csv', 'w') as fp:
        for iS, s in enumerate(seg_limits):
            fp.write(f'{s[0]},' f'{s[1]}\n')
    if continue_ == "n":
        exit()

    images, f_points, f_points_init, \
    [feats_simple, feats_deep], feat_names, [f1, f2], segments, seg_limits = ar.cluster_syllables(seg_limits, spectrogram,
                                             sp_freq, f_low, f_high,  ST_STEP)

    tsne = TSNE(n_components=2, perplexity=50, n_iter=5000, random_state=1)
    feats_2d_s = tsne.fit_transform(feats_simple)
    tsne = TSNE(n_components=2, perplexity=50, n_iter=5000, random_state=1)
    feats_2d_d = tsne.fit_transform(feats_deep)
    list_contour = np.array(f_points, dtype=object)
    images = np.array(images, dtype=object)
    f_points_all, f_points_init_all = [[], []], [[], []]

    for iS in range(len(seg_limits)):
        f_points_all[0] += f_points[iS][0]
        f_points_all[1] += f_points[iS][1]
        f_points_init_all[0] += f_points_init[iS][0]
        f_points_init_all[1] += f_points_init[iS][1]
    shapes2, shapes3 = [], []
    for x, y in zip(f_points_all[0], f_points_all[1]):
        s1 = {
            'type': 'rect',
            'x0': x - ST_STEP / 5,
            'y0': y - 1000,
            'x1': x + ST_STEP / 5,
            'y1': y + 1000,
            'line': {
                'color': 'rgba(128, 0, 0, 1)',
                'width': 1
            },
            'fillcolor': 'rgba(128, 0, 0, 1)'
        }
        shapes2.append(s1)

    for x, y in zip(f_points_init_all[0], f_points_init_all[1]):
        s1 = {
            'type': 'rect',
            'x0': x - ST_STEP / 15,
            'y0': y - 200,
            'x1': x + ST_STEP / 15,
            'y1': y + 200,
            'line': {
                'color': 'rgba(0, 128, 0, 1)',
                'width': 1
            },
            'fillcolor': 'rgba(128, 128, 0, 1)'
        }
        shapes3.append(s1)

    syllables = [{"st": s[0], "et": s[1]} for iS, s in enumerate(seg_limits)]

    shapes1 = get_shapes(seg_limits, f_low, f_high)
    if spec:
        layout = dbc.Container(
            [
                # Title
                dbc.Row(
                    dbc.Col(
                        html.H2("AMVOC",
                                style={
                                    'textAlign': 'center',
                                    'color': colors['text'],
                                    'marginBottom': 30,
                                    'marginTop': 30
                                }))),

                # Selected segment controls
                dbc.Row([
                    dbc.Col(
                        html.Label(id="label_sel_start",
                                   children="Selected start",
                                   style={
                                       'textAlign': 'center',
                                       'color': colors['text']
                                   }),
                        width=1,
                    ),
                    dbc.Col(
                        html.Label(id="label_sel_end",
                                   children="Selected end",
                                   style={
                                       'textAlign': 'center',
                                       'color': colors['text']
                                   }),
                        width=1,
                    ),
                    dbc.Col(
                        html.Label(id='label_class',
                                   children="Class",
                                   style={
                                       'textAlign': 'center',
                                       'color': colors['text']
                                   }),
                        width=1,
                    )
                ],
                        className="h-5"),

                # Main heatmap
                dbc.Row(
                    dbc.Col(dcc.Graph(
                        id='heatmap1',
                        figure={
                            'data': [
                                go.Heatmap(
                                    x=sp_time[::spec_resize_ratio_time],
                                    y=sp_freq[::spec_resize_ratio_freq],
                                    z=
                                    clean_spectrogram[::
                                                      spec_resize_ratio_time, ::
                                                      spec_resize_ratio_freq].
                                    T,
                                    name='F',
                                    colorscale='Jet',
                                    showscale=False)
                            ],
                            'layout':
                            go.Layout(title='Spectrogram of the signal',
                                      margin=dict(
                                          l=55, r=20, b=120, t=40, pad=4),
                                      xaxis=dict(title='Time (Sec)'),
                                      yaxis=dict(title='Freq (Hz)'),
                                      shapes=shapes1 + shapes2 + shapes3)
                        }),
                            width=12,
                            style={
                                "height": "100%",
                                "background-color": "white"
                            }),
                    className="h-50",
                ),
                dbc.Row([
                    dbc.Col(
                        dcc.Dropdown(
                            id='dropdown_cluster',
                            options=[
                                {
                                    'label': 'Agglomerative',
                                    'value': 'agg'
                                },
                                {
                                    'label': 'Birch',
                                    'value': 'birch'
                                },
                                {
                                    'label': 'Gaussian Mixture',
                                    'value': 'gmm'
                                },
                                {
                                    'label': 'K-Means',
                                    'value': 'kmeans'
                                },
                                {
                                    'label': 'Mini-Batch K-Means',
                                    'value': 'mbkmeans'
                                },
                                # {'label': 'Spectral', 'value': 'spec'},
                            ],
                            value='agg'),
                        width=2,
                    ),
                    dbc.Col(
                        dcc.Dropdown(id='dropdown_n_clusters',
                                     options=[{
                                         'label': i,
                                         'value': i
                                     } for i in range(2, 11)],
                                     value=2),
                        width=2,
                    ),
                    dbc.Col(
                        dcc.Dropdown(id='dropdown_feats_type',
                                     options=[
                                         {
                                             'label': 'Method 1',
                                             'value': 'deep'
                                         },
                                         {
                                             'label': 'Method 2',
                                             'value': 'simple'
                                         },
                                     ],
                                     value='deep'),
                        width=2,
                    ),
                    html.Table([
                        html.Tr([
                            html.Td(['Silhouette score']),
                            html.Td(id='silhouette')
                        ]),
                        html.Tr([
                            html.Td(['Calinski-Harabasz score']),
                            html.Td(id='cal-har')
                        ]),
                        html.Tr([
                            html.Td(['Davies-Bouldin score']),
                            html.Td(id='dav-bould')
                        ]),
                    ]),
                ]),
                dbc.Row([
                    dbc.Col(html.Div(children="Global cluster annotations"),
                            width=3,
                            style={
                                'marginBottom': 20,
                                'marginTop': 20
                            }),
                    dbc.Col(html.Div(children="Specific cluster annotations"),
                            width=3,
                            style={
                                'marginBottom': 20,
                                'marginTop': 20
                            }),
                    dbc.Col(html.Div(children="Point annotations"),
                            width=3,
                            style={
                                'marginBottom': 20,
                                'marginTop': 20
                            }),
                ]),
                dbc.Row([
                    dbc.Col(dcc.Dropdown(
                        id='dropdown_total_cluster_annotation',
                        options=[{
                            'label': 'No Validation',
                            'value': 'no'
                        }] + [{
                            'label': i,
                            'value': i
                        } for i in np.arange(1, 6)],
                        value='no'),
                            width=2,
                            style={'display': 'block'}),
                    dbc.Col(html.Button('Submit', id='btn_3', n_clicks=0),
                            width=1,
                            style={'display': 'block'}),
                    dbc.Col(dcc.Dropdown(id='dropdown_cluster_annotation',
                                         options=[{
                                             'label': 'No Validation',
                                             'value': 'no'
                                         }] + [{
                                             'label': i,
                                             'value': i
                                         } for i in np.arange(1, 6)],
                                         value='no'),
                            width=2,
                            style={'display': 'block'}),
                    dbc.Col(html.Button('Submit', id='btn_2', n_clicks=0),
                            width=1,
                            style={'display': 'block'}),
                    dbc.Col(dcc.Dropdown(id='dropdown_point_annotation',
                                         options=[
                                             {
                                                 'label': 'No Validation',
                                                 'value': 'no'
                                             },
                                             {
                                                 'label': 'Approve',
                                                 'value': 'approve'
                                             },
                                             {
                                                 'label': 'Reject',
                                                 'value': 'reject'
                                             },
                                         ],
                                         value='no'),
                            width=2,
                            style={'display': 'block'}),
                    dbc.Col(html.Button('Submit', id='btn_1', n_clicks=0),
                            width=1,
                            style={'display': 'block'}),
                ]),
                dbc.Row([
                    dbc.Col(dcc.Graph(id='cluster_graph'),
                            width=9,
                            md=8,
                            style={'marginLeft': 0}),
                    dbc.Col(
                        html.Div(children=[
                            html.Div([
                                DataTable(id='total_annotation',
                                          style_cell={
                                              'whiteSpace': 'normal',
                                              'height': 'auto',
                                              'width': 100
                                          },
                                          columns=[{
                                              'id': 'Global annotation',
                                              'name': 'Global annotation'
                                          }])
                            ],
                                     style={'marginBottom': 10}),
                            DataTable(id='cluster_table',
                                      style_cell={
                                          'whiteSpace': 'normal',
                                          'height': 'auto',
                                          'width': 100
                                      },
                                      columns=[{
                                          'id': column,
                                          'name': column
                                      } for column in [
                                          'Clusters', 'Cluster annotation',
                                          'Annotated points'
                                      ]])
                        ]),
                        style={
                            'marginTop': 10,
                            'marginLeft': 5,
                            'marginRight': 0
                        },
                        width='25%',
                    ),
                ],
                        justify='start'),
                dbc.Row([
                    dbc.Col(dcc.Graph(
                        id='spectrogram',
                        hoverData={'points': [{
                            'pointIndex': 0
                        }]}),
                            width=6,
                            style={'marginTop': 30}),
                    dbc.Col(dcc.Graph(
                        id='contour_plot',
                        hoverData={'points': [{
                            'pointIndex': 0
                        }]}),
                            width=6,
                            style={'marginTop': 30})
                ]),
                # these are intermediate values to be used for sharing content
                # between callbacks
                # (see here https://dash.plotly.com/sharing-data-between-callbacks)
                dbc.Row(id='intermediate_val_syllables',
                        style={'display': 'none'}),
                dbc.Row(id='intermediate_val_total_clusters',
                        style={'display': 'none'}),
                dbc.Row(id='intermediate_val_clusters',
                        style={'display': 'none'}),
                dbc.Row(id='clustering_info', style={'display': 'none'})
            ],
            style={"height": "100vh"})
    else:
        layout = dbc.Container(
            [
                # Title
                dbc.Row(
                    dbc.Col(
                        html.H2("AMVOC",
                                style={
                                    'textAlign': 'center',
                                    'color': colors['text'],
                                    'marginBottom': 30,
                                    'marginTop': 30
                                }))),
                dbc.Row([
                    dbc.Col(
                        dcc.Dropdown(
                            id='dropdown_cluster',
                            options=[
                                {
                                    'label': 'Agglomerative',
                                    'value': 'agg'
                                },
                                {
                                    'label': 'Birch',
                                    'value': 'birch'
                                },
                                {
                                    'label': 'Gaussian Mixture',
                                    'value': 'gmm'
                                },
                                {
                                    'label': 'K-Means',
                                    'value': 'kmeans'
                                },
                                {
                                    'label': 'Mini-Batch K-Means',
                                    'value': 'mbkmeans'
                                },
                                # {'label': 'Spectral', 'value': 'spec'},
                            ],
                            value='agg'),
                        width=2,
                    ),
                    dbc.Col(
                        dcc.Dropdown(id='dropdown_n_clusters',
                                     options=[{
                                         'label': i,
                                         'value': i
                                     } for i in range(2, 11)],
                                     value=2),
                        width=2,
                    ),
                    dbc.Col(
                        dcc.Dropdown(id='dropdown_feats_type',
                                     options=[
                                         {
                                             'label': 'Method 1',
                                             'value': 'deep'
                                         },
                                         {
                                             'label': 'Method 2',
                                             'value': 'simple'
                                         },
                                     ],
                                     value='deep'),
                        width=2,
                    ),
                    html.Table([
                        html.Tr([
                            html.Td(['Silhouette score']),
                            html.Td(id='silhouette')
                        ]),
                        html.Tr([
                            html.Td(['Calinski-Harabasz score']),
                            html.Td(id='cal-har')
                        ]),
                        html.Tr([
                            html.Td(['Davies-Bouldin score']),
                            html.Td(id='dav-bould')
                        ]),
                    ]),
                ]),
                dbc.Row([
                    dbc.Col(html.Div(children="Global cluster annotations"),
                            width=3,
                            style={
                                'marginBottom': 20,
                                'marginTop': 20
                            }),
                    dbc.Col(html.Div(children="Specific cluster annotations"),
                            width=3,
                            style={
                                'marginBottom': 20,
                                'marginTop': 20
                            }),
                    dbc.Col(html.Div(children="Point annotations"),
                            width=3,
                            style={
                                'marginBottom': 20,
                                'marginTop': 20
                            }),
                ]),
                dbc.Row([
                    dbc.Col(dcc.Dropdown(
                        id='dropdown_total_cluster_annotation',
                        options=[{
                            'label': 'No Validation',
                            'value': 'no'
                        }] + [{
                            'label': i,
                            'value': i
                        } for i in np.arange(1, 6)],
                        value='no'),
                            width=2,
                            style={'display': 'block'}),
                    dbc.Col(html.Button('Submit', id='btn_3', n_clicks=0),
                            width=1,
                            style={'display': 'block'}),
                    dbc.Col(dcc.Dropdown(id='dropdown_cluster_annotation',
                                         options=[{
                                             'label': 'No Validation',
                                             'value': 'no'
                                         }] + [{
                                             'label': i,
                                             'value': i
                                         } for i in np.arange(1, 6)],
                                         value='no'),
                            width=2,
                            style={'display': 'block'}),
                    dbc.Col(html.Button('Submit', id='btn_2', n_clicks=0),
                            width=1,
                            style={'display': 'block'}),
                    dbc.Col(dcc.Dropdown(id='dropdown_point_annotation',
                                         options=[
                                             {
                                                 'label': 'No Validation',
                                                 'value': 'no'
                                             },
                                             {
                                                 'label': 'Approve',
                                                 'value': 'approve'
                                             },
                                             {
                                                 'label': 'Reject',
                                                 'value': 'reject'
                                             },
                                         ],
                                         value='no'),
                            width=2,
                            style={'display': 'block'}),
                    dbc.Col(html.Button('Submit', id='btn_1', n_clicks=0),
                            width=1,
                            style={'display': 'block'}),
                ]),
                dbc.Row([
                    dbc.Col(dcc.Graph(id='cluster_graph'),
                            width=9,
                            md=8,
                            style={'marginLeft': 0}),
                    dbc.Col(
                        html.Div(children=[
                            html.Div([
                                DataTable(id='total_annotation',
                                          style_cell={
                                              'whiteSpace': 'normal',
                                              'height': 'auto',
                                              'width': 100
                                          },
                                          columns=[{
                                              'id': 'Global annotation',
                                              'name': 'Global annotation'
                                          }])
                            ],
                                     style={'marginBottom': 10}),
                            DataTable(id='cluster_table',
                                      style_cell={
                                          'whiteSpace': 'normal',
                                          'height': 'auto',
                                          'width': 100
                                      },
                                      columns=[{
                                          'id': column,
                                          'name': column
                                      } for column in [
                                          'Clusters', 'Cluster annotation',
                                          'Annotated points'
                                      ]])
                        ]),
                        style={
                            'marginTop': 10,
                            'marginLeft': 5,
                            'marginRight': 0
                        },
                        width='25%',
                    ),
                ],
                        justify='start'),
                dbc.Row([
                    dbc.Col(dcc.Graph(
                        id='spectrogram',
                        hoverData={'points': [{
                            'pointIndex': 0
                        }]}),
                            width=6,
                            style={'marginTop': 20}),
                    dbc.Col(dcc.Graph(
                        id='contour_plot',
                        hoverData={'points': [{
                            'pointIndex': 0
                        }]}),
                            width=6,
                            style={'marginTop': 20})
                ]),
                # these are intermediate values to be used for sharing content
                # between callbacks
                # (see here https://dash.plotly.com/sharing-data-between-callbacks)
                dbc.Row(id='intermediate_val_syllables',
                        style={'display': 'none'}),
                dbc.Row(id='intermediate_val_total_clusters',
                        style={'display': 'none'}),
                dbc.Row(id='intermediate_val_clusters',
                        style={'display': 'none'}),
                dbc.Row(id='clustering_info', style={'display': 'none'})
            ],
            style={"height": "100vh"})
    return layout