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

Add units to ResultsIndex #330

Merged
merged 5 commits into from
Mar 13, 2023
Merged

Add units to ResultsIndex #330

merged 5 commits into from
Mar 13, 2023

Conversation

PProfizi
Copy link
Contributor

@PProfizi PProfizi commented Mar 10, 2023

  • Store unit of each result in ResultsIndex.
  • Have them appear in the string representation of ResultsIndex, and thus of DataFrame.

We should also think of manipulating UnitSystem objects from PyDPF-Core -> at the Simulation level, to switch UnitSystem.

  • Expose a ResultsIndex.units(List[str]) setter? Exposed at the DataFrame level to have DataFrame.units() setter, checking if units are the same and if not, tries to apply a math.unit_convert_fc.

@PProfizi PProfizi added the enhancement New feature or request label Mar 10, 2023
@PProfizi PProfizi self-assigned this Mar 10, 2023
@PProfizi PProfizi requested review from cbellot000 and germa89 March 10, 2023 10:46
@codecov
Copy link

codecov bot commented Mar 10, 2023

Codecov Report

Merging #330 (3a90a63) into master (7ee22a4) will increase coverage by 0.04%.
The diff coverage is 93.33%.

@@            Coverage Diff             @@
##           master     #330      +/-   ##
==========================================
+ Coverage   82.68%   82.72%   +0.04%     
==========================================
  Files          35       35              
  Lines        3170     3184      +14     
==========================================
+ Hits         2621     2634      +13     
- Misses        549      550       +1     

Copy link
Contributor

@germa89 germa89 left a comment

Choose a reason for hiding this comment

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

LGTM! :)

Comment on lines 173 to 178
for i, unit in enumerate(units):
result_values[i] += f" ({unit})" if unit is not None else ""
if len(units) < len(values):
units.extend([None] * (len(values) - len(units)))
else:
units = [None] * len(values)
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably we want to avoid writing "()" if unit is None. :)

And personally, I read better the if before the for. But this is a nit pick :).

Suggested change
for i, unit in enumerate(units):
result_values[i] += f" ({unit})" if unit is not None else ""
if len(units) < len(values):
units.extend([None] * (len(values) - len(units)))
else:
units = [None] * len(values)
if len(units) < len(values):
units.extend([None] * (len(values) - len(units)))
for i, unit in enumerate(units):
result_values[i] = f" ({unit})" if unit is not None
else:
units = [None] * len(values)

Copy link
Contributor Author

@PProfizi PProfizi Mar 13, 2023

Choose a reason for hiding this comment

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

Probably we want to avoid writing "()" if unit is None. :)

And personally, I read better the if before the for. But this is a nit pick :).

Hi @germa89! Thx for the review! Just to nit pick too, the original line 174 does actually write "" if unit is None ;)

Also, the if after the for is because the operation of adding the unit to the string rep is not needed for None values of units, which is all we are adding in the if.
Thus having the if before the for might be a bit more readable, but is adds unnecessary operations. What I will do is add comments so it is more readable because you are right, it is not really readable.

Comment on lines 141 to 152
ref = (
"DataFrame<index=MultiIndex<[MeshIndex<name=\"node_ids\", dtype=<class 'int'>>, "
"Index<name=\"components\", dtype=<class 'str'>>]>, columns=MultiIndex<[ResultIndex<['U']>, " # noqa: E501
"SetIndex<values=[1]>]>>"
"DataFrame<"
"index=MultiIndex<["
"MeshIndex<name=\"node_ids\", dtype=<class 'int'>>, "
"Index<name=\"components\", dtype=<class 'str'>>"
"]>, "
"columns=MultiIndex<["
"ResultIndex<['U (m)']>, "
"SetIndex<values=[1]>"
"]>>"
)
assert repr(df) == ref
Copy link
Contributor

Choose a reason for hiding this comment

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

You might want to use this pytest plugin to automatize "golden" tests

https://github.com/oprypin/pytest-golden#pytest-golden

Feel free to ping me about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh nice, thx @germa89, I did not know this plugin!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you know if it works well for numerical values?

@PProfizi PProfizi merged commit 5c5d79a into master Mar 13, 2023
@PProfizi PProfizi deleted the feat/results_index_unit branch March 13, 2023 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants