Skip to content

Commit

Permalink
Debugged the bit added to views.py a bit; added the appropriate infor…
Browse files Browse the repository at this point in the history
…mation to the view template. Stumbled across #57 en route to #56.
  • Loading branch information
dgets committed Jan 11, 2019
1 parent b7ad6f8 commit 788a5fe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
18 changes: 17 additions & 1 deletion dataview/templates/dataview/halflife.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ <h1>{{ sub }}</h1>

<h2>Elimination Extrapolation</h2>

<h3>Lipid Insoluble Metabolite Breakdown Information</h3>
{% if not sub.lipid_solubility %}

<h3>Lipid Insoluble Metabolite Breakdown Information</h3>

{% else %}

<h3>Lipid Soluble Metabolite Breakdown Information</h3>

<p>Please note that results here should not be used for anything vital, important, or even tangentally
useful. The algorithm implemented is <i>very</i> rough around the edges. Please feel free to see
the <a href="https://www.mayocliniclabs.com/test-info/drug-book/marijuana.html">Mayo Clinic labs'
book on THC metabolite testing</a> for more information on the algorithm's basis.</p>

<p>Based on the last usage date of <i>{{ last_usage }}</i>, your estimated time of return to a negative
test result baseline from the most common method of testing is <i><b>{{ undetectable_target }}</b></i></p>

{% endif %}

<p>Based on the last usage date of <i>{{ last_usage }}</i>, your estimated time of return to pristine bodily
fluids is <i><b>{{ elimination_target }}</b></i></p>
Expand Down
28 changes: 14 additions & 14 deletions dataview/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,44 +113,44 @@ def get_context_data(self, **kwargs):
def extrapolate_halflife_data(request, sub_id):
substance = Substance.objects.filter(id=sub_id).first()
context = {}
elimination_datetime = None
full_elimination_datetime = None

if substance.lipid_solubility and ('marijuana' in substance.common_name or 'weed' in substance.common_name):
# we're working with weed, let's give this a shot based on the information available at
# https://www.mayocliniI sure hope auto-save was keeping upclabs.com/test-info/drug-book/marijuana.html
# FWIW we're just going to base our projection on the average of the last 2 weeks of usage
weeks_averaged = 2
relevant_since_date = datetime.now() - datetime.timedelta(weeks=weeks_averaged)
relevant_since_date = datetime.datetime.now() - datetime.timedelta(weeks=weeks_averaged)
relevant_usages = len(Usage.objects.filter(timestamp__gte=relevant_since_date))
most_recent_usage = Usage.objects.filter(sub=sub_id).order_by('-timestamp').first()
last_usage = Usage.objects.filter(sub=sub_id).order_by('-timestamp').first()
full_elimination_duration = int(float(substance.active_half_life) * 5.7)

# note that half-life durations here (not flat day count) are calculated @ 5.7 * half-life, as in the
# standard non-lipid-soluble substances; detectable metabolites will be out of the system sooner (hence
# the less precise flat day count)
if relevant_usages <= weeks_averaged:
# single use: detectable for a standard half-life duration
full_elimination_datetime = most_recent_usage.timestamp + \
datetime.timedelta(hours=full_elimination_duration)
detectable_elimination_datetime = most_recent_usage.timestamp + datetime.timedelta(days=3)
full_elimination_datetime = last_usage.timestamp + \
datetime.timedelta(hours=full_elimination_duration)
detectable_elimination_datetime = last_usage.timestamp + datetime.timedelta(days=3)

elif relevant_usages <= (weeks_averaged * 4):
# moderate use: detectable for standard half-life * 5/3, _or_ 5 days
full_elimination_datetime = most_recent_usage.timestamp + \
full_elimination_datetime = last_usage.timestamp + \
datetime.timedelta(hours=int(full_elimination_duration * (5/3)))
detectable_elimination_datetime = most_recent_usage.timestamp + datetime.timedelta(days=5)
detectable_elimination_datetime = last_usage.timestamp + datetime.timedelta(days=5)

elif relevant_usages <= (weeks_averaged * 7):
# heavy use: detectable for standard half-life * 10/3, _or_ 10 days
full_elimination_datetime = most_recent_usage.timestamp + \
full_elimination_datetime = last_usage.timestamp + \
datetime.timedelta(hours=int(full_elimination_duration * (10/3)))
detectable_elimination_datetime = most_recent_usage.timestamp + datetime.timedelta(days=10)
detectable_elimination_datetime = last_usage.timestamp + datetime.timedelta(days=10)

else:
# chronic heavy use: detectable for standard half-life * 10, _or_ 30 days
full_elimination_datetime = most_recent_usage.timestamp + \
full_elimination_datetime = last_usage.timestamp + \
datetime.timedelta(hours=(full_elimination_duration * 10))
detectable_elimination_datetime = most_recent_usage.timestamp + datetime.timedelta(days=30)
detectable_elimination_datetime = last_usage.timestamp + datetime.timedelta(days=30)

elif substance.lipid_solubility:
# we can't process this yet
Expand All @@ -163,8 +163,8 @@ def extrapolate_halflife_data(request, sub_id):
datetime.timedelta(hours=int(float(substance.half_life) * 5.7))
detectable_elimination_datetime = full_elimination_datetime

context = {'error_message': None, 'sub': substance, 'elimination_target': full_elimination_datetime,
'undetectable_target': detectable_elimination_datetime, 'last_usage': last_usage.timestamp}
context = {'error_message': None, 'sub': substance, 'elimination_target': full_elimination_datetime,
'undetectable_target': detectable_elimination_datetime, 'last_usage': last_usage.timestamp}

return render(request, 'dataview/halflife.html', add_header_info(context))

Expand Down

0 comments on commit 788a5fe

Please sign in to comment.