Skip to content

Only convert unit when nessecary - #361

Draft
damskii9992 wants to merge 2 commits into
developfrom
speed_up_dependent_parameters_a_little
Draft

damskii9992 wants to merge 2 commits into
developfrom
speed_up_dependent_parameters_a_little

Conversation

@damskii9992

@damskii9992 damskii9992 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

This PR only converts the unit in the add/sub dunder methods when the unit actually needs to be converted, saving some calculation time.

@damskii9992 damskii9992 added [scope] maintenance Code/tooling cleanup, no feature or bugfix (major.minor.PATCH) [priority] low Low importance labels Nov 5, 2026
@damskii9992
damskii9992 requested a review from rozyczko November 5, 2026 14:24
@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.31169% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.53%. Comparing base (03d807d) to head (45e0376).

Files with missing lines Patch % Lines
src/easyscience/variable/parameter.py 84.74% 9 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #361      +/-   ##
===========================================
- Coverage    92.62%   92.53%   -0.10%     
===========================================
  Files           47       47              
  Lines         3350     3374      +24     
===========================================
+ Hits          3103     3122      +19     
- Misses         247      252       +5     
Flag Coverage Δ
unittests 92.53% <88.31%> (-0.10%) ⬇️

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

Files with missing lines Coverage Δ
src/easyscience/variable/descriptor_number.py 93.16% <100.00%> (+0.09%) ⬆️
src/easyscience/variable/parameter.py 94.29% <84.74%> (-0.67%) ⬇️

@damskii9992
damskii9992 marked this pull request as draft November 5, 2026 15:34

@rozyczko rozyczko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would also be nice to have some benchmark numbers in the PR body, since this is what you're aiming at.
Also the name is misleading, there's nothing about dependent parameters here, it's about parameter arithmetics

return NotImplemented
return Parameter.from_scipp(full_value=new_full_value, min=min_value, max=max_value)

def __sub__(self, other: DescriptorNumber | Parameter | numbers.Real) -> Parameter:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did you forget to change __sub__?

Comment thread src/easyscience/variable/parameter.py Outdated
min_value = other.value - self.max
max_value = other.value - self.min
self._convert_unit(original_unit)
if other.unit != self.unit:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

isn't other.unit != self.unit check expensive, since it builds actual strings?

wouldn't a._scalar.unit != b._scalar_unit using scipp Unit be much faster?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Indeed it would be almost 10x faster

(era) C:\Users\piotr\projects\easy\ERA\core>python -m timeit -s "from easyscience.variable import DescriptorNumber as D; a=D(1.0,unit='m'); b=D(2.0,unit='m')" "a.unit != b.unit"
50000 loops, best of 5: 9.95 usec per loop

(era) C:\Users\piotr\projects\easy\ERA\core>python -m timeit -s "from easyscience.variable import DescriptorNumber as D; a=D(1.0,unit='m'); b=D(2.0,unit='m')" "a._scalar.unit != b._scalar.unit"
200000 loops, best of 5: 1.43 usec per loop

Comment thread src/easyscience/variable/parameter.py Outdated
Comment on lines +976 to +983
if other.unit != self.unit:
original_unit = other.unit
try:
other._convert_unit(self.unit)
except UnitError:
raise UnitError(
f'Values with units {self.unit} and {other.unit} cannot be added'
) from None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Those checks are identical in all methods. Refactor out?

if isinstance(other, Parameter)
else self.max + other.value
)
return Parameter.from_scipp(full_value=new_full_value, min=min_value, max=max_value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

remove this. We are already returning in line 1011

@rozyczko

rozyczko commented Nov 5, 2026

Copy link
Copy Markdown
Member

NVM I just noticed you put this PR in draft

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[priority] low Low importance [scope] maintenance Code/tooling cleanup, no feature or bugfix (major.minor.PATCH)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants