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

feat: add options to mean, median and variance #471

Merged
merged 25 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ebc2811
feat: add option to find mean from array of points
EscapedGibbon Jun 7, 2024
6e0e572
fix: add options to mean method in Image and add variance option
EscapedGibbon Jun 7, 2024
19f35a6
fix: minor fix in mean
EscapedGibbon Jun 7, 2024
77cb1ca
feat: add median option for points
EscapedGibbon Jun 7, 2024
796e015
fix: add options to median method
EscapedGibbon Jun 7, 2024
2d9e9b8
test: add test cases for mean function
EscapedGibbon Jun 7, 2024
98332dc
test: add test cases for median function
EscapedGibbon Jun 7, 2024
24b1b1c
fix: fix mean value calculation
EscapedGibbon Jun 7, 2024
bc9a433
fix: minor fix of variance function
EscapedGibbon Jun 7, 2024
e4769ae
test: add and fix testing cases
EscapedGibbon Jun 7, 2024
86d1988
feat: add variance to index
EscapedGibbon Jun 7, 2024
33fd253
refactor: rename variables in mean function
EscapedGibbon Jun 12, 2024
1471fcd
feat wip add throw if number of coordinates is invalid
EscapedGibbon Jun 12, 2024
ee2a932
refactor: remove ternary operator
EscapedGibbon Jun 12, 2024
e3ead66
feat wip add a check in median
EscapedGibbon Jun 12, 2024
5b7475c
feat: add a check if the coordinate is valid
EscapedGibbon Jun 12, 2024
9ca87e4
feat: add check for invalid coordinates in mean
EscapedGibbon Jun 12, 2024
185aad4
feat: add check for invalid coordinates in median
EscapedGibbon Jun 12, 2024
5aa80ae
test: add testing cases for mean
EscapedGibbon Jun 12, 2024
af89746
test: add testing cases for median
EscapedGibbon Jun 12, 2024
1a848c8
test: add testing cases
EscapedGibbon Jun 12, 2024
1ac1ff6
refactor: remplace getValueByPoint with getValue
EscapedGibbon Jun 12, 2024
d0812e7
Merge remote-tracking branch 'origin/main' into 470-imagemean-accept-…
EscapedGibbon Jun 14, 2024
9928bfd
refactor: change check for invalid points and change function to get …
EscapedGibbon Jun 14, 2024
9d1c023
test: add testing cases
EscapedGibbon Jun 14, 2024
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
25 changes: 25 additions & 0 deletions src/compute/__tests__/mean.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Point } from '../../geometry';
import { mean } from '../mean';

test('5x1 RGB image', () => {
Expand Down Expand Up @@ -65,3 +66,27 @@ test('mean from points in rgba image', () => {

expect(result).toStrictEqual([1, 2, 3, 0]);
});
test('must throw if array is empty', () => {
const image = testUtils.createRgbaImage([
[1, 2, 3, 0],
[1, 2, 3, 0],
]);
const points: Point[] = [];

expect(() => {
const result = image.mean({ points });
return result;
}).toThrow('Array of coordinates is empty.');
});
test("must throw if point's coordinates are invalid.", () => {
const image = testUtils.createRgbaImage([
[1, 2, 3, 0],
[1, 2, 3, 0],
]);
const points: Point[] = [{ column: 0, row: 2 }];
EscapedGibbon marked this conversation as resolved.
Show resolved Hide resolved

expect(() => {
const result = image.mean({ points });
return result;
}).toThrow('Invalid coordinate: {column: 0, row: 2}');
});
26 changes: 26 additions & 0 deletions src/compute/__tests__/median.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Point } from '../../geometry';
import { median } from '../median';

test('5x1 RGB image', () => {
Expand Down Expand Up @@ -54,6 +55,7 @@ test('median from points', () => {

expect(result).toStrictEqual([2]);
});

test('median from points on rgba image', () => {
const image = testUtils.createRgbaImage([
[1, 2, 2, 2],
Expand All @@ -68,3 +70,27 @@ test('median from points on rgba image', () => {

expect(result).toStrictEqual([1, 2, 2, 2]);
});

test('must throw if array is empty', () => {
const image = testUtils.createRgbaImage([
[1, 2, 2, 2],
[1, 2, 3, 2],
]);
const points: Point[] = [];
expect(() => {
const result = image.median({ points });
return result;
}).toThrow('Array of coordinates is empty.');
});

test("must throw if point's coordinates are invalid", () => {
const image = testUtils.createRgbaImage([
[1, 2, 2, 2],
[1, 2, 3, 2],
]);
const points: Point[] = [{ column: 0, row: 2 }];
expect(() => {
const result = image.median({ points });
return result;
}).toThrow('Invalid coordinate: {column: 0, row: 2}');
});
23 changes: 23 additions & 0 deletions src/compute/__tests__/variance.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Point } from '../../geometry';
import { variance } from '../variance';

test('1x1 RGB image', () => {
Expand Down Expand Up @@ -34,3 +35,25 @@ test('variance from points', () => {

expect(result).toStrictEqual([125]);
});
test('must throw if array is empty', () => {
const image = testUtils.createRgbaImage([
[1, 2, 2, 2],
[1, 2, 3, 2],
]);
const points: Point[] = [];
expect(() => {
const result = image.median({ points });
return result;
}).toThrow('Array of coordinates is empty.');
});
test("must throw if point's coordinates are invalid", () => {
const image = testUtils.createGreyImage([
[1, 2, 2, 2],
[1, 2, 3, 2],
]);
const points: Point[] = [{ column: 0, row: 2 }];
expect(() => {
const result = image.median({ points });
return result;
}).toThrow('Invalid coordinate: {column: 0, row: 2}');
});
10 changes: 8 additions & 2 deletions src/compute/mean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ export interface MeanOptions {
export function mean(image: Image, options?: MeanOptions): number[] {
const pixelSum = new Array<number>(image.channels).fill(0);
const nbValues = options ? options.points.length : image.size;
if (nbValues === 0) throw new RangeError('Invalid number of points.');
if (nbValues === 0) throw new RangeError('Array of coordinates is empty.');
if (options) {
for (const point of options.points) {
const index = point.row * image.width + point.column;
for (let channel = 0; channel < image.channels; channel++) {
pixelSum[channel] += image.getValueByPoint(point, channel);
if (index >= image.size || index < 0) {
throw new RangeError(
`Invalid coordinate: {column: ${point.column}, row: ${point.row}}.`,
);
}
pixelSum[channel] += image.getValueByIndex(index, channel);
}
}
} else {
Expand Down
9 changes: 8 additions & 1 deletion src/compute/median.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ export interface MedianOptions {
export function median(image: Image, options?: MedianOptions): number[] {
const pixel = new Array<number>(image.channels).fill(0);
if (options) {
//console.log(options.points.length);
EscapedGibbon marked this conversation as resolved.
Show resolved Hide resolved
if (options.points.length === 0) {
throw new RangeError('Invalid number of points.');
throw new RangeError('Array of coordinates is empty.');
}
for (let i = 0; i < image.channels; i++) {
const channel: number[] = [];
for (const point of options.points) {
const index = point.row * image.width + point.column;
if (index < 0 || index >= image.size) {
throw new RangeError(
`Invalid coordinate: {column: ${point.column}, row: ${point.row}}.`,
);
}
channel.push(image.getValueByPoint(point, i));
}
pixel[i] = quickMedian(channel);
Expand Down
Loading