{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 4.2: Computing things!\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "We have looked at a data set from Harvey and Orbidans on the cross-sectional area of *C. elegans* eggs. Recall, we loaded the data and converted everything to Numpy arrays like this:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('data/c_elegans_egg_xa.csv', comment='#')\n", "\n", "xa_high = df.loc[df['food']=='high', 'area (sq. um)'].values\n", "xa_low = df.loc[df['food']=='low', 'area (sq. um)'].values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we would like to compute the diameter of the egg from the cross-sectional area. Write a function that takes in an array of cross-sectional areas and returns an array of diameters. Recall that the diameter $d$ and cross-sectional area $A$ are related by $A = \\pi d^2/4$. There should be no `for` loops in your function! The call signature is\n", "\n", "```python\n", "xa_to_diameter(xa)\n", "```\n", "\n", "Use your function to compute the diameters of the eggs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Diameters of eggs from well fed mothers:\n", " [46.29105911 51.22642581 47.76657057 48.5596503 51.59790585 47.61973991\n", " 49.33998388 47.89966242 47.21697198 46.94654036 49.08125119 49.84064959\n", " 47.9926071 46.29105911 47.69988539 48.40207395 48.15152345 49.3141717\n", " 49.57168871 47.87307365 48.30991705 46.29105911 46.12573337 46.24978308\n", " 46.41466697 47.87307365 48.15152345 48.95137203 45.72372833 47.18999856\n", " 46.68817945 45.98750791 46.53794651 52.2111661 48.70364742 47.23045291\n", " 47.06842687 46.81073869 45.97366251 49.57168871 50.8397116 48.54653847\n", " 52.08909166 48.24398292]\n", "\n", "Diameters of eggs from poorly fed mothers:\n", " [48.40207395 51.58556628 52.55146594 50.31103472 53.06982074 54.57203767\n", " 50.32368681 52.24773281 53.99739399 49.44309786 53.87936676 47.9926071\n", " 52.41804019 47.87307365 52.11352942 51.21399674 52.44232467 50.47526453\n", " 50.8397116 51.56087828 49.84064959 55.96578669 50.72688754 50.58864976\n", " 52.18677405 52.44232467 51.78264653 52.57568879 51.86863366 52.67246879\n", " 49.05530287 52.67246879 50.72688754 50.07003758 52.32078957 49.18490759\n", " 53.72554372 46.67454189 49.19784929 51.88090591 51.85635852 54.8280819\n", " 52.07686848 51.22642581 51.96673046 48.29673743 53.04582353 52.07686848\n", " 52.35727972 50.57606396 51.70882946 53.54750652 52.23554675 53.54750652\n", " 53.18964437 51.96673046 55.38261517]\n" ] } ], "source": [ "import numpy as np\n", "\n", "def xa_to_diameter(xa):\n", " \"\"\"\n", " Convert an array of cross-sectional areas\n", " to diameters with commensurate units.\n", " \"\"\"\n", " # Compute diameter from area\n", " diameter = 2 * np.sqrt(xa / np.pi)\n", " \n", " return diameter\n", "\n", "print('Diameters of eggs from well fed mothers:\\n', xa_to_diameter(xa_high))\n", "print('\\nDiameters of eggs from poorly fed mothers:\\n', xa_to_diameter(xa_low))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing environment" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [ "hide-input" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python implementation: CPython\n", "Python version : 3.9.12\n", "IPython version : 8.3.0\n", "\n", "numpy : 1.21.5\n", "pandas : 1.4.2\n", "jupyterlab: 3.3.2\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -p numpy,pandas,jupyterlab" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }