Beispiel #1
0
 def enforce_2D(self, ):
     """Make sure the grid is 2-D.
     """
     msg = 'Pass a 2D array.'
     for data in (self.raw_fcstdata, self.raw_verifdata):
         if len(data.shape) == 1:
             raise FormatError(msg)
         elif len(data.shape) > 2:
             for dim in N.arange(len(data.shape) - 2):
                 if data.shape[0] != 1:
                     raise FormatError(msg)
         else:
             print("Passed 2D grid check.")
     return
Beispiel #2
0
 def do_square_check(self):
     """Assert that the grid is a power of 2.
     """
     for data in (self.raw_fcstdata, self.raw_verifdata):
         for dim in range(len(data.shape)):
             if not N.log2(data.shape[dim]).is_integer():
                 msg = "Data dimension not square/power of 2, but of shape {}.".format(
                     data.shape)
                 raise FormatError(msg)
             elif data.shape[0] != data.shape[1]:
                 raise FormatError(msg)
             else:
                 print("Passed square check.")
                 self.L = int(N.log2(data.shape[dim]))
     return
Beispiel #3
0
 def do_grid_check(self,):
     """ Make sure grids are identical size.
     """
     self.ydim, self.xdim = self.data_fcst.shape
     if self.data_obs.shape != (self.ydim,self.xdim):
         raise FormatError("Obs and forecast data not same size.")
     return
Beispiel #4
0
 def enforce_2D(self,):
     """Both data grids need to be 2D.
     """
     for data in (self.data_obs,self.data_fcst):
         shp = data.shape
         if len(shp) == 2:
             pass
         elif len(shp) == 3:
             if shp[0] == 0:
                 data = data[0,:,:]
         elif len(shp) == 4:
             if (shp[0] == 0) and (shp[1] == 0):
                 data = data[0,0,:,:]
         else:
             raise FormatError("Data needs to be 2D.")
     return
Beispiel #5
0
def _enforce_same_dimensions(*args):
    if not check_same_dimensions(*args):
        raise FormatError("Input arrays not all same size.")
    return
Beispiel #6
0
    def __init__(self,
                 fcst_arr,
                 obs_arr,
                 thresh,
                 overunder='over',
                 CLs=None,
                 enforce_limits=True):
        self.enforce_limits = enforce_limits
        self.overunder = overunder
        self.fcst_arr = fcst_arr
        self.thresh = thresh
        self.obs_arr = obs_arr

        if fcst_arr.ndim == 3:
            if fcst_arr.shape[0] == 1:
                fcst_arr = fcst_arr[0, :, :]
                self.FVens = False
            else:
                self.arr2x2s, self.J = self.compute_FV_prob()
                self.FVens = True
        else:
            self.FVens = False

        if self.FVens:
            self.pod = dict()
            self.pfd = dict()
            self.pcli = dict()
            self.kss = dict()
            for j in self.J:
                DS = DetScores(arr2x2=self.arr2x2s[j])
                self.pod[j] = DS.compute_pod()
                self.pfd[j] = DS.compute_pfd()
                self.pcli[j] = DS.compute_pcli()
                self.kss[j] = DS.compute_kss()

        else:
            super().__init__(fcst_arr=fcst_arr,
                             obs_arr=obs_arr,
                             thresh=thresh,
                             overunder=overunder)

            self.pod = self.compute_pod()
            self.pfd = self.compute_pfd()
            self.pcli = self.compute_pcli()
            self.kss = self.compute_kss()

        if CLs is None:
            CLs = N.arange(0.01, 1.01, 0.01)
        elif isinstance(CLs, (int, float, N.ndarray, tuple, list)):
            CLs = N.array(CLs)
        else:
            raise FormatError("CLs argument must be one of the following: \n"
                              "int, float, N.ndarray, tuple, list.")
        self.CLs = CLs

        self.FVs = []
        for cl in self.CLs:
            if self.FVens:
                self.FVs.append(self.compute_FVens(cl))
            else:
                self.FVs.append(self.compute_FV(cl))

        if self.enforce_limits:
            clipped_FVs = []
            overs = 0
            unders = 0
            for fv in self.FVs:
                if fv > 1:
                    fv = 1
                    overs += 1
                elif fv < 0:
                    fv = 0
                    unders += 1
                clipped_FVs.append(fv)
            print("{} FVs over 1 were set to 1.".format(overs))
            print("{} FVs under 0 were set to 0.".format(unders))
            self.raw_FVs = self.FVs
            self.FVs = clipped_FVs