Evaluates ROC and Reliability scores for forecasts over the full period from start hour to end hour

        Returns:
            A pandas DataFrame with full-period metadata and verification statistics
198654
def evaluate_period_forecasts(self):
        
        score_columns = ["Run_Date", "Ensemble Name", "Model_Name", "Forecast_Variable", "Neighbor_Radius",
                         "Smoothing_Radius", "Size_Threshold",  "ROC", "Reliability"]
        all_scores = pd.DataFrame(columns=score_columns)
        if self.coordinate_file is not None:
            coord_mask = np.where((self.coordinates["lon"] >= self.lon_bounds[0]) &
                                  (self.coordinates["lon"] <= self.lon_bounds[1]) &
                                  (self.coordinates["lat"] >= self.lat_bounds[0]) &
                                  (self.coordinates["lat"] <= self.lat_bounds[1]) &
                                  (self.period_obs[self.mask_variable] > 0))
        else:
            coord_mask = None
        for neighbor_radius in self.neighbor_radii:
            n_filter = disk(neighbor_radius)
            for s, size_threshold in enumerate(self.size_thresholds):
                period_obs = fftconvolve(self.period_obs[self.mrms_variable] >= self.obs_thresholds[s],
                                         n_filter, mode="same")
                period_obs[period_obs > 1] = 1
                if self.obs_mask and self.coordinate_file is None:
                    period_obs = period_obs[self.period_obs[self.mask_variable] > 0]
                elif self.obs_mask and self.coordinate_file is not None:
                    period_obs = period_obs[coord_mask[0], coord_mask[1]]
                else:
                    period_obs = period_obs.ravel()
                for smoothing_radius in self.smoothing_radii:
                    print("Eval period forecast {0} {1} {2} {3} {4} {5}".format(self.model_name,
                                                                                self.forecast_variable,
                                                                                self.run_date,
                                                                                neighbor_radius,
                                                                                size_threshold, smoothing_radius))
                    period_var = "neighbor_prob_{0:d}-hour_r_{1:d}_s_{2:d}_{3}_{4:0.2f}".format(self.end_hour -
                                                                                                self.start_hour + 1,
                                                                                                neighbor_radius,
                                                                                                smoothing_radius,
                                                                                                self.forecast_variable,
                                                                                                size_threshold)
                    if self.obs_mask and self.coordinate_file is None:
                        period_forecast = self.period_forecasts[period_var][self.period_obs[self.mask_variable] > 0]
                    elif self.obs_mask and self.coordinate_file is not None:
                        period_forecast = self.period_forecasts[period_var][coord_mask[0], coord_mask[1]]
                    else:
                        period_forecast = self.period_forecasts[period_var].ravel()
                    roc = DistributedROC(thresholds=self.probability_levels, obs_threshold=0.5)
                    roc.update(period_forecast, period_obs)
                    rel = DistributedReliability(thresholds=self.probability_levels, obs_threshold=0.5)
                    rel.update(period_forecast, period_obs)
                    row = [self.run_date, self.ensemble_name, self.model_name, self.forecast_variable, neighbor_radius,
                           smoothing_radius, size_threshold, roc, rel]
                    all_scores.loc[period_var] = row
        return all_scores
495748
def predict(self, dataframe):
        """"""
        if self.mean > 0.5:
            return numpy.ones(dataframe.shape[0])
        else:
            return numpy.zeros(dataframe.shape[0])
79099
def gen_FS_DF(df_output):
    
    df_day = pd.pivot_table(
        df_output,
        values=['T2', 'U10', 'Kdown', 'RH2'],
        index=['Year', 'Month', 'Day'],
        aggfunc=[min, max, np.mean, ])
    df_day_all_year = pd.pivot_table(
        df_output,
        values=['T2', 'U10', 'Kdown', 'RH2'],
        index=['Month', 'Day'],
        aggfunc=[min, max, np.mean, ])

    array_yr_mon = df_day.index.droplevel(
        'Day').to_frame().drop_duplicates().values

    df_fs = pd.DataFrame(
        {(yr, mon):
         (df_day.loc[(yr, mon)].apply(gen_score_ser) -
          df_day_all_year.loc[mon].apply(gen_score_ser)).abs().mean()
         for yr, mon in array_yr_mon})

    return df_fs
601671
def predict(self,h=5):
             

        if self.latent_variables.estimated is False:
            raise Exception("No latent variables estimated!")
        else:

            # Retrieve data, dates and (transformed) latent variables
            mu, Y = self._model(self.latent_variables.get_z_values()) 
            date_index = self.shift_dates(h)
            t_params = self.transform_z()

            # Expectation
            exps = self._forecast_mean(h,t_params,Y,None,None)

            for variable in range(0,exps.shape[0]):
                forecasted_values = exps[variable][-h:]
                if variable == 0:
                    result = pd.DataFrame(forecasted_values)
                    result.rename(columns={0:self.data_name[variable]}, inplace=True)
                    result.index = date_index[-h:]          
                else:
                    result[self.data_name[variable]] = forecasted_values    

            return result
255131
def load_data(self):
        
        units = ""
        if self.file_objects[0] is None:
            raise IOError()
        var_name, z_index = self.format_var_name(self.variable, list(self.file_objects[0].variables.keys()))
        ntimes = 0
        if 'time' in self.file_objects[0].variables[var_name].dimensions:
            ntimes = len(self.file_objects[0].dimensions['time'])

        if ntimes > 1:
            if z_index is None:
                data = self.file_objects[0].variables[var_name][self.forecast_hours].astype(np.float32)
            else:
                data = self.file_objects[0].variables[var_name][self.forecast_hours, z_index].astype(np.float32)
        else:
            y_dim, x_dim = self.file_objects[0].variables[var_name].shape[-2:]
            data = np.zeros((len(self.valid_dates), y_dim, x_dim), dtype=np.float32)
            for f, file_object in enumerate(self.file_objects):
                if file_object is not None:
                    if z_index is None:
                        data[f] = file_object.variables[var_name][0]
                    else:
                        data[f] = file_object.variables[var_name][0, z_index]
        if hasattr(self.file_objects[0].variables[var_name], "units"):
            units = self.file_objects[0].variables[var_name].units
        return data, units
1018366
def gen_TMY(df_output):
    '''

    '''

    # calculate weighted score
    ws = gen_WS_DF(df_output)

    # select year
    year_sel = pick_year(ws, df_output, n=5)

    # generate TMY data
    df_TMY = pd.concat(
        # shift(1) here is to conform the convention that
        # timestamps refer to the preceding period
        # [df_output.shift(1).groupby(['Month', 'Year']).get_group(grp)
        # shift(1) is not necessary
        [df_output.groupby(['Month', 'Year']).get_group(grp)
         for grp in year_sel.items()])

    # df_TMY = df_TMY.rename(columns=dict_supy_epw)

    return df_TMY
652773
def QA_util_make_hour_index(day, type_='1h'):
    

    if QA_util_if_trade(day) is True:
        return pd.date_range(
            str(day) + ' 09:30:00',
            str(day) + ' 11:30:00',
            freq=type_,
            closed='right'
        ).append(
            pd.date_range(
                str(day) + ' 13:00:00',
                str(day) + ' 15:00:00',
                freq=type_,
                closed='right'
            )
        )
    else:
        return pd.DataFrame(['No trade'])
367211
def holtWintersForecast(requestContext, seriesList):
    
    previewSeconds = 7 * 86400  # 7 days
    # ignore original data and pull new, including our preview
    newContext = requestContext.copy()
    newContext['startTime'] = (requestContext['startTime'] -
                               timedelta(seconds=previewSeconds))
    previewList = evaluateTokens(newContext, requestContext['args'][0])
    results = []
    for series in previewList:
        analysis = holtWintersAnalysis(series)
        predictions = analysis['predictions']
        windowPoints = previewSeconds // predictions.step
        result = TimeSeries("holtWintersForecast(%s)" % series.name,
                            predictions.start + previewSeconds,
                            predictions.end, predictions.step,
                            predictions[windowPoints:])
        result.pathExpression = result.name
        results.append(result)
    return results
1203667
def holtWintersConfidenceBands(requestContext, seriesList, delta=3):
    
    previewSeconds = 7 * 86400  # 7 days
    # ignore original data and pull new, including our preview
    newContext = requestContext.copy()
    newContext['startTime'] = (requestContext['startTime'] -
                               timedelta(seconds=previewSeconds))
    previewList = evaluateTokens(newContext, requestContext['args'][0])
    results = []
    for series in previewList:
        analysis = holtWintersAnalysis(series)

        data = analysis['predictions']
        windowPoints = previewSeconds // data.step
        forecast = TimeSeries(data.name, data.start + previewSeconds,
                              data.end, data.step, data[windowPoints:])
        forecast.pathExpression = data.pathExpression

        data = analysis['deviations']
        windowPoints = previewSeconds // data.step
        deviation = TimeSeries(data.name, data.start + previewSeconds,
                               data.end, data.step, data[windowPoints:])
        deviation.pathExpression = data.pathExpression

        seriesLength = len(forecast)
        i = 0
        upperBand = list()
        lowerBand = list()
        while i < seriesLength:
            forecast_item = forecast[i]
            deviation_item = deviation[i]
            i = i + 1
            if forecast_item is None or deviation_item is None:
                upperBand.append(None)
                lowerBand.append(None)
            else:
                scaled_deviation = delta * deviation_item
                upperBand.append(forecast_item + scaled_deviation)
                lowerBand.append(forecast_item - scaled_deviation)

        upperName = "holtWintersConfidenceUpper(%s)" % series.name
        lowerName = "holtWintersConfidenceLower(%s)" % series.name
        upperSeries = TimeSeries(upperName, forecast.start, forecast.end,
                                 forecast.step, upperBand)
        lowerSeries = TimeSeries(lowerName, forecast.start, forecast.end,
                                 forecast.step, lowerBand)
        upperSeries.pathExpression = series.pathExpression
        lowerSeries.pathExpression = series.pathExpression
        results.append(lowerSeries)
        results.append(upperSeries)
    return results
1065103
def pandas(self):
        
        names,prior,posterior = [],[],[]
        for iname,name in enumerate(self.posterior_parameter.row_names):
            names.append(name)
            posterior.append(np.sqrt(float(
                self.posterior_parameter[iname, iname]. x)))
            iprior = self.parcov.row_names.index(name)
            prior.append(np.sqrt(float(self.parcov[iprior, iprior].x)))
        for pred_name, pred_var in self.posterior_prediction.items():
            names.append(pred_name)
            posterior.append(np.sqrt(pred_var))
            prior.append(self.prior_prediction[pred_name])
        return pd.DataFrame({"posterior": posterior, "prior": prior},
                                index=names)
484865
def absolute(self):
        
        return Timeseries(np.absolute(self), self.tspan, self.labels)
223063
def _calc_frames(stats):
    
    timings = []
    callers = []
    for key, values in iteritems(stats.stats):
        timings.append(
            pd.Series(
                key + values[:-1],
                index=timing_colnames,
            )
        )
        for caller_key, caller_values in iteritems(values[-1]):
            callers.append(
                pd.Series(
                    key + caller_key + caller_values,
                    index=caller_columns,
                )
            )

    timings_df = pd.DataFrame(timings)
    callers_df = pd.DataFrame(callers)
    timings_df['filename:funcname'] = \
        (timings_df['filename'] + ':' + timings_df['funcname'])
    timings_df = timings_df.groupby('filename:funcname').sum()
    return timings_df, callers_df
182998
def timeseries(self, dataframe=False):
        

        self.query.by_period()
        ts = super().timeseries(dataframe=dataframe)
        ts['value'] = ts['value'].apply(lambda x: float("%.2f" % x))
        return ts
1199363
def compute_stability_classification(self, predicted_data, record, dataframe_record):
        ''''''
        stability_classification, stability_classication_x_cutoff, stability_classication_y_cutoff = None, self.stability_classication_x_cutoff, self.stability_classication_y_cutoff
        if record['DDG'] != None:
            stability_classification = fraction_correct([record['DDG']], [predicted_data[self.ddg_analysis_type]], x_cutoff = stability_classication_x_cutoff, y_cutoff = stability_classication_y_cutoff)
            stability_classification = int(stability_classification)
            assert(stability_classification == 0 or stability_classification == 1)
        dataframe_record['StabilityClassification'] = stability_classification
710025
def clusterMetrics(timeValues, massValues, massScalingFactor=1):
    
    metrics = dict()
    metrics['meanTime'] = numpy.mean(timeValues)
    metrics['meanMass'] = numpy.mean(massValues)
    metrics['devTime'] = timeValues - metrics['meanTime']
    metrics['devMass'] = massValues - metrics['meanMass']
    #metrics['devMass'] = (1-metrics['meanMass']/massValues)
    metrics['spreadTime'] = numpy.max(timeValues) - numpy.min(timeValues)
    metrics['spreadMass'] = numpy.max(massValues) - numpy.min(massValues)
    #metrics['spreadMass'] = (1-numpy.min(massValues) / numpy.max(massValues))
    metrics['devEuc'] = numpy.sqrt(numpy.power(metrics['devTime'], 2) +
                                   numpy.power(metrics['devMass']*massScalingFactor, 2)
                                   )
    metrics['meanEuc'] = numpy.mean(metrics['devEuc'])
    metrics['devTime'] = metrics['devTime'].tolist()
    metrics['devMass'] = metrics['devMass'].tolist()
    metrics['devEuc'] = metrics['devEuc'].tolist()
    return metrics
431582
def predict(self, date, obs_code=568):
        
        time = Time(date, scale='utc', precision=6)
        jd = ctypes.c_double(time.jd)
        # call predict with agbfile, jdate, obscode
        self.orbfit.predict.restype = ctypes.POINTER(ctypes.c_double * 5)
        self.orbfit.predict.argtypes = [ ctypes.c_char_p, ctypes.c_double, ctypes.c_int ]
        predict = self.orbfit.predict(ctypes.c_char_p(self.abg.name),
                       jd,
                       ctypes.c_int(obs_code))
        self.coordinate = coordinates.SkyCoord(predict.contents[0],
                                               predict.contents[1],
                                               unit=(units.degree, units.degree))
        self.dra = predict.contents[2]
        self.ddec = predict.contents[3]
        self.pa = predict.contents[4]
        self.date = str(time)
184329
def crps(self):
        
        return np.sum(self.errors["F_2"].values - self.errors["F_O"].values * 2.0 + self.errors["O_2"].values) / \
            (self.thresholds.size * self.num_forecasts)