Notebook to create animal sighting files
authorJustin Kitzes <jkitzes@berkeley.edu>
Tue, 13 Nov 2012 05:43:36 +0000 (21:43 -0800)
committerW. Trevor King <wking@tremily.us>
Fri, 1 Nov 2013 15:32:15 +0000 (08:32 -0700)
python/make_animals.ipynb [new file with mode: 0644]

diff --git a/python/make_animals.ipynb b/python/make_animals.ipynb
new file mode 100644 (file)
index 0000000..f75a2e8
--- /dev/null
@@ -0,0 +1,129 @@
+{
+ "metadata": {
+  "name": "make_animals"
+ }, 
+ "nbformat": 2, 
+ "worksheets": [
+  {
+   "cells": [
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "animals = ('Moose', 'Elk', 'Fox', 'Owl', 'Wolf', 'Muskox', 'Ptarmigan', 'Grizzly', 'Wolverine')"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 6
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "import datetime as dt", 
+      "import random"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 7
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "def make_animals(filename, lines=100):", 
+      "    date = dt.date(2011, 4, 22)", 
+      "    one_day = dt.timedelta(days=1)", 
+      "", 
+      "    f = open(filename, 'w')", 
+      "", 
+      "    f.write('Date,Time,Animal,Count\\n')", 
+      "    for _ in xrange(lines):", 
+      "        animal = random.choice(animals)", 
+      "        hour = random.randint(0, 23)", 
+      "        minute = random.randint(0, 59)", 
+      "        number = random.randint(1, 42)", 
+      "    ", 
+      "        date_str = '{}-{:02d}-{:02d}'.format(date.year, date.month, date.day)", 
+      "    ", 
+      "        time_str = '{:02d}:{:02d}'.format(hour, minute)", 
+      "    ", 
+      "        line = '{},{},{},{}\\n'.format(date_str, time_str, animal, number)", 
+      "    ", 
+      "        f.write(line)", 
+      "    ", 
+      "        if random.choice((True, False)):", 
+      "            date = date + one_day", 
+      "    ", 
+      "    f.close()"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 31
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "def make_animals_unstruct(filename, lines=100):", 
+      "    date = dt.date(2011, 4, 22)", 
+      "    one_day = dt.timedelta(days=1)", 
+      "", 
+      "    f = open(filename, 'w')", 
+      "", 
+      "    for i in range(lines):", 
+      "        animal = random.choice(animals)", 
+      "        hour = random.randint(0, 23)", 
+      "        minute = random.randint(0, 59)", 
+      "        number = random.randint(1, 42)", 
+      "    ", 
+      "        date_str = '{}-{:02d}-{:02d}'.format(date.year, date.month, date.day)", 
+      "    ", 
+      "        time_str = '{:02d}:{:02d}'.format(hour, minute)", 
+      "    ", 
+      "        line = '{} {{\\nDate: {}\\nTime: {}\\nAnimal: {}\\nCount: {}\\n}}\\n\\n'.format(i, date_str, time_str, animal, number)", 
+      "    ", 
+      "        f.write(line)", 
+      "    ", 
+      "        if random.choice((True, False)):", 
+      "            date = date + one_day", 
+      "    ", 
+      "    f.close()    "
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 32
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "make_animals('sightings_tab_sm.csv', 5)", 
+      "#make_animals_unstruct('sightings_recs_sm.txt', 3)"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 33
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "make_animals('sightings_tab_lg.csv', 200)", 
+      "make_animals_unstruct('sightings_recs_lg.txt', 200)"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 34
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [], 
+     "language": "python", 
+     "outputs": []
+    }
+   ]
+  }
+ ]
+}
\ No newline at end of file