{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9be665b8-0e4b-43f0-96b5-1cc821fc7d67",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-293ccdf5e42bf800",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Reading CSV files\n",
    "\n",
    "Step 1: Download file from https://archive.ics.uci.edu/ml/datasets/Wine+Quality . Click the \"Download\" button to get the `wine+quality.zip` file. Open this file and extract `winequality-red.csv`. Place it in the folder alongside this notebook.\n",
    "\n",
    "Let's look at the first lines of this file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5c2e4658-ad84-4a02-8176-6ca65fa9140f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "line 0: '\"fixed acidity\";\"volatile acidity\";\"citric acid\";\"residual sugar\";\"chlorides\";\"free sulfur dioxide\";\"total sulfur dioxide\";\"density\";\"pH\";\"sulphates\";\"alcohol\";\"quality\"'\n",
      "line 1: '7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5'\n",
      "line 2: '7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5'\n",
      "line 3: '7.8;0.76;0.04;2.3;0.092;15;54;0.997;3.26;0.65;9.8;5'\n",
      "line 4: '11.2;0.28;0.56;1.9;0.075;17;60;0.998;3.16;0.58;9.8;6'\n"
     ]
    }
   ],
   "source": [
    "fobj = open('winequality-red.csv')\n",
    "for line_num, line in enumerate(fobj.readlines()):\n",
    "    line = line.strip()\n",
    "    print(f\"line {line_num}: '{line}'\")\n",
    "    if line_num > 3:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b803dcd-a408-476e-9e05-bab37dd64aac",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-65efe24785650af1",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "## Q10 Read the file into a dict called `data`\n",
    "\n",
    "The dict should have a key for each column in the CSV file and each dictionary value should be a list with all the values in that column.\n",
    "\n",
    "For example, a CSV file like this:\n",
    "\n",
    "```\n",
    "name,home planet\n",
    "Arthur,Earth\n",
    "Zaphod,Betelgeuse V\n",
    "Trillian,Earth\n",
    "```\n",
    "\n",
    "Would result in a dictionary like this:\n",
    "\n",
    "```python\n",
    "{'name':['Arthur','Zaphod','Trillian'], 'home planet':['Earth', 'Betelgeuse V', 'Earth']}\n",
    "```\n",
    "\n",
    "But here, we read the file `winequality-red.csv` which you have uploaded into this folder. Note that in this wine quality \"CSV\" file, the values are separated with semicolons (`;`), not commas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5443bf3d-2303-4971-85f4-0af37b783247",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-bbe508684824a047",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['\"fixed acidity\"', '\"volatile acidity\"', '\"citric acid\"', '\"residual sugar\"', '\"chlorides\"', '\"free sulfur dioxide\"', '\"total sulfur dioxide\"', '\"density\"', '\"pH\"', '\"sulphates\"', '\"alcohol\"', '\"quality\"'])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fobj = open('winequality-red.csv')\n",
    "data = {}\n",
    "for line_num, line in enumerate(fobj.readlines()):\n",
    "    line = line.strip()\n",
    "    #print(f\"line {line_num}: '{line}'\")\n",
    "    entries = line.split(';')\n",
    "    if line_num == 0:\n",
    "        column_names = entries\n",
    "        for column_name in column_names:\n",
    "            data[column_name] = []\n",
    "        continue\n",
    "    for (colname, entry) in zip(column_names, entries):\n",
    "        data[colname].append(float(entry))\n",
    "data.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9880f13b-acc5-431c-836e-a0d34bdc632c",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-1978372e733238bd",
     "locked": true,
     "points": 0,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "assert len(data.keys()) == 12\n",
    "assert len(data['\"alcohol\"'])==1599\n",
    "acc = 0; [acc := acc+x for x in data['\"quality\"']]\n",
    "assert acc==9012"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "075f13ae-1d24-4e26-ba68-fa3828da1861",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c76a021eff929a4e",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    " ## Q11 Plot the \"Density\" (Y axis) versus \"Alcohol\" (X axis).\n",
    " \n",
    " Your plot should look like this:\n",
    " \n",
    "<img src=\"data:image/png;base64,[encoded data, 44372 characters]\" >"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "57ace93a-fc8e-48c7-9fb4-b1ebe2b521f0",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-0dd13f6a5af90429",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 44372 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.plot(data['\"density\"'], data['\"alcohol\"'], '.')\n",
    "plt.xlabel(\"Density\")\n",
    "plt.ylabel(\"Alcohol\");"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cff50a5-7642-47f6-b342-043bc398e981",
   "metadata": {},
   "source": [
    " ## Q12 Make a Python program that does this\n",
    "\n",
    "Create a Python program called `plot_red_wine.py` which makes the above plot (alcohol vs density for the red wine dataset) and saves the plot to a file called `red_wine.png`.\n",
    "\n",
    "Hint: save the figure using the `plt.savefig()` function. (You might also want to play around with the `plot.show()` function.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7ce822f-3b1e-4982-a6c7-cfabb3ee5f80",
   "metadata": {},
   "source": [
    "# Uploading the exercise\n",
    "\n",
    "For this exercise, the following files should be uploaded:\n",
    "\n",
    "* The two `.ipynb` files (overwriting the original ones, as usual).\n",
    "* `plot_red_wine.py` - Your Python script\n",
    "* `winequality-red.csv` - The file you downloaded\n",
    "* `red_wine.png` - The plot you generated using `plot_red_wine.py`."
   ]
  }
 ],
 "metadata": {
  "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
