Additional Modules
Modules
modules.py contains all kinds of functions used by other modules in the project. For detailed functionality of the modules, see the comments in the code itself.
- modules.add_device(elements, device_dict, room, device_type, own, own_devices=None)
This function adds a device of the given type to the cytoscape (including the socket and repositioning of the plus-node) and attaches it to the device dictionary.
- Parameters:
elements (list) – Elements of the room cytoscape
device_dict (dict) – Dictionary containing all devices in the custom house
room (str) – Room to add device to
device_type (str) – Type of device to add
own (bool) – Boolean if the device to add is an own device
own_devices (dict) – Dictionary of existing own devices
- Returns:
elements: Updated element list of cytoscape
- Returns:
device_dict: Updated dictionary of devices
- modules.calculate_costs(data, cost_kwh, device_dict)
Takes the energy data of each device and the kwh price and calculated the yearly costs of every device. Returns a list of tuples containing the name, the cost and the icon of the device.
- Parameters:
data – Energy result data of house calculation
cost_kwh – Cost of 1 kWh of electrical energy, from settings
device_dict – Dictionary containing all devices in the custom house
- Returns:
List of tuples of costs of devices
- Return type:
list
- modules.calculate_house(device_dict, timesteps)
Takes all devices and calculates: The sum of all devices per room and house; The energy used by devices, rooms and house.
- Parameters:
device_dict (dict) – Dictionary containing all devices in the custom house
timesteps (range) – Number of timesteps
- Returns:
df_power: Dataframe with power of each device in each timestep
- Returns:
df_sum: Dataframe with sum power of all rooms and house per timestep
- Returns:
df_energy: Dataframe with overall energy per room and house
- Returns:
figure[0]: Scatter plot of results
- Returns:
figure[1]: Sunburst plot of energies
- modules.calculate_power_flow(elements, grid_object_dict)
Main function to calculate the power flows in the created and configured grid. Built as a state-machine.
- Parameters:
grid_object_dict (dict) – Dictionary of objects in grid with id corresponding to node ids of cytoscape
elements (list) – Grid elements in form of cytoscape graph
- Returns:
Dataframe Flow, edge labels
- modules.check_power_profiles(graph)
Finds the power profile with the most timesteps and interpolate all other profiles to this length.
- Parameters:
graph (NetworkX graph) – Directed NetworkX graph
- Returns:
Updated NetworkX graph
- modules.connection_allowed(source, target, object_dict)
Check if the connection which is about to be added is allowed.
- Parameters:
source (str) – Id of source node
target (str) – Id of target node
object_dict (dict) – Dictionary containing all grid objects and their properties
- Returns:
The result of the check, True if allowed
- Return type:
bool
- modules.correct_cyto_edges(elements, graph)
Function takes the elements of the grid cytoscape and corrects all edges, so the point in the same direction as in the directed graph of the grid. Needed for the display of arrows.
- Parameters:
elements – Element list of dash cytoscape
graph – Directed Graph of the grid.
- Returns:
Edited Element list of dash cytoscape
- Return type:
list
- modules.create_device_object(device_id, device_type, database, own=False, own_devices=None)
Creates a dictionary containing all properties of a device. Chooses source of the data regarding own or standard devices.
- Parameters:
device_id (str) – Id to set to device
device_type (str) – Type of device to create
database (str) – SQL database to fetch device properties from
own (bool) – Boolean if the device to create is an own device
own_devices (dict) – Dictionary of existing own devices
- Returns:
Device as a dictionary
- Return type:
dict
- modules.extract_tutorial_steps(file_path)
Takes a markdown-file as the input and extracts all headings (level 2 - ##) with the content and help below it into a list of lists. It also extracts the path of the picture.
- Parameters:
file_path (str) – Path of the markdown file
- Returns:
list of lists with headings and content
- Return type:
list
- modules.generate_directed_graph(graph)
Generates a directed graph out of the undirected one. Performs a bfs for this and takes the external grid as the starting point. Checks if there are more ore less than one external grids.
- Parameters:
graph (NetworkX graph) – Undirected graph
- Returns:
Directed NetworkX graph
- modules.generate_equations(graph)
Generates the incidence matrix of the directed graph and adds a column to make it quadratic Also returns a dataframe with each power profile in it.
- Parameters:
graph – Directed NetworkX graph
- Returns:
inc: Incidence matrix
- Returns:
df_power: Dataframe with all power profiles
- modules.generate_grid_dataframes(elements, grid_objects)
Generate pandas DataFrames from given cytoscape elements.
- Parameters:
elements (list) – Cytoscape element list, nodes and edges
grid_objects (dict) – Dict of all grid objects to link them to the nodes
- Returns:
df_nodes: DataFrame containing all nodes of the grid
- Returns:
df_edges: DataFrame containing all edges of the grid.
- modules.generate_grid_graph(df_nodes, df_edges)
Generate a NetworkX graph from the given DataFrames for nodes and edges.
- Parameters:
df_nodes (dataframe) – DataFrame containing nodes with ‘id’ and ‘linkedObject’
df_edges (dataframe) – DataFrame containing nodes with ‘source’, ‘target’ and ‘id
- Returns:
NetworkX graph of the given grid
- modules.generate_grid_object(object_type, object_id, node_id)
Creates a grid object from the function in objects.py.
- Parameters:
object_type (str) – Type of object that should be created
object_id (str) – Id of the object
node_id (str) – Id of the cytoscape node which it is linked to
- Returns:
Grid object
- modules.get_button_dict()
Get a dictionary of all the buttons of the rooms in the custom house. The dictionary is split into each room and contains the menu-objects for each device. They consist of: name, dash button-id, icon Only the devices are considered, which have a standard room named in the database.
- Returns:
dictionary of buttons for the room menus
- Return type:
dict
- modules.get_connected_edges(elements, selected_element)
Find edges which are connected to a cytoscape node.
- Parameters:
elements (list) – Cytoscape elements
selected_element (dict) – Node to search for
- Returns:
List of connected edges
- Return type:
list
- modules.get_icon_url(icon_name: str)
Takes an icon name in the format “mdi:icon-name” and returns the Iconify URL for the icon.
- Parameters:
icon_name (str) – iconify name of icon
- Returns:
url of icon
- Return type:
str
- modules.get_last_id(elements)
Returns the last used id in a list of elements. It returns two ids, one for the nodes, one for the edges.
- Parameters:
elements (list) – List of elements with ids
- Returns:
Last used ids of [nodes, edges]
- Return type:
list[int]
- modules.get_monday_sunday_from_week(week_num, year)
Get the dates of monday and sunday of a given week in a given year.
- Parameters:
week_num (int) –
year (int) –
- Returns:
Date of monday
- Returns:
Date of sunday
- Return type:
date
- modules.interpolate_profile(values, number_steps, interpolation_type)
Interpolates a given profile to a given number of timesteps. The interpolation method can be defined. Possible kinds of interpolation: linear, nearest, nearest-up, zero, slinear, quadratic, cubic, previous, next
- Parameters:
values (list) – Original values of profile
number_steps (int) – Number of steps of final profile
interpolation_type (str) – Type of interpolation
- Returns:
Final interpolated profile
- Return type:
list
- modules.plot_graph(graph)
Plots the NetworkX graph which is created from the grid cytoscape.
- Parameters:
graph – NetworkX graph
- Returns:
base64 string of png picture
- modules.power_flow_statemachine(state, data)
State machine of grid calculation. Executes all checks and steps necessary.
- Parameters:
state (str) – Statemachine state to execute in this step
data (dict) – All needed data for the calculation
- Returns:
Next statemachine state
- Returns:
updated data dict
- Returns:
flag if calculation is done (bool)
- modules.recursive_addition(graph, node, timestep, df_flow)
This is a recursive algorithm to calculate the edge power flows in a tree graph. It starts at the root (external grid) and iterates through the graph recursively until the leaves are reached. From there, the power is added up.
- Parameters:
graph – NetworkX Graph
node (str) – Node to start recursion from
timestep (int) – Timestep to calculate power for
df_flow – Dataframe to store results
- Returns:
Total power of the node
- Return type:
float
- modules.save_settings_devices(children, device_dict, selected_element, house, day)
Save all settings of the selected device. This is done by looping over all children of the menu tab. After checking their type, it is selected what to do and how to save the input.
- Parameters:
children – Children of the menu tab, all inputs of the selected device
device_dict – Dictionary containing all devices in the custom house
selected_element – Cytoscape element which was clicked in the house
house – house-id of house in which a device should be saved, ‘house1’
day – Day selected in the pagination of the menu
- Returns:
device_dict: Dictionary containing all devices in the custom house
- modules.save_settings_house(children, gridObject_dict, selected_element, year, week, used_profiles, checkbox)
Save all settings of a selected house. Load random profile if wanted.
- Parameters:
children – Children of the menu tab, all inputs of the selected object
gridObject_dict – Dictionary containing all grid objects and their properties
selected_element – Cytoscape element which was clicked in the grid
year – Year set in the settings tab
week – Week of year set in the settings tab
used_profiles – Already used random profiles from IZES
checkbox – Bool if checkbox to load random profile is checked
- Returns:
Updated gridObject_dict
- Returns:
Updated used_profiles
- modules.save_settings_pv(children, gridObject_dict, selected_element, year, week)
Saves the settings of a PV-module. After all data was checked and prepared, it loads the solar data from renewables.ninja.
- Parameters:
children – Children of the menu tab, all inputs of the selected object
gridObject_dict – Dictionary containing all grid objects and their properties
selected_element – Cytoscape element which was clicked in the grid
year – Year set in the settings tab
week – Week of year set in the settings tab
- Returns:
Updated gridObject_dict
- Returns:
Notification
- modules.solve_flow(A, b)
Solves the load flow with a given incidence matrix and result vector. Checks if the matrix is quadratic.
- Parameters:
A – Incidence Matrix = equations to solve
b – Result vector
- Returns:
Flow vector
- Return type:
ndarray
- modules.update_settings(gridObject_dict, selected_element, year, week)
Updates the selected element with the stored settings year and week.
- Parameters:
gridObject_dict (dict) – Dictionary containing all grid objects and their properties
selected_element (str) – Element to update
year (int) – Year to fetch data from
week (int) – Week of the year
- Returns:
Updated gridObject_dict
- Return type:
dict
Plot
plot.py contains different functions to plot data in different graph types.
- plot.plot_all_devices_room(df_devices, df_sum, df_energy, device_dict)
Creates plots from the results of the house calculation.
Plot 1: Stacked scatter plot of the power timeseries of all devices
Plot 2: Sunburst plot showing the energy use of each room and devices in the rooms
- Parameters:
df_devices (dataframe) – Dataframe with power timeseries of each device
df_sum (dataframe) – Dataframe with sum of power for each room and house
df_energy (dataframe) – Dataframe with used energy of devices, rooms and house
device_dict (dict) – Dictionary containing all devices in the custom house
- Returns:
figure scatter plot; figure sunburst plot
- plot.plot_device_timeseries(timesteps, load, color)
Creates the figure to plot the power profile of a device.
- Parameters:
timesteps – List with timesteps to plot
load – Timeseries of device power
color (color) – Color of plot
- Returns:
Figure
- plot.plot_grid_results(df_power)
Creates the figure to plot the power results of the grid.
- Parameters:
df_power (list[int]) – Timeseries of house power
- Returns:
Figure
- plot.plot_house_timeseries(power, color)
Creates the figure to plot the power profile of a house.
- Parameters:
power (list[int]) – Timeseries of house power
color (color) – Color of plot
- Returns:
Figure
- plot.plot_pv_timeseries(timesteps, power, color)
Creates figure to display the infeed solar power. The power is inverted to positive for the graph.
- Parameters:
timesteps – List with timesteps to plot
power – Timeseries of infeed pv power
color (color) – Color of plot
- Returns:
Figure
Objects
objects.py contains functions to create grid objects. Each function returns a dictionary with all properties of the object.
- objects.create_BatteryObject(object_id, node_id)
Create a dictionary with properties of a battery object
- Parameters:
object_id (str) – unique id of the object
node_id (str) – id of the linked cytoscape node
- Returns:
Dictionary with object properties
- objects.create_BusinessObject(object_id, node_id)
Create a dictionary with properties of a business object
- Parameters:
object_id (str) – unique id of the object
node_id (str) – id of the linked cytoscape node
- Returns:
Dictionary with object properties
- objects.create_ExternalGridObject(object_id, node_id)
Create a dictionary with properties of a external grid object
- Parameters:
object_id (str) – unique id of the object
node_id (str) – id of the linked cytoscape node
- Returns:
Dictionary with object properties
- objects.create_HouseObject(object_id, node_id)
Create a dictionary with properties of a house object
- Parameters:
object_id (str) – unique id of the object
node_id (str) – id of the linked cytoscape node
- Returns:
Dictionary with object properties
- objects.create_LineObject(object_id, edge_id)
Create a dictionary with properties of a house object
- Parameters:
object_id (str) – unique id of the object
edge_id (str) – id of linked cytoscape edge
- Returns:
Dictionary with object properties
- objects.create_PVObject(object_id, node_id)
Create a dictionary with properties of a PV object
- Parameters:
object_id (str) – unique id of the object
node_id (str) – id of the linked cytoscape node
- Returns:
Dictionary with object properties
- objects.create_SmartMeterObject(object_id, node_id)
Create a dictionary with properties of a smart-meter object
- Parameters:
object_id (str) – unique id of the object
node_id (str) – id of the linked cytoscape node
- Returns:
Dictionary with object properties
- objects.create_SwitchCabinetObject(object_id, node_id)
Create a dictionary with properties of a switch cabinet object
- Parameters:
object_id (str) – unique id of the object
node_id (str) – id of the linked cytoscape node
- Returns:
Dictionary with object properties
- objects.create_TransformerHelperNodeObject()
Create a dictionary with properties of a transformer helper node object
- Returns:
Dictionary with object properties
- objects.create_TransformerObject(object_id, node_id)
Create a dictionary with properties of a transformer object
- Parameters:
object_id (str) – unique id of the object
node_id (str) – id of the linked cytoscape node
- Returns:
Dictionary with object properties