コード例 #1
0
    def interrupt_calculation(self):
        """
        Kills any running calculation.
        """
        if self.calculation_thread is None:
            Debug(
                self,
                ".interrupt_calculation: WARNING: No calculation thread to interrupt",
                color=Theme.WarningColor,
                force=True)
            return

        if self.calculation_thread.isRunning():
            Debug(self,
                  ".interrupt_calculation(): Requesting interruption",
                  color=Theme.PrimaryColor)
            self.calculation_thread.requestInterruption()

            if self.calculation_thread.wait(5000):
                Debug(self,
                      ".interrupt_calculation(): Exited gracefully",
                      color=Theme.PrimaryColor)
            else:
                Assert_Dialog(False, "Failed to terminate calculation thread")
                if self.calculation_thread is not None:
                    if self.calculation_thread.isRunning():
                        Debug(
                            self,
                            ".interrupt_calculation(): WARNING: Terminating ungracefully",
                            color=Theme.WarningColor,
                            force=True)
                        self.calculation_thread.terminate()
                        self.calculation_thread.wait()
        else:
            Debug(
                self,
                ".interrupt_calculation: WARNING: Calculation thread should be running",
                color=Theme.WarningColor,
                force=True)

        self.calculation_thread = None
コード例 #2
0
    def __init__(self, resolution: int):
        """
        Initializes an empty sampling volume, with zero bounds and no constraints.

        @param resolution: Resolution
        """
        Debug(self, ": Init")

        self._resolution = resolution

        self.constraints = []

        self._bounds_min = np.zeros(3)
        self._bounds_max = np.zeros(3)

        self._points = None
        self._permeabilities = None
        self._labeled_indices = None
        self._neighbor_indices = None

        Assert_Dialog(resolution > 0, "Resolution must be > 0")
コード例 #3
0
    def recalculate(self, sampling_volume, field, progress_callback) -> bool:
        """
        Recalculates color and alpha values for field.

        @param sampling_volume: SamplingVolume
        @param field: Field
        @param progress_callback: Progress callback
        @return: True (currently non-interruptable)
        """
        Debug(self, ".recalculate()", color=Theme.SuccessColor)

        n = len(field.get_vectors())

        progress_callback(0)

        # Sampling volume length element (only needed for divergence metric)
        dL = Metric.LengthScale / sampling_volume.get_resolution()

        # Calculate color metric values
        if self._color_preset["norm_id"] == "Divergence":
            # Calculate divergence
            color_norm_values = self._divergence_worker(
                sampling_volume.get_neighbor_indices(), field.get_vectors(),
                dL, self._color_preset["polarity"])
        else:
            # Calculate other norm
            color_norm_values = self._norm_worker(
                self._color_preset["norm_id"], field.get_vectors())

        progress_callback(25)

        # Calculate alpha metric values
        if self._alpha_preset["norm_id"] == "Divergence":
            # Calculate divergence
            alpha_norm_values = self._divergence_worker(
                sampling_volume.get_neighbor_indices(), field.get_vectors(),
                dL, self._alpha_preset["polarity"])
        else:
            # Calculate other norm
            alpha_norm_values = self._norm_worker(
                self._alpha_preset["norm_id"], field.get_vectors())

        progress_callback(50)

        # Select color range
        if self._color_preset["is_angle"]:
            color_norm_min, color_norm_max = 0.0, 1.0
        else:
            color_norm_min, color_norm_max = np.nanmin(
                color_norm_values), np.nanmax(color_norm_values)

        # Select alpha range
        if self._alpha_preset["is_angle"]:
            alpha_norm_min, alpha_norm_max = 0.0, 1.0
        else:
            alpha_norm_min, alpha_norm_max = np.nanmin(
                alpha_norm_values), np.nanmax(alpha_norm_values)

        # Select color normalizer
        if self._color_preset["is_log"]:
            # Logarithmic normalization

            Assert_Dialog(not self._color_preset["is_angle"],
                          "Logarithmic angles don't make any sense")

            # Adjust range for logarithm (out-of-range values will be clipped in the loop below)
            color_norm_min_ = max(
                color_norm_min, Metric.LogNormMinimum
            )  # avoiding ValueError: min must be positive
            color_norm_max_ = max(
                color_norm_min_,
                color_norm_max)  # avoiding ValueError: min must be <= max
        else:
            # Linear normalization
            color_norm_min_ = color_norm_min
            color_norm_max_ = color_norm_max

        # Select alpha normalizer
        if self._alpha_preset["is_log"]:
            # Logarithmic normalization

            Assert_Dialog(not self._alpha_preset["is_angle"],
                          "Logarithmic angles don't make any sense")

            # Adjust range for logarithm (out-of-range values will be clipped in the loop below)
            alpha_norm_min_ = max(
                alpha_norm_min, Metric.LogNormMinimum
            )  # avoiding ValueError: min must be positive
            alpha_norm_max_ = max(
                alpha_norm_min_,
                alpha_norm_max)  # avoiding ValueError: min must be <= max
        else:
            # Linear normalization
            alpha_norm_min_ = alpha_norm_min
            alpha_norm_max_ = alpha_norm_max

        colors = np.zeros([n, 4])

        # Calculate final color values
        # Note: If using logarithmic scaling, out-of-range values (still exceeding [0...1] here) may be clipped
        colors = self._normalize_worker(
            self._color_preset["colormap"], self._color_preset["is_log"],
            color_norm_values, color_norm_min_, color_norm_max_,
            self._alpha_preset["is_log"], alpha_norm_values, alpha_norm_min_,
            alpha_norm_max_, colors)

        self._colors = colors

        self._limits = {
            "color_min": color_norm_min,
            "color_max": color_norm_max,
            "alpha_min": alpha_norm_min,
            "alpha_max": alpha_norm_max
        }

        progress_callback(100)

        return True