Skip to content

Commit

Permalink
load and generators (need refacto for dup code)
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Jeandemange <[email protected]>
  • Loading branch information
jeandemanged committed Nov 11, 2024
1 parent 2510ca3 commit 803bfdb
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 5 deletions.
7 changes: 3 additions & 4 deletions yagat/frames/impl/bus_list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

class BusListView(tk.Frame):

def __init__(self, parent, context: AppContext, tab_name: str, *args, **kwargs):
def __init__(self, parent, context: AppContext, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self._tab_name = tab_name
self.sheet = tks.Sheet(self, index_align='left', data=[[1, 2], [3, 4]])
self.sheet.enable_bindings('single_select',
'drag_select',
Expand Down Expand Up @@ -57,7 +56,7 @@ def on_selection_changed(self, selection: tuple[Optional[str], Optional[str], Op

@property
def tab_name(self) -> str:
return self._tab_name
return 'Bus list'


if __name__ == "__main__":
Expand All @@ -69,7 +68,7 @@ def tab_name(self) -> str:
windll.shcore.SetProcessDpiAwareness(2)
root = tk.Tk()
ctx = AppContext(root)
bw = BusListView(root, ctx, 'Bus list')
bw = BusListView(root, ctx)
bw.pack(fill="both", expand=True)
ctx.network = pn.create_ieee9()
ctx.selection = 'S1'
Expand Down
76 changes: 76 additions & 0 deletions yagat/frames/impl/generator_list_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# Copyright (c) 2024, Damien Jeandemange (https://github.com/jeandemanged)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
#
import os
import tkinter as tk
from typing import Optional

import pypowsybl.network as pn
import tksheet as tks

from yagat.app_context import AppContext
from yagat.networkstructure import Connection


class GeneratorListView(tk.Frame):

def __init__(self, parent, context: AppContext, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.sheet = tks.Sheet(self, index_align='left', data=[[1, 2], [3, 4]])
self.sheet.enable_bindings('single_select',
'drag_select',
'row_select',
'column_select',
'copy',
'column_width_resize',
'double_click_column_resize',
'double_click_row_resize',
'row_width_resize',
'column_height_resize',
'arrowkeys',
)
self.context = context
self.context.add_selection_changed_listener(self.on_selection_changed)
self.variables = []

self.sheet.set_index_width(300)
self.sheet.pack(fill="both", expand=True)

def on_selection_changed(self, selection: tuple[Optional[str], Optional[str], Optional[Connection]]):
generators = self.context.network_structure.generators
voltage_levels = []
if selection[0] == 'voltage_level':
voltage_levels = [selection[1]]
elif selection[0] == 'substation':
voltage_levels = [vl.voltage_level_id for vl in self.context.network_structure.get_substation(selection[1]).voltage_levels]
if voltage_levels:
generators = generators.loc[generators['voltage_level_id'].isin(voltage_levels)]
self.sheet.data = [l.tolist() for l in generators.to_numpy()]
self.sheet.set_index_data(generators.index.tolist())
self.sheet.set_header_data(generators.columns)
self.sheet.set_all_cell_sizes_to_text()

@property
def tab_name(self) -> str:
return 'Generator list'


if __name__ == "__main__":

if os.name == 'nt':
# Fixing the blur UI on Windows
from ctypes import windll

windll.shcore.SetProcessDpiAwareness(2)
root = tk.Tk()
ctx = AppContext(root)
bw = GeneratorListView(root, ctx)
bw.pack(fill="both", expand=True)
ctx.network = pn.create_ieee9()
ctx.selection = 'S1'
ctx.selected_tab = bw.tab_name
root.mainloop()
76 changes: 76 additions & 0 deletions yagat/frames/impl/load_list_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# Copyright (c) 2024, Damien Jeandemange (https://github.com/jeandemanged)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
#
import os
import tkinter as tk
from typing import Optional

import pypowsybl.network as pn
import tksheet as tks

from yagat.app_context import AppContext
from yagat.networkstructure import Connection


class LoadListView(tk.Frame):

def __init__(self, parent, context: AppContext, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.sheet = tks.Sheet(self, index_align='left', data=[[1, 2], [3, 4]])
self.sheet.enable_bindings('single_select',
'drag_select',
'row_select',
'column_select',
'copy',
'column_width_resize',
'double_click_column_resize',
'double_click_row_resize',
'row_width_resize',
'column_height_resize',
'arrowkeys',
)
self.context = context
self.context.add_selection_changed_listener(self.on_selection_changed)
self.variables = []

self.sheet.set_index_width(300)
self.sheet.pack(fill="both", expand=True)

def on_selection_changed(self, selection: tuple[Optional[str], Optional[str], Optional[Connection]]):
loads = self.context.network_structure.loads
voltage_levels = []
if selection[0] == 'voltage_level':
voltage_levels = [selection[1]]
elif selection[0] == 'substation':
voltage_levels = [vl.voltage_level_id for vl in self.context.network_structure.get_substation(selection[1]).voltage_levels]
if voltage_levels:
loads = loads.loc[loads['voltage_level_id'].isin(voltage_levels)]
self.sheet.data = [l.tolist() for l in loads.to_numpy()]
self.sheet.set_index_data(loads.index.tolist())
self.sheet.set_header_data(loads.columns)
self.sheet.set_all_cell_sizes_to_text()

@property
def tab_name(self) -> str:
return 'Load list'


if __name__ == "__main__":

if os.name == 'nt':
# Fixing the blur UI on Windows
from ctypes import windll

windll.shcore.SetProcessDpiAwareness(2)
root = tk.Tk()
ctx = AppContext(root)
bw = LoadListView(root, ctx)
bw.pack(fill="both", expand=True)
ctx.network = pn.create_ieee9()
ctx.selection = 'S1'
ctx.selected_tab = bw.tab_name
root.mainloop()
14 changes: 13 additions & 1 deletion yagat/frames/impl/tabs_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from yagat.app_context import AppContext
from yagat.frames.impl.diagram_view_bus import DiagramViewBus
from yagat.frames.impl.bus_list_view import BusListView
from yagat.frames.impl.generator_list_view import GeneratorListView
from yagat.frames.impl.load_list_view import LoadListView
from yagat.networkstructure import BusView


Expand All @@ -34,10 +36,20 @@ def __init__(self, parent, context: AppContext, *args, **kwargs):
self.tab_control.pack(expand=True, fill=tk.BOTH)

# Bus List view tab
self.tab_bus_list = BusListView(self.tab_control, context, 'Bus list')
self.tab_bus_list = BusListView(self.tab_control, context)
self.tab_control.add(self.tab_bus_list, text=self.tab_bus_list.tab_name)
self.tab_control.pack(expand=True, fill=tk.BOTH)

# Generators List view tab
self.tab_gen_list = GeneratorListView(self.tab_control, context)
self.tab_control.add(self.tab_gen_list, text=self.tab_gen_list.tab_name)
self.tab_control.pack(expand=True, fill=tk.BOTH)

# Loads List view tab
self.tab_load_list = LoadListView(self.tab_control, context)
self.tab_control.add(self.tab_load_list, text=self.tab_load_list.tab_name)
self.tab_control.pack(expand=True, fill=tk.BOTH)

def on_tab_changed(self):
self.context.selected_tab = self.tab_control.tab(self.tab_control.select(), 'text')

Expand Down
8 changes: 8 additions & 0 deletions yagat/networkstructure/impl/network_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def network(self) -> pn.Network:
def buses(self) -> pd.DataFrame:
return self._buses_df

@property
def generators(self) -> pd.DataFrame:
return self._injections_df[ns.EquipmentType.GENERATOR]

@property
def loads(self) -> pd.DataFrame:
return self._injections_df[ns.EquipmentType.LOAD]

def refresh(self):
logging.info('refresh start')
self._substations_df = self._network.get_substations(all_attributes=True)
Expand Down

0 comments on commit 803bfdb

Please sign in to comment.