def test_create_blank(): modeling_periods = {} groupings = [] mps = ModelingPeriodSet(modeling_periods, groupings) groups = list(mps.iter_modeling_period_groups()) assert len(groups) == 0 modeling_periods = list(mps.iter_modeling_periods()) assert len(modeling_periods) == 0
def test_grouping_misreference(): modeling_periods = {} grouping = [ ("typo1", "typo2"), ] with pytest.raises(ValueError): ModelingPeriodSet(modeling_periods, grouping)
def _deserialize_single_modeling_period_group(modeling_period_group): # check for "baseline_period" key baseline_period = modeling_period_group.get('baseline_period', None) if baseline_period is None: return { 'error': ('Project serializations must provide key' ' "baseline_period".') } else: start_date = baseline_period.get("start", None) if start_date is not None: start_date = dateutil.parser.parse(start_date).astimezone(pytz.utc) end_date = baseline_period.get("end", None) if end_date is not None: end_date = dateutil.parser.parse(end_date).astimezone(pytz.utc) baseline = ModelingPeriod("BASELINE", start_date, end_date) # check for "reporting_period" key reporting_period = modeling_period_group.get('reporting_period', None) if reporting_period is None: return { 'error': ('Project serializations must provide key' ' "reporting_period".') } else: start_date = reporting_period.get("start", None) if start_date is not None: start_date = dateutil.parser.parse(start_date).astimezone(pytz.utc) end_date = reporting_period.get("end", None) if end_date is not None: end_date = dateutil.parser.parse(end_date).astimezone(pytz.utc) reporting = ModelingPeriod("REPORTING", start_date, end_date) modeling_periods = { "baseline": baseline, "reporting": reporting, } grouping = [ ("baseline", "reporting"), ] mps = ModelingPeriodSet(modeling_periods, grouping) return {"modeling_period_set": mps}
def test_create_basic(): modeling_period_1 = ModelingPeriod( "BASELINE", end_date=datetime(2000, 1, 1, tzinfo=pytz.UTC), ) modeling_period_2 = ModelingPeriod( "REPORTING", start_date=datetime(2000, 1, 1, tzinfo=pytz.UTC), ) modeling_periods = { "modeling_period_1": modeling_period_1, "modeling_period_2": modeling_period_2, } grouping = [ ("modeling_period_1", "modeling_period_2"), ] mps = ModelingPeriodSet(modeling_periods, grouping) groups = list(mps.iter_modeling_period_groups()) assert len(groups) == 1 group = groups[0] assert len(group) == 2 assert len(group[0]) == 2 assert group[0][0] == "modeling_period_1" assert group[0][1] == "modeling_period_2" assert group[1][0] == modeling_period_1 assert group[1][1] == modeling_period_2 modeling_periods = list(mps.iter_modeling_periods()) assert len(modeling_periods) == 2 assert modeling_periods[0][0] == "modeling_period_1" assert modeling_periods[1][0] == "modeling_period_2" assert modeling_periods[0][1] == modeling_period_1 assert modeling_periods[1][1] == modeling_period_2
def test_grouping_wrong_length(): modeling_periods = { "modeling_period_1": ModelingPeriod( "BASELINE", end_date=datetime(2000, 1, 1, tzinfo=pytz.UTC), ), "modeling_period_2": ModelingPeriod( "REPORTING", start_date=datetime(2000, 2, 1, tzinfo=pytz.UTC), ), } grouping = [("modeling_period1", )] with pytest.raises(ValueError): ModelingPeriodSet(modeling_periods, grouping)
def modeling_period_set(): modeling_period_1 = ModelingPeriod( "BASELINE", end_date=datetime(2000, 9, 1, tzinfo=pytz.UTC), ) modeling_period_2 = ModelingPeriod( "REPORTING", start_date=datetime(2001, 1, 1, tzinfo=pytz.UTC), ) modeling_periods = { "modeling_period_1": modeling_period_1, "modeling_period_2": modeling_period_2, } grouping = [ ("modeling_period_1", "modeling_period_2"), ] return ModelingPeriodSet(modeling_periods, grouping)
def get_modeling_period_set(interventions): ''' Creates an applicable modeling period set given a list of interventions. Parameters ---------- interventions : list of eemeter.structures.Intervention Interventions for which to build ModelingPeriodSet. ''' # don't attempt modeling where there are no interventions if len(interventions) == 0: logger.info("No interventions, so no modeling period set.") return None baseline_period_end = _get_earliest_intervention_start_date(interventions) reporting_period_start = _get_latest_intervention_end_date(interventions) if reporting_period_start is None: # fall back to baseline_period_end - interventions are still # ongoing. reporting_period_start = baseline_period_end modeling_periods = { "baseline": ModelingPeriod("BASELINE", end_date=baseline_period_end), "reporting": ModelingPeriod("REPORTING", start_date=reporting_period_start), } groupings = [("baseline", "reporting")] modeling_period_set = ModelingPeriodSet(modeling_periods, groupings) logger.info("Created one modeling period group.") return modeling_period_set
def test_repr(): modeling_period_1 = ModelingPeriod( "BASELINE", end_date=datetime(2000, 1, 1, tzinfo=pytz.UTC), ) modeling_period_2 = ModelingPeriod( "REPORTING", start_date=datetime(2000, 1, 1, tzinfo=pytz.UTC), ) modeling_periods = { "modeling_period_1": modeling_period_1, "modeling_period_2": modeling_period_2, } grouping = [ ("modeling_period_1", "modeling_period_2"), ] mps = ModelingPeriodSet(modeling_periods, grouping) assert str(mps).startswith('ModelingPeriodSet(modeling_periods={') assert str(mps).endswith( ', groupings=[(\'modeling_period_1\', \'modeling_period_2\')])')