Skip to content

Commit

Permalink
Remove hardcoded threshold; subtract reference implant weight from re…
Browse files Browse the repository at this point in the history
…ference weight pct calculation
  • Loading branch information
k1o0 committed May 23, 2024
1 parent 97eb0fa commit 94ab097
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
18 changes: 10 additions & 8 deletions alyx/actions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ def test_water_control_thresholds(self):
self.sub.save()
wc = self.sub.reinit_water_control()
wc.expected_weight()
self.assertAlmostEqual(self.wei[self.rwind], wc.reference_weight())
self.assertAlmostEqual(self.wei[self.rwind], wc.expected_weight())
# expected weight should be different to reference weight as the implant weight changes
expected = self.wei[self.rwind] + (wc.implant_weights[1][1] - wc.implant_weights[0][1])
self.assertAlmostEqual(expected, wc.reference_weight())
self.assertAlmostEqual(expected, wc.expected_weight())
# test implant weight values
self.assertEqual([4.56, 7.0], [x[1] for x in wc.implant_weights])
self.assertEqual(7.0, wc.implant_weight())
Expand All @@ -91,23 +93,23 @@ def test_water_control_thresholds(self):
self.sub.lab = Lab.objects.get(name='zscore')
self.sub.save()
wc = self.sub.reinit_water_control()
wc.expected_weight()
self.assertAlmostEqual(self.wei[self.rwind], wc.reference_weight())
zscore = wc.zscore_weight()
self.assertAlmostEqual(zscore, 38.049183673469386)
self.assertEqual(zscore, wc.expected_weight())
# test computation on mixed lab
self.sub.lab = Lab.objects.get(name='mixed')
self.sub.save()
wc = self.sub.reinit_water_control()
self.assertAlmostEqual(self.wei[self.rwind], wc.reference_weight())
self.assertAlmostEqual(expected, wc.reference_weight())
self.assertAlmostEqual(wc.expected_weight(), (wc.reference_weight() + zscore) / 2)
# test that the thresholds are all above 70%
self.assertTrue(all([thrsh[0] > 0.4 for thrsh in wc.thresholds]))
self.assertTrue(all(thrsh[0] > 0.4 for thrsh in wc.thresholds))
# if we change the reference weight of the water restriction, this should change in wc too
self.assertAlmostEqual(wc.reference_weight(), self.wr.reference_weight)
self.assertAlmostEqual(expected, wc.reference_weight())
self.wr.reference_weight = self.wr.reference_weight + 1
self.wr.save()
wc = self.sub.reinit_water_control()
self.assertAlmostEqual(wc.reference_weight(), self.wr.reference_weight)
self.assertAlmostEqual(expected + 1, wc.reference_weight())


class NotificationTests(TestCase):
Expand Down
20 changes: 16 additions & 4 deletions alyx/actions/water_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ def reference_implant_weight_at(self, date=None, return_date=False):
return self.last_implant_weight_before(wr or date, return_date)

def reference_weighing_at(self, date=None):
"""Return a tuple (date, weight) the reference weighing at the specified date, or today."""
"""Return a tuple (date, weight) the reference weighing at the specified date, or today.
NB: this does not account for changes in implant weight.
"""
if self.reference_weighing and (date is None or date >= self.reference_weighing[0]):
return self.reference_weighing
date = date or self.today()
Expand All @@ -271,10 +274,16 @@ def reference_weighing_at(self, date=None):
return ref_weight

def reference_weight(self, date=None):
"""Return the reference weight at a given date."""
"""Return the reference weight at a given date.
NB: Unlike `reference_weighing_at` this accounts for changes in implant
weight.
"""
rw = self.reference_weighing_at(date=date)
if rw:
return rw[1]
ref_iw = self.reference_implant_weight_at(date) or 0.
iw = self.last_implant_weight_before(date) or 0.
return (rw[1] - ref_iw) + iw
return 0.

def last_implant_weight_before(self, date=None, return_date=False):
Expand Down Expand Up @@ -414,7 +423,10 @@ def expected_water(self, date=None):
weight = self.last_weighing_before(date=date)
weight = weight[1] if weight else 0.
expected_weight = self.expected_weight(date=date) or 0.
return 0.05 * (weight - iw) if weight < 0.8 * expected_weight else 0.04 * (weight - iw)
pct_sum = (self.reference_weight_pct + self.zscore_weight_pct)
# Increase required water by 0.01mL/g if weight below pct reference
pct_weight = (pct_sum * expected_weight)
return 0.05 * (weight - iw) if weight < pct_weight else 0.04 * (weight - iw)

def given_water(self, date=None, has_session=None):
"""Return the amount of water given at a specified date."""
Expand Down
2 changes: 1 addition & 1 deletion alyx/alyx/settings_lab_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# ALYX-SPECIFIC
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/London'
TIME_ZONE = 'Europe/London' # NB: Changing the timezone here requires migrations
GLOBUS_CLIENT_ID = '525cc543-8ccb-4d11-8036-af332da5eafd'
SUBJECT_REQUEST_EMAIL_FROM = 'alyx@internationalbrainlab.org'
DEFAULT_SOURCE = 'IBL'
Expand Down

0 comments on commit 94ab097

Please sign in to comment.