hugo-site/content/posts/2024/source/analysis.ipynb

151 lines
4.3 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"import re\n",
"import numpy as np\n",
"from cds2py import plot_svg\n",
"from datetime import datetime, date\n",
"from collections import namedtuple\n",
"from slugify import slugify\n",
"from quantiphy import Quantity\n",
"from tabulate import tabulate"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"with open('diet.csv') as csvfile:\n",
" data = list(csv.reader(csvfile, delimiter=','))\n",
"\n",
"DietEntry = namedtuple(\"DietEntry\", [slugify(elem).replace(\"-\",\"_\") for elem in data[0]])\n",
"\n",
"def quantify_elem(elem:str, field:str):\n",
" if field == \"date\":\n",
" return datetime.strptime(elem,\"%Y-%m-%d\")\n",
" elif field == \"completed\":\n",
" return elem == \"true\"\n",
" elif field.split(\"_\")[-1] == \"mg\":\n",
" return Quantity(float(elem)*1e-3, units=\"g\")\n",
" elif field.split(\"_\")[-1] == \"ug\":\n",
" return Quantity(float(elem)*1e-6, units=\"g\")\n",
" elif field.split(\"_\")[-1] == \"iu\":\n",
" return Quantity(float(elem)*1e-6/40, units=\"g\")\n",
" elif field.split(\"_\")[-1] == \"kcal\":\n",
" return Quantity(float(elem)*1e3, units=\"cal\")\n",
" else:\n",
" return Quantity(elem, units=field.split(\"_\")[-1])\n",
"\n",
"quantities = [\n",
" DietEntry(\n",
" *[\n",
" quantify_elem(value, DietEntry._fields[index])\n",
" for index,value in enumerate(elem)\n",
" ]\n",
" ) for elem in data[1:]\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"darray = np.array(quantities)[:,1:-1].astype(float)\n",
"Names = [re.sub(\" [kmiu]\",\" \", elem.replace(\"_\",\" \")).replace(\" u\",\" g\") for elem in DietEntry._fields[1:-1]]\n",
"metric_table = np.vstack(\n",
" ( Names, [ Quantity(elem).render(prec=2) for elem in darray.mean(axis=0) ], [ Quantity(elem).render(prec=2) for elem in darray.std(axis=0) ] )\n",
" )\n",
"\n",
"with open(\"diet.table\",\"w\") as mdfile:\n",
" mdfile.write(\n",
" tabulate( metric_table.T, headers=[\"Metric\", \"Average\", \"Deviation\"],)\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"import importlib.resources as rsc\n",
"import json\n",
"import re\n",
"import nutrimetrics.resources.dri\n",
"\n",
"src_txt = rsc.read_text(nutrimetrics.resources.dri,\"ear-male.json\")\n",
"average_requirement = json.loads(re.sub(\"//.*\",\"\",src_txt))[\"dietary_reference_intakes\"]\n",
"src_txt = rsc.read_text(nutrimetrics.resources.dri,\"rda-male.json\")\n",
"average_allowance = json.loads(re.sub(\"//.*\",\"\",src_txt))[\"dietary_reference_intakes\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"folate\n",
"vitamin-a\n",
"vitamin-c\n",
"vitamin-d\n",
"vitamin-e\n",
"calcium\n",
"copper\n",
"iron\n",
"magnesium\n",
"manganese\n",
"phosphorus\n",
"potassium\n",
"selenium\n",
"sodium\n",
"zinc\n"
]
}
],
"source": [
"\n",
"for name, average, _ in metric_table.T:\n",
" nutrient = name.replace(\" \",\"-\").replace(\"-g\",\"\")\n",
" if nutrient in average_requirement:\n",
" print(nutrient)\n",
" # print(Quantity(average_requirement[nutrient],units=\"g\")/Quantity(average, units=\"g\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "git",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}