Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 6f6710d

Browse files
251 add property aspectratio (#271)
* feat!: add aspectRatio property to mbr * feat!: add aspectRatio property to mbr close: #251 * feat: add aspectRatio property to mbr close: #251 * chore: change ratio formula for mbr * test: add testing cases for aspectRatio * test: update snapshot to pass tests * test: update snapshots * chore: change version of ml-spectra-processing to pass the test
1 parent 70215cf commit 6f6710d

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

src/maskAnalysis/__tests__/getMbr.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ test('horizontal MBR', () => {
7070
height: 3,
7171
perimeter: 22,
7272
surface: 24,
73+
aspectRatio: 3 / 8,
7374
});
7475
});
7576

@@ -95,6 +96,7 @@ test('other horizontal MBR', () => {
9596
height: 3,
9697
surface: 18,
9798
perimeter: 18,
99+
aspectRatio: 0.5,
98100
});
99101
});
100102

@@ -202,6 +204,7 @@ test('empty mask', () => {
202204
height: 0,
203205
surface: 0,
204206
perimeter: 0,
207+
aspectRatio: 0,
205208
});
206209
});
207210

@@ -230,3 +233,49 @@ test('draw mbr on large image', () => {
230233

231234
expect(result).toMatchImageSnapshot();
232235
});
236+
237+
test('other horizontal MBR', () => {
238+
const mask = testUtils.createMask(
239+
`
240+
1 0 0 0 1 0
241+
0 1 1 1 1 0
242+
1 0 1 1 0 1
243+
`,
244+
);
245+
246+
const result = getMbr(mask);
247+
expect(result.aspectRatio).toBeCloseTo(0.5);
248+
});
249+
250+
test('small triangular ROI', () => {
251+
const mask = testUtils.createMask([
252+
[0, 1, 0],
253+
[1, 1, 1],
254+
[0, 1, 0],
255+
]);
256+
257+
const result = getMbr(mask).aspectRatio;
258+
259+
expect(result).toBeCloseTo(1);
260+
});
261+
262+
test('small triangular ROI', () => {
263+
const mask = testUtils.createMask([
264+
[0, 1, 0],
265+
[0, 1, 0],
266+
[0, 1, 0],
267+
[0, 1, 0],
268+
]);
269+
270+
const result = getMbr(mask).aspectRatio;
271+
272+
expect(result).toStrictEqual(0.25);
273+
});
274+
275+
test('small triangular ROI', () => {
276+
const mask = testUtils.createMask([[1, 1, 1, 1]]);
277+
278+
const result = getMbr(mask).aspectRatio;
279+
280+
expect(result).toStrictEqual(0.25);
281+
});

src/maskAnalysis/getMbr.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export interface Mbr {
3333
* Angle between the rectangle and a horizontal line in radians.
3434
*/
3535
angle: number;
36+
/**
37+
*Ratio between width and height.
38+
*/
39+
aspectRatio: number;
3640
}
3741

3842
/**

src/maskAnalysis/utils/getMbrFromPoints.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function getMbrFromPoints(points: readonly Point[]): Mbr {
2020
height: 0,
2121
surface: 0,
2222
perimeter: 0,
23+
aspectRatio: 0,
2324
};
2425
}
2526
if (points.length === 1) {
@@ -30,6 +31,7 @@ export function getMbrFromPoints(points: readonly Point[]): Mbr {
3031
angle: 0,
3132
width: 0,
3233
height: 0,
34+
aspectRatio: 1,
3335
};
3436
}
3537

@@ -92,6 +94,7 @@ export function getMbrFromPoints(points: readonly Point[]): Mbr {
9294
const width = mbr[0].column - mbr[2].column;
9395
const height = mbr[0].row - mbr[2].row;
9496
const mbrAngle = getMbrAngle(mbrRotated);
97+
const ratio = height / width;
9598

9699
return {
97100
points: mbrRotated,
@@ -100,5 +103,6 @@ export function getMbrFromPoints(points: readonly Point[]): Mbr {
100103
width,
101104
height,
102105
perimeter: 2 * width + 2 * height,
106+
aspectRatio: ratio,
103107
};
104108
}

src/roi/__tests__/__snapshots__/computeRois.test.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ exports[`3x3 mask 1`] = `
7171
"id": 1,
7272
"mbr": {
7373
"angle": -0,
74+
"aspectRatio": 1.4999999999999998,
7475
"height": 2.9999999999999996,
7576
"perimeter": 10,
7677
"points": [
@@ -171,6 +172,7 @@ exports[`3x3 mask 1`] = `
171172
"id": 2,
172173
"mbr": {
173174
"angle": 0,
175+
"aspectRatio": 1,
174176
"height": 1,
175177
"perimeter": 4,
176178
"points": [
@@ -284,6 +286,7 @@ exports[`3x3 mask 2`] = `
284286
"id": -1,
285287
"mbr": {
286288
"angle": -0,
289+
"aspectRatio": 1.4999999999999998,
287290
"height": 2.9999999999999996,
288291
"perimeter": 10,
289292
"points": [

0 commit comments

Comments
 (0)