WFProcess.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. import os, sys, math, functools
  4. import multiprocessing as mp
  5. import matplotlib
  6. import numpy as np
  7. import easygui
  8. import colour
  9. # import Qt
  10. # QT matplotlib
  11. # miam import
  12. from . import WFNode
  13. import miam.image.Image as MIMG
  14. import miam.histogram.Histogram as MHIST
  15. import miam.image.channel
  16. import miam.utils
  17. # gui import
  18. # ------------------------------------------------------------------------------------------
  19. # MIAM project 2020
  20. # ------------------------------------------------------------------------------------------
  21. # author: remi.cozot@univ-littoral.fr
  22. # ------------------------------------------------------------------------------------------
  23. class WFProcess(WFNode.WFNode):
  24. """description of class"""
  25. def __init__(self, name = 'processing:noname', process=None):
  26. super().__init__(name)
  27. # attibutes
  28. self.inputs = []
  29. self.process = process
  30. self.parameters ={}
  31. self.outputs = []
  32. def setParameters(self,p):
  33. self.parameters = p
  34. return self
  35. def isReady(self):
  36. res = functools.reduce(
  37. lambda x,y : x and y,
  38. list(map(lambda x: x.ready, self.inputs)),
  39. True)
  40. print("WFProcess[",self.name," is ready for computation:", res,"]")
  41. return res
  42. def compute(self):
  43. # managing input
  44. # -----------------------------------------------------------------------------------------
  45. # if single input: take self.inputs[0].image !! note that self.inputs[0].image could be a list
  46. # if multiple inputs: create a list of each input
  47. # -----------------------------------------------------------------------------------------
  48. if len(self.inputs)== 1:
  49. # single input
  50. img = self.inputs[0].image
  51. else:
  52. # multiple inputs
  53. img =[]
  54. for input in self.inputs:
  55. img.append(input.image)
  56. # compute process object
  57. # -----------------------------------------------------------------------------------------
  58. # img is single Image or Image list
  59. # -----------------------------------------------------------------------------------------
  60. print('WFProcess[',self.name,'].compute(',self.parameters,')')
  61. resImg = self.process.compute(img,**self.parameters)
  62. # push results
  63. # -----------------------------------------------------------------------------------------
  64. # two cases:
  65. # 1 - len of resImg is be equal to len of outputs: push one image to output
  66. # 2 - len of resImg > 1 and len of output is equal to 1: push image list to single output
  67. # -----------------------------------------------------------------------------------------
  68. if not isinstance(resImg,list):
  69. if len(self.outputs) == 1 :
  70. # case 1: just a single Image, just a single ouput
  71. self.outputs[0].image = resImg
  72. self.outputs[0].ready = True
  73. else:
  74. # error
  75. print("[ERROR] WFProcess(WFNode.WFNode).compute:: resImg is single Image but multiple outputs!")
  76. else:
  77. # a list of Image
  78. if len(resImg) == len(self.outputs):
  79. # case 1
  80. for i in range(len(self.outputs)):
  81. self.outputs[i].image = resImg[i]
  82. self.outputs[i].ready = True
  83. else:
  84. # case 2
  85. self.outputs[0].image = resImg
  86. self.outputs[0].ready = True