Code Monkey home page Code Monkey logo

Comments (10)

mauigna06 avatar mauigna06 commented on August 11, 2024 1

Input of Dynamic modeler node should not be lost. If you find that in the scene file that the dynamic modeler nodes refer to the correct nodes but when you load the scene those references do not exist then upload that scene somewhere, post the link here, and I'll have a look.

I've checked and it look as I was wrong. DynamicModeler references do save well.

from slicerbonereconstructionplanner.

lassoan avatar lassoan commented on August 11, 2024 1

Yes, we still do this.

If you delete many nodes in bulk then you can enable batch processing on the scene, which indicates that node updates can be ignored until batch processing ends.

Another option, which I think is better, because it makes the module more responsive in general is to not perform updates immediately when an input changes but start/reset a QTimer. If the inputs don't change for a while (e.g., 1 second) then the timer elapses and it calls your update function.

from slicerbonereconstructionplanner.

lassoan avatar lassoan commented on August 11, 2024

Node references should be preserved when a scene is saved and reloaded but indeed in scripted modules you need to re-attach the node observations when the scene load is completed (you can add an observer to the scene's EndImport event and add the observers in the callback function).

Input of Dynamic modeler node should not be lost. If you find that in the scene file that the dynamic modeler nodes refer to the correct nodes but when you load the scene those references do not exist then upload that scene somewhere, post the link here, and I'll have a look.

from slicerbonereconstructionplanner.

mauigna06 avatar mauigna06 commented on August 11, 2024

Node references should be preserved when a scene is saved and reloaded but indeed in scripted modules you need to re-attach the node observations when the scene load is completed (you can add an observer to the scene's EndImport event and add the observers in the callback function).

I can't get it to work.

class BoneReconstructionPlannerWidget(ScriptedLoadableModuleWidget, VTKObservationMixin):
  ...
  def setup(self):
    ...
    self.addObserver(slicer.mrmlScene, slicer.mrmlScene.EndImportEvent, self.onSceneEndImport)
    ...

def onSceneEndImport(self):
    print('check if this line executes')# It doesn't

    slicer.util.selectModule('BoneReconstructionPlanner')

    shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
    mandibularPlanesFolder = shNode.GetItemByName("Mandibular planes")
    mandibularPlanesList = createListFromFolderID(mandibularPlanesFolder)

    for i in range(len(mandibularPlanesList)):
      planeNodeObserver = mandibularPlanesList[i].AddObserver(slicer.vtkMRMLMarkupsNode.PointModifiedEvent,self.logic.onPlaneModified)

from slicerbonereconstructionplanner.

lassoan avatar lassoan commented on August 11, 2024

What do you mean by cannot get it to work? What do you expect to happen? What happens instead?

Do not call slicer.util.selectModule! You must not change which module is active just because a scene is loaded. It should be fine if planes are not moving if the module is not active. It is enough if you ensure observers are up-to-date when you enter the module; or scene is imported while the module is already active.

Always remove old observers before adding new ones. Save the observation IDs as member variables so that later you can remove them; or use VTKObservationMixin's self.addObserver (which stores the observation IDs internally).

from slicerbonereconstructionplanner.

mauigna06 avatar mauigna06 commented on August 11, 2024

It is enough if you ensure observers are up-to-date when you enter the module; or scene is imported while the module is already active.

That was the problem: I was trying to import the scene without having opened the BoneReconstructionPlanner module once.

How do I ensure the observers are up-to-date when you enter the module?

Always remove old observers before adding new ones.

Why this is needed? Lots of unused observers make the interactions slower?

Save the observation IDs as member variables so that later you can remove them

When should I remove the observers I saved?

from slicerbonereconstructionplanner.

lassoan avatar lassoan commented on August 11, 2024

How do I ensure the observers are up-to-date when you enter the module?

You can add observers in enter() method.

Why this is needed? Lots of unused observers make the interactions slower?

Adding observers does not remove old observers. If you have multiple observers then callback functions will be called multiple times, slowing down the updates enormously. Also, observers prevent objects from being deleted, so you would have memory leaks.

When should I remove the observers I saved?

You always need to clean up after yourself for the reasons above (performance degradation and memory leaks). Removing is also necessary because you cannot ask the user to please switch to the module to make it work. We can either state that updates happen only while the module is active; or updates happen all the time (regardless of the module has been opened before or not).

It is a question do decide when to add/remove observers.

  • Option A: Add observer whenever a suitable parameter node is added to the scene; remove observer when that parameter node is removed from the scene. It is nice that you don't have to activate the module for the cutting planes to be in sync, but adding you need to implement all the observations and updates in the module logic, add observers to all suitable parameter nodes (or use a singleton parameter node, or have a reference to the "active" parameter node in the selection singleton node), and instantiate the module logic in the module class (in the startupCompleted signal callback).
  • Option B: Add observers when you enter the module, remove when you exit the module. Everything is simpler, but if the module is not active then synchronization of planes does not happen. I think this is not a huge issue. We can also lock planes when we exit the module to reduce the chance that users accidentally move them.

from slicerbonereconstructionplanner.

mauigna06 avatar mauigna06 commented on August 11, 2024

Okey. So we'll use Option B.

What do we do when the user deletes the "Mandibular planes" folder or some plane inside of it?
Do I need to use the slicer.mrmlScene.NodeAboutToBeRemovedEvent and the subjectHierarchyNode to check if the node belongs to the "Mandibular planes" folder and in that case remove the observer related to the planeNode?

from slicerbonereconstructionplanner.

lassoan avatar lassoan commented on August 11, 2024

Yes. You need to observe the scene for node removal anyway to update the display, remove corresponding fibula cutting plane, etc. In the callback where you process the node removal, you will remove the event observation as well.

from slicerbonereconstructionplanner.

mauigna06 avatar mauigna06 commented on August 11, 2024

Each time a mandiblePlane is deleted generateFibulaPlanes will be called to update the fibula planes quantity, position and orientation. But if you delete all the planes inside the "Mandibular planes" folder at the same time (or if you delete the whole folder maybe) this process will be called many times, which maybe kinda slow. Do we still do this?

from slicerbonereconstructionplanner.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.