Guest User

Untitled

a guest
Feb 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. wave1 = [0.0, 50.0, 100.0]
  2. wave2 = [0.5, 20.5, 40.5, 60.5, 80.5, 100.5, 120.5, 140.5]
  3. wave3 = [1.0, 11.0, 21.0, 31.0, 41.0, 51.0, 61.0, 71.0, 81.0, 91.0, 101.0, 111.0, 121.0, 131.0, 141.0]
  4.  
  5. worst = []
  6. i = 0
  7. n = 6 # Number of worst combination we are looking for (the worst of the worst)
  8.  
  9. freqs = [20, 30, 40, 50, 60, 80, 100, 125, 250, 500]
  10.  
  11. for subset in itertools.combinations(freqs, 5):
  12. stim_overlap = optimal_delay(optimization_method, t0, tf, list(subset), shifts)
  13.  
  14. if i < n:
  15. worst.append((subset, stim_overlap[2]))
  16. i += 1
  17. continue
  18. elif True in [stim_overlap[2] > worst[n][1] for n in range(len(worst))]:
  19. id = [stim_overlap[2] > worst[n][1] for n in range(len(worst))].index(True)
  20. worst[id] = (subset, stim_overlap[2])
  21. i += 1
  22. continue
  23. else:
  24. i +=1
  25. continue
  26.  
  27. ((40, 60, 80, 250, 500), 1.0673737373737373)
  28. ((50, 60, 80, 250, 500), 1.0248737373737373)
  29. ((60, 80, 125, 250, 500), 0.961540404040404)
  30. ((40, 80, 125, 250, 500), 0.785)
  31. ((60, 80, 100, 250, 500), 0.7357070707070708)
  32. ((60, 80, 100, 125, 500), 0.6732070707070708)
  33.  
  34. [1.0673737373737373, 1.0358080808080807, 1.0248737373737373, 0.961540404040404, 0.9118434343434343, 0.9008333333333333, 0.8920578231292516, 0.8916414141414141, 0.8899747474747475, 0.8899747474747475]
  35.  
  36. # -*- coding: utf-8 -*-
  37.  
  38. import numpy as np
  39. import itertools
  40.  
  41. """-----------------------------------
  42. ------------- Pulse shape ------------
  43. -----------------------------------"""
  44.  
  45. # Pulse width
  46. global width
  47. width = 0.3
  48.  
  49. """-----------------------------------
  50. ------------- Parameters -------------
  51. -----------------------------------"""
  52.  
  53. t0 = 0
  54. tf = 200 # Pulses can't start after 200-0.3 ms.
  55. shifts = np.arange(0, 2.05, 0.05) # shifts range
  56.  
  57. """-----------------------------------
  58. ------------- Functions --------------
  59. -----------------------------------"""
  60.  
  61. def time_builder(f, t0=0, tf=300):
  62. """
  63. Function building a list of time points at which a pulse is triggered.
  64. """
  65. return list(np.round(np.arange(t0, tf, 1/f*1000),3))
  66.  
  67. def duo_stim_overlap(t1, t2):
  68. """
  69. Function taking 2 timelines build by time_builder function in input
  70. and returning the ids of overlapping pulses between the 2.
  71. len(t1) < len(t2)
  72. """
  73. pulse_id_t1 = [x for x in range(len(t1)) for y in range(len(t2)) if abs(t1[x] - t2[y]) < width]
  74. pulse_id_t2 = [x for x in range(len(t2)) for y in range(len(t1)) if abs(t2[x] - t1[y]) < width]
  75.  
  76. return pulse_id_t1, pulse_id_t2
  77.  
  78. def optimal_delay(t0, tf, frequences, shifts):
  79. """
  80. Function computing the optimal shift with the optimization method chosen for n waveforms.
  81. - t0 and tf in ms
  82. - frequences: a list of the frequencies to use
  83. - shifts: 1D array like with the shifts to test
  84. """
  85.  
  86. optimal = [] # Result tuple of the form (shift, dict{f_id: [overlapping pulses ids]}, optim parameter)
  87. First = True # First shift iteration condition
  88.  
  89. for s in shifts:
  90. delay = 0 # delay between signals, i.e shift
  91. overlap = dict()
  92. timelines = list()
  93.  
  94. for i in range(len(frequences)):
  95. timelines.append(time_builder(frequences[i], t0+delay, tf))
  96. overlap[i] = list()
  97. delay += s
  98.  
  99. for subset in itertools.combinations(timelines, 2):
  100. p1_stim, p2_stim = duo_stim_overlap(subset[0], subset[1])
  101. overlap[timelines.index(subset[0])] += p1_stim
  102. overlap[timelines.index(subset[1])] += p2_stim
  103.  
  104. optim_param = 0
  105. for key, items in overlap.items():
  106. optim_param += (len(list(set(items))) / len(timelines[key]))
  107.  
  108. if First or optim_param < optimal[2]:
  109. optimal = (s, overlap, optim_param)
  110. First = False
  111. else:
  112. continue
  113.  
  114. return optimal
  115.  
  116. """-----------------------------------
  117. ------------- Program ----------------
  118. -----------------------------------"""
  119.  
  120. worst = []
  121. i = 0
  122. n = 6 # Number of worst combination we are looking for (the worst of the worst)
  123.  
  124. freqs = [20, 30, 40, 50, 60, 80, 100, 125, 250, 500]
  125.  
  126. for subset in itertools.combinations(freqs, 5):
  127. stim_overlap = optimal_delay(t0, tf, list(subset), shifts)
  128.  
  129. if i < n:
  130. worst.append((subset, stim_overlap[2]))
  131. i += 1
  132. continue
  133. elif True in [stim_overlap[2] > worst[n][1] for n in range(len(worst))]:
  134. id = [stim_overlap[2] > worst[n][1] for n in range(len(worst))].index(True)
  135. worst[id] = (subset, stim_overlap[2])
  136. i += 1
  137. continue
  138. else:
  139. i +=1
  140. continue
Add Comment
Please, Sign In to add comment