Commits

Anonymous committed 4978643

plot graph of worker movement

Comments (0)

Files changed (2)

 def half_life_steps_to_ratio(steps):
     if steps==float('inf'):
         return 1.0
-    return math.exp(math.log(0.5)/steps)
+    return math.exp(math.log(0.5)/steps)
+
+def affine_cipher(integer):
+    return (1664525* integer + 1013904223) % (2**32)
+    
+def double_affine_cipher(integer):
+    return affine_cipher(affine_cipher(integer))
+from __future__ import division
 import matplotlib.pyplot as plt
 import numpy as np
 from pandas import DataFrame, merge
 from scipy import stats, mgrid, c_, reshape, random, rot90
+from lib.util import trace
+import re
+from lib.calcs import double_affine_cipher
+
+_parse_rep_re = re.compile(u'\<(\w+)\:(\d+)\>')
+def _parse_rep(rep):
+    "Parse a <Worker:id> string into (klass, id)"
+    klass, _id = _parse_rep_re.match(rep).groups()
+    return klass, int(_id)
 
 # data preening functions:
 def _delta_valuations(world):
         for worker in workers
     ])
 
+def _hash_to_colour(natural_n):
+    "convert a number to a colour in a stable fashion"
+    colour = double_affine_cipher(natural_n) >> 8
+    return "#%06x" % colour
+
 def _series_to_observations(dataframe):
     """Convert a DataFrame of TimeSeries columns into a melted array.
     I feel there should be a way to do this without going via an intermediate
         xmin=None, ymin=None,
         xmax=None, ymax=None,
         xlabel='', ylabel=''):
-    """plot a kernel density regression. scipy's functinoality here is not
+    """plot a kernel density regression. scipy's functionality here is not
     great, not supporting, e.g. bandwidth per-axis, or cross-validated
     bandwidth selection. Pfft.
     
-    Also the API is a little gross, necessitating much array creation and
-    manipulation, which should be hidden away in this private function."""
+    Also the API is a little gross, necessitating much array creation,
+    rotation and transposition, which should be hidden away in this private
+    function."""
     xmin = xmin or xdata.min()
     xmax = xmax or xdata.max()
     ymin = ymin or ydata.min()
 def disappointment_versus_knowledge(world, ax=None):
     return ax
 
+def staff_movement_chart(world, ax=None, step_limit=None):
+    if ax is None:
+        fig = plt.figure()
+        ax = plt.gca()
+    else:
+        fig = plt.gcf()
+    employment_strings = world.log._data['fns.track_employment']
+    if step_limit is None:
+        step_limit = len(employment_strings[employment_strings.keys()[0]])
+    employment_nums = {}
+    for worker_string, firm_string_list in employment_strings.iteritems():
+        worker_id = _parse_rep(worker_string)[1]
+        employment_nums[worker_id] = [
+            _parse_rep(firm_string)[1] for firm_string in firm_string_list
+        ]
+    del(employment_strings)
+    num_workers = len(employment_nums)
+    per_worker_inc = 0.5/num_workers
+    for worker_id in employment_nums:
+        employment_nums[worker_id] = [
+            firm + worker_id * per_worker_inc
+            for firm in employment_nums[worker_id]
+        ]
+    step_numbers = range(step_limit)
+    for worker_id in employment_nums:
+        plt.plot(
+            step_numbers,
+            employment_nums[worker_id][:step_limit],
+            color=_hash_to_colour(worker_id)
+        )
+
 def wage_versus_previous_wage(world, ax=None):
     if ax is None:
         fig = plt.figure()