fairyside 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ Metadata-Version: 2.4
2
+ Name: fairyside
3
+ Version: 0.1.0
4
+ Summary: FairySide - FEE-rical interface
5
+ Author: Coda Cave
6
+ Project-URL: Repository, https://gitlab.com/CodaCave/fairyside
7
+ Project-URL: Changelog, https://gitlab.com/CodaCave/fairyside/-/releases
8
+ Requires-Python: >=3.10
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: fairyex>=0.4.0
11
+ Requires-Dist: plotly
12
+ Requires-Dist: polars
13
+ Requires-Dist: PySide6
14
+
15
+ # FairySide
16
+
17
+ Join the fairy(ex) (py)side of energy modeling and enlight (dark) solution!
18
+
19
+ ## Cute feature
20
+
21
+ - Explore solution
22
+ - Export data
23
+
24
+ ## Quickstart
25
+
26
+ ```bash
27
+ pip install fairyside
28
+ python -m fairyside
29
+ ```
30
+
@@ -0,0 +1,16 @@
1
+ # FairySide
2
+
3
+ Join the fairy(ex) (py)side of energy modeling and enlight (dark) solution!
4
+
5
+ ## Cute feature
6
+
7
+ - Explore solution
8
+ - Export data
9
+
10
+ ## Quickstart
11
+
12
+ ```bash
13
+ pip install fairyside
14
+ python -m fairyside
15
+ ```
16
+
@@ -0,0 +1,2 @@
1
+ from ._version import __version__ as __version__
2
+ from .cutely import CuteApp as CuteApp
@@ -0,0 +1,16 @@
1
+ import sys
2
+
3
+ import fairyex as fx
4
+ from PySide6 import QtAsyncio, QtWidgets
5
+
6
+ from .cutely import CuteApp
7
+
8
+ app = QtWidgets.QApplication([])
9
+
10
+ fairy_app = CuteApp()
11
+ for filename in sys.argv[1:]:
12
+ ds = fx.DarkSol(filename)
13
+ fairy_app.add_tab(ds)
14
+ fairy_app.show()
15
+
16
+ QtAsyncio.run(handle_sigint=True)
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0"
@@ -0,0 +1,362 @@
1
+ import asyncio
2
+ import os
3
+ import tempfile
4
+
5
+ import fairyex as fx
6
+ import plotly.express as px
7
+ import polars as pl
8
+ from PySide6 import QtCore, QtWebEngineWidgets, QtWidgets
9
+
10
+ from .model import CutePolars
11
+ from .utils import cuteError, cuteTree
12
+
13
+
14
+ class CuteApp(QtWidgets.QMainWindow):
15
+ def __init__(self):
16
+ super().__init__()
17
+
18
+ self._dst = {}
19
+
20
+ self.setWindowTitle("FairySide")
21
+
22
+ self.menu = self._init_menubar()
23
+ self.tabs = self._init_widgets()
24
+ self.status = self.statusBar()
25
+
26
+ self.setCentralWidget(self.tabs)
27
+
28
+ def _init_menubar(self):
29
+ menu = self.menuBar()
30
+
31
+ file_menu = menu.addMenu("🗃️ File")
32
+ file_menu.addAction(
33
+ "+ Open",
34
+ lambda : asyncio.ensure_future(self.open_file()),
35
+ )
36
+
37
+ # help_menu = menu.addMenu("🦋 Help")
38
+ # help_menu.addAction("? About")
39
+
40
+ return menu
41
+
42
+ def _init_widgets(self):
43
+ tabs = QtWidgets.QTabWidget()
44
+ tabs.setTabsClosable(True)
45
+ tabs.tabCloseRequested.connect(self.close_tab)
46
+ return tabs
47
+
48
+ async def open_file(self):
49
+ filename = QtWidgets.QFileDialog.getOpenFileName(
50
+ self, "Open", ".", "Solution (*.zip)"
51
+ )
52
+ self.status.showMessage(f"Loading {filename[0]}...")
53
+ await asyncio.sleep(0.1) # Let time for QFileDialog to close
54
+ async def task():
55
+ ds = fx.DarkSol(filename[0])
56
+ self.add_tab(ds)
57
+ await asyncio.create_task(task())
58
+ self.status.showMessage('')
59
+
60
+ def add_tab(self, ds: fx.DarkSol):
61
+ dst = CuteSol()
62
+ dst.load_darksol(ds)
63
+ self._dst[self.tabs.count()] = dst
64
+ self.tabs.addTab(dst(), ds.query_model())
65
+
66
+ def close_tab(self, index: int):
67
+ self.tabs.removeTab(index)
68
+ self._dst[index].closeEvent()
69
+ # Reindexing
70
+ self._dst = {
71
+ (k if k < index else k - 1): dst
72
+ for k, dst in self._dst.items() if k != index
73
+ }
74
+
75
+ def move_tab(self, index_from: int, index_to: int):
76
+ raise NotImplementedError()
77
+
78
+ def closeEvent(self, *args):
79
+ """Overload"""
80
+ for dst in self._dst.values():
81
+ dst.closeEvent()
82
+
83
+
84
+ class CuteSol(QtWidgets.QWidget):
85
+ def __init__(self, parent=None):
86
+ super().__init__(parent)
87
+
88
+ ctrl = self._init_control_widgets()
89
+ view = self._init_view_widgets()
90
+
91
+ self._main = QtWidgets.QSplitter()
92
+ self._main.addWidget(ctrl)
93
+ self._main.addWidget(view)
94
+
95
+ def __call__(self):
96
+ return self._main
97
+
98
+ def _init_control_widgets(self):
99
+ others = self._init_other_widgets()
100
+ objects = self._init_objects_widgets()
101
+
102
+ control = QtWidgets.QSplitter(QtCore.Qt.Orientation.Horizontal)
103
+ control.addWidget(others)
104
+ control.addWidget(objects)
105
+
106
+ return control
107
+
108
+ def _init_buttons_widgets(self):
109
+ self._play_button = QtWidgets.QPushButton("▶ Show")
110
+ self._play_button.setStyleSheet("color: green;")
111
+
112
+ self._save_button = QtWidgets.QPushButton("⏺ Save")
113
+ self._save_button.setStyleSheet("color: darkred;")
114
+
115
+ buttons_layout = QtWidgets.QHBoxLayout()
116
+ buttons_layout.addWidget(self._play_button)
117
+ buttons_layout.addWidget(self._save_button)
118
+
119
+ buttons = QtWidgets.QGroupBox()
120
+ buttons.setLayout(buttons_layout)
121
+ return buttons
122
+
123
+ def _init_other_widgets(self):
124
+ layout = QtWidgets.QVBoxLayout()
125
+
126
+ # Buttons
127
+ buttons = self._init_buttons_widgets()
128
+ layout.addWidget(buttons)
129
+
130
+ # Phase
131
+ phase_header = QtWidgets.QLabel("Phase")
132
+ layout.addWidget(phase_header)
133
+ self._phase = QtWidgets.QComboBox()
134
+ layout.addWidget(self._phase)
135
+
136
+ # Collection
137
+ collection_header = QtWidgets.QLabel("Collection")
138
+ layout.addWidget(collection_header)
139
+ self._collection = QtWidgets.QComboBox()
140
+ layout.addWidget(self._collection)
141
+
142
+ # Properties
143
+ properties_header = QtWidgets.QLabel("Properties")
144
+ layout.addWidget(properties_header)
145
+ self._properties = QtWidgets.QListWidget()
146
+ self._properties.setSelectionMode(
147
+ QtWidgets.QAbstractItemView.SelectionMode.MultiSelection
148
+ )
149
+ layout.addWidget(self._properties)
150
+
151
+ # Samples
152
+ samples_header = QtWidgets.QLabel("Samples")
153
+ layout.addWidget(samples_header)
154
+ self._samples = QtWidgets.QListWidget()
155
+ self._samples.setSelectionMode(
156
+ QtWidgets.QAbstractItemView.SelectionMode.MultiSelection
157
+ )
158
+ layout.addWidget(self._samples)
159
+
160
+ others = QtWidgets.QGroupBox()
161
+ others.setLayout(layout)
162
+ return others
163
+
164
+ def _init_objects_widgets(self):
165
+ self._parent = QtWidgets.QTreeWidget()
166
+ self._parent.setHeaderLabel("Parent")
167
+
168
+ self._children = QtWidgets.QTreeWidget()
169
+ self._children.setHeaderLabel("Children")
170
+ self._children.setSelectionMode(
171
+ QtWidgets.QAbstractItemView.SelectionMode.MultiSelection
172
+ )
173
+
174
+ objects_widget = QtWidgets.QSplitter(QtCore.Qt.Orientation.Vertical)
175
+ objects_widget.addWidget(self._parent)
176
+ objects_widget.addWidget(self._children)
177
+
178
+ objects_widget.setStretchFactor(0, 0)
179
+ objects_widget.setStretchFactor(1, 1)
180
+ return objects_widget
181
+
182
+ def _init_view_widgets(self):
183
+ self.table = QtWidgets.QTableView()
184
+ self.plot = QtWebEngineWidgets.QWebEngineView()
185
+ self.plot.load("")
186
+
187
+ results = QtWidgets.QSplitter(QtCore.Qt.Orientation.Vertical)
188
+ results.addWidget(self.table)
189
+ results.addWidget(self.plot)
190
+
191
+ return results
192
+
193
+ def check_parent(self):
194
+ parent = self._parent.selectedItems()[0]
195
+
196
+ # Parent class
197
+ if parent.parent() is None:
198
+ parent_class = parent.text(0)
199
+ elif parent.parent().parent() is not None:
200
+ parent_class = parent.parent().parent().text(0)
201
+ else:
202
+ parent_class = parent.parent().text(0)
203
+
204
+ if parent.child(0):
205
+ return parent_class[2:], None
206
+ return parent_class[2:], parent.text(0)[2:]
207
+
208
+ def check_collection(self):
209
+ return self._collection.currentText()
210
+
211
+ def check_children(self):
212
+ children = self._children.selectedItems()
213
+ # No category
214
+ return [child.text(0)[2:] for child in children if not child.child(0)]
215
+
216
+ def load_darksol(self, ds: fx.DarkSol):
217
+ self._ds = ds
218
+ self._sample_names = {
219
+ sample_name: sample
220
+ for sample, sample_name in ds.query_sample_name().items()
221
+ }
222
+ self._collection_class = {
223
+ (parent_class, collection): child_class
224
+ for parent_class in ds.query_class()
225
+ for child_class in ds.query_class()
226
+ for collection in ds.query_collection(child_class, parent_class)
227
+ }
228
+ self._connect()
229
+ self._load_phase(ds)
230
+ self._load_samples(ds)
231
+ self._load_parent(ds)
232
+
233
+ def _connect(self):
234
+ """Add action and connect"""
235
+ self._play_button.clicked.connect(self.expose)
236
+ self._save_button.clicked.connect(self.export)
237
+
238
+ self._parent.itemSelectionChanged.connect(self._load_collection)
239
+
240
+ self._collection.currentTextChanged.connect(self._load_properties)
241
+ self._collection.currentTextChanged.connect(self._load_children)
242
+
243
+ def _load_phase(self, ds: fx.DarkSol):
244
+ for phase in ds.query_phase():
245
+ self._phase.addItem(phase)
246
+
247
+ def _load_samples(self, ds: fx.DarkSol):
248
+ for sample_name in self._sample_names:
249
+ self._samples.addItem(sample_name)
250
+
251
+ def _load_parent(self, ds: fx.DarkSol):
252
+ system_leaf = None
253
+ for class_, class_branch in cuteTree(ds.query_class(), prefix="📁 "):
254
+ # Categorized objects
255
+ for category, category_branch in cuteTree(ds.query_category(class_), prefix="📂 "):
256
+ class_branch.addChild(category_branch)
257
+ objects = ds.query_children(class_, category)
258
+ for _, object_leaf in cuteTree(objects, prefix="⚙️"):
259
+ category_branch.addChild(object_leaf)
260
+ self._parent.addTopLevelItem(object_leaf)
261
+ # Category-less objects
262
+ for object_name, object_leaf in cuteTree(ds.query_children(class_, category='-'), prefix="⚙️"):
263
+ class_branch.addChild(object_leaf)
264
+ # Flag "System"
265
+ if class_ == "System" and object_name == "System":
266
+ system_leaf = object_leaf
267
+ self._parent.addTopLevelItem(class_branch)
268
+ # Set "System" as default
269
+ if system_leaf is not None:
270
+ self._parent.setCurrentItem(system_leaf)
271
+
272
+ @QtCore.Slot()
273
+ def _load_children(self):
274
+ ds = self._ds
275
+ parent_class, _ = self.check_parent()
276
+ collection = self.check_collection()
277
+ children_class = self._collection_class[parent_class, collection]
278
+ self._children.clear()
279
+ # Categorized objects
280
+ for category, category_branch in cuteTree(ds.query_category(children_class), prefix="📂 "):
281
+ for child_name, child_leaf in cuteTree(ds.query_children(children_class, category), prefix="⚙️"):
282
+ category_branch.addChild(child_leaf)
283
+ self._children.addTopLevelItem(category_branch)
284
+ # Category-less objects
285
+ for object_name, object_leaf in cuteTree(ds.query_children(children_class, category='-'), prefix="⚙️"):
286
+ self._children.addTopLevelItem(child_leaf)
287
+
288
+ @QtCore.Slot()
289
+ def _load_collection(self):
290
+ ds = self._ds
291
+ parent_class, _ = self.check_parent()
292
+ self._collection.clear()
293
+ for class_ in ds.query_class():
294
+ for collection in ds.query_collection(class_, parent_class):
295
+ self._collection.addItem(collection)
296
+
297
+ @QtCore.Slot()
298
+ def _load_properties(self):
299
+ ds = self._ds
300
+ parent_class, _ = self.check_parent()
301
+ collection = self.check_collection()
302
+ children_class = self._collection_class[parent_class, collection]
303
+ self._properties.clear()
304
+ for prop in ds.query_property(children_class, parent_class, collection):
305
+ self._properties.addItem(prop)
306
+
307
+ def extract(self, ds) -> pl.DataFrame:
308
+ phase = self._phase.currentText()
309
+ properties = [prop.text() for prop in self._properties.selectedItems()]
310
+ samples = [
311
+ self._sample_names[sample.text()] for sample in self._samples.selectedItems()
312
+ ]
313
+ parent_class, parent = self.check_parent()
314
+ collection = self.check_collection()
315
+ children_class = self._collection_class[parent_class, collection]
316
+ children = self.check_children()
317
+ try:
318
+ df = ds.extract(
319
+ phase,
320
+ children_class,
321
+ children,
322
+ properties,
323
+ samples,
324
+ parent_class=parent_class,
325
+ parent=parent,
326
+ collection=collection,
327
+ )
328
+ return df
329
+ except (fx.utils.QueryError, KeyError, ValueError) as erf:
330
+ cuteError(self, str(erf))
331
+
332
+ @QtCore.Slot()
333
+ def expose(self):
334
+ df = self.extract(self._ds)
335
+ if not isinstance(df, pl.DataFrame):
336
+ return
337
+ # Table
338
+ self.table.setModel(CutePolars(df))
339
+ # Plot
340
+ fd = df.pivot(
341
+ df.columns[:2],
342
+ index="Datetime",
343
+ values=df.columns[3:],
344
+ )
345
+ fig = px.line(
346
+ fd,
347
+ x="Datetime",
348
+ y=fd.columns[1:],
349
+ )
350
+ tempfilename = os.path.join(tempfile.gettempdir(), "darksidexplotly.html")
351
+ fig.write_html(tempfilename)
352
+ self.plot.load("file:" + tempfilename)
353
+
354
+ @QtCore.Slot()
355
+ def export(self):
356
+ # df = self.extract(self._ds)
357
+ cuteError(self, "NotImplemented")
358
+
359
+ def closeEvent(self, *args):
360
+ """Overload"""
361
+ if hasattr(self, "_ds"):
362
+ self._ds.close()
@@ -0,0 +1,29 @@
1
+ import polars as pl
2
+ from PySide6 import QtCore
3
+
4
+
5
+ class CutePolars(QtCore.QAbstractTableModel):
6
+ def __init__(self, df: pl.DataFrame, parent=None):
7
+ super().__init__()
8
+ self._df = df
9
+
10
+ def rowCount(self, parent=QtCore.QModelIndex) -> int:
11
+ """Override method from QAbstractTableModel"""
12
+ return len(self._df)
13
+
14
+ def columnCount(self, parent=QtCore.QModelIndex) -> int:
15
+ """Override method from QAbstractTableModel"""
16
+ return len(self._df.columns)
17
+
18
+ def data(self, index: QtCore.QModelIndex, role=QtCore.Qt.ItemDataRole) -> str:
19
+ """Override method from QAbstractTableModel"""
20
+ if role == QtCore.Qt.ItemDataRole.DisplayRole:
21
+ return str(self._df[index.row(), index.column()])
22
+
23
+ def headerData(self, index: int, orientation: QtCore.Qt.Orientation, role: QtCore.Qt.ItemDataRole) -> str:
24
+ """Override method from QAbstractTableModel"""
25
+ if role == QtCore.Qt.ItemDataRole.DisplayRole:
26
+ if orientation == QtCore.Qt.Orientation.Horizontal:
27
+ return str(self._df.columns[index])
28
+ elif orientation == QtCore.Qt.Orientation.Vertical:
29
+ return str(index)
@@ -0,0 +1,21 @@
1
+ from collections.abc import Iterable
2
+
3
+ from PySide6 import QtWidgets
4
+
5
+
6
+ def cuteTree(keys: list, prefix: str ="") -> Iterable:
7
+ """Generate an Iterable of (key, QTreeWidgetItem) for each key,
8
+ each QTreeWidgetItem is named prefix + key.
9
+ """
10
+ for name in keys:
11
+ item = QtWidgets.QTreeWidgetItem()
12
+ item.setText(0, prefix + name)
13
+ yield name, item
14
+
15
+
16
+ def cuteError(parent=None, msg: str = ""):
17
+ """Display an error message."""
18
+ mbox = QtWidgets.QMessageBox(parent)
19
+ mbox.setWindowTitle("Error - How cute")
20
+ mbox.setText(msg)
21
+ mbox.exec()
@@ -0,0 +1,30 @@
1
+ Metadata-Version: 2.4
2
+ Name: fairyside
3
+ Version: 0.1.0
4
+ Summary: FairySide - FEE-rical interface
5
+ Author: Coda Cave
6
+ Project-URL: Repository, https://gitlab.com/CodaCave/fairyside
7
+ Project-URL: Changelog, https://gitlab.com/CodaCave/fairyside/-/releases
8
+ Requires-Python: >=3.10
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: fairyex>=0.4.0
11
+ Requires-Dist: plotly
12
+ Requires-Dist: polars
13
+ Requires-Dist: PySide6
14
+
15
+ # FairySide
16
+
17
+ Join the fairy(ex) (py)side of energy modeling and enlight (dark) solution!
18
+
19
+ ## Cute feature
20
+
21
+ - Explore solution
22
+ - Export data
23
+
24
+ ## Quickstart
25
+
26
+ ```bash
27
+ pip install fairyside
28
+ python -m fairyside
29
+ ```
30
+
@@ -0,0 +1,13 @@
1
+ README.md
2
+ pyproject.toml
3
+ fairyside/__init__.py
4
+ fairyside/__main__.py
5
+ fairyside/_version.py
6
+ fairyside/cutely.py
7
+ fairyside/model.py
8
+ fairyside/utils.py
9
+ fairyside.egg-info/PKG-INFO
10
+ fairyside.egg-info/SOURCES.txt
11
+ fairyside.egg-info/dependency_links.txt
12
+ fairyside.egg-info/requires.txt
13
+ fairyside.egg-info/top_level.txt
@@ -0,0 +1,4 @@
1
+ fairyex>=0.4.0
2
+ plotly
3
+ polars
4
+ PySide6
@@ -0,0 +1 @@
1
+ fairyside
@@ -0,0 +1,23 @@
1
+ [project]
2
+ name = "fairyside"
3
+ description = "FairySide - FEE-rical interface"
4
+ authors = [
5
+ {name="Coda Cave"},
6
+ ]
7
+ readme = "README.md"
8
+ version = "0.1.0"
9
+
10
+ requires-python = ">=3.10"
11
+ dependencies = [
12
+ "fairyex>=0.4.0",
13
+ "plotly",
14
+ "polars",
15
+ "PySide6",
16
+ ]
17
+
18
+ [tool.setuptools.packages.find]
19
+ include = ["fairyside"]
20
+
21
+ [project.urls]
22
+ Repository = "https://gitlab.com/CodaCave/fairyside"
23
+ Changelog = "https://gitlab.com/CodaCave/fairyside/-/releases"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+