Skip to content

Commit

Permalink
Compressed Alignment (#59)
Browse files Browse the repository at this point in the history
* getting direct widths for compressed segments

* remove console
  • Loading branch information
BryonLewis authored Feb 23, 2024
1 parent 7a6fb01 commit 14a54e1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
4 changes: 3 additions & 1 deletion bats_ai/core/models/spectrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,14 @@ def compressed(self):
starts_ = []
stops_ = []
domain = img.shape[1]
widths = []
for start, stop in ranges:
segment = img[:, start:stop]
segments.append(segment)

starts_.append(int(round(self.duration * (start / domain))))
stops_.append(int(round(self.duration * (stop / domain))))
widths.append(stop - start)

# buffer = np.zeros((len(img), 20, 3), dtype=img.dtype)
# segments.append(buffer)
Expand Down Expand Up @@ -271,7 +273,7 @@ def compressed(self):
buf.seek(0)
img_base64 = base64.b64encode(buf.getvalue()).decode('utf-8')

return img_base64, starts_, stops_
return img_base64, starts_, stops_, widths

@property
def image_np(self):
Expand Down
3 changes: 2 additions & 1 deletion bats_ai/core/views/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def get_spectrogram_compressed(request: HttpRequest, id: int):
return {'error': 'Recording not found'}

spectrogram = recording.spectrogram
compressed, starts, ends = spectrogram.compressed
compressed, starts, ends, widths = spectrogram.compressed

spectro_data = {
'base64_spectrogram': compressed,
Expand All @@ -281,6 +281,7 @@ def get_spectrogram_compressed(request: HttpRequest, id: int):
'end_times': ends,
'low_freq': spectrogram.frequency_min,
'high_freq': spectrogram.frequency_max,
'widths': widths,
},
}

Expand Down
1 change: 1 addition & 0 deletions client/src/components/geoJS/geoJSUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export interface SpectroInfo {
end_time: number;
start_times?: number[];
end_times?: number[];
widths?: number[], //widths of segements
low_freq: number;
high_freq: number;
}
Expand Down
21 changes: 11 additions & 10 deletions client/src/components/geoJS/layers/legendLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@ export default class LegendLayer {
const time = this.spectroInfo.end_time - this.spectroInfo.start_time;
const timeToPixels = this.spectroInfo.width / time;

const { start_times, end_times } = this.spectroInfo;
if (start_times && end_times) {
const { start_times, end_times, widths } = this.spectroInfo;
if (start_times && end_times && widths) {
// We need a pixel time to map to the 0 position
let pixelOffset = 0;
for (let i = 0; i < start_times.length; i += 1) {
const length = yBuffer * 4;
const start_time = start_times[i];
const end_time = end_times[i];
const width = widths[i];
this.lineDataX.push({
line: {
type: "LineString",
Expand All @@ -167,11 +168,11 @@ export default class LegendLayer {
type: "LineString",
coordinates: [
[
(end_time - start_time) * timeToPixels + pixelOffset,
width + pixelOffset,
baseYPos + yBuffer,
],
[
(end_time - start_time) * timeToPixels + pixelOffset,
width + pixelOffset,
baseYPos + topBuffer,
],
],
Expand All @@ -182,8 +183,8 @@ export default class LegendLayer {
line: {
type: "LineString",
coordinates: [
[(end_time - start_time) * timeToPixels + pixelOffset, baseTopPos],
[(end_time - start_time) * timeToPixels + pixelOffset, baseTopPos - topBuffer],
[width + pixelOffset, baseTopPos],
[width + pixelOffset, baseTopPos - topBuffer],
],
},
thicker: true,
Expand All @@ -193,10 +194,10 @@ export default class LegendLayer {
type: "LineString",
coordinates: [
[
(end_time - start_time) * timeToPixels + pixelOffset,
width + pixelOffset,
baseYPos + yBuffer,
],
[(end_time - start_time) * timeToPixels + pixelOffset, baseTopPos],
[width + pixelOffset, baseTopPos],
],
},
grid: true,
Expand All @@ -212,12 +213,12 @@ export default class LegendLayer {
});
this.textDataX.push({
text: `${end_time}ms`,
x: (end_time - start_time) * timeToPixels + pixelOffset,
x: width + pixelOffset,
y: baseTopPos,
offsetX: 3,
offsetY: baseTopPos === 0 ? -16 : 16,
});
pixelOffset += (end_time - start_time) * timeToPixels;
pixelOffset += width;
// Need to add the current
}
}
Expand Down

0 comments on commit 14a54e1

Please sign in to comment.