swirlspy.ver package

Submodules

swirlspy.ver.crosstab module

swirlspy.ver.crosstab.contingency(threshold, forecast, observed)

Generates contingency table statistics for traditional binary verification

Parameters:
  • threshold (float) – Threshold value of variable for verification.
  • forecast (xarray) – An xarray containing forecasted values of variable
  • observed (xarray) – An xarray containing observed values of variable
Returns:

contingency – tuple structure is (hit, miss, false_alarm, corrneg)

Return type:

tuple

Notes

hit: int
number of hits
miss: int
number of misses
false_alarm: int
number of false alarms
corrneg: int
number of correct negatives

swirlspy.ver.metric module

swirlspy.ver.metric.FSS(threshold, forecast, observed, winsize)

Generates FSS

Parameters:
  • threshold (float) – Threshold value of variable for verification.
  • forecast (numpy array) – An xarray containing forecasted values of variable
  • observed (numpy array) – An xarray containing observed values of variable
  • winsize (int) – Window size
Returns:

fss – FSS score

Return type:

float

swirlspy.ver.metric.accuracy(cont)

Calculates accuracy

Parameters:cont (tuple) – contingency tuple (refer to swirlspy.ver.crosstab)
Returns:accuracy – accuracy = (hit+corrneg)/total and total = hit+miss+false_alarm+corrneg
Return type:float
swirlspy.ver.metric.brier_skill_score(forecast, observation)

Function to calculate the Brier Skill Score. This function is a wrapper about sklearn.metrics.brier_score_loss.

Parameters:
  • forecast (xarray.DataArray) – xarray populated with forecasted probabilities ~ (0, 1).
  • observation (xarray.DataArray) – xarray containing binary observation data.
Returns:

bss – Brier skill score.

Return type:

float

swirlspy.ver.metric.csi(cont)

Calculates Critical Success Index

Parameters:cont (tuple) – contingency tuple (refer to swirlspy.ver.crosstab)
Returns:critical success index – csi = hit/(hit+false_alarm+miss)
Return type:float
swirlspy.ver.metric.ets(cont)

Returns Equitable Threat Score[0 = no skill][1 = perfect]

Parameters:cont (tuple) – contingency tuple (refer to swirlspy.ver.crosstab)
Returns:equitable threat score – ets = (hit - hit_expected)/(hit+miss+false_alarm-hit_expected) where hit_expected = hit+miss)*(hit+false_alarm)/total and total = hit + miss + false_alarm + corrneg
Return type:float
swirlspy.ver.metric.far(cont)

Calculates False Alarm Ratio

Parameters:cont (tuple) – contingency tuple (refer to swirlspy.ver.crosstab)
Returns:false alarm ratio – far = false_alarm/(hit+false_alarm)
Return type:float
swirlspy.ver.metric.freq_bias(cont)

Calculates Frequency Bias

Parameters:cont (tuple) – contingency tuple (refer to swirlspy.ver.crosstab)
Returns:frequency bias – freq_bias = (hit+false_alarm)/(hit+miss)
Return type:float or int
swirlspy.ver.metric.hss(cont)

Calculates Heidke Skill Score [0 = no skill][1=perfect]

Parameters:cont (tuple) – contingency tuple (refer to swirlspy.ver.crosstab)
Returns:Heidke Success Score – hss = (hit+corrneg-hit_corrneg_expected)/(total-hit_corrneg_expected) and hit_corrneg_expected = ((hit+miss)*(hit+false_alarm) (false_alarm+corrneg) * (miss+corrneg)) / total
Return type:int or float
swirlspy.ver.metric.pod(cont)

Calculates Probability of Detection

Parameters:cont (tuple) – contingency tuple (refer to swirlspy.ver.crosstab)
Returns:probability of detection – pod = hit/(hit+miss)
Return type:float
swirlspy.ver.metric.pofd(cont)

Calculates Probability of False Detection

Parameters:cont (tuple) – contingency tuple (refer to swirlspy.ver.crosstab)
Returns:probability of false detection – pofd = false_alarm/(false_alarm + corrneg)
Return type:float
swirlspy.ver.metric.reliability(forecast, observation, normalize=False, n_bins=5, strategy='uniform')

Function to generate data required for the plotting of reliability diagrams. Forecast and observation xarrays are considered to be geographically similar. This function is a wrapper around sklearn.calibration.calibration_curve.

Parameters:
  • forecast (xarray.DataArray) – xarray populated with forecasted probabilities ~ (0, 1).
  • observation (xarray.DataArray) – xarray containing binary observation data.
  • normalize (bool) – Whether y_prob needs to be normalized into the bin [0, 1], i.e. is not a proper probability. If True, the smallest value in y_prob is mapped onto 0 and the largest one onto 1. Defaults to False.
  • n_bins (int) – Number of bins. A bigger number requires more data. Bins with no data points (i.e. without corresponding values in y_prob) will not be returned, thus there may be fewer than n_bins in the return value.
  • strategy (str) – Strategy used to define the widths of the bins. Options are ‘uniform’ and ‘quantile’. Defaults to ‘uniform’.
Returns:

reliability_data – Contains data useful for plotting reliability diagrams. Data vars are: observed_rf (observed relative frequency) and nforecasts (number of forecasts). Climatology is included as an attribute.

Return type:

xarray.Dataset

swirlspy.ver.metric.roc(forecast, observation, average=None)

Function to compute Receiver Operating Characteristic. This function is a wrapper around sklearn.metrics.roc_curve and sklearn.metrics.roc_auc_score.

Parameters:
  • forecast (xarray.DataArray) – xarray populated with forecasted probabilities ~ (0, 1).
  • observation (xarray.DataArray) – xarray containing binary observation data.
  • average (string, [None, ‘micro’, ‘macro’ (default), ‘samples’, ‘weighted’]) –

    If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:

    ’micro’:
    Calculate metrics globally by considering each element of the label indicator matrix as a label.
    ’macro’:
    Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
    ’weighted’:
    Calculate metrics for each label, and find their average, weighted by support (the number of true instances for each label).
    ’samples’:
    Calculate metrics for each instance, and find their average.

    Will be ignored when observation is binary.

Returns:

roc_data – Contains Probability of Detection and Probability of False Detection as keys “pod” and “pofd” required for plotting ROC curves. Area under the ROC curve and thresholds are included as keys “auc” and “thresholds” respectively.

Return type:

dict