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

Data quirks #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 4 additions & 39 deletions src/config/assets/data-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,9 @@ import { dataLoaderManager } from '@/data-loader-manager';

import { HAZARD_TYPES, HazardType } from '../hazards/metadata';

/**
* handle quirks/inconsistencies in feature property naming for hazards
*/
function lookupHazard(hazard: string) {
if (hazard === 'fluvial') return 'river';
return hazard;
}

/**
* handle quirks/inconsistencies in feature property naming for RCP
*/
function lookupRcp(rcp: string, layer: string) {
if (rcp == null) return rcp;
if (rcp === 'baseline') {
if (layer.startsWith('road_') || layer.startsWith('rail_')) {
return 'historical';
}
return rcp;
}
return `rcp${rcp.replace(/\./g, 'p')}`;
}

/**
* handle quirks/inconsistencies in feature property naming for epoch
*/
function lookupEpoch(epoch: string, hazard: HazardType, layer: string) {
if (epoch === 'present') {
if (hazard === 'cyclone') {
return '2020';
}
if (layer.startsWith('road_') || layer.startsWith('rail_')) {
return '1980';
}
}
return epoch;
/* replace e.g. 4.5 with 4p5 */
function sanitiseRcp(rcp: string) {
return rcp?.replace(/\./g, 'p');
}

/**
Expand All @@ -55,10 +23,7 @@ function getExpectedDamageKey(
rcp: string,
epoch: string,
) {
return `${direct ? 'ead' : 'eael'}__${lookupHazard(hazard)}__rcp_${lookupRcp(
rcp,
layer,
)}__epoch_${lookupEpoch(epoch, hazard, layer)}`;
return `${direct ? 'ead' : 'eael'}__${hazard}__rcp_${sanitiseRcp(rcp)}__epoch_${epoch}`;
}

/**
Expand Down
142 changes: 0 additions & 142 deletions src/config/hazards/domain-data/drought.json

This file was deleted.

12 changes: 4 additions & 8 deletions src/config/hazards/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const HAZARD_DOMAINS_CONFIG: Record<HazardType, HazardDomainConfig> = {
defaults: {
rp: 100,
rcp: 'baseline',
epoch: 'present',
epoch: 'baseline',
gcm: 'MIROC-ESM-CHEM',
},
dependencies: {
Expand All @@ -28,7 +28,7 @@ export const HAZARD_DOMAINS_CONFIG: Record<HazardType, HazardDomainConfig> = {
coastal: {
defaults: {
rp: 100,
epoch: 'present',
epoch: 'baseline',
rcp: 'baseline',
gcm: 'None',
},
Expand All @@ -40,11 +40,7 @@ export const HAZARD_DOMAINS_CONFIG: Record<HazardType, HazardDomainConfig> = {
defaults: {
rp: 10,
gcm: 'constant',
/**
* epoch and rcp for cyclones are added programmatically upon load
* adjust custom code for data loading when these fields are added to the backend
*/
epoch: 'present',
epoch: 'baseline',
rcp: 'baseline',
},
dependencies: {
Expand All @@ -55,7 +51,7 @@ export const HAZARD_DOMAINS_CONFIG: Record<HazardType, HazardDomainConfig> = {
cyclone_iris: {
defaults: {
rp: 10,
epoch: 2020,
epoch: 'baseline',
ssp: 'constant',
},
dependencies: {
Expand Down
10 changes: 6 additions & 4 deletions src/config/hazards/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ export function getHazardDataPath({ hazardType, hazardParams, metric }: HazardSo
const { rp, ssp, epoch } = hazardParams;
path = `${hazardType}/${epoch}/${rp}/${ssp}`;
} else {
const { rp, rcp, epoch, gcm } = hazardParams;
const sanitisedRcp = rcp?.replace('.', 'x');
let { rp, rcp, epoch, gcm } = hazardParams;

// for the raster URL, RCP 4.5 maps to 4x5 (differently than in feature properties, where it's mapped to 4p5)
rcp = rcp?.replace('.', 'x');

if (hazardType === 'cyclone') {
path = `${hazardType}/${rp}/${gcm}`;
} else if (['extreme_heat', 'drought'].includes(hazardType)) {
path = `${hazardType}/${metric}/${sanitisedRcp}/${epoch}/${gcm}`;
path = `${hazardType}/${metric}/${rcp}/${epoch}/${gcm}`;
} else {
path = `${hazardType}/${rp}/${sanitisedRcp}/${epoch}/${gcm}`;
path = `${hazardType}/${rp}/${rcp}/${epoch}/${gcm}`;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/config/regional-risk/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ValueLabel } from '@/lib/controls/params/value-label';

// TODO: quirks - field names contain "historical" and "1980"
export const REGIONAL_EXPOSURE_VARIABLES = [
'pop_exposed_seismic_threshold0.1g',
'pop_exposed_seismic_threshold0.2g',
Expand Down
13 changes: 4 additions & 9 deletions src/details/features/damages/DamagesSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@ export const DamagesSection = ({ fd }) => {
);
};

export const QUIRKY_FIELDS_MAPPING = {
hazard: (h: string) => (h === 'river' ? 'fluvial' : h),
epoch: (e: string) => (e === '1980' ? 'present' : e),
rcp: (r: string) => {
if (r === 'historical') return 'baseline';
if (r.startsWith('rcp')) return r.substring(3).replace('p', '.'); // rcp4p5 -> 4.5
return r;
},
};
export function processRcpIncoming(rcp) {
if (rcp.match(/\dp\d/)) return rcp.replace('p', '.');

return rcp;
}
export function orderDamages<K, T extends K>(
damages: T[],
ordering: K[],
Expand Down
3 changes: 2 additions & 1 deletion src/details/features/damages/ExpectedDamageChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const makeSpec = (

// need to map special value to year to maintain chronological ordering on the X axis
function prepareEpoch(epoch: string) {
return epoch === 'present' ? '2020' : epoch;
// TODO: this only works with the assumption that 2020 is lower than all the other epochs
return epoch === 'baseline' ? '2020' : epoch;
}

interface ExpectedDamageChartProps {
Expand Down
25 changes: 10 additions & 15 deletions src/details/features/damages/ExpectedDamagesSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ import {
featureState,
hazardDataParamsState,
orderDamages,
QUIRKY_FIELDS_MAPPING,
processRcpIncoming,
} from './DamagesSection';
import { DamageTable } from './DamageTable';
import { ExpectedDamageChart } from './ExpectedDamageChart';
import { HazardSelect, selectedHazardState } from './param-controls';

function getDamageKey({ hazard, rcp, epoch }) {
return `${hazard}__rcp_${rcp}__epoch_${epoch}__conf_None`;
}

export interface ExpectedDamageCell {
key: string;
hazard: string;
Expand All @@ -33,23 +29,22 @@ export interface ExpectedDamageCell {
eael_amin: number;
eael_amax: number;
}

function getDamageKey({ hazard, rcp, epoch }) {
return `${hazard}__rcp_${rcp}__epoch_${epoch}__conf_None`;
}

function getExpectedDamageObject(d: ExpectedDamage): ExpectedDamageCell {
let { hazard, epoch, rcp } = _.mapValues(
QUIRKY_FIELDS_MAPPING,
(fn, key) => fn?.(d[key].toString()),
);
let { hazard, epoch, rcp } = d;

rcp = processRcpIncoming(rcp);

return {
key: getDamageKey({ hazard, rcp, epoch }),
hazard,
rcp,
epoch,
ead_mean: d.ead_mean,
ead_amin: d.ead_amin,
ead_amax: d.ead_amax,
eael_mean: d.eael_mean,
eael_amin: d.eael_amin,
eael_amax: d.eael_amax,
..._.pick(d, ['ead_mean', 'ead_amin', 'ead_amax', 'eael_mean', 'eael_amin', 'eael_amax']),
};
}

Expand Down
10 changes: 4 additions & 6 deletions src/details/features/damages/RPDamagesSection.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Stack, Typography } from '@mui/material';
import { Box } from '@mui/system';
import { ReturnPeriodDamage } from '@nismod/irv-api-client';
import _ from 'lodash';
import { selector, useRecoilValue } from 'recoil';

import { ButtonPlacement, DownloadButton } from '../DownloadButton';
Expand All @@ -10,7 +9,7 @@ import {
featureState,
hazardDataParamsState,
orderDamages,
QUIRKY_FIELDS_MAPPING,
processRcpIncoming,
} from './DamagesSection';
import { EpochSelect, selectedEpochState, selectedHazardState } from './param-controls';
import { ReturnPeriodDamageChart } from './ReturnPeriodDamageChart';
Expand All @@ -36,10 +35,9 @@ interface RPDamageCell {
}

function getRPDamageObject(d: ReturnPeriodDamage): RPDamageCell {
let { hazard, epoch, rcp } = _.mapValues(
QUIRKY_FIELDS_MAPPING,
(fn, key) => fn?.(d[key].toString()),
);
let { hazard, epoch, rcp } = d;

rcp = processRcpIncoming(rcp);

return {
key: getRPDamageKey({ hazard, epoch, rcp, rp: d.rp }),
Expand Down
2 changes: 1 addition & 1 deletion src/sidebar/ui/params/EpochControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useInputDisabled } from '../../../lib/data-selection/DisabledInput';
import { DataParam } from '../DataParam';

function epochLabel(value) {
if (value === 2010 || value === 'present' || value === 'baseline') return 'Present';
if (value === 'baseline') return 'Baseline';
return value;
}

Expand Down
Loading