Skip to content

Instantly share code, notes, and snippets.

@blankdots
Last active October 18, 2015 12:55
Show Gist options
  • Save blankdots/81986a0a8d1a851b855e to your computer and use it in GitHub Desktop.
Save blankdots/81986a0a8d1a851b855e to your computer and use it in GitHub Desktop.
Software for Scientists - October 2015
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!curl http://synesthesiam.com/assets/weather.zip > weather.zip"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!unzip weather.zip"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"y1 = ['1', '2', '3']\n",
"y2 = ['a', 'b', 'c']\n",
"x = zip (y1,y2)\n",
"\n",
"print x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as pyplot\n",
"from datetime import datetime\n",
"import os, calendar"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"event_types = ['Rain', 'Thunderstorm', 'Snow', 'Fog']\n",
"num_events = len(event_types)\n",
"\n",
"def event2int(event):\n",
" return event_types.index(event)\n",
"\n",
"def date2int(date_str):\n",
" date = datetime.strptime(date_str, '%Y-%m-%d')\n",
" return date.toordinal()\n",
"\n",
"def r_squared(actual, ideal):\n",
" actual_mean = np.mean(actual)\n",
" ideal_dev = np.sum([(val - actual_mean)**2 for val in ideal])\n",
" actual_dev = np.sum([(val - actual_mean)**2 for val in actual])\n",
"\n",
" return ideal_dev / actual_dev"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def read_weather(file_name):\n",
" dtypes = np.dtype({ 'names' : ('timestamp', 'max temp', 'mean temp', 'min temp', 'events'),\n",
" 'formats' : [np.int, np.float, np.float, np.float, 'S100'] })\n",
"\n",
" data = np.loadtxt(file_name, delimiter=',', skiprows=1, \n",
" converters = { 0 : date2int },\n",
" usecols=(0,1,2,3,21), dtype=dtypes)\n",
"\n",
" return data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def temp_plot(dates, mean_temps, min_temps = None, max_temps = None):\n",
"\n",
" year_start = datetime(2012, 1, 1)\n",
" days = np.array([(d - year_start).days + 1 for d in dates])\n",
"\n",
" fig = pyplot.figure()\n",
" pyplot.title('Temperatures in Bloomington 2012')\n",
" pyplot.ylabel('Mean Temperature (F)')\n",
" pyplot.xlabel('Day of Year')\n",
"\n",
" if (max_temps is None or min_temps is None):\n",
" pyplot.plot(days, mean_temps, marker='o')\n",
" else:\n",
" temp_err = np.vstack((mean_temps - min_temps,\n",
" max_temps - mean_temps))\n",
"\n",
" pyplot.errorbar(days, mean_temps, marker='o', yerr=temp_err)\n",
" pyplot.title('Temperatures in Bloomington 2012 (max/min)')\n",
"\n",
" slope, intercept = np.polyfit(days, mean_temps, 1)\n",
" ideal_temps = intercept + (slope * days)\n",
" r_sq = r_squared(mean_temps, ideal_temps)\n",
"\n",
" fit_label = 'Linear fit ({0:.3f})'.format(slope)\n",
" pyplot.plot(days, ideal_temps, color='red', linestyle='--', label=fit_label)\n",
" pyplot.annotate('r^2 = {0:.3f}'.format(r_sq), (0.05, 0.9), xycoords='axes fraction')\n",
" pyplot.legend(loc='lower right')\n",
"\n",
" return fig"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def hist_events(dates, events):\n",
" event_months = []\n",
"\n",
" for i in range(num_events):\n",
" event_months.append([])\n",
"\n",
" # Build up lists of months where events occurred\n",
" min_month = 11\n",
" max_month = 0\n",
"\n",
" for date, event_str in zip(dates, events):\n",
" if len(event_str) == 0:\n",
" continue # Skip blank events\n",
"\n",
" month = date.month\n",
" min_month = min(month, min_month)\n",
" max_month = max(month, max_month)\n",
"\n",
" for event in event_str.split('-'):\n",
" event_code = event2int(event)\n",
" event_months[event_code].append(month)\n",
"\n",
" # Plot histogram\n",
" fig = pyplot.figure()\n",
" pyplot.title('Weather Events in Bloomington 2012')\n",
" pyplot.xlabel('Month')\n",
" pyplot.ylabel('Event Count')\n",
" pyplot.axes().yaxis.grid()\n",
"\n",
" num_months = max_month - min_month + 1;\n",
" bins = np.arange(1, num_months + 2) # Need extra bin\n",
" pyplot.hist(event_months, bins=bins, label=event_types)\n",
"\n",
" # Align month labels to bin centers\n",
" month_names = calendar.month_name[min_month:max_month+1]\n",
" pyplot.xticks(bins + 0.5, month_names)\n",
"\n",
" pyplot.legend()\n",
" \n",
" return fig"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"# Read data and extract dates, temperatures, and events\n",
"data = read_weather(os.path.join('data', 'weather.csv'))\n",
"\n",
"min_temps = data['min temp']\n",
"mean_temps = data['mean temp']\n",
"max_temps = data['max temp']\n",
"dates = [datetime.fromordinal(d) for d in data['timestamp']]\n",
"events = data['events']\n",
"\n",
"# Do plotting\n",
"if not os.path.exists('plots'):\n",
" os.mkdir('plots')\n",
"\n",
"temp_plot(dates, mean_temps)\n",
"fig.show()\n",
"\n",
"temp_plot(dates, mean_temps, min_temps, max_temps)\n",
"fig.show()\n",
"\n",
"hist_events(dates, events)\n",
"fig.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment