def __init__(self, location, identifier):
     self._location = location
     self._identifier = identifier
     self._filenameStem = os.path.join(self._location, self._identifier)
     self._context = Context("MeshGenerator")
     self._initialise()
     self._region = self._context.createRegion()
     self._generator_model = MeshGeneratorModel(self._region, self._materialmodule)
     self._plane_model = MeshPlaneModel(self._region)
 def execute(self):
     """
     Kick off the execution of the step, in this case an interactive dialog.
     User invokes the _doneExecution() method when finished, via pushbutton.
     """
     self._model = MeshGeneratorModel(self._location, self._config['identifier'])
     self._view = MeshGeneratorWidget(self._model)
     self._view.registerDoneExecution(self._doneExecution)
     self._setCurrentWidget(self._view)
Ejemplo n.º 3
0
    def __init__(self, location, identifier):
        self._location = location
        self._identifier = identifier
        self._filenameStem = os.path.join(self._location, self._identifier)
        self._context = Context("MeshGenerator")
        self._timekeeper = self._context.getTimekeepermodule(
        ).getDefaultTimekeeper()
        self._timer = QtCore.QTimer()
        self._current_time = 0.0
        self._timeValueUpdate = None
        self._frameIndexUpdate = None
        self._initialise()
        self._region = self._context.createRegion()
        self._generator_model = MeshGeneratorModel(self._region,
                                                   self._materialmodule)
        self._annotation_model = MeshAnnotationModel()

        self._settings = {'frames-per-second': 25, 'time-loop': False}
        self._makeConnections()
class MasterModel(object):
    def __init__(self, location, identifier):
        self._location = location
        self._identifier = identifier
        self._filenameStem = os.path.join(self._location, self._identifier)
        self._context = Context("MeshGenerator")
        self._timekeeper = self._context.getTimekeepermodule(
        ).getDefaultTimekeeper()
        self._timer = QtCore.QTimer()
        self._current_time = 0.0
        self._timeValueUpdate = None
        self._frameIndexUpdate = None
        self._initialise()
        self._region = self._context.createRegion()
        self._generator_model = MeshGeneratorModel(self._region,
                                                   self._materialmodule)
        self._plane_model = MeshPlaneModel(self._region)
        self._fiducial_marker_model = FiducialMarkerModel(self._region)
        self._fiducial_marker_model.registerGetPlaneInfoMethod(
            self._plane_model.getPlaneInfo)
        self._settings = {'frames-per-second': 25, 'time-loop': False}
        self._makeConnections()
        # self._loadSettings()

    def printLog(self):
        logger = self._context.getLogger()
        for index in range(logger.getNumberOfMessages()):
            print(logger.getMessageTextAtIndex(index))

    def _initialise(self):
        self._filenameStem = os.path.join(self._location, self._identifier)
        tess = self._context.getTessellationmodule().getDefaultTessellation()
        tess.setRefinementFactors(12)
        # set up standard materials and glyphs so we can use them elsewhere
        self._materialmodule = self._context.getMaterialmodule()
        self._materialmodule.defineStandardMaterials()
        solid_blue = self._materialmodule.createMaterial()
        solid_blue.setName('solid_blue')
        solid_blue.setManaged(True)
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_AMBIENT,
                                     [0.0, 0.2, 0.6])
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_DIFFUSE,
                                     [0.0, 0.7, 1.0])
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_EMISSION,
                                     [0.0, 0.0, 0.0])
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_SPECULAR,
                                     [0.1, 0.1, 0.1])
        solid_blue.setAttributeReal(Material.ATTRIBUTE_SHININESS, 0.2)
        trans_blue = self._materialmodule.createMaterial()
        trans_blue.setName('trans_blue')
        trans_blue.setManaged(True)
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_AMBIENT,
                                     [0.0, 0.2, 0.6])
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_DIFFUSE,
                                     [0.0, 0.7, 1.0])
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_EMISSION,
                                     [0.0, 0.0, 0.0])
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_SPECULAR,
                                     [0.1, 0.1, 0.1])
        trans_blue.setAttributeReal(Material.ATTRIBUTE_ALPHA, 0.3)
        trans_blue.setAttributeReal(Material.ATTRIBUTE_SHININESS, 0.2)
        glyphmodule = self._context.getGlyphmodule()
        glyphmodule.defineStandardGlyphs()

    def _makeConnections(self):
        self._timer.timeout.connect(self._timeout)

    def _timeout(self):
        self._current_time += 1000 / self._settings['frames-per-second'] / 1000
        duration = self._plane_model.getFrameCount(
        ) / self._settings['frames-per-second']
        if self._settings['time-loop'] and self._current_time > duration:
            self._current_time -= duration
        self._timekeeper.setTime(self._scaleCurrentTimeToTimekeeperTime())
        self._timeValueUpdate(self._current_time)
        if not self._plane_model.isDisabled():
            frame_index = self._plane_model.getFrameIndexForTime(
                self._current_time, self._settings['frames-per-second']) + 1
            self._frameIndexUpdate(frame_index)

    def _scaleCurrentTimeToTimekeeperTime(self):
        scaled_time = 0.0
        duration = self._plane_model.getFrameCount(
        ) / self._settings['frames-per-second']
        if duration > 0:
            scaled_time = self._current_time / duration

        return scaled_time

    def getIdentifier(self):
        return self._identifier

    def getOutputModelFilename(self):
        return self._filenameStem + '.ex2'

    def getGeneratorModel(self):
        return self._generator_model

    def getPlaneModel(self):
        return self._plane_model

    def getFiducialMarkerModel(self):
        return self._fiducial_marker_model

    def getScene(self):
        return self._region.getScene()

    def getContext(self):
        return self._context

    def setFrameIndex(self, frame_index):
        frame_value = frame_index - 1
        self._current_time = self._plane_model.getTimeForFrameIndex(
            frame_value, self._settings['frames-per-second'])
        self._timekeeper.setTime(self._scaleCurrentTimeToTimekeeperTime())
        self._timeValueUpdate(self._current_time)

    def setTimeValue(self, time):
        self._current_time = time
        self._timekeeper.setTime(self._scaleCurrentTimeToTimekeeperTime())
        frame_index = self._plane_model.getFrameIndexForTime(
            time, self._settings['frames-per-second']) + 1
        self._frameIndexUpdate(frame_index)

    def setFramesPerSecond(self, value):
        self._settings['frames-per-second'] = value

    def getFramesPerSecond(self):
        return self._settings['frames-per-second']

    def setTimeLoop(self, state):
        self._settings['time-loop'] = state

    def isTimeLoop(self):
        return self._settings['time-loop']

    def play(self):
        self._timer.start(1000 / self._settings['frames-per-second'])

    def stop(self):
        self._timer.stop()

    def registerFrameIndexUpdateCallback(self, frameIndexUpdateCallback):
        self._frameIndexUpdate = frameIndexUpdateCallback

    def registerTimeValueUpdateCallback(self, timeValueUpdateCallback):
        self._timeValueUpdate = timeValueUpdateCallback

    def registerSceneChangeCallback(self, sceneChangeCallback):
        self._generator_model.registerSceneChangeCallback(sceneChangeCallback)

    def done(self):
        self._saveSettings()
        self._generator_model.writeModel(self.getOutputModelFilename())

    def _getSettings(self):
        settings = self._settings
        settings['generator_settings'] = self._generator_model.getSettings()
        settings['image_plane_settings'] = self._plane_model.getSettings()
        settings['fiducial-markers'] = self._fiducial_marker_model.getSettings(
        )
        return settings

    def loadSettings(self):
        try:
            settings = self._settings
            with open(self._filenameStem + '-settings.json', 'r') as f:
                settings.update(json.loads(f.read()))
            if not 'generator_settings' in settings:
                # migrate from old settings before named generator_settings
                settings = {'generator_settings': settings}
            if 'image_plane_settings' not in settings:
                settings.update(
                    {'image_plane_settings': self._plane_model.getSettings()})
            if 'fiducial-markers' not in settings:
                settings.update({
                    'fiducial-markers':
                    self._fiducial_marker_model.getSettings()
                })
        except:
            # no settings saved yet, following gets defaults
            settings = self._getSettings()
        self._generator_model.setSettings(settings['generator_settings'])
        self._plane_model.setSettings(settings['image_plane_settings'])
        self._fiducial_marker_model.setSettings(settings['fiducial-markers'])

    def _saveSettings(self):
        settings = self._getSettings()
        with open(self._filenameStem + '-settings.json', 'w') as f:
            f.write(
                json.dumps(settings,
                           default=lambda o: o.__dict__,
                           sort_keys=True,
                           indent=4))
class MasterModel(object):
    def __init__(self, location, identifier):
        self._location = location
        self._identifier = identifier
        self._filenameStem = os.path.join(self._location, self._identifier)
        self._context = Context("MeshGenerator")
        self._timekeeper = self._context.getTimekeepermodule(
        ).getDefaultTimekeeper()
        self._timer = QtCore.QTimer()
        self._current_time = 0.0
        self._timeValueUpdate = None
        self._frameIndexUpdate = None
        self._initialise()
        self._region = self._context.createRegion()
        self._generator_model = MeshGeneratorModel(self._context, self._region,
                                                   self._materialmodule)
        self._segmentation_data_model = SegmentationDataModel(
            self._region, self._materialmodule)
        self._annotation_model = MeshAnnotationModel()

        self._settings = {
            'segmentation_data_settings':
            self._segmentation_data_model.getSettings()
        }
        self._makeConnections()
        # self._loadSettings()

    def printLog(self):
        logger = self._context.getLogger()
        for index in range(logger.getNumberOfMessages()):
            print(logger.getMessageTextAtIndex(index))

    def _initialise(self):
        self._filenameStem = os.path.join(self._location, self._identifier)
        tess = self._context.getTessellationmodule().getDefaultTessellation()
        tess.setRefinementFactors(12)
        # set up standard materials and glyphs so we can use them elsewhere
        self._materialmodule = self._context.getMaterialmodule()
        self._materialmodule.defineStandardMaterials()
        solid_blue = self._materialmodule.createMaterial()
        solid_blue.setName('solid_blue')
        solid_blue.setManaged(True)
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_AMBIENT,
                                     [0.0, 0.2, 0.6])
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_DIFFUSE,
                                     [0.0, 0.7, 1.0])
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_EMISSION,
                                     [0.0, 0.0, 0.0])
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_SPECULAR,
                                     [0.1, 0.1, 0.1])
        solid_blue.setAttributeReal(Material.ATTRIBUTE_SHININESS, 0.2)
        trans_blue = self._materialmodule.createMaterial()
        trans_blue.setName('trans_blue')
        trans_blue.setManaged(True)
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_AMBIENT,
                                     [0.0, 0.2, 0.6])
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_DIFFUSE,
                                     [0.0, 0.7, 1.0])
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_EMISSION,
                                     [0.0, 0.0, 0.0])
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_SPECULAR,
                                     [0.1, 0.1, 0.1])
        trans_blue.setAttributeReal(Material.ATTRIBUTE_ALPHA, 0.3)
        trans_blue.setAttributeReal(Material.ATTRIBUTE_SHININESS, 0.2)
        glyphmodule = self._context.getGlyphmodule()
        glyphmodule.defineStandardGlyphs()

    def _makeConnections(self):
        pass

    def getIdentifier(self):
        return self._identifier

    def getOutputModelFilename(self):
        return self._filenameStem + '.exf'

    def getOutputAnnotationsFilename(self):
        return self._generator_model.getAnnotationsFilename(self._filenameStem)

    def getGeneratorModel(self):
        return self._generator_model

    def getMeshAnnotationModel(self):
        return self._annotation_model

    def getSegmentationDataModel(self):
        return self._segmentation_data_model

    def getScene(self):
        return self._region.getScene()

    def getContext(self):
        return self._context

    def registerSceneChangeCallback(self, sceneChangeCallback):
        self._generator_model.registerSceneChangeCallback(sceneChangeCallback)

    def done(self):
        self._saveSettings()
        self._generator_model.done()
        self._generator_model.writeModel(self.getOutputModelFilename())
        self._generator_model.writeAnnotations(self._filenameStem)
        self._generator_model.exportToVtk(self._filenameStem)

    def _getSettings(self):
        '''
        Ensures master model settings includes current settings for sub models.
        :return: Master setting dict.
        '''
        settings = self._settings
        settings['generator_settings'] = self._generator_model.getSettings()
        settings[
            'segmentation_data_settings'] = self._segmentation_data_model.getSettings(
            )
        return settings

    def loadSettings(self):
        try:
            settings = self._settings
            with open(self._filenameStem + '-settings.json', 'r') as f:
                savedSettings = json.loads(f.read(),
                                           object_hook=Scaffolds_decodeJSON)
                settings.update(savedSettings)
            if not 'generator_settings' in settings:
                # migrate from old settings before named generator_settings
                settings = {'generator_settings': settings}
        except:
            # no settings saved yet, following gets defaults
            settings = self._getSettings()
        self._generator_model.setSettings(settings['generator_settings'])
        self._segmentation_data_model.setSettings(
            settings['segmentation_data_settings'])
        self._annotation_model.setScaffoldTypeByName(
            self._generator_model.getEditScaffoldTypeName())
        self._getSettings()

    def _saveSettings(self):
        self._generator_model.updateSettingsBeforeWrite()
        settings = self._getSettings()
        with open(self._filenameStem + '-settings.json', 'w') as f:
            f.write(
                json.dumps(settings,
                           cls=Scaffolds_JSONEncoder,
                           sort_keys=True,
                           indent=4))

    def setSegmentationDataFile(self, data_filename):
        self._segmentation_data_model.setDataFilename(data_filename)
class MasterModel(object):

    def __init__(self, location, identifier):
        self._location = location
        self._identifier = identifier
        self._filenameStem = os.path.join(self._location, self._identifier)
        self._context = Context("MeshGenerator")
        self._initialise()
        self._region = self._context.createRegion()
        self._generator_model = MeshGeneratorModel(self._region, self._materialmodule)
        self._plane_model = MeshPlaneModel(self._region)
        # self._loadSettings()

    def _initialise(self):
        self._filenameStem = os.path.join(self._location, self._identifier)
        tess = self._context.getTessellationmodule().getDefaultTessellation()
        tess.setRefinementFactors(12)
        # set up standard materials and glyphs so we can use them elsewhere
        self._materialmodule = self._context.getMaterialmodule()
        self._materialmodule.defineStandardMaterials()
        solid_blue = self._materialmodule.createMaterial()
        solid_blue.setName('solid_blue')
        solid_blue.setManaged(True)
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_AMBIENT, [ 0.0, 0.2, 0.6 ])
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_DIFFUSE, [ 0.0, 0.7, 1.0 ])
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_EMISSION, [ 0.0, 0.0, 0.0 ])
        solid_blue.setAttributeReal3(Material.ATTRIBUTE_SPECULAR, [ 0.1, 0.1, 0.1 ])
        solid_blue.setAttributeReal(Material.ATTRIBUTE_SHININESS , 0.2)
        trans_blue = self._materialmodule.createMaterial()
        trans_blue.setName('trans_blue')
        trans_blue.setManaged(True)
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_AMBIENT, [ 0.0, 0.2, 0.6 ])
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_DIFFUSE, [ 0.0, 0.7, 1.0 ])
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_EMISSION, [ 0.0, 0.0, 0.0 ])
        trans_blue.setAttributeReal3(Material.ATTRIBUTE_SPECULAR, [ 0.1, 0.1, 0.1 ])
        trans_blue.setAttributeReal(Material.ATTRIBUTE_ALPHA , 0.3)
        trans_blue.setAttributeReal(Material.ATTRIBUTE_SHININESS , 0.2)
        glyphmodule = self._context.getGlyphmodule()
        glyphmodule.defineStandardGlyphs()

    def getIdentifier(self):
        return self._identifier

    def getOutputModelFilename(self):
        return self._filenameStem + '.ex2'

    def getGeneratorModel(self):
        return self._generator_model

    def getPlaneModel(self):
        return self._plane_model

    def getScene(self):
        return self._region.getScene()

    def getContext(self):
        return self._context

    def registerSceneChangeCallback(self, sceneChangeCallback):
        self._generator_model.registerSceneChangeCallback(sceneChangeCallback)

    def done(self):
        self._saveSettings()
        self._generator_model.writeModel(self.getOutputModelFilename())

    def _getSettings(self):
        generator_settings = self._generator_model.getSettings()
        image_plane_settings = self._plane_model.getSettings()
        return {'generator_settings': generator_settings, 'image_plane_settings': image_plane_settings}

    def loadSettings(self):
        try:
            settings = {}
            with open(self._filenameStem + '-settings.json', 'r') as f:
                settings.update(json.loads(f.read()))
            if not 'generator_settings' in settings:
                # migrate from old settings before named generator_settings
                settings = {'generator_settings': settings}
            if 'image_plane_settings' not in settings:
                settings.update({'image_plane_settings': self._plane_model.getSettings()})
        except:
            # no settings saved yet, following gets defaults
            settings = self._getSettings()
        self._generator_model.setSettings(settings['generator_settings'])
        self._plane_model.setSettings(settings['image_plane_settings'])

    def _saveSettings(self):
        settings = self._getSettings()
        with open(self._filenameStem + '-settings.json', 'w') as f:
            f.write(json.dumps(settings, default=lambda o: o.__dict__, sort_keys=True, indent=4))
Ejemplo n.º 7
0
class MeshGeneratorStep(WorkflowStepMountPoint):
    """
    Skeleton step which is intended to be a helpful starting point
    for new steps.
    """
    def __init__(self, location):
        super(MeshGeneratorStep, self).__init__('Mesh Generator', location)
        self._configured = False  # A step cannot be executed until it has been configured.
        self._category = 'Source'
        # Add any other initialisation code here:
        self._icon = QtGui.QImage(
            ':/meshgeneratorstep/images/model-viewer.png')
        # Ports:
        self.addPort(
            ('http://physiomeproject.org/workflow/1.0/rdf-schema#port',
             'http://physiomeproject.org/workflow/1.0/rdf-schema#provides',
             'http://physiomeproject.org/workflow/1.0/rdf-schema#file_location'
             ))
        # Port data:
        self._portData0 = None  # http://physiomeproject.org/workflow/1.0/rdf-schema#file_location
        # Config:
        self._config = {}
        self._config['identifier'] = ''
        self._config['AutoDone'] = False
        self._model = None
        self._view = None

    def execute(self):
        """
        Kick off the execution of the step, in this case an interactive dialog.
        User invokes the _doneExecution() method when finished, via pushbutton.
        """
        self._model = MeshGeneratorModel(self._location,
                                         self._config['identifier'])
        self._view = MeshGeneratorWidget(self._model)
        self._view.registerDoneExecution(self._myDoneExecution)
        self._setCurrentWidget(self._view)

    def _myDoneExecution(self):
        self._portData0 = self._model.getOutputModelFilename()
        self._view = None
        self._model = None
        self._doneExecution()

    def getPortData(self, index):
        """
        Add your code here that will return the appropriate objects for this step.
        The index is the index of the port in the port list.  If there is only one
        provides port for this step then the index can be ignored.

        :param index: Index of the port to return.
        """
        return self._portData0  # http://physiomeproject.org/workflow/1.0/rdf-schema#file_location

    def configure(self):
        """
        This function will be called when the configure icon on the step is
        clicked.  It is appropriate to display a configuration dialog at this
        time.  If the conditions for the configuration of this step are complete
        then set:
            self._configured = True
        """
        dlg = ConfigureDialog()
        dlg.identifierOccursCount = self._identifierOccursCount
        dlg.setConfig(self._config)
        dlg.validate()
        dlg.setModal(True)

        if dlg.exec_():
            self._config = dlg.getConfig()

        self._configured = dlg.validate()
        self._configuredObserver()

    def getIdentifier(self):
        """
        The identifier is a string that must be unique within a workflow.
        """
        return self._config['identifier']

    def setIdentifier(self, identifier):
        """
        The framework will set the identifier for this step when it is loaded.
        """
        self._config['identifier'] = identifier

    def serialize(self):
        """
        Add code to serialize this step to string.  This method should
        implement the opposite of 'deserialize'.
        """
        return json.dumps(self._config,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def deserialize(self, string):
        """
        Add code to deserialize this step from string.  This method should
        implement the opposite of 'serialize'.

        :param string: JSON representation of the configuration in a string.
        """
        self._config.update(json.loads(string))

        d = ConfigureDialog()
        d.identifierOccursCount = self._identifierOccursCount
        d.setConfig(self._config)
        self._configured = d.validate()