Coverage for tests/continuous/isoreg_test_data.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-02-28 12:51 +1100

1"""Test data for `scores.isoreg`.""" 

2 

3from functools import partial 

4 

5import numpy as np 

6import xarray as xr 

7from numpy import nan 

8from scipy import interpolate 

9 

10# _xr_to_np test data 

11 

12FCST_XRTONP = xr.DataArray( 

13 data=[ 

14 [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], 

15 [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], 

16 ], 

17 dims=["stn", "date", "leadday"], 

18 coords={"stn": [1001, 1002], "date": ["01", "02", "03"], "leadday": [1, 2, 3, 4]}, 

19) 

20 

21OBS_XRTONP = xr.DataArray( 

22 data=[ 

23 [[1, 1, 1, 1], [2, 2, 2, 2]], 

24 [[4, 4, 4, 4], [0, 0, 0, 0]], 

25 [[7, 7, 7, 7], [3, 3, 3, 3]], 

26 ], 

27 dims=["date", "stn", "leadday"], 

28 coords={"stn": [1001, 1003], "date": ["01", "02", "03"], "leadday": [1, 2, 3, 4]}, 

29) 

30 

31EXP_FCST_XRTONP = np.array( # dims=["stn", "date", "leadday"] 

32 [ 

33 [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], 

34 [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], 

35 [[nan, nan, nan, nan], [nan, nan, nan, nan], [nan, nan, nan, nan]], 

36 ] 

37) 

38 

39EXP_OBS_XRTONP = np.array( # dims=["stn", "date", "leadday"] 

40 [ 

41 [[1, 1, 1, 1], [4, 4, 4, 4], [7, 7, 7, 7]], 

42 [[nan, nan, nan, nan], [nan, nan, nan, nan], [nan, nan, nan, nan]], 

43 [[2, 2, 2, 2], [0, 0, 0, 0], [3, 3, 3, 3]], 

44 ] 

45) 

46 

47WT_XRTONP = xr.DataArray( 

48 data=[ 

49 [[10], [15], [20]], 

50 [[25], [30], [35]], 

51 ], 

52 dims=["stn", "date", "leadday"], 

53 coords={"stn": [1001, 1002], "date": ["01", "02", "03"], "leadday": [2]}, 

54) 

55 

56EXP_WT_XRTONP = np.array( # dims=["stn", "date", "leadday"] 

57 [ 

58 [[nan, 10, nan, nan], [nan, 15, nan, nan], [nan, 20, nan, nan]], 

59 [[nan, 25, nan, nan], [nan, 30, nan, nan], [nan, 35, nan, nan]], 

60 [[nan, nan, nan, nan], [nan, nan, nan, nan], [nan, nan, nan, nan]], 

61 ] 

62) 

63 

64EXP_XRTONP1 = (EXP_FCST_XRTONP, EXP_OBS_XRTONP, None) 

65 

66EXP_XRTONP2 = (EXP_FCST_XRTONP, EXP_OBS_XRTONP, EXP_WT_XRTONP) 

67 

68 

69OBS_XRTONP2 = xr.DataArray( 

70 data=[[[10], [20], [30]]], 

71 dims=["date", "leadhour", "stn"], 

72 coords={"date": [0], "stn": [1], "leadhour": [1, 2, 3]}, 

73) 

74 

75 

76# _tidy_ir_inputs data 

77 

78FCST_TIDY1 = np.array([[1.0, 2.0, nan, 4.0], [2.0, 5.0, 2.0, 1.0]]) 

79OBS_TIDY1 = np.array([[3.0, 1.0, 6.0, 7.0], [5.0, nan, -1.0, 0.0]]) 

80WEIGHT_TIDY1 = np.array([[1, 2, 3, 4], [5, 6, 7, nan]]) 

81EXP_TIDY1 = ( 

82 np.array([1.0, 1.0, 2.0, 2.0, 2.0, 4.0]), 

83 np.array([3.0, 0.0, 5.0, 1.0, -1.0, 7.0]), 

84 None, 

85) 

86EXP_TIDY2 = ( 

87 np.array([1.0, 2.0, 2.0, 2.0, 4.0]), 

88 np.array([3.0, 5.0, 1.0, -1.0, 7.0]), 

89 np.array([1, 5, 2, 7, 4]), 

90) 

91 

92# data for _contiguous_ir tests 

93 

94Y1 = np.array([1.0, 2, 3, 0, 5, 4, 1, 7]) 

95W1 = np.array([1, 1, 1, 1, 2, 1, 1, 1]) 

96EXP_IR_MEDIAN = np.array([1, 2, 2, 2, 4, 4, 4, 7]) 

97EXP_IR_MEAN = np.array([1, 5 / 3, 5 / 3, 5 / 3, 10 / 3, 10 / 3, 10 / 3, 7]) 

98EXP_IR_WMEAN = np.array([1, 5 / 3, 5 / 3, 5 / 3, 15 / 4, 15 / 4, 15 / 4, 7]) 

99 

100 

101# data and working for bootstrap tests 

102 

103BS_FCST = np.array([0.0, 1, 1, 3, 4, 5]) 

104BS_OBS = np.array([2.0, 3, 1, 0, 2, 6]) 

105BS_WT = np.array([1.0, 1, 1, 2, 1, 1]) 

106 

107# manual working with seed=1 

108# 1st boot sample: selection = [5, 3, 4, 0, 1, 3] 

109# samples then tidied: 

110# fcst_sample = [5, 3, 4, 0, 1, 3] -> [0, 1, 3, 3, 4, 5] 

111# obs_sample = [6, 0, 2, 2, 3, 0] -> [2, 3, 0, 0, 2, 6] 

112# wt_sample = [1, 2, 1, 1, 1, 2] -> [1, 1, 2, 2, 1, 1] 

113# regression output y_reg: [5/6, 5/6, 5/6, 5/6, 2, 6] 

114# interpolated to original fcst values: [5/6, 5/6, 5/6, 5/6, 2, 6] 

115# 2nd boot sample: selection = [5, 0, 0, 1, 4, 5] 

116# samples then tidied: 

117# fcst_sample = [5, 0, 0, 1, 4, 5] -> [0, 0, 1, 4, 5, 5] 

118# obs_sample = [6, 2, 2, 3, 2, 6] -> [2, 2, 3, 2, 6, 6] 

119# wt_sample = [1, 1, 1, 1, 1, 1] -> [1, 1, 1, 1, 1, 1] 

120# regression output y_reg: [2, 2, 5/2, 5/2, 6, 6] 

121# interpolated to original fcst values: [2, 5/2, 5/2, 5/2, 5/2, 6] 

122# 3rd boot sample: selection = [4, 1, 2, 4, 5, 2] 

123# fcst_sample = [4, 1, 1, 4, 5, 1] -> [1, 1, 1, 4, 4, 5] 

124# obs_sample = [2, 3, 1, 2, 6, 1] -> [3, 1, 1, 2, 2, 6] 

125# wt_sample = [1, 1, 1, 1, 1, 1] -> [1, 1, 1, 1, 1, 1] 

126# regression output y_reg: [5/3, 5/3, 5/3, 2, 2, 6] 

127# interpolated to original fcst values: [nan, 5/3, 5/3, 17/9, 2, 6] 

128BS_EXP1 = np.array( 

129 [ 

130 [5 / 6, 5 / 6, 5 / 6, 5 / 6, 2, 6], 

131 [2, 5 / 2, 5 / 2, 5 / 2, 5 / 2, 6], 

132 [nan, 5 / 3, 5 / 3, 17 / 9, 2, 6], # fcst=0 not in selection so gets NaN 

133 ] 

134) 

135 

136# 1st selection yreg = [5/4, 5/4, 5/4, 5/4, 2, 6] 

137# 2nd and 3rd selections same results as BS_EXP1 

138BS_EXP2 = np.array( 

139 [ 

140 [5 / 4, 5 / 4, 5 / 4, 5 / 4, 2, 6], 

141 [2, 5 / 2, 5 / 2, 5 / 2, 5 / 2, 6], 

142 [nan, 5 / 3, 5 / 3, 17 / 9, 2, 6], # fcst=0 not in selection so gets NaN 

143 ] 

144) 

145BS_EXP3 = np.array([[1.0, 1, 1, 1, 2, 6]]) 

146 

147 

148# data for _confidence_band test 

149 

150CB_BOOT_INPUT = np.array( 

151 [ 

152 [nan, 1.0, 1, 1, 6, 8, 9], 

153 [0, 3, 3, 3, 4, 7, 8], 

154 [nan, nan, -1, 3, 4, 6, 8], 

155 [0, 0, 3, 5, 6, 8, 9], 

156 [2, 5, 5, 5, 6, 6, 6], 

157 ] 

158) 

159 

160EXP_CB = ( 

161 np.array([nan, np.quantile([1.0, 3, 0, 5], 0.25), 1, 3, 4, 6, 8]), 

162 np.array([nan, np.quantile([1.0, 3, 0, 5], 0.75), 3, 5, 6, 8, 9]), 

163) 

164 

165 

166EXP_CB2 = { 

167 0.75: np.array([1, 3.5, 3, 5, 6, 8, 9]), 

168 0.25: np.array([0, 0.75, 1, 3, 4, 6, 8]), 

169} 

170 

171# data for isotonic_fit tests 

172# we'll base this around the same data for bootstrap tests 

173# FCST_ARRAY and OBS_ARRAY are rearrangements of BS_FCST and BS_OBS with additional NaNs 

174FCST_ARRAY = np.array([[0.0, 3, 1, nan], [4, 1, 12, 5]]) 

175OBS_ARRAY = np.array([[2.0, 0, 1, -30], [2, 3, nan, 6]]) 

176 

177DIMS = ["a", "b"] 

178COORDS = {"a": [1, 2], "b": [1, 2, 3, 4]} 

179FCST_XR = xr.DataArray(data=FCST_ARRAY, dims=DIMS, coords=COORDS) 

180OBS_XR = xr.DataArray(data=OBS_ARRAY, dims=DIMS, coords=COORDS) 

181 

182# mean functional regression of BS_OBS (i.e., sorted OBS_ARRAY) 

183Y_REG = np.array([1.5, 1.5, 1.5, 1.5, 2, 6]) 

184FCST_COUNTS = np.array([1, 2, 1, 1, 1]) 

185 

186TEST_POINTS = np.linspace(0.0, 5.0) 

187 

188Q1 = np.quantile([5 / 4, 5 / 2, 5 / 3], (0.25, 0.75)) 

189Q2 = np.quantile([5 / 4, 5 / 2, 17 / 9], (0.25, 0.75)) 

190Q3 = np.quantile([2, 5 / 2, 2], (0.25, 0.75)) 

191CB_LOWER = np.array([nan, Q1[0], Q1[0], Q2[0], Q3[0], 6]) 

192CB_UPPER = np.array([nan, Q1[1], Q1[1], Q2[1], Q3[1], 6]) 

193 

194EXP_IF1 = { 

195 "fcst_sorted": np.unique(BS_FCST), 

196 "fcst_counts": FCST_COUNTS, 

197 "weight_sorted": None, 

198 "regression_values": np.delete(Y_REG, 1), 

199 "regression_func": interpolate.interp1d(BS_FCST, Y_REG, bounds_error=False), 

200 "confidence_band_lower_values": None, 

201 "confidence_band_upper_values": None, 

202 "confidence_band_lower_func": partial(np.full_like, fill_value=np.nan), 

203 "confidence_band_upper_func": partial(np.full_like, fill_value=np.nan), 

204 "confidence_band_levels": (None, None), 

205} 

206 

207EXP_IF2 = { 

208 "fcst_sorted": np.unique(BS_FCST), 

209 "fcst_counts": FCST_COUNTS, 

210 "weight_sorted": None, 

211 "regression_values": np.delete(Y_REG, 1), 

212 "regression_func": interpolate.interp1d(BS_FCST, Y_REG, bounds_error=False), 

213 "confidence_band_lower_values": np.delete(CB_LOWER, 1), 

214 "confidence_band_upper_values": np.delete(CB_UPPER, 1), 

215 "confidence_band_lower_func": interpolate.interp1d(BS_FCST, CB_LOWER, bounds_error=False), 

216 "confidence_band_upper_func": interpolate.interp1d(BS_FCST, CB_UPPER, bounds_error=False), 

217 "confidence_band_levels": (0.25, 0.75), 

218} 

219 

220EXP_IF3 = {**EXP_IF2, "bootstrap_results": BS_EXP2}