src/stats/Connectedness.js

  1.  
  2. import Stat from "./Stat.js"
  3. import ConnectedComponentsByCell from "./ConnectedComponentsByCell.js"
  4.  
  5. /** This Stat computes the 'connectedness' of cells on the grid.
  6. Keys are the {@link CellId} of all cells on the grid, corresponding values the
  7. connectedness of the corresponding cell.
  8. @example
  9. * let CPM = require( "path/to/build" )
  10. *
  11. * // Make a CPM, seed a cell, and get the Connectedness
  12. * let C = new CPM.CPM( [100,100], {
  13. * T:20,
  14. * J:[[0,20],[20,10]],
  15. * V:[0,200],
  16. * LAMBDA_V:[0,2]
  17. * } )
  18. * let gm = new CPM.GridManipulator( C )
  19. * gm.seedCell(1)
  20. * for( let t = 0; t < 100; t++ ){ C.timeStep() }
  21. * C.getStat( CPM.Connectedness )
  22. */
  23. class Connectedness extends Stat {
  24.  
  25. /** This method computes the connectedness of a specific cell.
  26. @return {number} the connectedness value of this cell, a number between 0 and 1.
  27. */
  28. connectednessOfCell( cellid ){
  29. let ccbc = this.M.getStat( ConnectedComponentsByCell )
  30. const v = ccbc[cellid]
  31. //let s = {}, r = {}, i, j
  32. let s = 0, r = 0
  33. for( let comp in Object.keys( v ) ){
  34. let volume = v[comp].length
  35. s += volume
  36. }
  37. for( let comp in Object.keys( v ) ){
  38. let volume = v[comp].length
  39. r += (volume/s)*(volume/s)
  40. }
  41. return r
  42.  
  43. }
  44.  
  45. /** The compute method of Connectedness creates an object with
  46. connectedness of each cell on the grid.
  47. @return {CellObject} object with for each cell on the grid
  48. a connectedness value.
  49. */
  50. compute(){
  51. // initialize the object
  52. let connectedness = { }
  53. // The this.M.pixels() iterator returns coordinates and cellid for all
  54. // non-background pixels on the grid. See the appropriate Grid class for
  55. // its implementation.
  56. for( let ci of this.M.cellIDs() ){
  57. connectedness[ci] = this.connectednessOfCell( ci )
  58. }
  59. return connectedness
  60. }
  61. }
  62.  
  63. export default Connectedness