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(interpretation): start_date = datetime(2000, 1, 1, tzinfo=pytz.UTC) end_date = datetime(2000, 1, 2, tzinfo=pytz.UTC) mp = ModelingPeriod(interpretation, start_date, end_date) assert mp.interpretation == interpretation assert mp.start_date == start_date assert mp.end_date == end_date
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_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_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\')])')
def test_repr(interpretation): start_date = datetime(2000, 1, 1, tzinfo=pytz.UTC) end_date = datetime(2000, 1, 2, tzinfo=pytz.UTC) mp = ModelingPeriod(interpretation, start_date, end_date) assert str(mp).startswith("ModelingPeriod")
def test_start_date_blank_baseline_ok(interpretation): end_date = datetime(2000, 1, 2, tzinfo=pytz.UTC) mp = ModelingPeriod("BASELINE", end_date=end_date) assert mp.start_date is None assert mp.end_date == end_date
def test_end_date_blank_baseline_not_ok(interpretation): start_date = datetime(2000, 1, 2, tzinfo=pytz.UTC) with pytest.raises(ValueError): ModelingPeriod("BASELINE", start_date=start_date)
def test_start_date_blank_reporting_not_ok(interpretation): end_date = datetime(2000, 1, 2, tzinfo=pytz.UTC) with pytest.raises(ValueError): ModelingPeriod("REPORTING", end_date=end_date)
def test_end_date_blank_reporting_ok(interpretation): start_date = datetime(2000, 1, 2, tzinfo=pytz.UTC) mp = ModelingPeriod("REPORTING", start_date=start_date) assert mp.start_date == start_date assert mp.end_date is None
def test_tz_unaware(interpretation): start_date = datetime(2000, 1, 1) end_date = datetime(2000, 1, 2) with pytest.raises(ValueError): ModelingPeriod(interpretation, start_date, end_date)
def test_end_before_start(interpretation): start_date = datetime(2000, 1, 2, tzinfo=pytz.UTC) end_date = datetime(2000, 1, 1, tzinfo=pytz.UTC) with pytest.raises(ValueError): ModelingPeriod(interpretation, start_date, end_date)
def test_bad_interprtation(): interpretation = "INVALID" start_date = datetime(2000, 1, 1, tzinfo=pytz.UTC) end_date = datetime(2000, 1, 2, tzinfo=pytz.UTC) with pytest.raises(ValueError): ModelingPeriod(interpretation, start_date, end_date)
def reporting_period_no_end_date(): return ModelingPeriod("REPORTING", start_date=datetime(2015, 1, 1, tzinfo=pytz.UTC))