<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Functions\n",
    "\n",
    "For this tutorial, we will use the modified US Census baby name dataset.\n",
    "\n",
    "> US Social Security Administration (2022). \"National data on the relative frequency of given names in the population of U.S. births wjere te individual has a Social Security Number.\" <https://www.ssa.gov/oact/babynames/limits.html>\n",
    "\n",
    "The dataset has been modified from the source to include column names, and to limit the date range from 2010-2021.\n",
    "\n",
    "First, a review of concepts:\n",
    "\n",
    "* lists\n",
    "* dictionaries\n",
    "* loops\n",
    "* conditionals\n",
    "\n",
    "For this we will refer back to tutorial 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n# syntax\\ndef function_name(arg1, arg2, ...):\\n    do stuff here\\n    return\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# functions - syntax and example\n",
    "# note the workflow here is based on the Carpentries\n",
    "\n",
    "\"\"\"\n",
    "# syntax\n",
    "def function_name(arg1, arg2, ...):\n",
    "    do stuff here\n",
    "    return\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def kilograms_to_pounds(weight_k):\n",
    "    weight_lbs = weight_k * 2.2\n",
    "    return weight_lbs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my dog weighs 33.0 pounds\n"
     ]
    }
   ],
   "source": [
    "print(\"my dog weighs\", kilograms_to_lbs(15), 'pounds')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can calculate our weight on the moon\n",
    "\n",
    "\"\"\"\n",
    "Formula for this calculation is\n",
    "\n",
    "weight_moon = (weight_earth/9.81m/s^2) * 1.622m/s^2\n",
    "\"\"\"\n",
    "\n",
    "def moon_weight_kilograms(earth_weight_k):\n",
    "    moon_weight_k = (earth_weight_k/9.81) * 1.622\n",
    "    return round(moon_weight_k, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.48"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "moon_weight_kilograms(15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# my dog is overweight - should she get more exercise or move to the moon?\n",
    "# let's get her weight in pounds by _composing_ functions using the output of other functions\n",
    "\n",
    "# all the work in the next function is done by our other functions\n",
    "def moon_weight_pounds(earth_weight_k):\n",
    "    moon_weight_k = moon_weight_kilograms(earth_weight_k)\n",
    "    moon_weight_lbs = kilograms_to_pounds(moon_weight_k)\n",
    "    return moon_weight_lbs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on the moon my dog would weigh 5.456 pounds\n"
     ]
    }
   ],
   "source": [
    "print('on the moon my dog would weigh', moon_weight_pounds(15), 'pounds')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
<commit_msg>functions for popular baby names<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Functions\n",
    "\n",
    "For this tutorial, we will use the modified US Census baby name dataset.\n",
    "\n",
    "> US Social Security Administration (2022). \"National data on the relative frequency of given names in the population of U.S. births wjere te individual has a Social Security Number.\" <https://www.ssa.gov/oact/babynames/limits.html>\n",
    "\n",
    "The dataset has been modified from the source to include column names, and to limit the date range from 2010-2021.\n",
    "\n",
    "First, a review of concepts:\n",
    "\n",
    "* lists\n",
    "* dictionaries\n",
    "* loops\n",
    "* conditionals\n",
    "\n",
    "For this we will refer back to tutorial 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n# syntax\\ndef function_name(arg1, arg2, ...):\\n    do stuff here\\n    return\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# functions - syntax and example\n",
    "# note the workflow here is based on the Carpentries\n",
    "\n",
    "\"\"\"\n",
    "# syntax\n",
    "def function_name(arg1, arg2, ...):\n",
    "    do stuff here\n",
    "    return\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def kilograms_to_pounds(weight_k):\n",
    "    weight_lbs = weight_k * 2.2\n",
    "    return weight_lbs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my dog weighs 33.0 pounds\n"
     ]
    }
   ],
   "source": [
    "print(\"my dog weighs\", kilograms_to_lbs(15), 'pounds')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can calculate our weight on the moon\n",
    "\n",
    "\"\"\"\n",
    "Formula for this calculation is\n",
    "\n",
    "weight_moon = (weight_earth/9.81m/s^2) * 1.622m/s^2\n",
    "\"\"\"\n",
    "\n",
    "def moon_weight_kilograms(earth_weight_k):\n",
    "    moon_weight_k = (earth_weight_k/9.81) * 1.622\n",
    "    return round(moon_weight_k, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.48"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "moon_weight_kilograms(15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# my dog is overweight - should she get more exercise or move to the moon?\n",
    "# let's get her weight in pounds by _composing_ functions using the output of other functions\n",
    "\n",
    "# all the work in the next function is done by our other functions\n",
    "def moon_weight_pounds(earth_weight_k):\n",
    "    moon_weight_k = moon_weight_kilograms(earth_weight_k)\n",
    "    moon_weight_lbs = kilograms_to_pounds(moon_weight_k)\n",
    "    return moon_weight_lbs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on the moon my dog would weigh 5.456 pounds\n"
     ]
    }
   ],
   "source": [
    "print('on the moon my dog would weigh', moon_weight_pounds(15), 'pounds')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# using baby name data without functions\n",
    "# let's get the most popular baby names for a given year\n",
    "# note we are using standard libraries - pandas is recommended for tabular data\n",
    "\n",
    "import glob\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "all files: ['./names\\\\2010', './names\\\\2011', './names\\\\2012', './names\\\\2013', './names\\\\2014', './names\\\\2015', './names\\\\2016', './names\\\\2017', './names\\\\2018', './names\\\\2019', './names\\\\2020', './names\\\\2021']\n",
      "\n",
      "the first file in the list is: ./names\\2010\n"
     ]
    }
   ],
   "source": [
    "# get a list of file names and also just the first filename\n",
    "\n",
    "baby_name_files = glob.glob('./names/*')\n",
    "print('all files:', baby_name_files)\n",
    "\n",
    "one_year_names = baby_name_files[0]\n",
    "print('\\nthe first file in the list is:', one_year_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# most popular names - since the files include the names most popular first,\n",
    "# we could just eyeball it but we'd have to search for the most popular boy name\n",
    "\n",
    "# read csv data into a list\n",
    "name_data = []\n",
    "with open(one_year_names, 'r') as f:\n",
    "    reader = csv.DictReader(f)\n",
    "    for row in reader:\n",
    "        name_data.append(row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'name': 'Isabella', 'sex': 'F', 'count': '22925'},\n",
       " {'name': 'Sophia', 'sex': 'F', 'count': '20648'},\n",
       " {'name': 'Emma', 'sex': 'F', 'count': '17354'},\n",
       " {'name': 'Olivia', 'sex': 'F', 'count': '17030'},\n",
       " {'name': 'Ava', 'sex': 'F', 'count': '15436'}]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# note the structure - table is a list of dictionaries\n",
    "# each row is a dictionary, with column names as keys\n",
    "\n",
    "name_data[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# comparing values is a bit clumsy with CSV library\n",
    "# this is one way\n",
    "\n",
    "f_max_c = 0\n",
    "f_popular = None\n",
    "m_max_c = 0\n",
    "m_popular = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "for name in name_data:\n",
    "        if name['sex'] == 'F':\n",
    "            if int(name['count']) > int(f_max_c):\n",
    "                f_max_c = int(name['count'])\n",
    "                f_popular = name['name']\n",
    "        elif name['sex'] == 'M':\n",
    "            if int(name['count']) > int(m_max_c):\n",
    "                m_max_c = int(name['count'])\n",
    "                m_popular = name['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2010 most popular girl name: Isabella ( 22925 )\n",
      "2010 most popular boy name: Jacob ( 22139 )\n"
     ]
    }
   ],
   "source": [
    "# clean up the filename a little\n",
    "y = one_year_names.replace('./names\\\\', '')\n",
    "\n",
    "# output results\n",
    "print(y, 'most popular girl name:', f_popular, '(', f_max_c, ')')\n",
    "print(y, 'most popular boy name:', m_popular, '(', m_max_c, ')')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2010 most popular girl name: Isabella ( 22925 )\n",
      "2010 most popular boy name: Jacob ( 22139 )\n"
     ]
    }
   ],
   "source": [
    "# our full code for the above is\n",
    "\n",
    "name_data = []\n",
    "with open(one_year_names, 'r') as f:\n",
    "    reader = csv.DictReader(f)\n",
    "    for row in reader:\n",
    "        name_data.append(row)\n",
    "f_max_c = 0\n",
    "f_popular = None\n",
    "m_max_c = 0\n",
    "m_popular = None\n",
    "for name in name_data:\n",
    "        if name['sex'] == 'F':\n",
    "            if int(name['count']) > int(f_max_c):\n",
    "                f_max_c = int(name['count'])\n",
    "                f_popular = name['name']\n",
    "        elif name['sex'] == 'M':\n",
    "            if int(name['count']) > int(m_max_c):\n",
    "                m_max_c = int(name['count'])\n",
    "                m_popular = name['name']\n",
    "# clean up the filename a little\n",
    "y = one_year_names.replace('./names\\\\', '')\n",
    "\n",
    "# output results\n",
    "print(y, 'most popular girl name:', f_popular, '(', f_max_c, ')')\n",
    "print(y, 'most popular boy name:', m_popular, '(', m_max_c, ')')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "# that's okay but it is a bit hard to read, and doesn't scale well\n",
    "# to get the results for a different year we need the file's index position in the file list\n",
    "# also, we're using the same logic twice- once for girls' names and a second time for boys'\n",
    "# these considerations (easy to break and repetitive) make this a good candidate for some functions\n",
    "\n",
    "# simplify the counting process first\n",
    "def popular_name(name_data, sex):\n",
    "    name_c = 0\n",
    "    popular = None\n",
    "    for name in name_data:\n",
    "        if name['sex'] == sex:\n",
    "            if int(name['count']) > int(name_c):\n",
    "                name_c = int(name['count'])\n",
    "                popular = name['name']\n",
    "    return popular, name_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "most popular girl name in the first file is: Isabella\n",
      "there were 22925 girls named Isabella in the file ./names\\2010\n"
     ]
    }
   ],
   "source": [
    "# test the function\n",
    "# we have replaced 13 lines of code with only 9 lines of code! (it adds up!)\n",
    "\n",
    "pop_girl_name, count_named = popular_name(name_data, 'F')\n",
    "print('most popular girl name in the first file is:', pop_girl_name)\n",
    "print('there were', count_named, 'girls named', pop_girl_name, 'in the file', one_year_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "# something we repear is reading files, so that also makes a good function\n",
    "# in this case we're not really reducing the number of lines of code, but\n",
    "# we are modularizing our code for reuse - anytime we need to read a csv file from here on\n",
    "# only requires us to call this function\n",
    "\n",
    "def csv_to_list(file_pointer):\n",
    "    csv_data = []\n",
    "    with open(file_pointer, 'r') as f:\n",
    "        reader = csv.DictReader(f)\n",
    "        for row in reader:\n",
    "            csv_data.append(row)\n",
    "    return csv_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2010 most popular girl name: Isabella ( 22925 )\n",
      "2010 most popular boy name: Jacob ( 22139 )\n"
     ]
    }
   ],
   "source": [
    "# here is our above process using functions\n",
    "\n",
    "name_data = csv_to_list(one_year_names)\n",
    "pop_girl_name, count_girls_named = popular_name(name_data, 'F')\n",
    "pop_boy_name, count_boys_named = popular_name(name_data, 'M')\n",
    "\n",
    "y = one_year_names.replace('./names\\\\', '')\n",
    "\n",
    "print(y, 'most popular girl name:', pop_girl_name, '(', count_girls_named, ')')\n",
    "print(y, 'most popular boy name:', pop_boy_name, '(', count_boys_named, ')')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

{'fraction_non_alphanumeric': 0.19796086751532296, 'fraction_numerical': 0.019448373408769447, 'mean_word_length': 2.327254901960784, 'pattern_counts': {'":': 234, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1872506', 'n_tokens_mistral': 6526, 'n_tokens_neox': 6176, 'n_words': 1483}