Skip to content

Commit

Permalink
Merge pull request #131 from RAMP-project/fix/convert-old-excel-files
Browse files Browse the repository at this point in the history
Add ramp_convert .py to .xlsx files to command line
  • Loading branch information
Bachibouzouk authored May 15, 2024
2 parents 6b6d578 + 5ea07a1 commit ef010ae
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 35 deletions.
9 changes: 6 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,15 @@ Other options are documented in the help of `ramp`, which you access with the ``
ramp -h
If you have existing python input files, you can convert them to
spreadsheet. To do so, go to the ``\ramp`` folder and run
If you have existing python input files from RAMP version prior to 0.5, you can convert them to
spreadsheets input files. Simply run

.. code-block:: bash
python ramp_convert_old_input_files.py -i <path to the input file you wish to convert>
ramp_convert -i <path to the .py input file you wish to convert>
If you want to save a RAMP model you created with a .py file into a spreadsheet refer to
this `example <https://rampdemand.readthedocs.io/en/latest/examples/using_excel/using_excel.html#exporting-the-database>`_

For other examples of command lines options, such as setting date ranges, please visit `the dedicated section <https://rampdemand.readthedocs.io/en/latest/examples/year_simulation/year_simulation.html#setting-date-range>`_ of the documentation.

Expand Down
10 changes: 0 additions & 10 deletions docs/notebooks/fixed_flat_app.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@
"text": [
"You will simulate 7 day(s) from 2023-01-01 00:00:00 until 2023-01-08 00:00:00\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\users\\tahavorm\\downloads\\gitrepos\\ramp\\ramp\\core\\core.py:299: FutureWarning: 'T' is deprecated and will be removed in a future version. Please use 'min' instead of 'T'.\n",
" end=self.days[-1] + pd.Timedelta(1, \"d\") - pd.Timedelta(1, \"T\"),\n",
"c:\\users\\tahavorm\\downloads\\gitrepos\\ramp\\ramp\\core\\core.py:297: FutureWarning: 'T' is deprecated and will be removed in a future version, please use 'min' instead.\n",
" self.__datetimeindex = pd.date_range(\n"
]
}
],
"source": [
Expand Down
1 change: 1 addition & 0 deletions docs/source/api_references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Utilities

load_data
download_example
ramp_py2xlsx


*************
Expand Down
8 changes: 0 additions & 8 deletions docs/source/examples/fixed_flat_app/fixed_flat_app.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ number of days for generating the profiles:
You will simulate 7 day(s) from 2023-01-01 00:00:00 until 2023-01-08 00:00:00
.. parsed-literal::
c:\users\tahavorm\downloads\gitrepos\ramp\ramp\core\core.py:299: FutureWarning: 'T' is deprecated and will be removed in a future version. Please use 'min' instead of 'T'.
end=self.days[-1] + pd.Timedelta(1, "d") - pd.Timedelta(1, "T"),
c:\users\tahavorm\downloads\gitrepos\ramp\ramp\core\core.py:297: FutureWarning: 'T' is deprecated and will be removed in a future version, please use 'min' instead.
self.__datetimeindex = pd.date_range(
.. code:: ipython3
fixed_flat = school_case.generate_daily_load_profiles()
Expand Down
3 changes: 3 additions & 0 deletions ramp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from ramp.core.utils import yearly_pattern, get_day_type
from ramp.example.examples import load_data, download_example
from ramp.post_process.post_process import Plot
from ramp.ramp_convert_old_input_files import (
convert_old_user_input_file as ramp_py2xlsx,
)

__authors__ = "Listed in AUTHORS"
__copyright__ = (
Expand Down
12 changes: 7 additions & 5 deletions ramp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
BASE_PATH = os.path.dirname(os.path.abspath(__file__))

parser = argparse.ArgumentParser(
prog="python ramp_run.py", description="Execute RAMP code"
prog="ramp",
description="Execute RAMP code",
epilog="To convert '.py' input files into '.xlsx' input files use the command 'ramp_convert'",
)
parser.add_argument(
"-i",
Expand Down Expand Up @@ -200,7 +202,7 @@ def main():
series_frame = pd.DataFrame(
np.hstack(year_profile),
index=pd.date_range(
start=f"{year}-1-1", end=f"{year}-12-31 23:59", freq="T"
start=f"{year}-1-1", end=f"{year}-12-31 23:59", freq="min"
),
)
# Save to minute and hour resolution
Expand All @@ -220,9 +222,9 @@ def main():
)
resampled = pd.DataFrame()

resampled["mean"] = series_frame.resample("H").mean()
resampled["max"] = series_frame.resample("H").max()
resampled["min"] = series_frame.resample("H").min()
resampled["mean"] = series_frame.resample("h").mean()
resampled["max"] = series_frame.resample("h").max()
resampled["min"] = series_frame.resample("h").min()
# TODO add more columns with other resampled functions (do this in Jupyter)
resampled.to_csv(
os.path.join(ofname, "yearly_profile_hourly_resolution.csv")
Expand Down
37 changes: 28 additions & 9 deletions ramp/ramp_convert_old_input_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from ramp.core.core import UseCase

parser = argparse.ArgumentParser(
prog="python ramp_convert_old_input_files.py",
description="Convert old python input files to xlsx ones",
prog="ramp_convert", description="Convert RAMP python input files to xlsx ones"
)
parser.add_argument(
"-i",
Expand All @@ -18,10 +17,7 @@
help="path to the input file (including filename)",
)
parser.add_argument(
"-o",
dest="output_path",
type=str,
help="path where to save the converted filename",
"-o", dest="output_path", type=str, help="path where to save the converted filename"
)
parser.add_argument(
"--suffix",
Expand All @@ -44,8 +40,25 @@
def convert_old_user_input_file(
fname_path, output_path=None, suffix="", keep_names=True
):
"""
Imports an input file from a path and returns a processed User_list
"""Convert old RAMP python input files to xlsx ones
The old (RAMP version < 0.5) .py input files defined all users and gathered them in a variable named User_list,
this variable must be defined in the .py file to be converted to .xlsx.
To convert a .py input file to an .xlsx using the UseCase objects, please refer to
https://rampdemand.readthedocs.io/en/latest/examples/using_excel/using_excel.html#exporting-the-database
Parameters
----------
fname_path: path
path to a .py ramp input file containing a variable named User_list
output_path: path, optional
path to the converted .xlsx ramp input file, by default the same folder as the .py file
suffix: str, optional
suffix to be added to the converted .xlsx ramp input file name, default ''
keep_names: bool, optional
keep the variable names of the Appliance instances as their 'name' attribute, default True
"""

line_to_change = -1
Expand Down Expand Up @@ -81,14 +94,16 @@ def convert_old_user_input_file(
output_fname = fname_path.split(os.path.sep)[-1].replace(".py", suffix)
output_fname = os.path.join(output_path, output_fname)

if os.name != "posix":
sys.path.insert(0, os.path.dirname(os.path.abspath(fname_path)))
file_module = importlib.import_module(fname)

user_list = file_module.User_list

UseCase(users=user_list).save(output_fname)


if __name__ == "__main__":
def cli():
args = vars(parser.parse_args())
fname = args["fname_path"]
output_path = args.get("output_path")
Expand All @@ -105,3 +120,7 @@ def convert_old_user_input_file(
)
else:
convert_old_user_input_file(fname, output_path=output_path, suffix=suffix)


if __name__ == "__main__":
cli()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
entry_points={
"console_scripts": [
"ramp=ramp.cli:main",
"ramp_convert=ramp.ramp_convert_old_input_files:cli",
],
},
)
13 changes: 13 additions & 0 deletions tests/test_input_file_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ def test_convert_py_to_xlsx(self):
if old_user != new_user:
pytest.fail()

def test_convert_py_to_xlsx_command_line(self):
"""Convert the 3 example .py input files to xlsx and compare each appliance of each user"""
for i, j in enumerate(self.input_files_to_run):
old_user_list = load_py_usecase(j=j)
output_path = os.path.join("ramp", "test")
os.system(
f"ramp_convert -i {self.py_fnames[i]} -o {output_path} --suffix {self.file_suffix}"
)
new_user_list = load_xlsx_usecase(fname=self.xlsx_fnames[i])
for old_user, new_user in zip(old_user_list, new_user_list):
if old_user != new_user:
pytest.fail()


def test_define_appliance_window_directly_equivalent_to_use_windows_method():
user = User("test user", 1)
Expand Down

0 comments on commit ef010ae

Please sign in to comment.