courtemanche2D_kleinere_diffusionmaps.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/env python3
  2. import ithildin as ith
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import sys
  6. from typing import List
  7. from scipy.interpolate import RectBivariateSpline
  8. from pydiffmap.diffusion_map import DiffusionMap
  9. from pydiffmap import visualization as diff_visualization
  10. import matplotlib
  11. # Courtemanche1998 model, 2 ruimtedimensies, 21 variabelen
  12. data = ith.SimData.from_stem("myokit12/myokit_2")
  13. # Vermijd randeffecten en initialiatie
  14. # TODO: begintijd zou in principe in de log moeten staan
  15. def snij_randen_weg(variabele,begintijd, eindtijd):
  16. def begin(index):
  17. if index == 0:
  18. return begintijd
  19. return variabele.shape[index]//8
  20. def eind(index):
  21. if index == 0:
  22. return eindtijd
  23. if variabele.shape[index] < 20:
  24. return variabele.shape[index]
  25. return (7 * variabele.shape[index])//8
  26. return variabele[begin(0):eind(0),begin(1):eind(1),begin(2):eind(2),begin(3):eind(3)]
  27. def snij_randen_weg2(variables,begintijd,eindtijd):
  28. newdata = dict()
  29. for key in variables.keys():
  30. newdata[key] = snij_randen_weg(variables[key], begintijd,eindtijd)
  31. return newdata
  32. def onderzoek(vars, vt0, t1, aantal_punten, eps, var1, var2, var3, var4,k=64):
  33. vars = snij_randen_weg2(vars, t0, t1)
  34. # We bekijken de faseruimte, tijds- en ruimtecoordinaten zijn daarin niet van belang.
  35. for key in vars.keys():
  36. vars[key] = vars[key].ravel()
  37. # Verlaag aantal punten om geheugengebruik en tijdsduur te beperken.
  38. # replace=True strikt genomen incorrect maar verlaagt geheugengebruik
  39. # en wegens de hoge hoeveelheid punten weinig belangrijk
  40. keuzes = np.random.choice(vars['u'].shape[0], aantal_punten,replace=True)
  41. for key in vars.keys():
  42. vars[key] = vars[key][keuzes]
  43. # Nu de randen verwijderd zijn en we punten in een faseruimte hebben, kunnens we proberen
  44. # diffusion maps te gebruiken.
  45. dmap = DiffusionMap.from_sklearn(epsilon=eps,n_evecs=4,k=k)
  46. dmap.fit(np.column_stack((vars[var1], vars[var2], vars[var3], vars[var4])))
  47. scatter_kwargs = {'c' : dmap.dmap[:,3] }
  48. diff_visualization.embedding_plot(dmap,dim=3, scatter_kwargs=scatter_kwargs)
  49. return dmap
  50. # Different variables are in different units, so normalise them
  51. vars = dict(data.vars)
  52. assert(np.std(data.vars['K']) == 0) # it is constant, so don't plot it (the contrentation of K+ ions remains constant?)
  53. del vars['K']
  54. for key in vars.keys(): # K is constant
  55. vars[key] = vars[key] - np.mean(vars[key])
  56. vars[key] = vars[key] / np.std(vars[key])
  57. t0 = 1
  58. t1 = 150
  59. aantal_punten = 5000
  60. matplotlib.use("TkAgg")
  61. # Choose random combinations of vour variables.
  62. keys = list(vars.keys())
  63. keys.sort() # determinism
  64. np.random.seed(1234) # determinism
  65. for i in range(0,500):
  66. which_keys = np.random.choice(keys, 4, replace=False)
  67. print(which_keys)
  68. onderzoek(vars, t0, t1, aantal_punten, 20, which_keys[0], which_keys[1], which_keys[2], which_keys[3],k=2048)
  69. # 3Dig?
  70. # ['gateh' 'gatexs' 'gated' 'gateoa'] eps=20/k=2048
  71. # ['gateu' 'gatev' 'gatef' 'gatefCa']
  72. # ['Na' 'gateh' 'gatexr' 'gatef']
  73. # ['gatew' 'gateh' 'gatexr' 'gateua']
  74. # ['gateui' 'gateu' 'gatefCa' 'gatew']
  75. onderzoek(vars, t0, t1, aantal_punten, 20, 'gateh', 'gateu', 'gatef', 'gateu')
  76. # ['u' 'gatef' 'Caup' 'gateui']
  77. #/gnu/store/fk9540xx4zv8bpad7zsvfdln7281ydf3-profile/lib/python3.9/site-packages/pydiffmap/diffusion_map.py:159: RuntimeWarning: invalid value encountered in sqrt
  78. # dmap = np.dot(evecs, np.diag(np.sqrt(-1. / evals)))
  79. #['gatexr' 'gateh' 'gatefCa' 'Na']
  80. #/gnu/store/fk9540xx4zv8bpad7zsvfdln7281ydf3-profile/lib/python3.9/site-packages/pydiffmap/diffusion_map.py:159: RuntimeWarning: invalid value encountered in sqrt
  81. # dmap = np.dot(evecs, np.diag(np.sqrt(-1. / evals)))
  82. # ['gatew' 'Carel' 'gatem' 'gatej'], ['gatew' 'gatexs' 'Ca' 'gateu'] --> vertakking
  83. # ['gatej' 'gateoi' 'gateu' 'Caup'] -> loop + ??? vertakking?
  84. # ['gateoi' 'gated' 'gatexs' 'gatej'] --> vertakking
  85. onderzoek(t0, t1, aantal_punten, 0.08, 'gateu', 'Caup', 'gatev', 'gatefCa') # 1D (2D verdwenen?)
  86. onderzoek(t0, t1, aantal_punten, 0.08, 'gatej', 'gateui', 'gatev', 'gateoa') # 2D verdwenen, uitsteeksel??
  87. dmap = onderzoek(t0, t1, aantal_punten, 0.08, 'gateu', 'Caup', 'gateoa', 'gateoi') # 2D verdwenen, vertakking
  88. Another issue is the number of embedding dimensions. The number of significant dimensions of the diffusion map is determined where a remarkable gap occurs in its sorted eigenvalues plot. This is not intrinsically bound to the conventional visualizable dimensions two or three. In contrast, for some other methods such as t-SNE, one can pre-determine the number of visualization dimensions for the embedding optimization to two or three dimensions.