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

HOTFIX: 2D .CSV Function and missing set_get_value_opt call #478

Merged
merged 4 commits into from
Nov 22, 2023

Conversation

MateusStano
Copy link
Member

Pull request type

  • Code changes (bugfix, features)

Checklist

  • Tests for the changes have been added (if needed)
  • Docs have been reviewed and added / updated
  • Lint (black rocketpy/ tests/) has passed locally
  • All tests (pytest --runslow) have passed locally

Current behavior

If you create a Function based on a 2D .csv file, it will not work properly due to the interpolation being set to None. It also has no get_value_opt defined

New behavior

2D .csv based Functions now work as expected.

Also, tests checking the issues here have beend added

Breaking change

  • Yes
  • No

Copy link

codecov bot commented Nov 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (hotfix/v1.1.1@7d45342). Click here to learn what that means.

Additional details and impacted files
@@               Coverage Diff                @@
##             hotfix/v1.1.1     #478   +/-   ##
================================================
  Coverage                 ?   70.87%           
================================================
  Files                    ?       55           
  Lines                    ?     9236           
  Branches                 ?        0           
================================================
  Hits                     ?     6546           
  Misses                   ?     2690           
  Partials                 ?        0           
Flag Coverage Δ
unittests 70.87% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MateusStano MateusStano changed the base branch from master to hotfix/v1.1.1 November 21, 2023 22:59
Comment on lines +2828 to +2833
if isinstance(source, (list, np.ndarray, str, Path)):
# Deal with csv or txt
if isinstance(source, (str, Path)):
# Convert to numpy array
source = np.loadtxt(source, delimiter=",", dtype=float)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I understand that this is necessary in order to check the validity of the inputs/outputs, but it is quite strange to read the csv here, throw away the result, and read it again in set_source.

Copy link
Member

Choose a reason for hiding this comment

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

I would say that loading it twice is really not a good idea. It can be a huge problem if the CSV file is large and takes a while to read.

Furthermore, I would not recommend changing this in this hotfix PR. It doesn't exactly fix a bug.

Copy link
Member Author

Choose a reason for hiding this comment

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

I understand that this is necessary in order to check the validity of the inputs/outputs, but it is quite strange to read the csv here, throw away the result, and read it again in set_source.

Yep, it is pretty strange, but the whole _check_user_input is already strange. It is called twice (and I am not sure but depending on the source type it might be called 3 times) during the __init__. Also it has to process the source to be able to check if it is a valid input, but it does not save that processing, the saving is left to be dealt in set_source where it has to do all the calculations again. The source is processed twice for sources of type list and np.array as well

And if the source is anything besides (list, np.ndarray, str, Path) this _check_user_input method does essentially nothing

Also _check_user_input is a static_method for some reason?

What I am getting at is that all the user's inputs arguments should not be checked inside the same function, but rather in the calls of the set methods for those attributes. Maybe even inside .setter decorators. Structure wise, this method does not really work for the Function class, but this is a only a hotfix pr, we can focus on improving the method later

Furthermore, I would not recommend changing this in this hotfix PR. It doesn't exactly fix a bug.

This str and Path check is half of the bug corrections in this PR. Without these lines 2D .csv Functions will not have interpolation

Copy link
Collaborator

@phmbressan phmbressan Nov 22, 2023

Choose a reason for hiding this comment

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

Indeed, in retrospect, it is a bit odd to perform the same operations multiple times in check inputs and while setting the source.

I approved the PR, since I don't think changes to the check method is in the scope of a hotfix. The PR fixes the issue with csv_2d. In a future PR the check inputs could be made as a way of pre-processing the source so as to only return a np.array or a callable to the set_source.

This would decouple responsabilities a fair bit and could be implemented in various ways, maybe with functools.singledispatch.

if isinstance(source, (list, np.ndarray)):
if isinstance(source, (list, np.ndarray, str, Path)):
# Deal with csv or txt
if isinstance(source, (str, Path)):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe a try-except with FileNotFound would be beneficial here, just because it is a good practice. But again, this is also done in set_source.

Comment on lines +2828 to +2833
if isinstance(source, (list, np.ndarray, str, Path)):
# Deal with csv or txt
if isinstance(source, (str, Path)):
# Convert to numpy array
source = np.loadtxt(source, delimiter=",", dtype=float)

Copy link
Member

Choose a reason for hiding this comment

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

I would say that loading it twice is really not a good idea. It can be a huge problem if the CSV file is large and takes a while to read.

Furthermore, I would not recommend changing this in this hotfix PR. It doesn't exactly fix a bug.

tests/conftest.py Outdated Show resolved Hide resolved
@MateusStano MateusStano merged commit e18ca22 into hotfix/v1.1.1 Nov 22, 2023
11 checks passed
@MateusStano MateusStano deleted the hotfix/csv-2d-function branch November 22, 2023 22:13
@MateusStano MateusStano mentioned this pull request Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

3 participants