/
assets.py
223 lines (191 loc) · 7.99 KB
/
assets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from typing import Dict, List, Tuple, Union
import isodate
import timely_beliefs as tb
from sqlalchemy.orm import Query
from flexmeasures.data.config import db
from flexmeasures.data.models.time_series import TimedValue
from flexmeasures.utils.entity_address_utils import build_entity_address
from flexmeasures.utils.flexmeasures_inflection import humanize, pluralize
class AssetType(db.Model):
"""Describing asset types for our purposes"""
name = db.Column(db.String(80), primary_key=True)
# The name we want to see (don't unnecessarily capitalize, so it can be used in a sentence)
display_name = db.Column(db.String(80), default="", unique=True)
# The explanatory hovel label (don't unnecessarily capitalize, so it can be used in a sentence)
hover_label = db.Column(db.String(80), nullable=True, unique=False)
is_consumer = db.Column(db.Boolean(), nullable=False, default=False)
is_producer = db.Column(db.Boolean(), nullable=False, default=False)
can_curtail = db.Column(db.Boolean(), nullable=False, default=False, index=True)
can_shift = db.Column(db.Boolean(), nullable=False, default=False, index=True)
daily_seasonality = db.Column(db.Boolean(), nullable=False, default=False)
weekly_seasonality = db.Column(db.Boolean(), nullable=False, default=False)
yearly_seasonality = db.Column(db.Boolean(), nullable=False, default=False)
def __init__(self, **kwargs):
super(AssetType, self).__init__(**kwargs)
self.name = self.name.replace(" ", "_").lower()
if "display_name" not in kwargs:
self.display_name = humanize(self.name)
@property
def plural_name(self) -> str:
return pluralize(self.display_name)
@property
def preconditions(self) -> Dict[str, bool]:
"""Assumptions about the time series data set, such as normality and stationarity
For now, this is usable input for Prophet (see init), but it might evolve or go away."""
return dict(
daily_seasonality=self.daily_seasonality,
weekly_seasonality=self.weekly_seasonality,
yearly_seasonality=self.yearly_seasonality,
)
@property
def weather_correlations(self) -> List[str]:
"""Known correlations of weather sensor type and asset type."""
correlations = []
if self.name == "solar":
correlations.append("radiation")
if self.name == "wind":
correlations.append("wind_speed")
if self.name in (
"one-way_evse",
"two-way_evse",
"battery",
"building",
):
correlations.append("temperature")
return correlations
def __repr__(self):
return "<AssetType %r>" % self.name
class Asset(db.Model, tb.SensorDBMixin):
"""Each asset is an energy- consuming or producing hardware. """
# The name
name = db.Column(db.String(80), default="", unique=True)
# The name we want to see (don't unnecessarily capitalize, so it can be used in a sentence)
display_name = db.Column(db.String(80), default="", unique=True)
# The name of the assorted AssetType
asset_type_name = db.Column(
db.String(80), db.ForeignKey("asset_type.name"), nullable=False
)
# How many MW at peak usage
capacity_in_mw = db.Column(db.Float, nullable=False)
# State of charge in MWh and its datetime and udi event
min_soc_in_mwh = db.Column(db.Float, nullable=True)
max_soc_in_mwh = db.Column(db.Float, nullable=True)
soc_in_mwh = db.Column(db.Float, nullable=True)
soc_datetime = db.Column(db.DateTime(timezone=True), nullable=True)
soc_udi_event_id = db.Column(db.Integer, nullable=True)
# latitude is the North/South coordinate
latitude = db.Column(db.Float, nullable=False)
# longitude is the East/West coordinate
longitude = db.Column(db.Float, nullable=False)
# owner
owner_id = db.Column(db.Integer, db.ForeignKey("fm_user.id", ondelete="CASCADE"))
# market
market_id = db.Column(db.Integer, db.ForeignKey("market.id"), nullable=True)
def __init__(self, **kwargs):
super(Asset, self).__init__(**kwargs)
self.name = self.name.replace(" (MW)", "")
if "display_name" not in kwargs:
self.display_name = humanize(self.name)
asset_type = db.relationship("AssetType", backref=db.backref("assets", lazy=True))
owner = db.relationship(
"User",
backref=db.backref(
"assets", lazy=True, cascade="all, delete-orphan", passive_deletes=True
),
)
market = db.relationship("Market", backref=db.backref("assets", lazy=True))
@property
def power_unit(self) -> float:
"""Return the 'unit' property of the generic asset, just with a more insightful name."""
return self.unit
@property
def entity_address(self) -> str:
return build_entity_address(
dict(owner_id=self.owner_id, asset_id=self.id), "connection"
)
@property
def location(self) -> Tuple[float, float]:
return self.latitude, self.longitude
def capacity_factor_in_percent_for(self, load_in_mw) -> int:
if self.capacity_in_mw == 0:
return 0
return min(round((load_in_mw / self.capacity_in_mw) * 100, 2), 100)
@property
def is_pure_consumer(self) -> bool:
"""Return True if this asset is consuming but not producing."""
return self.asset_type.is_consumer and not self.asset_type.is_producer
@property
def is_pure_producer(self) -> bool:
"""Return True if this asset is producing but not consuming."""
return self.asset_type.is_producer and not self.asset_type.is_consumer
def to_dict(self) -> Dict[str, Union[str, float]]:
return dict(
name=self.name,
display_name=self.display_name,
asset_type_name=self.asset_type_name,
latitude=self.latitude,
longitude=self.longitude,
capacity_in_mw=self.capacity_in_mw,
)
def __repr__(self):
return "<Asset %s:%r (%s), res.: %s on market %s>" % (
self.id,
self.name,
self.asset_type_name,
self.event_resolution,
self.market,
)
def assets_share_location(assets: List[Asset]) -> bool:
"""
Return True if all assets in this list are located on the same spot.
TODO: In the future, we might soften this to compare if assets are in the same "housing" or "site".
"""
if not assets:
return True
return all([a.location == assets[0].location for a in assets])
class Power(TimedValue, db.Model):
"""
All measurements of power data are stored in one slim table.
Negative values indicate consumption.
TODO: datetime objects take up most of the space (12 bytes each)). One way out is to normalise them out to a table.
TODO: If there are more than one measurement per asset per time step possible, we can expand rather easily.
"""
asset_id = db.Column(
db.Integer(),
db.ForeignKey("asset.id", ondelete="CASCADE"),
primary_key=True,
index=True,
)
asset = db.relationship(
"Asset",
backref=db.backref(
"measurements",
lazy=True,
cascade="all, delete-orphan",
passive_deletes=True,
),
)
@classmethod
def make_query(
cls,
**kwargs,
) -> Query:
"""Construct the database query."""
return super().make_query(asset_class=Asset, **kwargs)
def to_dict(self):
return {
"datetime": isodate.datetime_isoformat(self.datetime),
"asset_id": self.asset_id,
"value": self.value,
"horizon": self.horizon,
}
def __init__(self, **kwargs):
super(Power, self).__init__(**kwargs)
def __repr__(self):
return "<Power %.2f on Asset %s at %s by DataSource %s, horizon %s>" % (
self.value,
self.asset_id,
self.datetime,
self.data_source_id,
self.horizon,
)