Ejemplo n.º 1
0
def monthly_anomalies(data, reference_period=None, base_year=-9999):
    """
    Calculate monthly anomalies, by subtracting from every datum
    the mean for its month.  A pair of (monthly_mean, monthly_anom) is
    returned.  *monthly_mean* is a 12-long sequence giving the mean for
    each of the 12 months; *monthly_anom* is a 12-long sequence giving
    the anomalized series for each of the 12 months.

    If *reference_period* is supplied then it should be a pair (*first*,
    *last*) and the mean for a month is taken over the period (an
    example would be reference_period=(1951,1980)).  *base_year*
    specifies the first year of the data.
    
    The input data is a flat sequence, one datum per month.
    Effectively the data changes shape as it passes through this
    function.
    """

    years = len(data) // 12
    if reference_period:
        base = reference_period[0] - base_year
        limit = reference_period[1] - base_year + 1
    else:
        # Setting base, limit to (0,0) is a bit of a hack, but it
        # does work.
        base = 0
        limit = 0
    monthly_mean = []
    monthly_anom = []
    for m in range(12):
        row = data[m::12]
        mean = valid_mean(row[base:limit])
        if invalid(mean):
            # Fall back to using entire period
            mean = valid_mean(row)
        monthly_mean.append(mean)
        if valid(mean):
            def asanom(datum):
                """Convert a single datum to anomaly."""
                if valid(datum):
                    return datum - mean
                return MISSING
            monthly_anom.append(map(asanom, row))
        else:
            monthly_anom.append([MISSING]*years)
    return monthly_mean, monthly_anom
Ejemplo n.º 2
0
def valid_mean(seq, min=1):
    """
    Takes a sequence, *seq*, and computes the mean of the valid
    items (using the valid() function).  If there are fewer than *min*
    valid items, the mean is MISSING.
    """

    count = 0
    sum = 0.0
    for x in seq:
        if valid(x):
            sum += x
            count += 1
    if count >= min:
        return sum/float(count)
    else:
        return MISSING
Ejemplo n.º 3
0
def annual_anomaly(monthly):
    """
    Take a monthly series and convert to annual anomaly.  All months
    (Jan to Dec) are required to be present to compute an anomaly
    value.
    """

    # Convert to monthly anomalies...
    means, anoms = series.monthly_anomalies(monthly)
    result = []
    # Then take 12 months at a time and annualise.
    for year in zip(*anoms):
        if all(valid(month) for month in year):
            # All months valid
            result.append(sum(year)/12.0)
        else:
            result.append(MISSING)
    return result
Ejemplo n.º 4
0
def combine_stations(stations):
    """
    Takes a list of stations (each with a .series attribute), and
    combine them into one new Station.
    """

    # Number of months in fixed length record.
    M = max_series_length
    # Make sure all the records are the same length, namely *M*.
    combined = [MISSING]*M

    if len(stations) == 0:
        return combined

    def good_months(station):
        count = 0
        for v in station.series:
            if v != MISSING:
                count += 1
        return count

    stations = iter(sorted(stations, key=good_months, reverse=True))

    first_station = stations.next()
    combined[:len(first_station.series)] = first_station.series
    combined_weight = [valid(v) for v in combined]
    log("LONGEST %s %s" % (first_station.id, "1"*12))

    for i,station in enumerate(stations):
        new = [MISSING]*len(combined)
        new[:len(station.series)] = station.series
        months_combined = series.combine(combined, combined_weight,
            new, 1.0,
            combine_overlap)
        sys.stderr.write('\r%d' % i)
        months_used = ''.join(
          "01"[months_combined[i] > 0] for i in range(12))
        log("COMBINE %s %s" % (station.id, months_used))
    sys.stderr.write('\n')
    import ghcn
    return ghcn.Station(series=combined, id="NEW")
Ejemplo n.º 5
0
                             nPlanes,
                             residual_blocks=False,
                             downsample=[2, 2])).add(scn.BatchNormReLU(m)).add(
                                 scn.OutputLayer(dimension))
        self.linear = nn.Linear(m, data.nClassesTotal)

    def forward(self, x):
        x = self.sparseModel(x)
        x = self.linear(x)
        return x


model = Model()
print(model)
trainIterator = data.train()
validIterator = data.valid()

criterion = nn.CrossEntropyLoss()
p = {}
p['n_epochs'] = 100
p['initial_lr'] = 1e-1
p['lr_decay'] = 4e-2
p['weight_decay'] = 1e-4
p['momentum'] = 0.9
p['check_point'] = False
p['use_cuda'] = torch.cuda.is_available()
dtype = 'torch.cuda.FloatTensor' if p['use_cuda'] else 'torch.FloatTensor'
dtypei = 'torch.cuda.LongTensor' if p['use_cuda'] else 'torch.LongTensor'
if p['use_cuda']:
    model.cuda()
    criterion.cuda()
Ejemplo n.º 6
0
 def asanom(datum):
     """Convert a single datum to anomaly."""
     if valid(datum):
         return datum - mean
     return MISSING
Ejemplo n.º 7
0
def format1(val):
    if not valid(val):
        return ''
    return "{: 7.3f}".format(val)