explore5d.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Read a data from ascii file and make a simple analysis
  2. visualizing 3 of the 5 dimensions of the dataset"""
  3. import numpy as np
  4. from vedo import download, Points, Axes, show
  5. from vedo.pyplot import histogram
  6. ################################### Read the csv data:
  7. delimiter=','
  8. fpath = download('https://vedo.embl.es/examples/data/genes.csv')
  9. with open(fpath, "r") as f:
  10. lines = f.readlines()
  11. data = []
  12. for i,lns in enumerate(lines):
  13. if i==0:
  14. names = lns.split(delimiter) # read header
  15. continue
  16. ln = lns.split(delimiter)
  17. vals = [float(x) for x in ln]
  18. data.append(vals)
  19. data = np.array(data)
  20. print("Print first 5 rows:\n", names)
  21. print(data[:5])
  22. print("Number of rows:", len(data))
  23. ##################################################
  24. # extract the columns into separate vectors:
  25. g0, g1, g2, g3, g4 = data.T # unpack genes
  26. n0, n1, n2, n3, n4 = names
  27. # now create and show histograms of the gene expressions
  28. h0 = histogram(g0, xtitle=n0, c=0)
  29. h1 = histogram(g1, xtitle=n1, c=1)
  30. h2 = histogram(g2, xtitle=n2, c=2)
  31. h3 = histogram(g3, xtitle=n3, c=3, logscale=True)
  32. h4 = histogram(g4, xtitle=n4, c=4)
  33. # this is where you choose what variables to show as 3D points
  34. pts = np.c_[g4,g2,g3] # form an array of 3d points from the columns
  35. pts_1 = pts[g0>0] # select only points that have g0>0
  36. p1 = Points(pts_1).ps(4).c('red5') # create the vedo object (ps=point size)
  37. print("after selection nr. of points is", len(pts_1))
  38. pts_2 = pts[(g0<0) & (g1>.5)] # select excluded points that have g1>0.5
  39. p2 = Points(pts_2).ps(8).c('green') # create the vedo object
  40. axes = Axes(p1+p2, xtitle='gene4', ytitle='gene2', ztitle='gene3', c='k')
  41. # Show the two clouds superposed on a new plotter window:
  42. show([h0, h1, h2, h3, h4, (p1,p2, axes, __doc__)],
  43. shape="1/5", # 1 spaces above and 5 below
  44. sharecam=0, axes=0, zoom=1.4, interactive=True,
  45. ).close()