Commit bd7fb073 authored by Lou Morriet's avatar Lou Morriet Committed by lou-morriet
Browse files

add get_value_with_dates() and unittest to get dataframe of values

parent de7c4526
......@@ -27,6 +27,7 @@ objectives) formulated in LP or MILP**
import datetime
import numpy as np
import pandas as pd
from pulp import LpContinuous
__docformat__ = "restructuredtext en"
......@@ -241,6 +242,36 @@ class Quantity:
else:
raise TypeError('you can only use the get_value() method on'
'quantities')
def get_value_with_date(self):
""" return the values of the quantity associated to a date in a
dataframe if the values are a list or a dict (only).
"""
if isinstance(self, Quantity):
print(self.value)
if isinstance(self.value, dict):
print(self.parent.time)
print(self.value.values())
df = pd.DataFrame(data=list(self.value.values()),
index=self.parent.time.DATES,
columns=['Value in {}'.format(self.unit)])
return df
if isinstance(self.value, list):
df = pd.DataFrame(data=list(self.value),
index=self.parent.time.DATES,
columns=['Value in {}'.format(self.unit)])
return df
else:
raise TypeError(
'the value {} should be a list or a dict'.format(
self.name))
else:
raise TypeError('you can only use the get_value() method on'
'quantities')
def _add_quantity_list(self):
if self.parent:
......
......@@ -26,6 +26,8 @@ import unittest
from omegalpes.general.optimisation.elements import *
from pulp import LpContinuous
from omegalpes.general.time import TimeUnit
from omegalpes.energy.units.energy_units import EnergyUnit
from pandas.testing import assert_frame_equal
class TestQuantityFloatValue(unittest.TestCase):
......@@ -343,6 +345,51 @@ class TestGetValue(unittest.TestCase):
"""Checking if get_value() on an dict gives correct value"""
self.assertEqual(self.quant_dict.get_value(), [54, 420])
class TestGetValueWithDate(unittest.TestCase):
"""Checking the get_value_with_date() method"""
def setUp(self):
eu = EnergyUnit(time=TimeUnit(periods=2, dt=1), name='EU')
self.quant_list = Quantity(name="quant0", opt=False, unit="s.u",
vlen=None, value=[8, 12],
description="", vtype=LpContinuous, lb=None,
ub=None, parent=eu)
self.quant_dict = Quantity(name="quant1", opt=False, unit="s.u",
vlen=None, value={"p1": 54, "p2": 420},
description="",
vtype=LpContinuous, lb=None, ub=None,
parent=eu)
self.df_list = pd.DataFrame(data=[8, 12],
index=TimeUnit(periods=2, dt=1).DATES,
columns=['Value in s.u'])
self.df_dict = pd.DataFrame(data=[54, 420],
index=TimeUnit(periods=2, dt=1).DATES,
columns=['Value in s.u'])
def test_list_get_dataframe(self):
"""Checking if get_value_with_date() on a list gives a dataframe"""
self.assertIsInstance(self.quant_list.get_value_with_date(),
pd.DataFrame)
def test_list_get_correct_dataframe(self):
"""Checking if get_value_with_date() on a list gives a
correct dataframe"""
assert_frame_equal(self.quant_list.get_value_with_date(),
self.df_list)
def test_dict_get_dataframe(self):
"""Checking if get_value_with_date() on a dict gives a dataframe"""
self.assertIsInstance(self.quant_dict.get_value_with_date(),
pd.DataFrame)
def test_dict_get_correct_dataframe(self):
"""Checking if get_value_with_date() on a dict gives a
correct dataframe"""
assert_frame_equal(self.quant_dict.get_value_with_date(),
self.df_dict)
class TestConstraint(unittest.TestCase):
"""Checking the attributes of Constraint are properly set"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment