Skip to content

Commit

Permalink
refactor: set up flexibility for multiple version of MATReader
Browse files Browse the repository at this point in the history
  • Loading branch information
danielolsen committed Apr 20, 2020
1 parent 5fbc6f5 commit 92828d2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
24 changes: 18 additions & 6 deletions powersimdata/input/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import warnings

from powersimdata.input.usa_tamu_model import TAMU
from powersimdata.input.mat_reader import MATReader
from powersimdata.input.mat_reader import REISEMATReader
from powersimdata.input.grid_fields \
import Branch, Bus, DCLine, GenCost, Plant, Storage, Sub

Expand All @@ -15,20 +15,32 @@ class Grid(object):
fields = {}
transform = {}

def __init__(self, interconnect, source='usa_tamu'):
def __init__(self, interconnect, source='usa_tamu', engine='REISE'):
"""Constructor
:param list interconnect: interconnect name(s).
:param str source: model used to build the network
:raises TypeError: if source is not a string.
:raises ValueError: if model does not exist.
:param str source: model used to build the network.
:param str engine: engine used to run scenario, if using MATReader.
:raises TypeError: if source and engine are not both strings.
:raises ValueError: if model or engine does not exist.
:raises NotImplementedError: if engine is not yet built (see REISE.jl).
"""
if not isinstance(source, str):
raise TypeError('source must be a string')
if not isinstance(engine, str):
got_type = type(engine).__name__
raise TypeError('engine must be a str, instead got %s' % got_type)

if source == 'usa_tamu':
data = TAMU(interconnect)
elif os.path.splitext(source)[1] == '.mat':
data = MATReader(source)
if engine == 'REISE':
data = REISEMATReader(source)
elif engine == 'REISE.jl':
raise NotImplementedError
else:
raise ValueError('Unknown engine %s!' % engine)

else:
raise ValueError('%s not implemented' % source)

Expand Down
13 changes: 12 additions & 1 deletion powersimdata/input/mat_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MATReader(AbstractGrid):
def __init__(self, filename):
"""Constructor.
:param filename: path to file
:param str filename: path to file.
"""
super().__init__()
self._set_data_loc(filename)
Expand All @@ -34,6 +34,17 @@ def _set_data_loc(self, filename):
else:
self.data_loc = filename

def _build_network(self):
"""Defines how to interpret the MAT file data to build a network.
Not implemented for MATReader, but must be defined for subclasses.
"""
pass


class REISEMATReader(MATReader):
"""MATLAB file reader, for MAT files created by REISE/MATPOWER
"""
def _build_network(self):
data = loadmat(self.data_loc, squeeze_me=True, struct_as_record=False)
mpc = data['mdi'].mpc
Expand Down
25 changes: 15 additions & 10 deletions powersimdata/input/profiles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from powersimdata.utility.transfer_data import download
from powersimdata.utility import const
from powersimdata.input.grid import Grid

import os
import pandas as pd
Expand Down Expand Up @@ -46,9 +45,9 @@ def get_data(self, scenario_id, field_name):
:param str scenario_id: scenario id.
:param str field_name: *'demand'*, *'hydro'*, *'solar'*, *'wind'*,
*'ct'* or *'grid'*.
:return: (*pandas.DataFrame*, *dict* or *powersimdata.input.Grid*) --
:return: (*pandas.DataFrame*, *dict*, or *str*) --
demand, hydro, solar or wind as a data frame, change table as a
dictionary or grid instance.
dictionary, or the path to a matfile with Grid data.
:raises FileNotFoundError: if file not found on local machine.
"""
self._check_field(field_name)
Expand All @@ -75,18 +74,24 @@ def get_data(self, scenario_id, field_name):
def _read_data(file_name):
"""Reads data.
:param str file_name: file name
:return: (*pandas.DataFrame or dict*) -- demand, hydro, solar or wind as a
data frame or change table as a dictionary.
:param str file_name: file name, extension either 'pkl', 'csv', or 'mat'.
:return: (*pandas.DataFrame*, *dict*, or *str*) -- demand, hydro, solar or
wind as a data frame, change table as a dict, or str containing a
local path to a matfile of grid data.
:raises ValueError: if extension is unknown.
"""
ext = file_name.split(".")[-1]
filepath = os.path.join(const.LOCAL_DIR, file_name)
if ext == 'pkl':
data = pd.read_pickle(os.path.join(const.LOCAL_DIR, file_name))
data = pd.read_pickle(filepath)
elif ext == 'csv':
data = pd.read_csv(os.path.join(const.LOCAL_DIR, file_name),
index_col=0, parse_dates=True)
data = pd.read_csv(filepath, index_col=0, parse_dates=True)
data.columns = data.columns.astype(int)
elif ext == 'mat':
# Try to load the matfile, just to check if it exists locally
open(filepath, 'r')
data = filepath
else:
data = Grid([None], source=os.path.join(const.LOCAL_DIR, file_name))
raise ValueError('Unknown extension! %s' % ext)

return data
7 changes: 6 additions & 1 deletion powersimdata/scenario/analyze.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from powersimdata.input.grid import Grid
from powersimdata.input.scaler import Scaler
from powersimdata.input.profiles import InputData
from powersimdata.output.profiles import OutputData
Expand Down Expand Up @@ -179,7 +180,11 @@ def get_grid(self):
"""
try:
id = InputData(self._ssh)
grid = id.get_data(self._scenario_info['id'], 'grid')
grid_mat_path = id.get_data(self._scenario_info['id'], 'grid')
grid = Grid(
interconnect=[None],
source=grid_mat_path,
engine=self._scenario_info['engine'])
except FileNotFoundError as e:
print(e)
print('Using local grid')
Expand Down

0 comments on commit 92828d2

Please sign in to comment.