| 800 | } |
| 801 | |
| 802 | void mitk::nnInteractiveTool::StartSession() |
| 803 | { |
| 804 | if (this->IsSessionRunning()) |
| 805 | this->EndSession(); |
| 806 | |
| 807 | auto prefs = GetPreferences(); |
| 808 | |
| 809 | // Materialize the session-defining preferences with their effective values so a |
| 810 | // later no-op preferences "OK" does not register as a value change and tear the |
| 811 | // session down (mitk::Preferences fires its change event only on an actual |
| 812 | // change, and a default written into a never-stored key counts as one). Safe |
| 813 | // here: no session is running yet, so the GUI's stale-session observer is a |
| 814 | // no-op for these writes. |
| 815 | for (const auto& [key, defaultValue] : GetSessionDefiningPreferences()) |
| 816 | prefs->Put(key, prefs->Get(key, defaultValue)); |
| 817 | |
| 818 | m_Impl->Remote = prefs->Get("nnInteractive/inferenceMode", "local") == "remote"; |
| 819 | |
| 820 | // The context is created lightweight (see CreatePythonContext) so the |
| 821 | // pre-session checks map no venv native libraries. A session does need the |
| 822 | // bindings (NumPy for mask exchange, the MITK module), so import them now. |
| 823 | // Activate() is idempotent, so re-running it here only adds the bindings. |
| 824 | if (auto* pythonContext = m_Impl->GetPythonContext()) |
| 825 | pythonContext->Activate(); |
| 826 | |
| 827 | try |
| 828 | { |
| 829 | if (m_Impl->Remote) |
| 830 | this->ConstructRemoteSession(); |
| 831 | else |
| 832 | this->ConstructLocalSession(); |
| 833 | } |
| 834 | catch (...) |
| 835 | { |
| 836 | // If construction failed before a session was established, do not leave the |
| 837 | // remote flag set: IsRemoteSession() must report false when nothing runs, |
| 838 | // and EndSession() (the usual reset path) is a no-op without a session. |
| 839 | if (!this->IsSessionRunning()) |
| 840 | m_Impl->Remote = false; |
| 841 | |
| 842 | throw; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | void mitk::nnInteractiveTool::ConstructRemoteSession() |
| 847 | { |
no test coverage detected