ROIrelevance.py 4.6 KB

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