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

Fix unnecessary and harmful copy of pandas objects #147

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ In this file noteworthy changes of new releases of pyLife are documented since
* Drop support for python-3.8 ast it is end of life


### Minor improvments
### Minor improvements

* Connectivity information is now transfered from odbserver to odbclient as
* Connectivity information is now transferred from odbserver to odbclient as
ints.


### Bugfixes

* Fix unnecessary and harmful copy of pandas object in Wöhler classes (#146)


## pylife-2.1.3

### Improvements
Expand Down
4 changes: 0 additions & 4 deletions src/pylife/materiallaws/woehlercurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ class WoehlerCurve(PylifeSignal):
or calculated from ``TN`` if given.
"""

def __init__(self, pandas_obj):
self._obj = pandas_obj.copy()
self._validate()
Copy link
Contributor

Choose a reason for hiding this comment

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

But what is the difference now? The exact same constructor is in PylifeSignal so nothing changed?

Copy link
Contributor

Choose a reason for hiding this comment

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

ah except the copy


def _validate(self):
self.fail_if_key_missing(['k_1', 'ND', 'SD'])
self._k_2 = self._obj.get('k_2', np.inf)
Expand Down
15 changes: 4 additions & 11 deletions src/pylife/strength/woehler_fkm_nonlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ class WoehlerCurvePRAM(PylifeSignal):
* ``P_RAM_D`` : The damage parameter value of the endurance limit of the component, computed as P_RAM_D_WS / f_RAM (FKM nonlinear, eq. 2.6-89)
"""

def __init__(self, pandas_obj):
self._obj = pandas_obj.copy()
self._validate()

def _validate(self):
self.fail_if_key_missing(['P_RAM_Z', 'P_RAM_D', 'd_1', 'd_2'])

Expand Down Expand Up @@ -195,13 +191,6 @@ class WoehlerCurvePRAJ(PylifeSignal):
* ``P_RAJ_D_0`` : The initial load level of the endurance limit
"""

def __init__(self, pandas_obj):
self._obj = pandas_obj.copy()
self._validate()

# eq. (2.9-27)
self._P_RAJ_D = self._obj.P_RAJ_D_0

def _validate(self):
self.fail_if_key_missing(['P_RAJ_Z', 'P_RAJ_D_0', 'd_RAJ'])

Expand All @@ -212,6 +201,10 @@ def _validate(self):
if self._obj.d_RAJ >= 0:
raise ValueError(f"d_RAJ ({self._obj.d_RAJ}) has to be negative!")

# eq. (2.9-27)
self._P_RAJ_D = self._obj.P_RAJ_D_0


def update_P_RAJ_D(self, P_RAJ_D):
"""This method is used to update the fatigue strength P_RAJ_D, which is stored in this woehler curve.

Expand Down
16 changes: 16 additions & 0 deletions tests/materiallaws/test_woehlercurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,19 @@ def test_load_is_basquin_load(SD, k_1, ND, cycles):
def test_cycles_is_basquin_cycles(SD, k_1, ND, load):
wc = WoehlerCurve.from_parameters(SD=SD, k_1=k_1, ND=ND)
assert wc.cycles(load) == wc.basquin_cycles(load)


def test_changed_TN():
wc = pd.Series({
'k_1': 7.,
'TN': 1.75,
'ND': 1e6,
'SD': 300.0
})

_ = wc.woehler

wc['TN'] = 1.48
new_wc = wc.woehler.to_pandas()

assert new_wc['TN'] == 1.48
29 changes: 29 additions & 0 deletions tests/strength/test_woehler_fkm_nonlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ def test_woehler_curve_P_RAM_exceptions(P_RAM_Z, P_RAM_D, d_1, d_2):
component_woehler_curve_P_RAM = assessment_parameters.woehler_P_RAM


def test_woehler_curve_P_RAM_change_params():
ap = pd.Series({
"P_RAM_Z": 400.0,
"P_RAM_D": 150.0,
"d_1": -0.3,
"d_2": -0.2
})

_ = ap.woehler_P_RAM

ap["d_2"] = -0.9

assert ap.woehler_P_RAM.to_pandas()["d_2"] == -0.9


# ---------- P_RAJ --------------
@pytest.mark.parametrize(
"P_RAJ_Z, P_RAJ_D_0, d_RAJ", [
Expand Down Expand Up @@ -260,3 +275,17 @@ def test_woehler_curve_P_RAJ_exceptions(P_RAJ_Z, P_RAJ_D_0, d_RAJ):

with pytest.raises(ValueError, match="has to be"):
component_woehler_curve_P_RAJ = assessment_parameters.woehler_P_RAJ


def test_woehler_curve_P_RAJ_change_params():
ap = pd.Series({
"P_RAJ_Z": 150.0,
"P_RAJ_D_0": 0.1,
"d_RAJ": -0.5,
})

_ = ap.woehler_P_RAJ

ap["d_RAJ"] = -0.7

assert ap.woehler_P_RAJ.to_pandas()["d_RAJ"] == -0.7
Loading