Skip to content

Commit

Permalink
support multiple no data values
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Jan 14, 2024
1 parent d207915 commit 81373ed
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 144 deletions.
53 changes: 28 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ const result = fastMin(pixel_values);
```

# no data value
If you want to ignore a specific value, you can set the no_data value.
If you want to ignore one or more specific values, you can set the no_data value.
```javascript
import fastMin from 'fast-min';

const numbers = [99, 0, 7, 99, 5, ...]);
const numbers = [99, 0, 7, 99, 5, ...];
const result = fastMin(numbers, { no_data: 0 });
// result is 5

const result = fastMax(numbers, { no_data: [5, 99] });
// result is now 7
```

# performance tests
Expand All @@ -45,26 +48,26 @@ Tests have been conducted by creating an array of ten million random numbers fro
| array type | library | average duration in milliseconds |
| ---------- | ------- | -------------------------------- |
| Int8Array | fast-min | **0.1** |
| Int8Array | lodash | 20.7 |
| Int8Array | underscore | 14.9 |
| Uint8Array | fast-min | **11.5** |
| Uint8Array | lodash | 22.1 |
| Uint8Array | underscore | 12.6 |
| Int16Array | fast-min | **0.6** |
| Int16Array | lodash | 21.9 |
| Int16Array | underscore | 12.4 |
| Uint16Array | fast-min | **12.2** |
| Uint16Array | lodash | 21.2 |
| Uint16Array | underscore | 12.5 |
| Int32Array | fast-min | **13.1** |
| Int32Array | lodash | 20.6 |
| Int32Array | underscore | 12.6 |
| Uint32Array | fast-min | **12.7** |
| Uint32Array | lodash | 66.6 |
| Uint32Array | underscore | 15.3 |
| BigInt64Array | fast-min | **247.6** |
| BigInt64Array | lodash | 253 |
| BigInt64Array | underscore | 241.6 |
| BigUint64Array | fast-min | **194.5** |
| BigUint64Array | lodash | 206.1 |
| BigUint64Array | underscore | 205.4 |
| Int8Array | lodash | 23.2 |
| Int8Array | underscore | 10.3 |
| Uint8Array | fast-min | **< 1** |
| Uint8Array | lodash | 23.2 |
| Uint8Array | underscore | 10.3 |
| Int16Array | fast-min | **0.4** |
| Int16Array | lodash | 23.5 |
| Int16Array | underscore | 10.4 |
| Uint16Array | fast-min | **0.8** |
| Uint16Array | lodash | 23.5 |
| Uint16Array | underscore | 10.6 |
| Int32Array | fast-min | **65.6** |
| Int32Array | lodash | 23.5 |
| Int32Array | underscore | 11 |
| Uint32Array | fast-min | **139.7** |
| Uint32Array | lodash | 27.2 |
| Uint32Array | underscore | 16.4 |
| BigInt64Array | fast-min | **56.1** |
| BigInt64Array | lodash | 78.8 |
| BigInt64Array | underscore | 63.9 |
| BigUint64Array | fast-min | **55.9** |
| BigUint64Array | lodash | 122.1 |
| BigUint64Array | underscore | 114.9 |
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function fastMin(
numbers: ARRAY_TYPE,
options?: {
debug?: boolean | undefined;
no_data?: number | undefined;
theoretical_max?: number | undefined;
no_data?: number[] | number | readonly number[] | undefined;
theoretical_min?: number | undefined;
}
): number;
93 changes: 24 additions & 69 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,88 +8,43 @@ function fastMin(
theoretical_min: undefined
}
) {
if (debug)
console.log("[fast-min] starting with numbers:", numbers.slice(0, 10));
if (debug) console.log("[fast-min] starting with numbers:", numbers.slice(0, 10));

if (!numbers.length) {
if (debug)
console.error(
"[fast-min] Instead of an array of numbers, you passed in",
numbers
);
if (debug) console.error("[fast-min] Instead of an array of numbers, you passed in", numbers);
throw new Error("[fast-min] You didn't pass in an array of numbers");
}
if (numbers.length === 0)
throw new Error("[fast-min] You passed in an empty array");
if (numbers.length === 0) throw new Error("[fast-min] You passed in an empty array");

if (Array.isArray(no_data) === false) {
if (typeof no_data === "number") {
no_data = [no_data];
} else {
no_data = [];
}
}

let min;
const length = numbers.length;

if (debug) console.log("[fast-min] constructor:", numbers.constructor.name);

if (theoretical_min === undefined)
if (theoretical_min === undefined || theoretical_min === null)
theoretical_min = getTheoreticalMin(numbers.constructor.name);
if (debug) console.log("[fast-min] theoretical minimunm is", theoretical_min);
if (theoretical_min) {
if (no_data !== undefined) {
min = Infinity;
for (let i = 0; i < length; i++) {
const value = numbers[i];
if (value < min && value !== no_data) {
min = value;
if (value === theoretical_min) {
if (debug)
console.log(
"[fast-min] found minimum value of " +
value +
" at index " +
i +
" of " +
length
);
break;
}
}
}
if (min === Infinity) min = undefined;
} else {
min = numbers[0];
for (let i = 1; i < length; i++) {
const value = numbers[i];
if (value < min) {
min = value;
if (value === theoretical_min) {
if (debug)
console.log(
"[fast-min] found minimum value of " +
value +
" at index " +
i +
" of " +
length
);
break;
}
}
}
}
} else {
if (no_data !== undefined) {
min = Infinity;
for (let i = 1; i < length; i++) {
const value = numbers[i];
if (value < min && value !== no_data) {
min = value;
}

for (let i = 0; i < length; i++) {
const value = numbers[i];
if (typeof value === "number" && value === value && no_data.indexOf(value) === -1) {
if (typeof min === "undefined") {
min = value;
} else if (value < min) {
min = value;
}
if (min === Infinity) min = undefined;
} else {
min = numbers[0];
for (let i = 1; i < length; i++) {
const value = numbers[i];
if (value < min) {
min = value;
}
if (typeof theoretical_min === "number" && value <= theoretical_min) {
if (debug) console.log("[fast-min] found minimum value of " + value + " at index " + i + " of " + length);
min = value;
break;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"scripts": {
"f": "npm run format",
"format": "npx prettier --arrow-parens=avoid --trailing-comma=none --write *.js *.ts",
"format": "npx prettier --arrow-parens=avoid --print-width=120 --trailing-comma=none --write *.js *.ts",
"setup": "node setup.js",
"perf": "./perf",
"test": "npm run test:js && npm run test:ts",
Expand Down Expand Up @@ -43,9 +43,9 @@
},
"homepage": "https://github.com/DanielJDufour/fast-min#readme",
"devDependencies": {
"flug": "^2.3.1",
"flug": "^2.7.2",
"lodash.min": "^4.0.1",
"underscore": "^1.13.4"
"underscore": "^1.13.6"
},
"dependencies": {
"typed-array-ranges": "^0.0.0"
Expand Down
4 changes: 1 addition & 3 deletions perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ const ARRAY_CONSTRUCTORS = {
};

const numbers = ARRAY_CONSTRUCTORS[array_type].from(
JSON.parse(
fs.readFileSync(array_type.toLowerCase() + "-numbers.json", "utf-8")
)
JSON.parse(fs.readFileSync(array_type.toLowerCase() + "-numbers.json", "utf-8"))
);

const times = [];
Expand Down
6 changes: 1 addition & 5 deletions setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ const getRandomNums = (nbits, signed) => {
{ name: "biguint64", bits: 64, signed: false }
].forEach(({ name, bits, signed }) => {
const filename = name + "-numbers.json";
fs.writeFileSync(
filename,
JSON.stringify(getRandomNums(bits, signed)),
"utf-8"
);
fs.writeFileSync(filename, JSON.stringify(getRandomNums(bits, signed)), "utf-8");
console.log("wrote " + filename);
});
47 changes: 25 additions & 22 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
const fs = require("fs");
const test = require("flug");
const min = require("./index");
const fastMin = require("./index");

test("gettings minimum from a normal array", ({ eq }) => {
const numbers = [920, -550, 340, 690, 550, -340, 840, 700, 550, 210, 540];
const result = min(numbers, { debug: false });
const result = fastMin(numbers, { debug: false });
eq(result, -550);
});

test("getting minimum from an array of image band values", ({ eq }) => {
const numbers = Uint8Array.from(
JSON.parse(fs.readFileSync("uint8-numbers.json", "utf-8"))
);
const numbers = Uint8Array.from(JSON.parse(fs.readFileSync("uint8-numbers.json", "utf-8")));
console.log("loaded uint8 numbers of length:", numbers.length);
const result = min(numbers, { debug: true });
const result = fastMin(numbers, { debug: true });
eq(result, 0);
});

Expand All @@ -24,41 +22,46 @@ test("getting minimum from typed arrays", ({ eq }) => {
[Int16Array, -32768],
[Uint16Array, 0]
].forEach(([array_type, expected_min]) => {
const filename =
array_type.name.replace("Array", "").toLowerCase() + "-numbers.json";
const numbers = array_type.from(
JSON.parse(fs.readFileSync(filename, "utf-8"))
);
const result = min(numbers, { debug: true });
const filename = array_type.name.replace("Array", "").toLowerCase() + "-numbers.json";
const numbers = array_type.from(JSON.parse(fs.readFileSync(filename, "utf-8")));
const result = fastMin(numbers, { debug: true });
eq(result, expected_min);
});
});

/// new
test("getting no minimum from normal arrays with all no data values", ({
eq
}) => {
test("getting no minimum from normal arrays with all no data values", ({ eq }) => {
const numbers = [99, 99, 99, 99];
const result = min(numbers, { no_data: 99 });
const result = fastMin(numbers, { no_data: 99 });
eq(result, undefined);
});

test("getting minimum from normal arrays with some data values", ({ eq }) => {
const numbers = [1, 99, 2, 99, 4, 99, 6, 99, -10];
const result = min(numbers, { no_data: 99 });
const result = fastMin(numbers, { no_data: 99 });
eq(result, -10);
});

test("getting no minimum from typed arrays with all no data values", ({
eq
}) => {
test("getting no minimum from typed arrays with all no data values", ({ eq }) => {
const numbers = Uint8Array.from([99, 99, 99, 99]);
const result = min(numbers, { no_data: 99 });
const result = fastMin(numbers, { no_data: 99 });
eq(result, undefined);
});

test("getting minimum from typed arrays with some data values", ({ eq }) => {
const numbers = Int8Array.from([1, 99, 2, 99, 4, 99, 6, 99, -10]);
const result = min(numbers, { no_data: 99 });
const result = fastMin(numbers, { no_data: 99 });
eq(result, -10);
});

test("multiple no data values", ({ eq }) => {
const numbers = Int8Array.from([1, 99, 2, 99, 4, 99, 6, 99, -10]);
const result = fastMin(numbers, { no_data: [-10, 99] });
eq(result, 1);
});

test("multiple no data values", ({ eq }) => {
const numbers = [NaN, 1, 99, 2, 99, 4, 99, 6, 99, -10, null, undefined];
const result = fastMin(numbers, { no_data: [-10, 99] });
eq(result, 1);
});
27 changes: 12 additions & 15 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ test("gettings minimum from a normal array", ({ eq }) => {
});

test("getting minimum from an array of image band values", ({ eq }) => {
const numbers = Uint8Array.from(
JSON.parse(readFileSync("uint8-numbers.json", "utf-8"))
);
const numbers = Uint8Array.from(JSON.parse(readFileSync("uint8-numbers.json", "utf-8")));
console.log("loaded uint8 numbers of length:", numbers.length);
const result = fastMin(numbers, { debug: true });
eq(result, 0);
Expand All @@ -24,22 +22,17 @@ test("getting minimum from typed arrays", ({ eq }) => {
[Int16Array, -32768] as const,
[Uint16Array, 0] as const
].forEach(([array_type, expected_min]) => {
const filename =
array_type.name.replace("Array", "").toLowerCase() + "-numbers.json";
const numbers = array_type.from(
JSON.parse(readFileSync(filename, "utf-8"))
);
const filename = array_type.name.replace("Array", "").toLowerCase() + "-numbers.json";
const numbers = array_type.from(JSON.parse(readFileSync(filename, "utf-8")));
const result = fastMin(numbers, { debug: true });
eq(result, expected_min);
});
});

/// new
test("getting no minimum from normal arrays with all no data values", ({
eq
}) => {
test("getting no minimum from normal arrays with all no data values", ({ eq }) => {
const numbers = [99, 99, 99, 99];
const result = fastMin(numbers, { no_data: 99 });
const result = fastMin(numbers, { debug: true, no_data: 99 });
eq(result, undefined);
});

Expand All @@ -49,9 +42,7 @@ test("getting minimum from normal arrays with some data values", ({ eq }) => {
eq(result, -10);
});

test("getting no minimum from typed arrays with all no data values", ({
eq
}) => {
test("getting no minimum from typed arrays with all no data values", ({ eq }) => {
const numbers = Uint8Array.from([99, 99, 99, 99]);
const result = fastMin(numbers, { no_data: 99 });
eq(result, undefined);
Expand All @@ -62,3 +53,9 @@ test("getting minimum from typed arrays with some data values", ({ eq }) => {
const result = fastMin(numbers, { no_data: 99 });
eq(result, -10);
});

test("multiple no data values", ({ eq }) => {
const numbers = [1, 99, 2, 99, 4, 99, 6, 99, -10];
const result = fastMin(numbers, { no_data: [-10, 99] as const });
eq(result, 1);
});

0 comments on commit 81373ed

Please sign in to comment.