Guest User

Untitled

a guest
Feb 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 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. # -*- coding: utf-8 -*-
  35.  
  36. import numpy as np
  37. import itertools
  38.  
  39. """-----------------------------------
  40. ------------- Pulse shape ------------
  41. -----------------------------------"""
  42.  
  43. # Pulse width
  44. global width
  45. width = 0.3
  46.  
  47. """-----------------------------------
  48. ------------- Parameters -------------
  49. -----------------------------------"""
  50.  
  51. t0 = 0
  52. tf = 200 # Pulses can't start after 200-0.3 ms.
  53. shifts = np.arange(0, 2.05, 0.05) # shifts range
  54.  
  55. """-----------------------------------
  56. ------------- Functions --------------
  57. -----------------------------------"""
  58.  
  59. def time_builder(f, t0=0, tf=300):
  60. """
  61. Function building a list of time points at which a pulse is triggered.
  62. """
  63. return list(np.round(np.arange(t0, tf, 1/f*1000),3))
  64.  
  65. def duo_stim_overlap(t1, t2):
  66. """
  67. Function taking 2 timelines build by time_builder function in input
  68. and returning the ids of overlapping pulses between the 2.
  69. len(t1) < len(t2)
  70. """
  71. pulse_id_t1 = [x for x in range(len(t1)) for y in range(len(t2)) if abs(t1[x] - t2[y]) < width]
  72. pulse_id_t2 = [x for x in range(len(t2)) for y in range(len(t1)) if abs(t2[x] - t1[y]) < width]
  73.  
  74. return pulse_id_t1, pulse_id_t2
  75.  
  76. def optimal_delay(t0, tf, frequences, shifts):
  77. """
  78. Function computing the optimal shift with the optimization method chosen for n waveforms.
  79. - t0 and tf in ms
  80. - frequences: a list of the frequencies to use
  81. - shifts: 1D array like with the shifts to test
  82. """
  83.  
  84. optimal = [] # Result tuple of the form (shift, dict{f_id: [overlapping pulses ids]}, optim parameter)
  85. First = True # First shift iteration condition
  86.  
  87. for s in shifts:
  88. delay = 0 # delay between signals, i.e shift
  89. overlap = dict()
  90. timelines = list()
  91.  
  92. for i in range(len(frequences)):
  93. timelines.append(time_builder(frequences[i], t0+delay, tf))
  94. overlap[i] = list()
  95. delay += s
  96.  
  97. for subset in itertools.combinations(timelines, 2):
  98. p1_stim, p2_stim = duo_stim_overlap(subset[0], subset[1])
  99. overlap[timelines.index(subset[0])] += p1_stim
  100. overlap[timelines.index(subset[1])] += p2_stim
  101.  
  102. optim_param = 0
  103. for key, items in overlap.items():
  104. optim_param += (len(list(set(items))) / len(timelines[key]))
  105.  
  106. if First or optim_param < optimal[2]:
  107. optimal = (s, overlap, optim_param)
  108. First = False
  109. else:
  110. continue
  111.  
  112. return optimal
  113.  
  114. """-----------------------------------
  115. ------------- Program ----------------
  116. -----------------------------------"""
  117.  
  118. worst = []
  119. i = 0
  120. n = 6 # Number of worst combination we are looking for (the worst of the worst)
  121.  
  122. freqs = [20, 30, 40, 50, 60, 80, 100, 125, 250, 500]
  123.  
  124. for subset in itertools.combinations(freqs, 5):
  125. stim_overlap = optimal_delay(t0, tf, list(subset), shifts)
  126.  
  127. if i < n:
  128. worst.append((subset, stim_overlap[2]))
  129. i += 1
  130. continue
  131. elif True in [stim_overlap[2] > worst[n][1] for n in range(len(worst))]:
  132. id = [stim_overlap[2] > worst[n][1] for n in range(len(worst))].index(True)
  133. worst[id] = (subset, stim_overlap[2])
  134. i += 1
  135. continue
  136. else:
  137. i +=1
  138. continue
Add Comment
Please, Sign In to add comment