• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nens / ThreeDiToolbox / #2591

23 Sep 2025 06:30PM UTC coverage: 34.864% (-0.3%) from 35.146%
#2591

push

coveralls-python

web-flow
Merge 12e11f2e2 into f6f4be1e7

79 of 375 new or added lines in 42 files covered. (21.07%)

9 existing lines in 6 files now uncovered.

4875 of 13983 relevant lines covered (34.86%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.09
/tool_fraction_analysis/fraction_plot.py
1
import re
1✔
2
from qgis.PyQt.QtGui import QColor
1✔
3
import pyqtgraph as pg
1✔
4
from qgis.PyQt.QtCore import pyqtSignal
1✔
5

6
from threedi_results_analysis.utils.geo_utils import distance_to_polyline
1✔
7
from threedi_results_analysis.utils.color import reduce_saturation, increase_value
1✔
8

9
pg.setConfigOption("background", "w")
1✔
10
pg.setConfigOption("foreground", "k")
1✔
11
from qgis.PyQt.QtCore import Qt
1✔
12
from threedi_results_analysis.threedi_plugin_model import ThreeDiPluginModel
1✔
13
from threedi_results_analysis.tool_fraction_analysis.fraction_model import FractionModel
1✔
14

15

16
class FractionPlot(pg.PlotWidget):
1✔
17
    hover_plot = pyqtSignal(str)
1✔
18

19
    def __init__(self, parent, result_model: ThreeDiPluginModel, fraction_model: FractionModel):
1✔
20
        super().__init__(parent)
×
21
        self.showGrid(True, True, 0.5)
×
NEW
22
        self.proxy = pg.SignalProxy(self.scene().sigMouseMoved, rateLimit=10, slot=self.mouse_moved)
×
23
        self.fraction_model = fraction_model
×
24
        self.result_model = result_model
×
25
        # map from substance name to (list of) plot
26
        self.item_map = {}
×
27
        self.setLabel("bottom", "Time", "hrs")
×
28
        self.setLabel("left", "Concentration", "")
×
29
        self.getAxis("left").enableAutoSIPrefix(False)
×
NEW
30
        self.mouseLabel = pg.TextItem(text="", anchor=(1, 1), color=(0, 0, 0))
×
31

NEW
32
        self.mouseMarker = pg.ScatterPlotItem(
×
33
            [0], [0],
34
            symbol='o',
35
            size=12,
36
            brush='r',
37
            pen='k'
38
        )
NEW
39
        self.mouseMarker.setVisible(False)
×
NEW
40
        self.addItem(self.mouseMarker)
×
NEW
41
        self.addItem(self.mouseLabel)
×
42

43
    def clear_plot(self):
1✔
44
        self.clear()
×
45
        self.item_map.clear()
×
46
        self.setLabel("left", "Concentration", "")
×
NEW
47
        self.addItem(self.mouseMarker)
×
NEW
48
        self.addItem(self.mouseLabel)
×
NEW
49
        self.mouseLabel.setText("")
×
NEW
50
        self.mouseMarker.setVisible(False)
×
51

52
    def item_checked(self, model_item):
1✔
NEW
53
        if not self.item_map:  # No plots yet
×
NEW
54
            return
×
55
        substance = model_item.data()
×
56
        for plot in self.item_map[substance]:
×
NEW
57
            plot.setVisible(model_item.checkState() == Qt.CheckState.Checked)
×
58

59
    def mouse_moved(self, pos):
1✔
60
        # Translate scene position to plot coordinates
61
        # As we are using a ProxySignal, we get a list of events
NEW
62
        mouse_point = self.plotItem.vb.mapSceneToView(pos[0])
×
NEW
63
        x = mouse_point.x()
×
NEW
64
        y = mouse_point.y()
×
65

66
        # find closest plot (if any)
NEW
67
        min_dist = float("inf")
×
NEW
68
        closest_substance = None
×
NEW
69
        closest_data_point = None
×
NEW
70
        for item in self.plotItem.listDataItems():
×
NEW
71
            x_data, y_data = item.getData()
×
NEW
72
            dist, data_point = distance_to_polyline(x, y, x_data, y_data)
×
NEW
73
            if dist < 1:
×
NEW
74
                if dist < min_dist:
×
NEW
75
                    min_dist = dist
×
76
                    # Check which substance this plot corresponds to
NEW
77
                    for substance, plots in self.item_map.items():
×
NEW
78
                        for plot in plots:
×
NEW
79
                            if plot is item:
×
NEW
80
                                closest_substance = substance
×
NEW
81
                                closest_data_point = data_point
×
NEW
82
                                break
×
NEW
83
                assert closest_substance  # We should always find a plot
×
84

NEW
85
        if closest_substance is not None:
×
NEW
86
            self.mouseLabel.setText("(%0.1f, %0.1f)" % (closest_data_point[0], closest_data_point[1]))
×
NEW
87
            self.mouseLabel.setPos(x, y)
×
NEW
88
            self.mouseMarker.setVisible(True)
×
NEW
89
            self.mouseMarker.setData([closest_data_point[0]], [closest_data_point[1]])
×
90
        else:
NEW
91
            self.mouseLabel.setText("")
×
NEW
92
            self.mouseMarker.setVisible(False)
×
93

NEW
94
        self.hover_plot.emit(closest_substance)
×
95

96
    def item_color_changed(self, color_model_item):
1✔
NEW
97
        if not self.item_map:  # No plots yet
×
NEW
98
            return
×
99

100
        # Retrieve the substance name from the model
NEW
101
        row = color_model_item.index().row()
×
NEW
102
        selected_model_item = color_model_item.model().item(row, 0)
×
NEW
103
        substance = selected_model_item.data()
×
104

NEW
105
        style, color, width = color_model_item.data()[0]
×
NEW
106
        pen = pg.mkPen(color=QColor(*color), width=width, style=style)
×
NEW
107
        self.item_map[substance][0].setPen(pen)
×
108

NEW
109
        if len(self.item_map[substance]) == 2:
×
110
            # there is a fill, also change that color
NEW
111
            fill_color = reduce_saturation(QColor(*color))
×
NEW
112
            self.item_map[substance][1].setBrush(pg.mkBrush(fill_color))
×
113

114
    def highlight_plot(self, row):
1✔
115

NEW
116
        if not self.item_map:  # No plots yet
×
NEW
117
            return
×
118

NEW
119
        self.unhighlight_plots()
×
120

NEW
121
        hovered_model_item = self.fraction_model.item(row, 0)
×
NEW
122
        substance = hovered_model_item.data()
×
123

NEW
124
        hovered_color_item = self.fraction_model.item(row, 1)
×
125
        # Original color
NEW
126
        style, color, width = hovered_color_item.data()[1]
×
NEW
127
        highlight_color = increase_value(QColor(*color))
×
NEW
128
        pen = pg.mkPen(color=highlight_color, width=width, style=style)
×
NEW
129
        self.item_map[substance][0].setPen(pen)
×
130
        # also set fill color
NEW
131
        if len(self.item_map[substance]) == 2:
×
NEW
132
            self.item_map[substance][1].setBrush(pg.mkBrush(highlight_color))
×
133

134
    def unhighlight_plots(self):
1✔
NEW
135
        if not self.item_map:  # No plots yet
×
NEW
136
            return
×
137

NEW
138
        for row in range(self.fraction_model.rowCount()):
×
NEW
139
            substance = self.fraction_model.item(row, 0).data()
×
140
            # original color
NEW
141
            style, color, width = self.fraction_model.item(row, 1).data()[1]
×
142

NEW
143
            pen = pg.mkPen(color=QColor(*color), width=width, style=style)
×
NEW
144
            self.item_map[substance][0].setPen(pen)
×
NEW
145
            if len(self.item_map[substance]) == 2:
×
NEW
146
                fill_color = reduce_saturation(QColor(*color))
×
NEW
147
                self.item_map[substance][1].setBrush(pg.mkBrush(fill_color))
×
148

149
    def fraction_selected(self, feature_id, substance_unit: str, time_unit: str, stacked: bool, volume: bool):
1✔
150
        """
151
        Retrieve info from model and create plots
152
        """
153
        self.clear_plot()
×
154

155
        substance_unit_conversion = 1.0
×
156

157
        if volume:
×
158
            # for known units, we apply basic conversion
159
            pattern = r"^(.*)/\s*(m3|l)\s*$"
×
160
            matches = re.findall(pattern, substance_unit, flags=re.IGNORECASE)
×
161
            if len(matches) == 1 and len(matches[0]) == 2:
×
162
                if matches[0][1].lower() == "l":
×
163
                    substance_unit_conversion = 1000.0
×
164
                    processed_substance_unit = matches[0][0].strip()
×
165
                    volume_label = "Load"
×
166
                elif matches[0][1].lower() == "m3":
×
167
                    substance_unit_conversion = 1.0
×
168
                    processed_substance_unit = matches[0][0].strip()
×
169
                    volume_label = "Load"
×
170
            elif substance_unit.strip() == "%":
×
171
                substance_unit_conversion = 1.0
×
172
                processed_substance_unit = "m<sup>3</sup>"
×
173
                volume_label = "Volume"
×
174
            else:  # unknown, take original unit as-is and append x m3
175
                substance_unit_conversion = 1.0
×
176
                processed_substance_unit = f"{substance_unit} ยท m<sup>3</sup>"
×
177
                volume_label = "Load"
×
178

179
        plots = self.fraction_model.create_plots(feature_id, time_unit, stacked, volume, substance_unit_conversion)
×
180
        prev_plot = None
×
NEW
181
        for substance, plot, visible in plots:
×
182
            self.item_map[substance] = [plot]
×
183
            plot.setZValue(100)
×
NEW
184
            plot.setVisible(visible)
×
UNCOV
185
            self.addItem(plot)
×
186

187
            if stacked:
×
188
                # Add fill between consecutive plots
189
                plot_color = plot.opts['pen'].color()
×
190
                # Reduce saturation for fill
NEW
191
                fill_color = reduce_saturation(plot_color)
×
192
                if not prev_plot:
×
193
                    # this is the first, just fill downward to axis
194
                    plot.setFillLevel(0)
×
195
                    plot.setFillBrush(pg.mkBrush(fill_color))
×
196
                else:
197
                    fill = pg.FillBetweenItem(plot, prev_plot, pg.mkBrush(fill_color))
×
198
                    fill.setZValue(20)
×
NEW
199
                    fill.setVisible(visible)
×
200
                    self.addItem(fill)
×
201
                    self.item_map[substance].append(fill)
×
202

203
            prev_plot = plot
×
204

205
        self.setLabel("left", volume_label if volume else "Concentration", processed_substance_unit if volume else substance_unit)
×
206
        self.plotItem.vb.menu.viewAll.triggered.emit()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc