-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: changed 'avg' to 'average'; finished unit tests
- Loading branch information
1 parent
91567ad
commit 06cf426
Showing
9 changed files
with
158 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { test } from "uvu"; | ||
import * as assert from "uvu/assert"; | ||
|
||
import moment from "moment-timezone"; | ||
import { dailyAverage } from "../index.js"; | ||
|
||
// Start of Valentine's Day in Greenwich | ||
let start = moment.tz("2023-02-14 00:00:00", "UTC"); | ||
let datetime = []; | ||
let x = []; | ||
|
||
// Precisely 10 days worth of data | ||
let day = 0; | ||
for (var i = 0; i < 240; i++) { | ||
if (i % 24 === 0) day++; | ||
datetime[i] = new Date(start + i * 3600 * 1000); | ||
let val = day * 10 + 5 * Math.sin((i * Math.PI) / 12); | ||
x[i] = Math.round(val * 10) / 10; | ||
} | ||
|
||
test("daily averages work for 'UTC'", () => { | ||
let timezone = "UTC"; | ||
let daily = dailyAverage(datetime, x, timezone); | ||
let day0 = new Date(moment.tz("2023-02-14 00:00:00", "UTC")); | ||
let day1 = new Date(moment.tz("2023-02-15 00:00:00", "UTC")); | ||
assert.equal(daily.datetime.slice(0, 2), [day0, day1]); | ||
assert.equal(daily.average.slice(0, 2), [10, 20]); | ||
}); | ||
|
||
test("daily averages work for 'America/Chicago'", () => { | ||
let timezone = "America/Chicago"; | ||
let daily = dailyAverage(datetime, x, timezone); | ||
let day0 = new Date(moment.tz("2023-02-14 06:00:00", "UTC")); | ||
let day1 = new Date(moment.tz("2023-02-15 06:00:00", "UTC")); | ||
assert.equal(daily.datetime.slice(0, 2), [day0, day1]); | ||
assert.equal(daily.average.slice(0, 2), [12.5, 22.5]); | ||
}); | ||
|
||
// ----- Run all tests --------------------------------------------------------- | ||
|
||
test.run(); | ||
|
||
// ----- Notes ----------------------------------------------------------------- | ||
|
||
// // Manual testing for "America/Chicago" | ||
// x.slice(6,30).reduce((a,o) => a + o) / 24 | ||
// // 12.5 | ||
// x.slice(30,54).reduce((a,o) => a + o) / 24 | ||
// // 22.5 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { test } from "uvu"; | ||
import * as assert from "uvu/assert"; | ||
|
||
import moment from "moment-timezone"; | ||
import { diurnalAverage } from "../index.js"; | ||
|
||
// Start of Valentine's Day in Greenwich | ||
let start = moment.tz("2023-02-14 00:00:00", "UTC"); | ||
let datetime = []; | ||
let x = []; | ||
|
||
// Precisely 10 days worth of data | ||
let day = 0; | ||
for (var i = 0; i < 240; i++) { | ||
if (i % 24 === 0) day++; | ||
datetime[i] = new Date(start + i * 3600 * 1000); | ||
let val = 10 + 5 * Math.sin((i * Math.PI) / 12); | ||
x[i] = Math.round(val * 10) / 10; | ||
} | ||
|
||
let hours = [ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, | ||
22, 23, | ||
]; | ||
|
||
test("diurnal averages work for 'UTC'", () => { | ||
let timezone = "UTC"; | ||
let diurnal = diurnalAverage(datetime, x, timezone); | ||
let averages = [ | ||
10, 11.3, 12.5, 13.5, 14.3, 14.8, 15, 14.8, 14.3, 13.5, 12.5, 11.3, 10, 8.7, | ||
7.5, 6.5, 5.7, 5.2, 5, 5.2, 5.7, 6.5, 7.5, 8.7, | ||
]; | ||
assert.equal(diurnal.hour, hours); | ||
assert.equal(diurnal.averages); | ||
}); | ||
|
||
test("diurnal averages work for 'America/Chicago'", () => { | ||
let timezone = "America/Chicago"; | ||
let diurnal = diurnalAverage(datetime, x, timezone); | ||
let averages = [ | ||
15, 14.8, 14.3, 13.5, 12.5, 11.3, 10, 8.7, 7.5, 6.5, 5.7, 5.2, 5, 5.2, 5.7, | ||
6.5, 7.5, 8.7, 10, 11.3, 12.5, 13.5, 14.3, 14.8, | ||
]; | ||
assert.equal(diurnal.hour, hours); | ||
assert.equal(diurnal.averages); | ||
}); | ||
|
||
// ----- Run all tests --------------------------------------------------------- | ||
|
||
test.run(); | ||
|
||
// ----- Notes ----------------------------------------------------------------- | ||
|
||
// Manual testing for "America/Chicago" | ||
|
||
// x.slice(0,24) | ||
// // (24) [10, 11.3, 12.5, 13.5, 14.3, 14.8, 15, 14.8, 14.3, 13.5, 12.5, 11.3, 10, 8.7, 7.5, 6.5, 5.7, 5.2, 5, 5.2, 5.7, 6.5, 7.5, 8.7] | ||
// x.slice(6,30) | ||
// // (24) [15, 14.8, 14.3, 13.5, 12.5, 11.3, 10, 8.7, 7.5, 6.5, 5.7, 5.2, 5, 5.2, 5.7, 6.5, 7.5, 8.7, 10, 11.3, 12.5, 13.5, 14.3, 14.8] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { test } from "uvu"; | ||
import * as assert from "uvu/assert"; | ||
|
||
import { pm_nowcast } from "../index.js"; | ||
|
||
test("nowcast works properly", () => { | ||
// Data from: https://forum.airnowtech.org/t/the-nowcast-for-pm2-5-and-pm10/172 | ||
// Answer should be 28.4 ug/m3 or 85 AQI | ||
let pm25 = [34.9, 43, 50, 64.9, 69.2, 66.2, 53.7, 48.6, 49.2, 35, null, 21]; | ||
let nowcast = pm_nowcast(pm25); | ||
assert.is(nowcast.length, pm25.length); | ||
assert.is(nowcast[0], null); | ||
assert.is(nowcast[11], 28.4); | ||
}); | ||
|
||
test("nowcast handles missing values properly", () => { | ||
let pm25 = [34.9, 43, 50, 64.9, 69.2, 66.2, 53.7, 48.6, 49.2, 35, null, null]; | ||
assert.is(pm_nowcast(pm25)[11], null); | ||
}); | ||
|
||
test.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters