octopus.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import pandas as pd
  4. import seaborn as sb
  5. import os
  6. import scipy.io
  7. from scipy import stats
  8. # Define Function for Cartesian/Polar Vector Transformation
  9. def cart2pol(x, y):
  10. rho = np.sqrt(x**2 + y**2)
  11. phi = np.arctan2(y, x)
  12. if phi < 0:
  13. phi = phi + 2*np.pi
  14. return(rho, phi)
  15. def pol2cart(rho, phi):
  16. x = rho * np.cos(phi)
  17. y = rho * np.sin(phi)
  18. return x, y
  19. # Define Function for calculating Motion Opponency index
  20. def MOI(rot_rad, radius):
  21. # Find indices of Depolarizing/Hyperpolarizing Responses
  22. D_idx = np.asarray(np.where(radius>0))[0,:]
  23. H_idx = np.asarray(np.where(radius<0))[0,:]
  24. #Calculate Vectors and Magnitudes for Depolarization/Hyperpolarization
  25. D_cart = np.array(pol2cart(radius[D_idx],rot_rad[D_idx]))
  26. D_vect = np.sum(D_cart,1)
  27. D_vect_pol = cart2pol(D_vect[0], D_vect[1])
  28. print ('D_magn =' + str(D_vect_pol[0]) + ' D_deg = ' + str(np.degrees(D_vect_pol[1])))
  29. H_cart = np.array(pol2cart(radius[H_idx],rot_rad[H_idx]))
  30. H_vect = np.sum(H_cart,1)
  31. H_vect_pol = cart2pol(H_vect[0], H_vect[1])
  32. print ('H_magn =' + str(H_vect_pol[0]) + ' H_deg = ' + str(np.degrees(H_vect_pol[1])))
  33. # Calculate MOI (Motion Opponency index)
  34. if D_vect_pol < H_vect_pol:
  35. MOI = (np.cos(D_vect_pol[1]-H_vect_pol[1]))*(D_vect_pol[0]/H_vect_pol[0])
  36. else:
  37. MOI = (np.cos(D_vect_pol[1]-H_vect_pol[1]))*(H_vect_pol[0]/D_vect_pol[0])
  38. print ('MOI = ' + str(MOI))
  39. return MOI
  40. # Define Function for calculating the Direction Selectivity Index LDir
  41. def LDir(rot_rad, radius):
  42. R_cart = np.array(pol2cart(radius,rot_rad))
  43. R_vect = np.sum(R_cart, 1)
  44. R_vect_pol = cart2pol(R_vect[0], R_vect[1])
  45. R_indiv_magn = np.sum(np.sqrt(R_cart[0]**2 + R_cart[1]**2))
  46. R_deg = (np.degrees(R_vect_pol[1]))
  47. LDir = R_vect_pol[0] / R_indiv_magn
  48. print ('R_magn =' + str(R_vect_pol[0]) + ' R_deg = ' + str(np.degrees(R_vect_pol[1])))
  49. print ('R_indiv_magn = ' + str(R_indiv_magn))
  50. print ('LDir = ' + str(LDir))
  51. return LDir
  52. def R_deg(rot_rad, radius):
  53. R_cart = np.array(pol2cart(radius,rot_rad))
  54. R_vect = np.sum(R_cart, 1)
  55. R_vect_pol = cart2pol(R_vect[0], R_vect[1])
  56. R_indiv_magn = np.sum(np.sqrt(R_cart[0]**2 + R_cart[1]**2))
  57. R_deg = (np.degrees(R_vect_pol[1]))
  58. LDir = R_vect_pol[0] / R_indiv_magn
  59. #print 'R_magn =' + str(R_vect_pol[0]) + ' R_deg = ' + str(np.degrees(R_vect_pol[1]))
  60. #print 'R_indiv_magn = ' + str(R_indiv_magn)
  61. #print 'LDir = ' + str(LDir)
  62. return R_deg
  63. def R_magn(rot_rad, radius):
  64. R_cart = np.array(pol2cart(radius,rot_rad))
  65. R_vect = np.sum(R_cart, 1)
  66. R_vect_pol = cart2pol(R_vect[0], R_vect[1])
  67. R_indiv_magn = np.sum(np.sqrt(R_cart[0]**2 + R_cart[1]**2))
  68. R_deg = (np.degrees(R_vect_pol[1]))
  69. LDir = R_vect_pol[0] / R_indiv_magn
  70. #print 'R_magn =' + str(R_vect_pol[0]) + ' R_deg = ' + str(np.degrees(R_vect_pol[1]))
  71. #print 'R_indiv_magn = ' + str(R_indiv_magn)
  72. #print 'LDir = ' + str(LDir)
  73. return R_vect_pol[0]