Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added colors to gestures #504

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/Gesture.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@
<div class="items-center flex mb-1">
<!-- Title of gesture-->
<GestureTilePart mr small>
<div
class="absolute rounded-full w-3 h-3 m-3"
style={`background-color:${gesture.getColor()}`}>
</div>
<div class="grid grid-cols-5 place-items-center p-2 w-50 h-30">
<div
class="w-40 col-start-2 col-end-5 text-center
Expand Down
15 changes: 0 additions & 15 deletions src/components/filters/FilterListRow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@
alt="data representation icon"
class="w-6 hover:opacity-60 mr-0.5 cursor-pointer" />
</div>
<!-- old icon
<div class="min-w-6 border-l-2">
<img
on:mouseenter={() => {
highlightedFilter.set(filterType);
showHighlighted.set(true);
}}
on:mouseleave={() => {
showHighlighted.set(false);
}}
src="imgs/ML_predict.svg"
alt="data representation icon"
class="w-6 hover:opacity-60 cursor-pointer" />
</div>
-->
</div>
</div>
{/key}
8 changes: 6 additions & 2 deletions src/components/graphs/knngraph/KNNModelGraphController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class KNNModelGraphController {
private rotationX: Writable<number>;
private rotationY: Writable<number>;
private rotationZ: Writable<number>;
private graphColors: string[];
private origin: Writable<{ x: number; y: number }>;
private scale: Writable<number>;
private graphDrawer: KNNModelGraphDrawer;
Expand All @@ -46,6 +47,7 @@ class KNNModelGraphController {
private trainingDataGetter: () => TrainingData,
origin: { x: number; y: number },
classId: string,
colors: string[],
axis?: Axes,
) {
this.filters = stores.getClassifier().getFilters();
Expand All @@ -56,6 +58,7 @@ class KNNModelGraphController {
this.rotationZ = writable(0);
this.scale = writable(100);
this.origin = writable(origin);
this.graphColors = colors;

const noOfPoints = this.trainingData
.map(el => el.length)
Expand Down Expand Up @@ -112,7 +115,7 @@ class KNNModelGraphController {
return [this.arrayToPoint(sample.value)];
}

private getControllerData() {
private getControllerData(): { config: GraphDrawConfig, data: TimestampedData<MicrobitAccelerometerDataVector>[] } {
const classifier = stores.getClassifier();
const xRot = get(this.rotationX);
const yRot = get(this.rotationY);
Expand Down Expand Up @@ -156,7 +159,8 @@ class KNNModelGraphController {
zRot,
origin,
scale,
} as GraphDrawConfig,
colors: this.graphColors
},
data: liveData,
};
}
Expand Down
11 changes: 7 additions & 4 deletions src/components/graphs/knngraph/KNNModelGraphDrawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type GraphDrawConfig = {
zRot: number;
origin: { x: number; y: number };
scale: number;
colors: string[]; // The number of colors should be equal to the number of classes plus one for live data
};

export type GrahpDrawData = {
Expand All @@ -39,15 +40,17 @@ class KNNModelGraphDrawer {
constructor(
private svg: d3.Selection<d3.BaseType, unknown, HTMLElement, any>,
private classId: string,
) {}
) { }

public drawLiveData = (drawConfig: GraphDrawConfig, drawData: Point3D) => {
if (isNaN(drawData.y)) {
return;
}
const pointTransformer = this.getPointTransformer(drawConfig);
const color =
StaticConfiguration.gestureColors[stores.getGestures().getNumberOfGestures()];
const color = drawConfig.colors.slice(-1)[0] // Fetch the last element of the colors array
if (!color) {
throw new Error('No color available for live data');
}
const drawableLivePoint: DrawablePoint = {
pointTransformed: pointTransformer(drawData),
color,
Expand Down Expand Up @@ -118,7 +121,7 @@ class KNNModelGraphDrawer {
drawData.forEach((clazz, classIndex) => {
clazz.forEach((sample, exampleIndex) => {
sample.forEach((axisValue, axisIndex) => {
const color = StaticConfiguration.gestureColors[classIndex];
const color = drawConfig.colors[classIndex];
const transformedPoint: Point3DTransformed = pointTransformer(axisValue);
this.drawnTrainingPoints.push(transformedPoint);
drawablePoints.push({
Expand Down
9 changes: 9 additions & 0 deletions src/components/graphs/knngraph/KnnModelGraph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import KnnPointToolTipView from './KnnPointToolTipView.svelte';
import { stores } from '../../../script/stores/Stores';
import { get } from 'svelte/store';
import StaticConfiguration from '../../../StaticConfiguration';

const classifierFactory = new ClassifierFactory();

Expand Down Expand Up @@ -51,11 +52,19 @@

const initSingle = (axis: Axes) => {
const svgSingle = d3.select('.d3-3d-single');
const graphColors = [
...$gestures.map(data => data.color),
StaticConfiguration.gestureColors[$gestures.length],
];
if (graphColors.length <= $gestures.length) {
throw new Error('Not enough colors');
}
const controller = new KNNModelGraphController(
svgSingle,
() => dataGetter(),
{ x: canvasWidth / 2, y: canvasHeight / 2 },
'd3-3d-single-',
graphColors,
axis,
);
return controller;
Expand Down
4 changes: 4 additions & 0 deletions src/components/output/OutputGestureStack.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@
<main class="mb-4 items-center flex flex-row">
<!-- NAMES AND CONFIDENCE METER -->
<GestureTilePart>
<div
class="absolute rounded-full w-3 h-3 m-3"
style={`background-color:${gesture.getColor()}`}>
</div>
<div class="items-center flex p-2">
<div
class="w-36 text-center font-semibold rounded-xl
Expand Down
Loading
Loading