Skip to content

Commit

Permalink
refactor: change mapping into timestamps_to_timepoints for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
BainanXia committed Jun 15, 2021
1 parent 9193783 commit fc3c542
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
29 changes: 15 additions & 14 deletions switchwrapper/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,16 @@ def make_plant_indices(plant_ids, storage_candidates=None):
return indices


def load_mapping(filename):
"""Takes a file path to a timepoint mapping csv and converts
the given mapping into a format expected by the conversion function
def load_timestamps_to_timepoints(filename):
"""Read timestamps_to_timepoints csv file from the given file path using pandas.
:param str filename: path to the mapping csv
:param str filename: path to the timestamps_to_timepoints csv.
:return: (*pandas.DataFrame*) -- a dataframe with of timepoints to a list containing
all the component time stamps
all the component timestamps.
"""
mapping = pd.read_csv(filename, index_col=0)
timestamps_to_timepoints = pd.read_csv(filename, index_col=0)

return mapping
return timestamps_to_timepoints


def make_branch_indices(branch_ids, dc=False):
Expand All @@ -67,7 +66,7 @@ def make_branch_indices(branch_ids, dc=False):
return [f"{i}dc" if dc else f"{i}ac" for i in branch_ids]


def parse_timepoints(var_dict, variables, mapping):
def parse_timepoints(var_dict, variables, timestamps_to_timepoints):
"""Takes the solution variable dictionary contained in the output pickle
file of `switch` and un-maps the temporal reduction timepoints back into
a timestamp-indexed dataframe.
Expand All @@ -77,15 +76,15 @@ def parse_timepoints(var_dict, variables, mapping):
are a dictionary where Value is the datapoint for that combination of
variable name and parameters.
:param list variables: a list of timeseries variable strings to parse out
:param dict mapping: a dictionary of timepoints to a list containing
all the component time stamps
:param pandas.DataFrame timestamps_to_timepoints: data frame indexed by
timestamps with a column of timepoints for each timestamp.
:return (*dict*): a dictionary where the keys are the variable name strings
and the values are pandas dataframes. The index of these dataframes
are the timestamps contained in the mapping dictionary values.
are the timestamps contained in the timestamps_to_timepoints data frame.
The columns of these dataframes are a comma-separated string of the
parameters embedded in the key of the original input dictionary with
the timepoint removed and preserved order otherwise. If no variables
are found in the input dictionary, the value will be None
are found in the input dictionary, the value will be None.
"""
# Initialize final dictionary to return
Expand All @@ -106,10 +105,12 @@ def parse_timepoints(var_dict, variables, mapping):

# Unstack such that the timepoints are the indices
df = df.set_index(["timepoint", "params"]).unstack()
# Cast timepoints as ints to match mapping
# Cast timepoints as ints to match timestamps_to_timepoints
df.index = df.index.astype(int)
# Expand rows to all timestamps
df = df.loc[mapping["timepoint"]].set_index(mapping.index)
df = df.loc[timestamps_to_timepoints["timepoint"]].set_index(
timestamps_to_timepoints.index
)

parsed_data[key] = df

Expand Down
20 changes: 13 additions & 7 deletions switchwrapper/switch_to_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from switchwrapper import const # noqa: F401
from switchwrapper.helpers import (
branch_indices_to_bus_tuple,
load_mapping,
load_timestamps_to_timepoints,
parse_timepoints,
recover_plant_indices,
)
Expand Down Expand Up @@ -84,15 +84,19 @@ def reconstruct_input_profiles(


class ExtractTimeseries:
def __init__(self, results_file, mapping_file, timepoints_file, grid):
def __init__(
self, results_file, timestamps_to_timepoints_file, timepoints_file, grid
):
"""Extract timeseries results from Switch results.
:param str results_file: file path of Switch results pickle file.
:param str mapping_file: file path of mapping.csv.
:param str timestamps_to_timepoints_file: file path of mapping.csv.
:param str timepoints_file: file path of timepoints.csv.
:param powersimdata.input.grid.Grid grid: grid instance.
"""
self.mapping = load_mapping(mapping_file)
self.timestamps_to_timepoints = load_timestamps_to_timepoints(
timestamps_to_timepoints_file
)
self._timestamp_to_investment_year(timepoints_file)
self._get_parsed_data(results_file)
self.plant_id_mapping, _ = recover_plant_indices(
Expand All @@ -113,8 +117,8 @@ def _timestamp_to_investment_year(self, timepoints_file):
timepoints = pd.read_csv(timepoints_file)
timepoints.set_index("timepoint_id", inplace=True)
self.timestamp_to_investment_year = pd.Series(
self.mapping["timepoint"].map(timepoints["ts_period"]),
index=self.mapping.index,
self.timestamps_to_timepoints["timepoint"].map(timepoints["ts_period"]),
index=self.timestamps_to_timepoints.index,
)

def _get_parsed_data(self, results_file):
Expand All @@ -126,7 +130,9 @@ def _get_parsed_data(self, results_file):
self.results = pickle.load(f)
data = self.results.solution._list[0].Variable
variables_to_parse = ["DispatchGen", "DispatchTx"]
self.parsed_data = parse_timepoints(data, variables_to_parse, self.mapping)
self.parsed_data = parse_timepoints(
data, variables_to_parse, self.timestamps_to_timepoints
)

def get_pg(self):
"""Get timeseries power generation for each plant.
Expand Down

0 comments on commit fc3c542

Please sign in to comment.