Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate autoreporting mvs #284

Merged
merged 43 commits into from May 15, 2020
Merged

Conversation

Bachibouzouk
Copy link
Collaborator

@Bachibouzouk Bachibouzouk commented Apr 27, 2020

Fix #282
Fix #310

Changes proposed in this pull request:
Fixed:

The following steps were realized, as well (if applies):

  • Use in-line comments to explain your code
  • Write docstrings to your code
  • For new functionalities: Explain in readthedocs
  • Write test(s) for your new patch of code
  • Update the CHANGELOG.md
  • Apply black (black . --exclude docs/)
  • Check if benchmark tests pass locally (pytest tests/test_benchmark.py)

For more information on how to contribute check the CONTRIBUTING.md.

@Bachibouzouk
Copy link
Collaborator Author

@mahendrark - check the changes I made here to successfully run the code :)

@smartie2076
Copy link
Collaborator

Doesn`t this solely implement parsing the argument that initiates the pdf report? I can not see that F2 is actually integrated ie. executed by the mvs jet. Can you change the title of this PR in that case?

@Bachibouzouk
Copy link
Collaborator Author

Doesn`t this solely implement parsing the argument that initiates the pdf report? I can not see that F2 is actually integrated ie. executed by the mvs jet. Can you change the title of this PR in that case?

I wrote the function print_pdf() which can be called for that, I haven't got the time to integrate it yesterday because it was not easy to figure out how to pack a server in a function and kill it after generating a pdf. I also don't know if the function works on windows. I should have created a draft PR not a PR I agree

@smartie2076
Copy link
Collaborator

Doesn`t this solely implement parsing the argument that initiates the pdf report? I can not see that F2 is actually integrated ie. executed by the mvs jet. Can you change the title of this PR in that case?

I wrote the function print_pdf() which can be called for that, I haven't got the time to integrate it yesterday because it was not easy to figure out how to pack a server in a function and kill it after generating a pdf. I also don't know if the function works on windows. I should have created a draft PR not a PR I agree

Ah, okay! I did not find your comment about the server anymore, so I was not sure if you wanted to tackle this here, too.

@smartie2076 smartie2076 marked this pull request as draft April 30, 2020 20:56
@Bachibouzouk Bachibouzouk force-pushed the feature/integrate-autoreporting-mvs branch 3 times, most recently from d35ca58 to 1debd24 Compare May 3, 2020 22:17
@Bachibouzouk Bachibouzouk marked this pull request as ready for review May 3, 2020 22:18
@Bachibouzouk
Copy link
Collaborator Author

@mahendrark, please make your changes to the F2 reporting from this branch and as PR towards this branch

@Bachibouzouk
Copy link
Collaborator Author

@mahendrark - could you push your changes so we can merge them in this branch and merge this branch in dev. Right now what prevent me to do so are the non-general calls to specific values of the output. I mean non-general in the sense that they only apply to a specific simulation. We should write the code so that it can autogenerate a report for any kind of output folder. In the present situation the call to access directly "input_timeseries_Habor_kW.png" which prevent us to autogenerate reports for any simulation which would not have generated this file (which is specific to only one of the pilots).

I can make the code more general, however I understood you were tackling this issue so I don't want to step on your toes, let me know :)

@mahendrark
Copy link
Contributor

@Bachibouzouk
I will make the code changes required to make the code general and then push the changes once I am done with that.

@smartie2076
Copy link
Collaborator

Nice @Bachibouzouk , that you have figured out how to store the report as pdf!

I am exited to see the the report being more integrated to the mvs @mahendrark :) Let me know if you do not know how to acess specific values in the PR request that you create for that.

@mahendrark
Copy link
Contributor

mahendrark commented May 13, 2020

@Bachibouzouk, @smartie2076

The solution to that issue is basically plotting the graphs in F2, instead of looking for the images of the plots in Outputs folder.

Shall I plot all the necessary graphs with Plotly then?

@Bachibouzouk
Copy link
Collaborator Author

Bachibouzouk commented May 13, 2020

The quick and dirty solution is to add .png image one after the other at the end of the report, I would go for this one at first so that it unlocks #284. I think plots with plotly make sense then :)

@smartie2076 has the last word on that though

@smartie2076
Copy link
Collaborator

The quick and dirty solution is to add .png image one after the other at the end of the report, I would go for this one at first so that it unlocks #284. I think plots with plotly make sense then :)

@smartie2076 has the last word on that though

I agree, lets keep the png for now. For that, I could also adapt the resulting json so that it includes the paths to all generated plots. We can talk about that in our call later.

@smartie2076
Copy link
Collaborator

Hi @Bachibouzouk ! There are actually issues before I or mahendra can continue working on this PR.

First issue, when executing ´python mvs_tool.py -i inputs -f -ext csv`. It does not terminate the simulation or anything:

Traceback (most recent call last):
  File "mvs_tool.py", line 4, in <module>
    main()
  File "D:\PycharmProjects\mvs_eland\mvs_eland_tool\mvs_eland_tool.py", line 154, in main
    output_processing.evaluate_dict(dict_values)
  File "D:\PycharmProjects\mvs_eland\src\F0_output.py", line 108, in evaluate_dict
    dict_values["simulation_settings"]["path_output_folder"],
  File "D:\PycharmProjects\mvs_eland\src\F2_autoreport.py", line 87, in create_app
    dfprojectData = pd.read_csv(os.path.join(path_input_folder, "project_data.csv"))
  File "D:\PycharmProjects\mvs_eland\venv_windows\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "D:\PycharmProjects\mvs_eland\venv_windows\lib\site-packages\pandas\io\parsers.py", line 429, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "D:\PycharmProjects\mvs_eland\venv_windows\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)
  File "D:\PycharmProjects\mvs_eland\venv_windows\lib\site-packages\pandas\io\parsers.py", line 1122, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "D:\PycharmProjects\mvs_eland\venv_windows\lib\site-packages\pandas\io\parsers.py", line 1853, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 387, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas\_libs\parsers.pyx", line 705, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File b'inputs\\project_data.csv' does not exist: b'inputs\\project_data.csv'

Second issue, when executing ´python src/F2_autoreport.py´ (even after installing ´wkhtmltopdf ´, which has to be added to the requirements.txt), the pdf is not generated:

Loading formatted geocoded file...
 * Serving Flask app "F2_autoreport" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
INFORMATION: Es konnten keine Dateien mit dem angegebenen
Muster gefunden werden.
Traceback (most recent call last):
  File "D:\PycharmProjects\mvs_eland\venv_windows\lib\site-packages\pdfkit\configuration.py", line 21, in __init__
    with open(self.wkhtmltopdf) as f:
FileNotFoundError: [Errno 2] No such file or directory: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "src/F2_autoreport.py", line 810, in <module>
    print_pdf(app)
  File "src/F2_autoreport.py", line 32, in print_pdf
    pdfkit.from_url("http://127.0.0.1:8050", path_report_pdf)
  File "D:\PycharmProjects\mvs_eland\venv_windows\lib\site-packages\pdfkit\api.py", line 24, in from_url
    configuration=configuration, cover_first=cover_first)
  File "D:\PycharmProjects\mvs_eland\venv_windows\lib\site-packages\pdfkit\pdfkit.py", line 42, in __init__
    self.configuration = (Configuration() if configuration is None
  File "D:\PycharmProjects\mvs_eland\venv_windows\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
    'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
OSError: No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

I will probably do some coding on Friday, in case we need to have a call on this.

@Bachibouzouk
Copy link
Collaborator Author

First issue, when executing ´python mvs_tool.py -i inputs -f -ext csv`. It does not terminate the simulation or anything:

I know the code does not work smoothly, that is what @mahendrark should solve :) (i did comment lines so that the code works on my end)

@smartie2076
Copy link
Collaborator

First issue, when executing ´python mvs_tool.py -i inputs -f -ext csv`. It does not terminate the simulation or anything:

I know the code does not work smoothly, that is what @mahendrark should solve :) (i did comment lines so that the code works on my end)

Do you know if the solution to issue 1 and issue 2 is one and the same for both? Because the link to ´inputs/project_data.csv´ seems to be something with relative paths (issue 1), while for issue 2 basically might be wrong because of any of the png. Is there a way to print the name of the file that it can not find?

@Bachibouzouk
Copy link
Collaborator Author

Issue for 2) might be Windows ... I didn't have it...

@smartie2076
Copy link
Collaborator

Issue 1) will be solved by @Bachibouzouk. Issue 2 is due to issues installing wkhtmltopdf, and the output was generated now. I will create a dictionary of paths to the created png files so that they can easily be called from F2. For this PR, we do not need the plotlydash plots but will only use pngs.

@mahendrark : Please refer to this on how to get the pdf to compile!

@smartie2076
Copy link
Collaborator

With the last commits, I created a dictionary of paths to plots, that can be accessed for the pdf report. This is now included in ´json_with_results.json´:

    "paths_to_plots": {
        "costs": [
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\annuity.png",
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\annuity_minor.png",
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\operation_and_maintenance_costs.png",
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\operation_and_maintenance_costs_minor.png"
        ],
        "performance": [
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\optimal_additional_capacities.png"
        ],
        "flows_on_busses": [
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\Electricity_flows_365_days.png",
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\Electricity bus_flows_365_days.png",
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\PV plant (mono) bus_flows_365_days.png",
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\ESS Li-Ion bus_flows_365_days.png",
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\Electricity (DSO) bus_flows_365_days.png"
        ],
        "nx": [],
        "demands": [
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\input_timeseries_Habor_kW.png",
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\input_timeseries_Remaining demand_kW.png",
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\input_timeseries_Shore power (docked ships)_kW.png"
        ],
        "resources": [
            "D:\\PycharmProjects\\mvs_eland\\MVS_outputs\\input_timeseries_PV plant (mono)_kW.png"
        ]
    },

The simulation does not run through due to issue 1. Also, I have not added tests for the generation of this dict yet (not sure if I have to, as they will be removed with plotlydash plots anyway). I need issue 1 to be fixed first to fix the failing tests.

I have indicated in following pdf (from standalone execution of F2) where the different plots should go. @Bachibouzouk - please look out for PF, those messages are for you, @mahendrark please look out for MR, those remarks are for you.

out_commented.pdf

@Bachibouzouk : Do you want to take over here and reference the paths in F2? This would take a long time for me to understand, and I would have no clue if simple loops would work.

You can find the constants with which to access the json dict of plots here.

@mahendrark please wait for @Bachibouzouk to be done with integrating the png, before you start finetuning the rest.

@Bachibouzouk
Copy link
Collaborator Author

@smartie2076, @mahendrark - it does run through now, however pdfkit has a problem to embed the images in the outputed pdf. In my last commit I indicated where you can increase the time to be able to vizualise the report in your browser before the "print_to_pdf" command is executed :)

@smartie2076
Copy link
Collaborator

Thank you @Bachibouzouk !

The problem seems to have to do with the time allowed to store the pdf or something.
If I use

time.sleep(0) / time.sleep(10)

The images are not stored: out.pdf

time.sleep(60)

Nothing is stored at all: out.pdf

@smartie2076
Copy link
Collaborator

Okay, there still are 14 failing tests, but that doesn´t stop us from working further on this. @mahendrark we can have a call on what you can do today or tomorrow.

@Bachibouzouk
Copy link
Collaborator Author

Thank you @Bachibouzouk !

The problem seems to have to do with the time allowed to store the pdf or something.
If I use

time.sleep(0) / time.sleep(10)

The images are not stored: out.pdf

time.sleep(60)

Nothing is stored at all: out.pdf

This time is the time one waits before executing the command. I think the problem lies within the html to pdf converter, because if you set 60 seconds, then you can visit 127.0.0.1:8050 and you see the whole report. Another problem might be that the dash renderer is not properly called by the pdf converter... (The renderer turns tthe phyrhon code into html code which is then rendered by. Browser)

@smartie2076
Copy link
Collaborator

This time is the time one waits before executing the command. I think the problem lies within the html to pdf converter, because if you set 60 seconds, then you can visit 127.0.0.1:8050 and you see the whole report. Another problem might be that the dash renderer is not properly called by the pdf converter... (The renderer turns tthe phyrhon code into html code which is then rendered by. Browser)

I know that the .sleep() is giving me time to open the browser - I just thought that it might be a hint to whats going wrong, if this .sleep() actually influences the pdf that is being generated...? It seemed very weird to me.

Another question: Would it easily be possible to store the generated html? That could help us to finetune the formatting while the pdf is not generated. Also, later when we include dashplots, it may retain the interactive nature of the plots...?

@smartie2076
Copy link
Collaborator

Sorry for the multiple pushes, somehow the pytests locally result in something different then the travis tests :/

PLOTS_COSTS,
)

dict_plots = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would tend to create such a dict_plot in constants.py

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... yeah, makes sense. But don´t I need to use .deepcopy() then to make sure that the changes in eg. dict_values are not applied to the dict_plots in the tests as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d1 = dict(a=1, b=2)
d2 = dict(c=3)
d2.update(d1)
print(d2)
d1['d']=4
print(d2)

d2 is not modified, so probably updates takes care of that for you :)

@Bachibouzouk Bachibouzouk force-pushed the feature/integrate-autoreporting-mvs branch from d69aadc to 34db9d8 Compare May 15, 2020 20:46
@Bachibouzouk Bachibouzouk merged commit 10dac5f into dev May 15, 2020
@Bachibouzouk Bachibouzouk deleted the feature/integrate-autoreporting-mvs branch May 15, 2020 21:24
@mahendrark
Copy link
Contributor

Hey all,

I am getting this error when I try to run f2 script:

Traceback (most recent call last):
File "/home/mr/Projects/mvs_eland/src/F2_autoreport.py", line 736, in
test_app = create_app(CSV_FOLDER, OUTPUT_FOLDER)
TypeError: create_app() takes 1 positional argument but 2 were given

@smartie2076
Copy link
Collaborator

Hey all,

I am getting this error when I try to run f2 script:

Traceback (most recent call last):
File "/home/mr/Projects/mvs_eland/src/F2_autoreport.py", line 736, in
test_app = create_app(CSV_FOLDER, OUTPUT_FOLDER)
TypeError: create_app() takes 1 positional argument but 2 were given

Hi @mahendrark ! As this PR is closed, can you please create an issue and report a bug with the bug report template? This will also help us to keep track of existing bugs and whether they are solved.

Also, if you include me/PF with @+github handle, we automatically receive a mail :)

@mahendrark
Copy link
Contributor

@smartie2076 thank you

Will do that right away :)

@Bachibouzouk Bachibouzouk mentioned this pull request May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Logging error connected to maximumCap The module F2 is not integrated into the mvs
3 participants