From a035086a78a92f968cdbb1793a69a93324e8f10e Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Mon, 15 Apr 2024 12:07:00 +0200 Subject: [PATCH 01/10] Add ramp_convert .py to .xlsx files to command line --- ramp/__init__.py | 1 + ramp/ramp_convert_old_input_files.py | 7 +++++-- setup.py | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ramp/__init__.py b/ramp/__init__.py index d3753eb8..df02155e 100644 --- a/ramp/__init__.py +++ b/ramp/__init__.py @@ -18,6 +18,7 @@ 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__ = ( diff --git a/ramp/ramp_convert_old_input_files.py b/ramp/ramp_convert_old_input_files.py index 747e30e8..9fc1355a 100644 --- a/ramp/ramp_convert_old_input_files.py +++ b/ramp/ramp_convert_old_input_files.py @@ -8,7 +8,7 @@ parser = argparse.ArgumentParser( prog="python ramp_convert_old_input_files.py", - description="Convert old python input files to xlsx ones", + description="Convert RAMP python input files to xlsx ones", ) parser.add_argument( "-i", @@ -88,7 +88,7 @@ def convert_old_user_input_file( 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") @@ -105,3 +105,6 @@ def convert_old_user_input_file( ) else: convert_old_user_input_file(fname, output_path=output_path, suffix=suffix) + +if __name__ == "__main__": + cli() diff --git a/setup.py b/setup.py index 1dffb354..f833a97a 100644 --- a/setup.py +++ b/setup.py @@ -39,6 +39,7 @@ entry_points={ "console_scripts": [ "ramp=ramp.cli:main", + "ramp_convert=ramp.ramp_convert_old_input_files:cli", ], }, ) From 7472b9f891d14279ea1d905573729e3ebf34727a Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Mon, 13 May 2024 13:56:33 +0200 Subject: [PATCH 02/10] Fix Windows bug for py to xlsx converter --- ramp/ramp_convert_old_input_files.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ramp/ramp_convert_old_input_files.py b/ramp/ramp_convert_old_input_files.py index 9fc1355a..e89a1e50 100644 --- a/ramp/ramp_convert_old_input_files.py +++ b/ramp/ramp_convert_old_input_files.py @@ -81,6 +81,8 @@ 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.abspath(fname_path)) file_module = importlib.import_module(fname) user_list = file_module.User_list From dc4be52cabd69bfb943a43f21699c3bfac1a73a3 Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Mon, 13 May 2024 14:18:26 +0200 Subject: [PATCH 03/10] Update documentation about ramp and ramp_convert commands --- README.rst | 5 +++-- ramp/cli.py | 4 +++- ramp/ramp_convert_old_input_files.py | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 50be80fd..f7084d38 100644 --- a/README.rst +++ b/README.rst @@ -194,11 +194,12 @@ Other options are documented in the help of `ramp`, which you access with the `` If you have existing python input files, you can convert them to -spreadsheet. To do so, go to the ``\ramp`` folder and run +spreadsheet. Simply run .. code-block:: bash - python ramp_convert_old_input_files.py -i + ramp_convert -i + For other examples of command lines options, such as setting date ranges, please visit `the dedicated section `_ of the documentation. diff --git a/ramp/cli.py b/ramp/cli.py index e53ab22e..dc6a19fe 100644 --- a/ramp/cli.py +++ b/ramp/cli.py @@ -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", diff --git a/ramp/ramp_convert_old_input_files.py b/ramp/ramp_convert_old_input_files.py index e89a1e50..7c753261 100644 --- a/ramp/ramp_convert_old_input_files.py +++ b/ramp/ramp_convert_old_input_files.py @@ -7,7 +7,7 @@ from ramp.core.core import UseCase parser = argparse.ArgumentParser( - prog="python ramp_convert_old_input_files.py", + prog="ramp_convert", description="Convert RAMP python input files to xlsx ones", ) parser.add_argument( From 7592a7bee135281c926a4089613185b75e7fba56 Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Mon, 13 May 2024 14:43:37 +0200 Subject: [PATCH 04/10] Fix Windows path of .py to convert to .xlsx --- ramp/ramp_convert_old_input_files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ramp/ramp_convert_old_input_files.py b/ramp/ramp_convert_old_input_files.py index 7c753261..6ed48416 100644 --- a/ramp/ramp_convert_old_input_files.py +++ b/ramp/ramp_convert_old_input_files.py @@ -82,7 +82,7 @@ def convert_old_user_input_file( output_fname = os.path.join(output_path, output_fname) if os.name != "posix": - sys.path.insert(0, os.path.abspath(fname_path)) + sys.path.insert(0, os.path.dirname(os.path.abspath(fname_path))) file_module = importlib.import_module(fname) user_list = file_module.User_list From aadb8384a1b1d14d2fef1081f01c7f042175aeda Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Tue, 14 May 2024 15:56:34 +0200 Subject: [PATCH 05/10] Lint with black --- ramp/__init__.py | 4 +++- ramp/cli.py | 2 +- ramp/ramp_convert_old_input_files.py | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ramp/__init__.py b/ramp/__init__.py index df02155e..235e2796 100644 --- a/ramp/__init__.py +++ b/ramp/__init__.py @@ -18,7 +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 +from ramp.ramp_convert_old_input_files import ( + convert_old_user_input_file as ramp_py2xlsx, +) __authors__ = "Listed in AUTHORS" __copyright__ = ( diff --git a/ramp/cli.py b/ramp/cli.py index dc6a19fe..619f9c9e 100644 --- a/ramp/cli.py +++ b/ramp/cli.py @@ -11,7 +11,7 @@ parser = argparse.ArgumentParser( prog="ramp", description="Execute RAMP code", - epilog="To convert '.py' input files into '.xlsx' input files use the command 'ramp_convert'" + epilog="To convert '.py' input files into '.xlsx' input files use the command 'ramp_convert'", ) parser.add_argument( "-i", diff --git a/ramp/ramp_convert_old_input_files.py b/ramp/ramp_convert_old_input_files.py index 6ed48416..5622ded2 100644 --- a/ramp/ramp_convert_old_input_files.py +++ b/ramp/ramp_convert_old_input_files.py @@ -108,5 +108,6 @@ def cli(): else: convert_old_user_input_file(fname, output_path=output_path, suffix=suffix) + if __name__ == "__main__": cli() From eb71971015672bcd9d66a89c376f7b56bb955a3a Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Wed, 15 May 2024 11:56:04 +0200 Subject: [PATCH 06/10] Fix pandas deprecation warnings and update doc --- docs/notebooks/fixed_flat_app.ipynb | 10 ---------- docs/source/examples/fixed_flat_app/fixed_flat_app.rst | 8 -------- ramp/cli.py | 8 ++++---- 3 files changed, 4 insertions(+), 22 deletions(-) diff --git a/docs/notebooks/fixed_flat_app.ipynb b/docs/notebooks/fixed_flat_app.ipynb index 3a2791ed..c85348d5 100644 --- a/docs/notebooks/fixed_flat_app.ipynb +++ b/docs/notebooks/fixed_flat_app.ipynb @@ -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": [ diff --git a/docs/source/examples/fixed_flat_app/fixed_flat_app.rst b/docs/source/examples/fixed_flat_app/fixed_flat_app.rst index 4af34757..2a9d1cfd 100644 --- a/docs/source/examples/fixed_flat_app/fixed_flat_app.rst +++ b/docs/source/examples/fixed_flat_app/fixed_flat_app.rst @@ -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() diff --git a/ramp/cli.py b/ramp/cli.py index 619f9c9e..394f2387 100644 --- a/ramp/cli.py +++ b/ramp/cli.py @@ -202,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 @@ -222,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") From dbafb77912bd6235042e475033d22fb4c9ac7119 Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Wed, 15 May 2024 11:56:27 +0200 Subject: [PATCH 07/10] Add py to xlsx conversion fonction to the API doc --- docs/source/api_references.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/api_references.rst b/docs/source/api_references.rst index c19abb51..ffc68c09 100644 --- a/docs/source/api_references.rst +++ b/docs/source/api_references.rst @@ -76,6 +76,7 @@ Utilities load_data download_example + ramp_py2xlsx ************* From 6513851436bf7f31193b1e5e0143f93b62111265 Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Wed, 15 May 2024 12:22:55 +0200 Subject: [PATCH 08/10] Improve docstring --- ramp/ramp_convert_old_input_files.py | 29 ++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/ramp/ramp_convert_old_input_files.py b/ramp/ramp_convert_old_input_files.py index 5622ded2..7b740c3d 100644 --- a/ramp/ramp_convert_old_input_files.py +++ b/ramp/ramp_convert_old_input_files.py @@ -7,8 +7,7 @@ from ramp.core.core import UseCase parser = argparse.ArgumentParser( - prog="ramp_convert", - description="Convert RAMP python input files to xlsx ones", + prog="ramp_convert", description="Convert RAMP python input files to xlsx ones" ) parser.add_argument( "-i", @@ -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", @@ -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 From c8d68544145b4d9ae33ec3166d7f566b5f8878f3 Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Wed, 15 May 2024 12:24:35 +0200 Subject: [PATCH 09/10] Improve README section about conversion from py to xlsx --- README.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index f7084d38..2ef6da3f 100644 --- a/README.rst +++ b/README.rst @@ -193,13 +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. Simply 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 - ramp_convert -i + ramp_convert -i +If you want to save a RAMP model you created with a .py file into a spreadsheet refer to +this `example `_ For other examples of command lines options, such as setting date ranges, please visit `the dedicated section `_ of the documentation. From 5ea07a1ec4676acaff2ef4030f23e9d6a5159307 Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Wed, 15 May 2024 12:38:45 +0200 Subject: [PATCH 10/10] Add test for command line conversion --- tests/test_input_file_conversion.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_input_file_conversion.py b/tests/test_input_file_conversion.py index a2f259e3..68f30e78 100644 --- a/tests/test_input_file_conversion.py +++ b/tests/test_input_file_conversion.py @@ -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)