Esempio n. 1
0
def view_cluster(group_id, cluster_name):
    group = ClusterGroup.query.get(group_id)
    permission = ViewResultsPermission(group.project_id)
    if permission.can():
        page = request.args.get('page', 1, type=int)
        filter_form = FilterForm(request.args)
        filter_form.select_label.query = Cluster.query.filter_by(
            cg_id=group_id, cluster_name=cluster_name).distinct(Cluster.label)
        q = Cluster.query.filter(Cluster.cg_id == group_id).filter(
            Cluster.cluster_name == cluster_name)
        if filter_form.validate():
            if filter_form.select_label.data:
                q = q.filter(
                    Cluster.label == filter_form.select_label.data.label)
        clips = q.join(AudioFile).order_by(AudioFile.timestamp).order_by(
            Cluster.start).paginate(page, current_app.config['ITEMS_PER_PAGE'],
                                    False)
        cluster_schema = ClusterSchema(many=True)
        clips_json = cluster_schema.dumps(clips.items)
        return render_template('clusters/cluster_view.html',
                               title='Selections in sound cluster',
                               clips=clips,
                               clips_json=clips_json,
                               group=group,
                               cluster_name=cluster_name,
                               filter_form=filter_form)
    # permission denied
    abort(403)
Esempio n. 2
0
def dashboard(project_id):
    permission = ViewResultsPermission(project_id)
    if permission.can():
        return render_template('results_dash/dashboard.html',
                               title='Visualize Results',
                               project_id=project_id)
    # permission denied
    abort(403)
Esempio n. 3
0
def view_clip(file_name, offset):
    delete_form = DeleteForm()
    form = EditForm()
    form.select_type.query = LabelType.query.filter(
        LabelType.parent_id == None)
    wav_file = AudioFile.query.filter_by(name=file_name).first()
    station = wav_file.recording_device.station
    project_id = station.project_id
    lq = Label.query.join(LabelType).join(
        ProjectLabel, Label.id == ProjectLabel.label_id).filter(
            ProjectLabel.project_id == project_id)
    form.select_label.query = lq.filter(LabelType.parent_id == None)
    form.select_sub_label.query = lq.filter(LabelType.parent_id != None)
    add_permission = AddLabelPermission(project_id)
    view_permission = ViewResultsPermission(project_id)
    if form.validate_on_submit():
        if add_permission.can():
            label = LabeledClip(file_name=file_name,
                                offset=offset,
                                duration=current_app.config['CLIP_SECS'],
                                certain=form.certainty.data,
                                label=form.select_label.data,
                                sub_label=form.select_sub_label.data,
                                notes=form.notes.data,
                                user=current_user,
                                modified=datetime.utcnow())
            db.session.add(label)
            db.session.commit()
            flash('Label added.')
            return redirect(
                url_for('labels.view_clip', file_name=file_name,
                        offset=offset))
        # permission denied
        abort(403)
    if view_permission.can():
        admin_permission = ManageLabelsPermission(project_id)
        labels = LabeledClip.query.filter_by(
            file_name=file_name,
            offset=offset).order_by(LabeledClip.modified.desc()).all()
        return render_template('labels/view_clip.html',
                               title='Add/edit labels',
                               delete_form=delete_form,
                               form=form,
                               labels=labels,
                               wav_file=wav_file,
                               offset=offset,
                               station=station,
                               admin=admin_permission.can())
    # permission denied
    abort(403)
Esempio n. 4
0
def cluster_groups(project_id):
    permission = ViewResultsPermission(project_id)
    if permission.can():
        delete_form = DeleteForm()
        page = request.args.get('page', 1, type=int)
        q = ClusterGroup.query.filter(ClusterGroup.project_id == project_id)
        groups = q.order_by(ClusterGroup.name).paginate(
            page, current_app.config['ITEMS_PER_PAGE'], False)
        project = Project.query.get(project_id)
        return render_template('clusters/cluster_groups.html',
                               title='All cluster groups',
                               groups=groups,
                               project=project,
                               delete_form=delete_form)
    # permission denied
    abort(403)
Esempio n. 5
0
def list_clusters(group_id):
    group = ClusterGroup.query.get(group_id)
    permission = ViewResultsPermission(group.project_id)
    if permission.can():
        page = request.args.get('page', 1, type=int)
        filter_form = FilterForm(request.args, meta={'csrf': False})
        filter_form.select_label.query = Cluster.query.filter_by(
            cg_id=group_id).distinct(Cluster.label)
        q = Cluster.query.filter(Cluster.cg_id == group_id)
        if filter_form.validate():
            if filter_form.select_label.data:
                q = q.filter(
                    Cluster.label == filter_form.select_label.data.label)
        clusters = q.distinct(Cluster.cluster_name).order_by(
            Cluster.cluster_name).paginate(
                page, current_app.config['ITEMS_PER_PAGE'], False)
        return render_template('clusters/cluster_list.html',
                               title='Sound clusters in group',
                               clusters=clusters,
                               group=group,
                               filter_form=filter_form)
    # permission denied
    abort(403)
Esempio n. 6
0
def list_labels(project_id=None):
    permission = ViewResultsPermission(project_id)
    if permission.can():
        page = request.args.get('page', 1, type=int)
        filter_form = FilterForm(request.args, meta={'csrf': False})
        fq = Label.query.join(LabelType).filter(LabelType.parent_id == None)
        q = LabeledClip.query.join(AudioFile).join(LabeledClip.label)
        view_restricted = ManageLabelsPermission(project_id)
        if not view_restricted.can():
            q = q.filter((Label.restricted == False)
                         | (Label.restricted == None))
        if project_id:
            q = q.join(AudioFile.monitoring_station).filter(
                MonitoringStation.project_id == project_id)
            fq = fq.join(ProjectLabel,
                         Label.id == ProjectLabel.label_id).filter(
                             ProjectLabel.project_id == project_id)
        filter_form.select_label.query = fq
        if filter_form.validate():
            if filter_form.select_label.data:
                q = q.filter(Label.id == filter_form.select_label.data.id)
            if filter_form.certain.data == '1':
                q = q.filter(LabeledClip.certain == True)
            if filter_form.certain.data == '0':
                q = q.filter(LabeledClip.certain == False)
        clips = q.order_by(AudioFile.timestamp).order_by(
            LabeledClip.offset).paginate(page,
                                         current_app.config['ITEMS_PER_PAGE'],
                                         False)
        return render_template('labels/label_list.html',
                               title='Labels',
                               clips=clips,
                               project_id=project_id,
                               filter_form=filter_form)
    # permission denied
    abort(403)
Esempio n. 7
0
def list_outputs(project_id):
    permission = ViewResultsPermission(project_id)
    if permission.can():
        page = request.args.get('page', 1, type=int)
        per_page = request.args.get(
            'per_page', type=int) or current_app.config['ITEMS_PER_PAGE']
        q = ModelOutput.query.join(AudioFile).join(
            AudioFile.monitoring_station).join(ModelIteration)
        iq = ModelIteration.query.join(MLModel).filter(
            MLModel.project_id == project_id).filter(
                ModelIteration.status == StatusEnum.finished).order_by(
                    ModelIteration.updated.desc())
        latest_iteration = iq.first()
        if latest_iteration:
            latest_iteration_id = latest_iteration.id
        else:
            latest_iteration_id = 0
        iteration = request.args.get('iteration',
                                     latest_iteration_id,
                                     type=int)
        q = q.filter(ModelIteration.id == iteration)
        predicted_label = request.args.get('label', type=int)
        if predicted_label:
            q = q.filter(ModelOutput.label_id == predicted_label)
        min_prob = request.args.get('min_prob', 0.99, type=float)
        q = q.filter(ModelOutput.probability >= min_prob)
        max_prob = request.args.get('max_prob', 1.0, type=float)
        q = q.filter(ModelOutput.probability <= max_prob)
        station = request.args.get('station', type=int)
        if station:
            q = q.filter(MonitoringStation.id == station)
        start_date = request.args.get('start_date')
        if start_date:
            q = q.filter(func.date(AudioFile.timestamp) >= start_date)
        end_date = request.args.get('end_date')
        if end_date:
            q = q.filter(func.date(AudioFile.timestamp) <= end_date)
        start_hour = request.args.get('start_hour', type=int)
        if start_hour and start_hour > 0:
            q = q.filter(extract('hour', AudioFile.timestamp) >= start_hour)
        end_hour = request.args.get('end_hour', type=int)
        if end_hour and end_hour < 23:
            q = q.filter(extract('hour', AudioFile.timestamp) <= end_hour)
        verification = request.args.get('verification')
        if verification == 'confirmed':
            q = q.join(LabeledClip,
                       (LabeledClip.file_name == ModelOutput.file_name) &
                       (LabeledClip.offset == ModelOutput.offset) &
                       (LabeledClip.label_id == ModelOutput.label_id))
        if verification == 'unverified':
            q = q.outerjoin(LabeledClip,
                            (LabeledClip.file_name == ModelOutput.file_name) &
                            (LabeledClip.offset == ModelOutput.offset)).filter(
                                LabeledClip.id == None)
        sort = request.args.get('sort')
        single = request.args.get('single')
        if sort == 'prob' or sort == None:
            if single == 'on':
                subq = q.with_entities(
                    ModelOutput.id,
                    func.row_number().over(
                        order_by=ModelOutput.probability.desc(),
                        partition_by=ModelOutput.file_name).label(
                            'rank')).subquery()
                q = ModelOutput.query.join(
                    subq, ModelOutput.id == subq.c.id).filter(
                        subq.c.rank == 1).order_by(
                            ModelOutput.probability.desc())
            else:
                q = q.order_by(ModelOutput.probability.desc())
        if sort == 'earliest':
            q = q.order_by(ModelOutput.file_name).order_by(ModelOutput.offset)
            if single == 'on':
                q = q.distinct(ModelOutput.file_name)
        if sort == 'latest':
            q = q.order_by(ModelOutput.file_name.desc()).order_by(
                ModelOutput.offset.desc())
            if single == 'on':
                q = q.distinct(ModelOutput.file_name)
        csv = request.args.get('csv', type=int)
        if csv:
            q = q.join(Label).with_entities(
                MonitoringStation.name.label('station'), AudioFile.timestamp,
                ModelOutput.offset, ModelOutput.duration,
                Label.name.label('label'), ModelOutput.probability).all()
            column_names = [
                'station', 'timestamp', 'offset', 'duration', 'label',
                'probability'
            ]
            return excel.make_response_from_query_sets(
                q, column_names, 'csv', file_name='predictions.csv')
        predictions = q.paginate(page, per_page, False)
        output_schema = ModelOutputSchema(many=True)
        clips_json = output_schema.dumps(predictions.items)
        project = Project.query.get(project_id)
        return render_template('ml/output_list.html',
                               title='Machine Learning Output',
                               predictions=predictions,
                               clips_json=clips_json,
                               project=project)
    # permission denied
    abort(403)