Skip to content

Commit

Permalink
fix: Rename API tmp. #1601
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Nov 18, 2022
1 parent 10f0aea commit 0f7e86e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 75 deletions.
36 changes: 18 additions & 18 deletions py/h2o_wave/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6732,46 +6732,46 @@ class AudioAnnotatorItem:
"""
def __init__(
self,
from: float,
to: float,
range_from: float,
range_to: float,
tag: str,
):
_guard_scalar('AudioAnnotatorItem.from', from, (float, int,), False, False, False)
_guard_scalar('AudioAnnotatorItem.to', to, (float, int,), False, False, False)
_guard_scalar('AudioAnnotatorItem.range_from', range_from, (float, int,), False, False, False)
_guard_scalar('AudioAnnotatorItem.range_to', range_to, (float, int,), False, False, False)
_guard_scalar('AudioAnnotatorItem.tag', tag, (str,), False, False, False)
self.from = from
self.range_from = range_from
"""The start of the audio annotation in seconds."""
self.to = to
self.range_to = range_to
"""The end of the audio annotation in seconds."""
self.tag = tag
"""The `name` of the audio annotator tag to refer to for the `label` and `color` of this item."""

def dump(self) -> Dict:
"""Returns the contents of this object as a dict."""
_guard_scalar('AudioAnnotatorItem.from', self.from, (float, int,), False, False, False)
_guard_scalar('AudioAnnotatorItem.to', self.to, (float, int,), False, False, False)
_guard_scalar('AudioAnnotatorItem.range_from', self.range_from, (float, int,), False, False, False)
_guard_scalar('AudioAnnotatorItem.range_to', self.range_to, (float, int,), False, False, False)
_guard_scalar('AudioAnnotatorItem.tag', self.tag, (str,), False, False, False)
return _dump(
from=self.from,
to=self.to,
range_from=self.range_from,
range_to=self.range_to,
tag=self.tag,
)

@staticmethod
def load(__d: Dict) -> 'AudioAnnotatorItem':
"""Creates an instance of this class using the contents of a dict."""
__d_from: Any = __d.get('from')
_guard_scalar('AudioAnnotatorItem.from', __d_from, (float, int,), False, False, False)
__d_to: Any = __d.get('to')
_guard_scalar('AudioAnnotatorItem.to', __d_to, (float, int,), False, False, False)
__d_range_from: Any = __d.get('range_from')
_guard_scalar('AudioAnnotatorItem.range_from', __d_range_from, (float, int,), False, False, False)
__d_range_to: Any = __d.get('range_to')
_guard_scalar('AudioAnnotatorItem.range_to', __d_range_to, (float, int,), False, False, False)
__d_tag: Any = __d.get('tag')
_guard_scalar('AudioAnnotatorItem.tag', __d_tag, (str,), False, False, False)
from: float = __d_from
to: float = __d_to
range_from: float = __d_range_from
range_to: float = __d_range_to
tag: str = __d_tag
return AudioAnnotatorItem(
from,
to,
range_from,
range_to,
tag,
)

Expand Down
12 changes: 6 additions & 6 deletions py/h2o_wave/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2506,22 +2506,22 @@ def audio_annotator_tag(


def audio_annotator_item(
from: float,
to: float,
range_from: float,
range_to: float,
tag: str,
) -> AudioAnnotatorItem:
"""Create an annotator item with initial selected tags or no tags.
Args:
from: The start of the audio annotation in seconds.
to: The end of the audio annotation in seconds.
range_from: The start of the audio annotation in seconds.
range_to: The end of the audio annotation in seconds.
tag: The `name` of the audio annotator tag to refer to for the `label` and `color` of this item.
Returns:
A `h2o_wave.types.AudioAnnotatorItem` instance.
"""
return AudioAnnotatorItem(
from,
to,
range_from,
range_to,
tag,
)

Expand Down
16 changes: 8 additions & 8 deletions r/R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -2911,21 +2911,21 @@ ui_audio_annotator_tag <- function(

#' Create an annotator item with initial selected tags or no tags.
#'
#' @param from The start of the audio annotation in seconds.
#' @param to The end of the audio annotation in seconds.
#' @param range_from The start of the audio annotation in seconds.
#' @param range_to The end of the audio annotation in seconds.
#' @param tag The `name` of the audio annotator tag to refer to for the `label` and `color` of this item.
#' @return A AudioAnnotatorItem instance.
#' @export
ui_audio_annotator_item <- function(
from,
to,
range_from,
range_to,
tag) {
.guard_scalar("from", "numeric", from)
.guard_scalar("to", "numeric", to)
.guard_scalar("range_from", "numeric", range_from)
.guard_scalar("range_to", "numeric", range_to)
.guard_scalar("tag", "character", tag)
.o <- list(
from=from,
to=to,
range_from=range_from,
range_to=range_to,
tag=tag)
class(.o) <- append(class(.o), c(.wave_obj, "WaveAudioAnnotatorItem"))
return(.o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<option name="Python" value="true"/>
</context>
</template>
<template name="w_audio_annotator_item" value="ui.audio_annotator_item(from=$from$,to=$to$,tag='$tag$'),$END$" description="Create a minimal Wave AudioAnnotatorItem." toReformat="true" toShortenFQNames="true">
<variable name="from" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="to" expression="" defaultValue="" alwaysStopAt="true"/>
<template name="w_audio_annotator_item" value="ui.audio_annotator_item(range_from=$range_from$,range_to=$range_to$,tag='$tag$'),$END$" description="Create a minimal Wave AudioAnnotatorItem." toReformat="true" toShortenFQNames="true">
<variable name="range_from" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="range_to" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="tag" expression="" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="Python" value="true"/>
Expand Down Expand Up @@ -994,9 +994,9 @@
<option name="Python" value="true"/>
</context>
</template>
<template name="w_full_audio_annotator_item" value="ui.audio_annotator_item(from=$from$,to=$to$,tag='$tag$'),$END$" description="Create Wave AudioAnnotatorItem with full attributes." toReformat="true" toShortenFQNames="true">
<variable name="from" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="to" expression="" defaultValue="" alwaysStopAt="true"/>
<template name="w_full_audio_annotator_item" value="ui.audio_annotator_item(range_from=$range_from$,range_to=$range_to$,tag='$tag$'),$END$" description="Create Wave AudioAnnotatorItem with full attributes." toReformat="true" toShortenFQNames="true">
<variable name="range_from" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="range_to" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="tag" expression="" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="Python" value="true"/>
Expand Down
4 changes: 2 additions & 2 deletions tools/vscode-extension/component-snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"Wave AudioAnnotatorItem": {
"prefix": "w_audio_annotator_item",
"body": [
"ui.audio_annotator_item(from=$1, to=$2, tag='$3'),$0"
"ui.audio_annotator_item(range_from=$1, range_to=$2, tag='$3'),$0"
],
"description": "Create a minimal Wave AudioAnnotatorItem."
},
Expand Down Expand Up @@ -961,7 +961,7 @@
"Wave Full AudioAnnotatorItem": {
"prefix": "w_full_audio_annotator_item",
"body": [
"ui.audio_annotator_item(from=$1, to=$2, tag='$3'),$0"
"ui.audio_annotator_item(range_from=$1, range_to=$2, tag='$3'),$0"
],
"description": "Create a full Wave AudioAnnotatorItem."
},
Expand Down
18 changes: 9 additions & 9 deletions ui/src/audio_annotator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { wave } from './ui'
const
name = 'audio_annotator',
items = [
{ from: 0, to: 20, tag: 'tag1' },
{ from: 60, to: 90, tag: 'tag2' },
{ range_from: 0, range_to: 20, tag: 'tag1' },
{ range_from: 60, range_to: 90, tag: 'tag2' },
],
model: AudioAnnotator = {
name,
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('AudioAnnotator.tsx', () => {
fireEvent.click(canvasEl, { clientX: 40, clientY: 20, buttons: 1 })

expect(wave.args[name]).toHaveLength(3)
expect(wave.args[name]).toMatchObject([items[0], { tag: 'tag1', from: 30, to: 40 }, items[1]])
expect(wave.args[name]).toMatchObject([items[0], { tag: 'tag1', range_from: 30, range_to: 40 }, items[1]])
})

it('Does not draw a new annotation if too small', async () => {
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('AudioAnnotator.tsx', () => {
fireEvent.click(canvasEl, { clientX: 40, clientY: 20, buttons: 1 })

expect(wave.args[name]).toHaveLength(3)
expect(wave.args[name]).toMatchObject([items[0], { tag: 'tag2', from: 30, to: 40 }, items[1]])
expect(wave.args[name]).toMatchObject([items[0], { tag: 'tag2', range_from: 30, range_to: 40 }, items[1]])
})

it('Removes annotation after clicking remove btn', async () => {
Expand Down Expand Up @@ -236,7 +236,7 @@ describe('AudioAnnotator.tsx', () => {
fireEvent.click(canvasEl, { clientX: 10 + moveOffset, clientY: 60 })

const { from, to } = items[0]
expect(wave.args[name]).toMatchObject([{ ...items[0], from: from + moveOffset, to: to + moveOffset }, items[1]])
expect(wave.args[name]).toMatchObject([{ ...items[0], range_from: from + moveOffset, range_to: to + moveOffset }, items[1]])
})

it('Does not move annotation if left mouse btn not pressed (dragging)', async () => {
Expand Down Expand Up @@ -264,7 +264,7 @@ describe('AudioAnnotator.tsx', () => {
fireEvent.mouseMove(canvasEl, { clientX: from - moveOffset, clientY: 60, buttons: 1 })
fireEvent.click(canvasEl, { clientX: from - moveOffset, clientY: 60 })

expect(wave.args[name]).toMatchObject([items[0], { ...items[1], from: from - moveOffset }])
expect(wave.args[name]).toMatchObject([items[0], { ...items[1], range_from: from - moveOffset }])
})

it('Resizes annotation from and exceeds the "to"', async () => {
Expand All @@ -279,7 +279,7 @@ describe('AudioAnnotator.tsx', () => {
fireEvent.mouseMove(canvasEl, { clientX: from + moveOffset, clientY: 60, buttons: 1 })
fireEvent.click(canvasEl, { clientX: from + moveOffset, clientY: 60 })

expect(wave.args[name]).toMatchObject([items[0], { ...items[1], from: to, to: to + 5 }])
expect(wave.args[name]).toMatchObject([items[0], { ...items[1], range_from: to, range_to: to + 5 }])
})

it('Resizes annotation to', async () => {
Expand All @@ -294,7 +294,7 @@ describe('AudioAnnotator.tsx', () => {
fireEvent.mouseMove(canvasEl, { clientX: to + moveOffset, clientY: 60, buttons: 1 })
fireEvent.click(canvasEl, { clientX: to + moveOffset, clientY: 60 })

expect(wave.args[name]).toMatchObject([{ ...items[0], to: to + moveOffset }, items[1]])
expect(wave.args[name]).toMatchObject([{ ...items[0], range_to: to + moveOffset }, items[1]])
})

it('Resizes annotation to and exceeds the "from"', async () => {
Expand All @@ -309,7 +309,7 @@ describe('AudioAnnotator.tsx', () => {
fireEvent.mouseMove(canvasEl, { clientX: to - moveOffset, clientY: 60, buttons: 1 })
fireEvent.click(canvasEl, { clientX: to - moveOffset, clientY: 60 })

expect(wave.args[name]).toMatchObject([items[0], { ...items[1], from: to - moveOffset, to: from }])
expect(wave.args[name]).toMatchObject([items[0], { ...items[1], range_from: to - moveOffset, range_to: from }])
})

})
Expand Down
52 changes: 26 additions & 26 deletions ui/src/audio_annotator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ interface AudioAnnotatorTag {
/** Create an annotator item with initial selected tags or no tags. */
interface AudioAnnotatorItem {
/** The start of the audio annotation in seconds. */
from: F
range_from: F
/** The end of the audio annotation in seconds. */
to: F
range_to: F
/** The `name` of the audio annotator tag to refer to for the `label` and `color` of this item. */
tag: S
}
Expand Down Expand Up @@ -126,12 +126,12 @@ const
.filter((v, i) => v !== "00" || i > 0)
.join(":")
},
getIntersectingEdge = (x: U, { from, to }: DrawnAudioAnnotatorItem) => {
getIntersectingEdge = (x: U, { range_from: from, range_to: to }: DrawnAudioAnnotatorItem) => {
if (Math.abs(from - x) <= ANNOTATION_HANDLE_OFFSET) return 'resize-from'
if (Math.abs(to - x) <= ANNOTATION_HANDLE_OFFSET) return 'resize-to'
},
isAnnotationIntersecting = (a1: DrawnAudioAnnotatorItem, a2: DrawnAudioAnnotatorItem) => {
return (a2.from >= a1.from && a2.from <= a1.to) || (a1.from >= a2.from && a1.from <= a2.to)
return (a2.range_from >= a1.range_from && a2.range_from <= a1.range_to) || (a1.range_from >= a2.range_from && a1.range_from <= a2.range_to)
},
createAnnotation = (start: U, end: U, tag: S) => ({
from: Math.min(start, end),
Expand All @@ -141,11 +141,11 @@ const
canvasY: 0
}),
getIntersectedAnnotation = (annotations: DrawnAudioAnnotatorItem[], x: U, y: U) => {
return annotations.find(a => isIntersectingRect(x, y, { x1: a.from, x2: a.to, y1: a.canvasY, y2: a.canvasHeight + a.canvasY }))
return annotations.find(a => isIntersectingRect(x, y, { x1: a.range_from, x2: a.range_to, y1: a.canvasY, y2: a.canvasHeight + a.canvasY }))
},
getCanvasDimensions = (intersections: DrawnAudioAnnotatorItem[], annotation: DrawnAudioAnnotatorItem, maxDepth?: U) => {
const verticalIntersections = intersections
.filter(a => a !== annotation && annotation.from >= a.from && annotation.from <= a.to)
.filter(a => a !== annotation && annotation.range_from >= a.range_from && annotation.range_from <= a.range_to)
.sort((a, b) => a.canvasY - b.canvasY)
let canvasY = 0
let j = 0
Expand Down Expand Up @@ -176,8 +176,8 @@ const
})), [tags, theme]),
getMaxDepth = (idx: U, annotation: DrawnAudioAnnotatorItem, currMax: U) => {
// TODO: Super ugly perf-wise.
let currmax = annotations.filter(a => annotation.from >= a.from && annotation.from <= a.to).length
for (let j = idx + 1; annotations[j]?.from >= annotation?.from && annotations[j]?.from <= annotation?.to; j++) {
let currmax = annotations.filter(a => annotation.range_from >= a.range_from && annotation.range_from <= a.range_to).length
for (let j = idx + 1; annotations[j]?.range_from >= annotation?.range_from && annotations[j]?.range_from <= annotation?.range_to; j++) {
currmax = Math.max(currmax, getMaxDepth(j, annotations[j], currMax + 1))
}
return currmax
Expand All @@ -189,7 +189,7 @@ const
const annotation = annotations[i]
// TODO: Super ugly perf-wise.
const intersections = annotations.filter(a => a !== annotation && isAnnotationIntersecting(a, annotation))
const bottomIntersections = intersections.filter(a => a !== annotation && a.from >= annotation.from && a.from <= annotation.to).length
const bottomIntersections = intersections.filter(a => a !== annotation && a.range_from >= annotation.range_from && a.range_from <= annotation.range_to).length
// TODO: Add memoization.
const maxDepth = getMaxDepth(i, annotation, 1)
const shouldFillRemainingSpace = !bottomIntersections || maxDepth < currMaxDepth
Expand All @@ -205,7 +205,7 @@ const
const ctx = ctxRef.current
if (!ctx || !canvas) return
ctx.clearRect(0, 0, canvas.width, canvas.height)
annotations.forEach(({ from, to, tag, canvasHeight, canvasY, isFocused }) => {
annotations.forEach(({ range_from: from, range_to: to, tag, canvasHeight, canvasY, isFocused }) => {
ctx.fillStyle = colorsMap.get(tag)?.transparent || 'red'
ctx.fillRect(from, canvasY, to - from, canvasHeight)
if (isFocused) {
Expand Down Expand Up @@ -245,7 +245,7 @@ const
const intersected = getIntersectedAnnotation(annotations, cursor_x, cursor_y)
setTooltipProps(!intersected ? null : {
title: colorsMap.get(intersected.tag)?.label || '',
range: `${formatTime(intersected.from / canvas.width * duration)} - ${formatTime(intersected.to / canvas.width * duration)}`,
range: `${formatTime(intersected.range_from / canvas.width * duration)} - ${formatTime(intersected.range_to / canvas.width * duration)}`,
top: cursor_y + TOP_TOOLTIP_OFFSET,
left: cursor_x + LEFT_TOOLTIP_OFFSET
})
Expand All @@ -270,22 +270,22 @@ const
}
else if (action === 'move' && currIntersected) {
const movedOffset = cursor_x - currDrawnAnnotation.current.from
currIntersected.from += movedOffset
currIntersected.to += movedOffset
tooltipFrom = currIntersected.from
tooltipTo = currIntersected.to
currIntersected.range_from += movedOffset
currIntersected.range_to += movedOffset
tooltipFrom = currIntersected.range_from
tooltipTo = currIntersected.range_to
currDrawnAnnotation.current.from += movedOffset
}
else if (action === 'resize-from' && currIntersected) {
currIntersected.from = cursor_x
tooltipFrom = currIntersected.from
tooltipTo = currIntersected.to
currIntersected.range_from = cursor_x
tooltipFrom = currIntersected.range_from
tooltipTo = currIntersected.range_to
canvas.style.cursor = 'ew-resize'
}
else if (action === 'resize-to' && currIntersected) {
currIntersected.to = cursor_x
tooltipFrom = currIntersected.from
tooltipTo = currIntersected.to
currIntersected.range_to = cursor_x
tooltipFrom = currIntersected.range_from
tooltipTo = currIntersected.range_to
canvas.style.cursor = 'ew-resize'
}

Expand Down Expand Up @@ -333,9 +333,9 @@ const
else if (action === 'resize-from' || action === 'resize-to') {
const resized = currDrawnAnnotation.current.intersected
if (resized) {
const { from, to } = resized
resized.from = Math.min(from, to)
resized.to = Math.max(from, to)
const { range_from: from, range_to: to } = resized
resized.range_from = Math.min(from, to)
resized.range_to = Math.max(from, to)
}
}
onAnnotate(newAnnotation)
Expand Down Expand Up @@ -418,7 +418,7 @@ export const XAudioAnnotator = ({ model }: { model: AudioAnnotator }) => {
fetchedAudioUrlRef = React.useRef<S>(),
audioPositionIntervalRef = React.useRef<U>(),
setWaveArgs = (annotations: DrawnAudioAnnotatorItem[]) => {
wave.args[model.name] = annotations.map(({ from, to, tag }) => ({ from, to, tag })) as unknown as Rec[]
wave.args[model.name] = annotations.map(({ range_from: from, range_to: to, tag }) => ({ from, to, tag })) as unknown as Rec[]
if (model.trigger) wave.push()
},
activateTag = (tagName: S) => () => {
Expand Down Expand Up @@ -502,7 +502,7 @@ export const XAudioAnnotator = ({ model }: { model: AudioAnnotator }) => {
onAnnotate = (newAnnotation?: DrawnAudioAnnotatorItem) => {
setAnnotations(prev => {
const newAnnotations = newAnnotation ? [...prev, newAnnotation] : prev
newAnnotations.sort((a, b) => a.from - b.from)
newAnnotations.sort((a, b) => a.range_from - b.range_from)
setWaveArgs(newAnnotations)
return newAnnotations
})
Expand Down

0 comments on commit 0f7e86e

Please sign in to comment.