Advertisement
smathot

Difference plot by bin

Oct 10th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3.  
  4. import numpy as np
  5. from matplotlib import pyplot as plt
  6. import sys
  7. from exparser.DataMatrix import DataMatrix
  8. from exparser.PivotMatrix import PivotMatrix
  9. from exparser.AnovaMatrix import AnovaMatrix
  10.  
  11. dv = 'sacc_lat' # Dependent variable
  12. nBin = 10 # Number of bins
  13. binKeys = ['FILE', 'cond'] # Bin by subject and condition
  14.  
  15. # Load the data, add a field for the bin, and calculate the bins
  16. a = np.loadtxt('data.csv', delimiter=',', dtype=str)
  17. dm = DataMatrix(a)
  18. dm = dm.addField('bin', dtype=float)
  19. dm = dm.calcPerc(dv, 'bin', keys=binKeys, nBin=nBin)
  20.  
  21. lY = []
  22. lX = []
  23. # Walk through all bins
  24. for _bin in dm.unique('bin'):  
  25.     # Filter out all but one bin
  26.     _dm = dm.select('bin == %f' % _bin)
  27.    
  28.     # Get the mean for both conditions. Note that using this method, the mean
  29.     # is across all subjects, and not the mean of the mean of the subjects.
  30.     # But this will not make any real difference for the overall pattern. You
  31.     # can prevent this by withinizing the data first.
  32.     m1 = _dm.select('cond == "act"', verbose=False)[dv].mean()
  33.     m2 = _dm.select('cond == "mir"', verbose=False)[dv].mean()
  34.     y = m2 - m1 # We are going to plot the difference score on the Y-axis
  35.     # The bin average will be on the X-axis, so you can see how the bins are
  36.     # distributed (i.e. the the bins in the middle are closer together than
  37.     # the bins in the end, at least usually)       
  38.     x = _dm[dv].mean()
  39.     lY.append(y)
  40.     lX.append(x)
  41.    
  42. plt.plot(lX, lY, '.-')
  43. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement