Skip to content

Commit

Permalink
Merge remote-tracking branch 'gh/python3'
Browse files Browse the repository at this point in the history
  • Loading branch information
nworbmot committed Jan 19, 2016
2 parents a210182 + de43a60 commit 60968b6
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 51 deletions.
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import

#Relative paths seem to be necessary for Python 3

Expand Down
21 changes: 12 additions & 9 deletions components.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import
from six import iteritems
from six.moves import map


__version__ = "0.1"
Expand All @@ -41,7 +44,7 @@

from .descriptors import Float, String, Series, GraphDesc, OrderedGraph, Integer, Boolean, get_simple_descriptors, get_series_descriptors

from io import export_to_csv_folder, import_from_csv_folder, import_from_pypower_ppc
from .io import export_to_csv_folder, import_from_csv_folder, import_from_pypower_ppc

import inspect

Expand Down Expand Up @@ -425,7 +428,7 @@ def __init__(self, csv_folder_name=None, **kwargs):
self.import_from_csv_folder(csv_folder_name)
#self.determine_network_topology()

for key, value in kwargs.iteritems():
for key, value in iteritems(kwargs):
setattr(self, key, value)


Expand All @@ -434,23 +437,23 @@ def __init__(self, csv_folder_name=None, **kwargs):
def build_dataframes(self):
for cls in (Load, ShuntImpedance, SubNetwork, Generator, Line, Bus,
StorageUnit, TransportLink, Transformer, Source, Converter):
columns = list((k, v.typ) for k, v in self.component_simple_descriptors[cls].iteritems())
columns = list((k, v.typ) for k, v in iteritems(self.component_simple_descriptors[cls]))

#store also the objects themselves
columns.append(("obj", object))

#very important! must tell the descriptor what it's name is
for k,v in self.component_simple_descriptors[cls].iteritems():
for k,v in iteritems(self.component_simple_descriptors[cls]):
v.name = k

df = pd.DataFrame({k: pd.Series(dtype=d) for k, d in columns},
columns=map(itemgetter(0), columns))
columns=list(map(itemgetter(0), columns)))

df.index.name = "name"

setattr(self,cls.list_name,df)

for k,v in self.component_series_descriptors[cls].iteritems():
for k,v in iteritems(self.component_series_descriptors[cls]):
v.name = k
series_df = pd.DataFrame(index=self.snapshots, dtype=v.dtype)
series_df.index.name = "snapshots"
Expand All @@ -468,7 +471,7 @@ def set_snapshots(self,snapshots):

df = getattr(self,cls.list_name)

for k,v in self.component_series_descriptors[cls].iteritems():
for k,v in iteritems(self.component_series_descriptors[cls]):
series_df = getattr(df,k)
setattr(df,k,series_df.reindex(index=self.snapshots,fill_value=v.default))

Expand Down Expand Up @@ -514,11 +517,11 @@ def add(self,class_name,name,**kwargs):
cls_df.loc[obj.name,col] = self.component_simple_descriptors[cls][col].default


for k,v in self.component_series_descriptors[cls].iteritems():
for k,v in iteritems(self.component_series_descriptors[cls]):
getattr(cls_df,k)[obj.name] = v.default


for key,value in kwargs.iteritems():
for key,value in iteritems(kwargs):
setattr(obj,key,value)

return obj
Expand Down
4 changes: 3 additions & 1 deletion descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import
from six import iteritems



Expand Down Expand Up @@ -211,7 +213,7 @@ def get_descriptors(cls,allowed_descriptors=[]):
mro.reverse()

for kls in mro:
for k,v in vars(kls).iteritems():
for k,v in iteritems(vars(kls)):
if type(v) in allowed_descriptors:
d[k] = v
return d
Expand Down
1 change: 1 addition & 0 deletions examples/ac-dc-meshed/ac-dc-lopf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import


import pypsa,os
Expand Down
1 change: 1 addition & 0 deletions examples/ac-dc-meshed/ac-dc-lpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import


import pypsa, os
Expand Down
11 changes: 7 additions & 4 deletions examples/opf-scigrid-de/add_load_gen_to_scigrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
# In[1]:


# make the code as Python 3 compatible as possible
# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import


import pypsa

import pandas as pd
from six import iteritems
from six.moves import range


# In[2]:
Expand Down Expand Up @@ -214,7 +217,7 @@

# In[27]:

[k.osm_name for k,v in network.graph.node.iteritems() if 'region' not in v]
[k.osm_name for k,v in iteritems(network.graph.node) if 'region' not in v]


# In[28]:
Expand All @@ -236,9 +239,9 @@ def backup_capacity_german_grid(G):

def nodeofaplant(x):
if np.isnan(x["lon"]) or np.isnan(x["lat"]):
return random.choice(cells.keys())
return random.choice(list(cells.keys()))
p = Point(x["lon"], x["lat"])
for n, cell in cells.iteritems():
for n, cell in iteritems(cells):
if cell.contains(p):
return n
else:
Expand Down
1 change: 1 addition & 0 deletions examples/opf-scigrid-de/opf_with_scigrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import


import pypsa
Expand Down
2 changes: 2 additions & 0 deletions examples/opf-storage-hvdc/export_opf-storage_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import

import pypsa

Expand All @@ -23,6 +24,7 @@
import inspect

import os
from six.moves import range



Expand Down
1 change: 1 addition & 0 deletions examples/opf-storage-hvdc/opf-storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import

import pypsa

Expand Down
11 changes: 7 additions & 4 deletions io.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import
from six import iteritems
from six.moves import filter


__version__ = "0.1"
Expand All @@ -40,7 +43,7 @@

def get_cls_from_list_name(list_name):

for k,v in vars(pypsa.components).iteritems():
for k,v in iteritems(vars(pypsa.components)):
if hasattr(v,"list_name") and v.list_name == list_name:
return v

Expand All @@ -62,7 +65,7 @@ def export_to_csv_folder(network,csv_folder_name,time_series={},verbose=True):

#exportable component types
#what about None????
allowed_types = [float,int,str,bool] + np.typeDict.values()
allowed_types = [float,int,str,bool] + list(np.typeDict.values())

#first export network properties

Expand Down Expand Up @@ -112,7 +115,7 @@ def export_to_csv_folder(network,csv_folder_name,time_series={},verbose=True):
print(attr)
filter_f = time_series[list_name][attr]

sub_selection = filter(filter_f,getattr(network,list_name).obj)
sub_selection = list(filter(filter_f,getattr(network,list_name).obj))

df = getattr(list_df,attr)[[s.name for s in sub_selection]]

Expand Down Expand Up @@ -225,7 +228,7 @@ def import_from_csv_folder(network,csv_folder_name):

import_components_from_dataframe(network,df,cls.__name__)

file_attrs = filter(lambda n: n.startswith(list_name+"-") and n.endswith(".csv"),os.listdir(csv_folder_name))
file_attrs = [n for n in os.listdir(csv_folder_name) if n.startswith(list_name+"-") and n.endswith(".csv")]

for file_name in file_attrs:
df = pd.read_csv(os.path.join(csv_folder_name,file_name),index_col=0)
Expand Down
61 changes: 33 additions & 28 deletions opf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import
from six import iteritems


__version__ = "0.1"
Expand Down Expand Up @@ -416,46 +418,49 @@ def extract_optimisation_results(network,snapshots):

model = network.model

def set_on(df, series):
def as_series(indexedvar):
return pd.Series(indexedvar.get_values())

def set_from_series(df, series):
df.loc[snapshots] = series.unstack(0).reindex_axis(df.columns, axis=1)

if len(network.generators):
set_on(network.generators.p, pd.Series(model.generator_p.get_values()))
set_from_series(network.generators.p, as_series(model.generator_p))

if len(network.storage_units):
set_on(network.storage_units.p,
pd.Series(model.storage_p_dispatch.get_values())
- pd.Series(model.storage_p_store.get_values()))
set_from_series(network.storage_units.p,
as_series(model.storage_p_dispatch)
- as_series(model.storage_p_store))

set_on(network.storage_units.state_of_charge,
pd.Series(model.state_of_charge.get_values()))
set_from_series(network.storage_units.state_of_charge,
as_series(model.state_of_charge))

if len(network.loads):
network.loads.p.loc[snapshots] = network.loads.p_set.loc[snapshots]

if len(network.buses):
set_on(network.buses.v_ang,
pd.Series(model.voltage_angles.get_values()))
set_from_series(network.buses.v_ang,
as_series(model.voltage_angles))
network.buses.p.loc[snapshots] = \
pd.concat({n: assets.p.loc[snapshots].multiply(assets.sign, axis=1)
.groupby(assets.bus, axis=1).sum()
for n,assets in dict(g=network.generators,
l=network.loads,
s=network.storage_units).iteritems()}) \
for n,assets in iteritems(dict(g=network.generators,
l=network.loads,
s=network.storage_units))}) \
.sum(level=1) \
.reindex_axis(network.buses.p.columns, axis=1, fill_value=0.)

set_on(network.buses.marginal_price,
pd.Series(model.power_balance.values(),
index=pd.MultiIndex.from_tuples(model.power_balance.keys()))
.map(pd.Series(model.dual.values(), index=model.dual.keys())))
set_from_series(network.buses.marginal_price,
pd.Series(list(model.power_balance.values()),
index=pd.MultiIndex.from_tuples(list(model.power_balance.keys())))
.map(pd.Series(list(model.dual.values()), index=list(model.dual.keys()))))

# active branches
controllable_branches = pd.Series(model.controllable_branch_p.get_values())
for typ, df in dict(Converter=network.converters,
TransportLink=network.transport_links).iteritems():
controllable_branches = as_series(model.controllable_branch_p)
for typ, df in iteritems(dict(Converter=network.converters,
TransportLink=network.transport_links)):
if len(df):
set_on(df.p0, controllable_branches.loc[typ])
set_from_series(df.p0, controllable_branches.loc[typ])
df.p1.loc[snapshots] = - df.p0.loc[snapshots]

# TODO : Eliminate for loop
Expand All @@ -468,23 +473,23 @@ def get_v_angs(buses):
v = network.buses.v_ang.loc[snapshots,buses]
v.set_axis(1, buses.index)
return v
for typ, df in dict(Line=network.lines,
Transformer=network.transformers).iteritems():
for typ, df in iteritems(dict(Line=network.lines,
Transformer=network.transformers)):
if len(df):
attrs = df.sub_network.map(network.sub_networks.current_type).map(dict(AC='x_pu', DC='r_pu'))
df.p0.loc[snapshots] = (get_v_angs(df.bus0) - get_v_angs(df.bus1)).divide(df.lookup(attrs.index, attrs), axis=1)
df.p1.loc[snapshots] = - df.p1.loc[snapshots]

network.generators.loc[network.generators.p_nom_extendable, 'p_nom'] = \
pd.Series(network.model.generator_p_nom.get_values())
as_series(network.model.generator_p_nom)

network.storage_units.loc[network.storage_units.p_nom_extendable, 'p_nom'] = \
pd.Series(network.model.storage_p_nom.get_values())
as_series(network.model.storage_p_nom)

s_nom_extendable_branches = pd.Series(model.branch_s_nom.get_values())
for typ, df in dict(Line=network.lines,
TransportLink=network.transport_links,
Converter=network.converters).iteritems():
s_nom_extendable_branches = as_series(model.branch_s_nom)
for typ, df in iteritems(dict(Line=network.lines,
TransportLink=network.transport_links,
Converter=network.converters)):
if len(df):
df.loc[df.s_nom_extendable, 's_nom'] = s_nom_extendable_branches.loc[typ]

Expand Down
12 changes: 7 additions & 5 deletions pf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

# make the code as Python 3 compatible as possible
from __future__ import print_function, division
from __future__ import absolute_import
from six.moves import range


__version__ = "0.1"
Expand Down Expand Up @@ -391,7 +393,7 @@ def find_bus_controls(sub_network,verbose=True):

#order buses
sub_network.buses_o = pd.concat((buses.loc[[sub_network.slack_bus]],sub_network.pvpqs))
sub_network.buses_o["i"] = range(len(sub_network.buses_o))
sub_network.buses_o["i"] = list(range(len(sub_network.buses_o)))


def calculate_dependent_values(network):
Expand Down Expand Up @@ -528,18 +530,18 @@ def calculate_Y(sub_network,verbose=True):
bus1 = join.i_1

#connection matrices
C0 = csr_matrix((ones(num_branches), (range(num_branches), bus0)), (num_branches, num_buses))
C1 = csr_matrix((ones(num_branches), (range(num_branches), bus1)), (num_branches, num_buses))
C0 = csr_matrix((ones(num_branches), (np.arange(num_branches), bus0)), (num_branches, num_buses))
C1 = csr_matrix((ones(num_branches), (np.arange(num_branches), bus1)), (num_branches, num_buses))

#build Y{0,1} such that Y{0,1} * V is the vector complex branch currents

i = r_[range(num_branches), range(num_branches)]
i = r_[np.arange(num_branches), np.arange(num_branches)]
sub_network.Y0 = csr_matrix((r_[Y00,Y01],(i,r_[bus0,bus1])), (num_branches,num_buses))
sub_network.Y1 = csr_matrix((r_[Y10,Y11],(i,r_[bus0,bus1])), (num_branches,num_buses))

#now build bus admittance matrix
sub_network.Y = C0.T * sub_network.Y0 + C1.T * sub_network.Y1 + \
csr_matrix((Y_sh, (range(num_buses), range(num_buses))))
csr_matrix((Y_sh, (np.arange(num_buses), np.arange(num_buses))))



Expand Down
1 change: 1 addition & 0 deletions test/test_ac_dc_lopf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function, division
from __future__ import absolute_import

import pypsa

Expand Down
1 change: 1 addition & 0 deletions test/test_ac_dc_lpf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function, division
from __future__ import absolute_import

import pypsa

Expand Down
Loading

0 comments on commit 60968b6

Please sign in to comment.