mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
feat(environment): register env signals and wire weather into correlation discovery
Register the seven environmental-exposure signals through the signal registry as a new "environment" kind (correlation-eligible; off Coach/MCP/FHIR in v1), and fold their daily series into the correlation engine as lagged behaviour channels. Weather/daylight now pair against mood, sleep, and vitals through the existing FDR / effect-size / shrinkage gates, so a thin series degrades to absent rather than a spurious link. All env channels share one ENVIRONMENT family so two same-day fields never lag-correlate against each other.
This commit is contained in:
1 parent
76dc03ae7f
commit
cdee3e3efc
10 files changed
+265
-12
No files matched your search
@@ -24,11 +24,13 @@ import {
|
||||
import {
|
||||
MEDICATION_COMPLIANCE_CHANNEL_KEY,
|
||||
SYMPTOM_SEVERITY_CHANNEL_KEY,
|
||||
type DailySeriesPoint,
|
||||
type LabDrawPoint,
|
||||
type NamedSeries,
|
||||
} from "@/lib/insights/correlation-discovery";
|
||||
import { resolveLabFields } from "@/lib/labs/serialise";
|
||||
import { wallClockInTz } from "@/lib/tz/wall-clock";
|
||||
import { ENVIRONMENT_FIELDS } from "@/lib/environment/fields";
|
||||
import {
|
||||
buildComplianceDailySeries,
|
||||
buildSymptomSeverityDailySeries,
|
||||
@@ -183,6 +185,59 @@ export async function fetchSymptomSeries(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* v1.25 (W-ENV) — build the user's environmental-exposure BEHAVIOUR channels.
|
||||
*
|
||||
* One {@link NamedSeries} per registered env field (temperature, daylight,
|
||||
* sunshine, precipitation, pressure mean + intraday swing), read from the daily
|
||||
* `EnvironmentContext` rows the nightly job stores. Each row's `date` is already
|
||||
* a YYYY-MM-DD key, so points need no re-keying. Sunshine / daylight are stored
|
||||
* in seconds and surfaced as hours (correlation r is scale-invariant; hours just
|
||||
* keep the series readable). A field with no non-null values yields an empty
|
||||
* series that degrades to absent — never a fabricated constant. The whole set is
|
||||
* empty when the user has no environment rows (module off / no home set).
|
||||
*/
|
||||
export async function fetchEnvironmentSeries(
|
||||
userId: string,
|
||||
since: Date,
|
||||
): Promise<NamedSeries[]> {
|
||||
const sinceKey = since.toISOString().slice(0, 10);
|
||||
const rows = await prisma.environmentContext.findMany({
|
||||
where: { userId, date: { gte: sinceKey } },
|
||||
orderBy: { date: "asc" },
|
||||
take: 1000,
|
||||
select: {
|
||||
date: true,
|
||||
tempMean: true,
|
||||
tempMin: true,
|
||||
tempMax: true,
|
||||
apparentMean: true,
|
||||
sunshineSec: true,
|
||||
daylightSec: true,
|
||||
precipSum: true,
|
||||
pressureMean: true,
|
||||
pressureDelta: true,
|
||||
humidityMean: true,
|
||||
cloudMean: true,
|
||||
},
|
||||
});
|
||||
|
||||
return ENVIRONMENT_FIELDS.map((field) => {
|
||||
const points: DailySeriesPoint[] = [];
|
||||
for (const row of rows) {
|
||||
const raw = row[field.column];
|
||||
if (raw == null || !Number.isFinite(raw)) continue;
|
||||
// Seconds → hours for the duration fields; pass through otherwise.
|
||||
const value =
|
||||
field.column === "sunshineSec" || field.column === "daylightSec"
|
||||
? raw / 3600
|
||||
: raw;
|
||||
points.push({ day: row.date, value });
|
||||
}
|
||||
return { key: field.key, role: "behaviour" as const, points };
|
||||
});
|
||||
}
|
||||
|
||||
/** Day key (YYYY-MM-DD) for an instant in the user's display timezone. */
|
||||
function tzDayKey(at: Date, tz: string): string {
|
||||
const { year, month, day } = wallClockInTz(at, tz);
|
||||
|
||||
@@ -39,6 +39,10 @@
|
||||
* link. The series builders live in `correlation-series-builders.ts`.
|
||||
*/
|
||||
import { pearson, MIN_PAIRED_N } from "@/lib/insights/correlations";
|
||||
import {
|
||||
getEnvironmentField,
|
||||
isEnvironmentChannelKey,
|
||||
} from "@/lib/environment/fields";
|
||||
|
||||
/** Default Benjamini-Hochberg target false-discovery rate. */
|
||||
export const FDR_Q = 0.1;
|
||||
@@ -102,6 +106,12 @@ export function metricFamily(key: string): string {
|
||||
// tautology as MOOD→MOOD and is excluded too.
|
||||
if (key.startsWith(FACTOR_CHANNEL_PREFIX) || key === "MOOD") return "MOOD";
|
||||
if (key.startsWith("BLOOD_PRESSURE")) return "BLOOD_PRESSURE";
|
||||
// v1.25 (W-ENV) — every environmental-exposure channel (ENV_TEMP_MEAN,
|
||||
// ENV_DAYLIGHT, …) shares ONE family so the same-family guard never
|
||||
// lag-correlates two same-day weather fields against each other (a
|
||||
// near-tautology). They stay free to pair cross-domain against mood / sleep /
|
||||
// vitals — the whole point of the module.
|
||||
if (key.startsWith("ENV_")) return "ENVIRONMENT";
|
||||
// v1.21.0 (FDREXTEND) — medication compliance and symptom severity each form
|
||||
// their OWN single-channel family, so the loop's same-family guard collapses
|
||||
// only the self-lag (compliance→compliance, symptom→symptom). The returned
|
||||
@@ -312,6 +322,11 @@ function humanise(key: string): string {
|
||||
if (key.startsWith("FACTOR:")) {
|
||||
return `rated ${key.slice("FACTOR:".length).replace(/[_-]/g, " ").toLowerCase()}`;
|
||||
}
|
||||
// v1.25 (W-ENV) — env channels read with their descriptive label ("daily
|
||||
// temperature", "daylight") rather than the raw "env temp mean" key.
|
||||
if (isEnvironmentChannelKey(key)) {
|
||||
return getEnvironmentField(key)?.narrationLabel ?? key.toLowerCase();
|
||||
}
|
||||
return key.replace(/_/g, " ").toLowerCase();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user