Skip to content

Commit

Permalink
fix-presigned url (#104)
Browse files Browse the repository at this point in the history
* fix-presigned url

* fix-presigned url

* adding admin viewing of compressed spectrograms

* linting

* fix compressed link
  • Loading branch information
BryonLewis authored Oct 29, 2024
1 parent 829541c commit d408399
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 34 deletions.
14 changes: 14 additions & 0 deletions bats_ai/core/admin/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class RecordingAdmin(admin.ModelAdmin):
'name',
'audio_file',
'spectrogram_status',
'compressed_spectrogram_status',
'owner',
'recorded_date',
'recorded_time',
Expand Down Expand Up @@ -60,6 +61,19 @@ def spectrogram_status(self, recording: Recording):
return link
return None

@admin.display(
description='Compressed Spectrogram',
empty_value='Not computed',
)
def compressed_spectrogram_status(self, recording: Recording):
if recording.has_compressed_spectrogram:
spectrogram = recording.compressed_spectrogram
href = reverse('admin:core_compressedspectrogram_change', args=(spectrogram.pk,))
description = str(spectrogram)
link = mark_safe(f'<a href="{href}">{description}</a>')
return link
return None

@admin.action(description='Compute Spectrograms')
def compute_spectrograms(self, request: HttpRequest, queryset: QuerySet):
counter = 0
Expand Down
22 changes: 22 additions & 0 deletions bats_ai/core/models/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ def spectrogram(self):

return spectrogram

@property
def has_compressed_spectrogram(self):
return len(self.compressed_spectrograms) > 0

@property
def compressed_spectrograms(self):
from bats_ai.core.models import CompressedSpectrogram

query = CompressedSpectrogram.objects.filter(recording=self).order_by('-created')
return query.all()

@property
def compressed_spectrogram(self):
pass

compressed_spectrograms = self.compressed_spectrograms

assert len(compressed_spectrograms) >= 1
spectrogram = compressed_spectrograms[0] # most recently created

return spectrogram


@receiver(models.signals.pre_delete, sender=Recording)
def delete_content(sender, instance, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion bats_ai/core/views/guanometadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def default_data(
'nabat_site_name': gfile.get('NABat|Site Name', None),
}
if (
nabat_fields['nabat_longitude'] and nabat_fields['nabat_longitude'] > 0
nabat_fields['nabat_longitude'] and float(nabat_fields['nabat_longitude']) > 0
): # individuals don't put the - in the longitude
nabat_fields['nabat_longitude'] = str(float(nabat_fields['nabat_longitude']) * -1)
# Extract additional fields with conditionals
Expand Down
4 changes: 0 additions & 4 deletions bats_ai/core/views/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,6 @@ def get_spectrogram_compressed(request: HttpRequest, id: int):
except recording.DoesNotExist:
return {'error': 'Recording does not exist'}

with colormap():
label, score, confs = compressed_spectrogram.predict()
print(label, score, confs)

spectro_data = {
'url': compressed_spectrogram.image_url,
'spectroInfo': {
Expand Down
107 changes: 78 additions & 29 deletions client/src/views/Spectrogram.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<script lang="ts">
import { computed, defineComponent, onMounted, onUnmounted, Ref, ref, watch } from "vue";
import {
computed,
defineComponent,
onMounted,
onUnmounted,
Ref,
ref,
watch,
} from "vue";
import {
getSpecies,
getAnnotations,
Expand Down Expand Up @@ -57,16 +65,22 @@ export default defineComponent({
const recordingInfo = ref(false);
const getAnnotationsList = async (annotationId?: number) => {
const response = await getAnnotations(props.id);
annotations.value = response.data.sort((a, b) => a.start_time - b.start_time);
annotations.value = response.data.sort(
(a, b) => a.start_time - b.start_time
);
const tempResp = await getTemporalAnnotations(props.id);
temporalAnnotations.value = tempResp.data.sort((a, b) => a.start_time - b.start_time);
temporalAnnotations.value = tempResp.data.sort(
(a, b) => a.start_time - b.start_time
);
if (annotationId !== undefined) {
selectedId.value = annotationId;
}
};
const selectedIndex = computed(() => {
if (annotations.value && selectedId.value !== null) {
return annotations.value.findIndex((item) => item.id === selectedId.value);
return annotations.value.findIndex(
(item) => item.id === selectedId.value
);
}
return -1;
});
Expand All @@ -88,18 +102,33 @@ export default defineComponent({
const response = compressed.value
? await getSpectrogramCompressed(props.id)
: await getSpectrogram(props.id);
if (response.data['url']) {
image.value.src = response.data['url'];
if (response.data["url"]) {
if (import.meta.env.PROD) {
const updateHost = `${window.location.protocol}//${window.location.hostname}/`;
const updatedURL = response.data["url"].replace(
"http://127.0.0.1:9000/",
updateHost
);
image.value.src = updatedURL.split("?")[0];
} else {
image.value.src = response.data['url'];
}
} else {
// TODO Error Out if there is no URL
console.error('No URL found for the spectrogram');
console.error("No URL found for the spectrogram");
}
image.value.onload = () => {
loadedImage.value = true;
};
spectroInfo.value = response.data["spectroInfo"];
annotations.value = response.data["annotations"]?.sort((a, b) => a.start_time - b.start_time) || [];
temporalAnnotations.value = response.data["temporal"]?.sort((a, b) => a.start_time - b.start_time) || [];
annotations.value =
response.data["annotations"]?.sort(
(a, b) => a.start_time - b.start_time
) || [];
temporalAnnotations.value =
response.data["temporal"]?.sort(
(a, b) => a.start_time - b.start_time
) || [];
if (response.data.currentUser) {
currentUser.value = response.data.currentUser;
}
Expand All @@ -116,14 +145,26 @@ export default defineComponent({
selectedId.value = annotationId;
};
const selectedAnnotation = computed(() => {
if (selectedId.value !== null && selectedType.value === 'pulse' && annotations.value) {
const found = annotations.value.findIndex((item) => item.id === selectedId.value);
if (
selectedId.value !== null &&
selectedType.value === "pulse" &&
annotations.value
) {
const found = annotations.value.findIndex(
(item) => item.id === selectedId.value
);
if (found !== -1) {
return annotations.value[found];
}
}
if (selectedId.value !== null && selectedType.value === 'sequence' && temporalAnnotations.value) {
const found = temporalAnnotations.value.findIndex((item) => item.id === selectedId.value);
if (
selectedId.value !== null &&
selectedType.value === "sequence" &&
temporalAnnotations.value
) {
const found = temporalAnnotations.value.findIndex(
(item) => item.id === selectedId.value
);
if (found !== -1) {
return temporalAnnotations.value[found];
}
Expand All @@ -133,9 +174,12 @@ export default defineComponent({
watch(gridEnabled, () => {
toggleLayerVisibility("grid");
});
watch(() => props.id, () => {
loadData();
});
watch(
() => props.id,
() => {
loadData();
}
);
onMounted(() => loadData());
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parentGeoViewerRef: Ref<any> = ref(null);
Expand Down Expand Up @@ -169,15 +213,23 @@ export default defineComponent({
const otherUsers = computed(() => Object.keys(otherUserAnnotations.value));
const deleteChip = (item: string) => {
selectedUsers.value.splice(selectedUsers.value.findIndex((data) => data === item));
selectedUsers.value.splice(
selectedUsers.value.findIndex((data) => data === item)
);
setSelectedUsers(selectedUsers.value);
};
watch(selectedUsers, () => {
setSelectedUsers(selectedUsers.value);
});
const processSelection = ({id, annotationType}: { id: number, annotationType: 'pulse' | 'sequence'}) => {
const processSelection = ({
id,
annotationType,
}: {
id: number;
annotationType: "pulse" | "sequence";
}) => {
selectedId.value = id;
selectedType.value = annotationType;
};
Expand Down Expand Up @@ -233,10 +285,7 @@ export default defineComponent({
<v-toolbar>
<v-container>
<v-row align="center">
<v-tooltip

bottom
>
<v-tooltip bottom>
<template #activator="{ props: subProps }">
<v-icon
v-bind="subProps"
Expand All @@ -246,11 +295,9 @@ export default defineComponent({
mdi-information-outline
</v-icon>
</template>
<span>
Recording Information
</span>
<span> Recording Information </span>
</v-tooltip>

<v-col cols="2">
<div>
<b>Time:</b>
Expand Down Expand Up @@ -280,13 +327,15 @@ export default defineComponent({
class="px-0"
style="font-size: 20px"
>
<div v-if="annotationState !== '' && annotationState !== 'disabled'">
<div
v-if="annotationState !== '' && annotationState !== 'disabled'"
>
<b>Mode:</b>
<span> {{ annotationState }}</span>
</div>
</v-col>
<v-col
v-if="otherUsers.length && colorScale"
v-if="otherUsers.length && colorScale"
cols="3"
class="ma-0 pa-0 pt-5"
>
Expand All @@ -309,7 +358,7 @@ export default defineComponent({
text-color="gray"
@click:close="deleteChip(item.value)"
>
{{ item.value.replace(/@.*/, '') }}
{{ item.value.replace(/@.*/, "") }}
</v-chip>
</template>
</v-select>
Expand Down

0 comments on commit d408399

Please sign in to comment.