relvaneOfPulses.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Aug 17 21:52:28 2020
  5. @author: p277634
  6. """
  7. import numpy as np
  8. import sys
  9. import visArchis_seq as v
  10. # import optimProbs as oudp
  11. import myKAOctrl_stageSeq as udp
  12. import scipy as sp
  13. import copy
  14. nSample = 1000
  15. topNbest = 10000 #~the best 1%
  16. d = v.LoadData()
  17. d.nameModel = 'stgSequence_Lopt_allPu1500_wE_P030_optIni_v5_fixG0p15L0p6_Stg'
  18. d.nameEnding =''
  19. d.stages = [1,2,3,4] # te read file names
  20. # saveTo = '/Users/p277634/kaoAsoMemSimu/analysis/relevanceOfPulses_P040_G0p3L0p7.npz'
  21. saveTo = './dataSave/relevanceOfPulses_P030_G0p15L0p6.npz'
  22. # get data
  23. pulse = d.getPulses()
  24. fit = d.getFitness()
  25. m = d.getBestModel()
  26. # pli = d.getPLI()
  27. # Merge all islands
  28. allPulse, allCost, allsCor, allRenv, allFC = [None]*pulse.nStg, [None]*pulse.nStg, [None]*pulse.nStg, [None]*pulse.nStg, [None]*pulse.nStg
  29. for sIx in range(pulse.nStg):
  30. mergeP_, mergeC_, mergeScor_, mergeRenv_ = [], [], [], []
  31. # mergeFC_ = []
  32. for iIx in range(pulse.nIsla):
  33. mergeP_.append(pulse.d[iIx][sIx])
  34. mergeC_.append(fit.cost[iIx][sIx])
  35. mergeScor_.append(fit.sCor[iIx][sIx].ravel())
  36. mergeRenv_.append(np.squeeze(fit.renv[iIx][sIx]))
  37. # mergeFC_.append(pli[iIx][sIx])
  38. allPulse[sIx] = np.vstack(mergeP_)
  39. allCost[sIx] = np.hstack(mergeC_)
  40. allsCor[sIx] = np.hstack(mergeScor_)
  41. allRenv[sIx] = np.vstack(mergeRenv_)
  42. # allFC[sIx] = np.squeeze(np.vstack(mergeFC_))
  43. # Keep top N best solutions
  44. pulseTh, costTh, fcTh, sCorTh, rEnvTh = [None]*4, [None]*4, [None]*4, [None]*4, [None]*4
  45. minCostTh = np.zeros(4)
  46. for i in range(4):
  47. pulseTh[i] = allPulse[i][ np.argsort(allCost[i])[-topNbest:], :]
  48. costTh[i] = allCost[i][ np.argsort(allCost[i])[-topNbest:]]
  49. sCorTh[i] = allsCor[i][ np.argsort(allCost[i])[-topNbest:]]
  50. rEnvTh[i] = allRenv[i][ np.argsort(allCost[i])[-topNbest:], :]
  51. # fcTh[i] = allFC[i][np.argsort(allCost[i])[-topNbest:],:]
  52. minCostTh[i]= np.min(costTh[i])
  53. iXpick = np.random.choice(np.arange(topNbest), size=nSample, replace=False)
  54. del pulse, fit, allPulse, allCost, allRenv, allsCor
  55. # best sequence of pulses and scores
  56. pulseBest = np.zeros((m.nroi,m.nStg))
  57. fitBest = np.zeros(m.nStg)
  58. sCorBest = np.zeros(m.nStg)
  59. rEnvBest = np.zeros((m.nStg,68))
  60. for i in range(m.nStg):
  61. pulseBest[:,i] = m.logs[i][0][0]
  62. fitBest[i] = m.logs[i][0][3]
  63. sCorBest[i] = m.logs[i][0][6]
  64. rEnvBest[i,:] = np.squeeze(m.logs[i][0][9])
  65. fitRef = np.zeros((nSample,4))
  66. sCorRef = np.zeros((nSample,4))
  67. rEnvRef = np.zeros((nSample,4,68))
  68. pliRef = np.zeros((nSample,2278,4))
  69. # pliTe = np.zeros((nSample,68,2278,4))
  70. fitTe = np.zeros((nSample,68,4))
  71. sCorTe = np.zeros((nSample,68,4))
  72. rEnvTe = np.zeros((nSample,68,4,68))
  73. pulseTe = np.zeros((nSample,68,4))
  74. for iXt in range(nSample):
  75. for iXs in range(4):
  76. pulseRand = pulseTh[iXs][iXpick[iXt],:] # pulse to be tested
  77. pulseTe[iXt,:,iXs] = pulseRand
  78. stages = list(range(iXs+1))
  79. if len(stages) == 1:
  80. kao = udp.KAOmodel(stageIx=stages[0])
  81. else:
  82. kao = udp.KAOmodel(stageIx=stages)
  83. # reference model with all pulses
  84. # kao = udp.KAOmodel(stageIx=stages)
  85. kao._setHistDelayAndContainers()
  86. kao._edgesImpulse()
  87. kao._getEmpiReativEnv()
  88. kao.doNoise(variance=0)
  89. # do pulses
  90. pulseTest = copy.deepcopy(pulseBest[:,stages])
  91. pulseTest[:,iXs] = pulseRand
  92. kao.pulse = pulseTest
  93. kao.runModel_Pulsein_noise()
  94. fit_ = kao.similarityAllPLIs()
  95. fitRef[iXt, iXs] = fit_[iXs]
  96. pliRef[iXt, :, iXs] = kao.pli[iXs,:]
  97. kao.relativeEnvBuPre()
  98. kao.getScorrelRelEnv()
  99. sCorRef[iXt, iXs] = kao.sCor[iXs]
  100. rEnvRef[iXt, iXs,:] = kao.relEnv[:,iXs]
  101. # relevance of a missing pulse
  102. for iXroi in range(68):
  103. if len(stages) == 1:
  104. kao = udp.KAOmodel(stageIx=stages[0])
  105. else:
  106. kao = udp.KAOmodel(stageIx=stages)
  107. kao._setHistDelayAndContainers()
  108. kao._edgesImpulse()
  109. kao._getEmpiReativEnv()
  110. kao.doNoise(variance=0)
  111. # do pulses
  112. pulseTest = copy.deepcopy(pulseBest[:,stages])
  113. pulseTest[:,iXs] = pulseRand
  114. pulseTest[iXroi,iXs] = 0
  115. kao.pulse = pulseTest
  116. kao.runModel_Pulsein_noise()
  117. fit_ = kao.similarityAllPLIs()
  118. fitTe[iXt, iXroi, iXs] = fit_[iXs]
  119. kao.relativeEnvBuPre()
  120. kao.getScorrelRelEnv()
  121. sCorTe[iXt, iXroi, iXs] = kao.sCor[iXs]
  122. rEnvTe[iXt, iXroi, iXs,:] = kao.relEnv[:,iXs]
  123. np.savez(saveTo, rEnvTe=rEnvTe, sCorTe=sCorTe, fitTe=fitTe, pulseTe=pulseTe, pulseTh=pulseTh,costTh=costTh,iXpick=iXpick,
  124. fitRef=fitRef, sCorRef=sCorRef, pliRef=pliRef, rEnvRef=rEnvRef, sCorTh=sCorTh, rEnvTh=rEnvTh,
  125. pulseBest=pulseBest, fitBest=fitBest, sCorBest=sCorBest, rEnvBest=rEnvBest)