NREL-erad 0.0.0a0__py3-none-any.whl → 1.0.0__py3-none-any.whl
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.
- erad/__init__.py +1 -0
- erad/constants.py +20 -20
- erad/cypher_queries/load_data_v1.cypher +211 -211
- erad/data/World_Earthquakes_1960_2016.csv +23410 -23410
- erad/db/assets/critical_infras.py +170 -170
- erad/db/assets/distribution_lines.py +101 -101
- erad/db/credential_model.py +20 -20
- erad/db/disaster_input_model.py +23 -23
- erad/db/inject_earthquake.py +52 -52
- erad/db/inject_flooding.py +53 -53
- erad/db/neo4j_.py +162 -162
- erad/db/utils.py +13 -13
- erad/exceptions.py +68 -68
- erad/metrics/check_microgrid.py +208 -208
- erad/metrics/metric.py +178 -178
- erad/programs/backup.py +61 -61
- erad/programs/microgrid.py +44 -44
- erad/scenarios/abstract_scenario.py +102 -102
- erad/scenarios/common.py +92 -92
- erad/scenarios/earthquake_scenario.py +161 -161
- erad/scenarios/fire_scenario.py +160 -160
- erad/scenarios/flood_scenario.py +493 -493
- erad/scenarios/flows.csv +671 -0
- erad/scenarios/utilities.py +75 -75
- erad/scenarios/wind_scenario.py +89 -89
- erad/utils/ditto_utils.py +252 -252
- erad/utils/hifld_utils.py +147 -147
- erad/utils/opendss_utils.py +357 -357
- erad/utils/overpass.py +76 -76
- erad/utils/util.py +178 -178
- erad/visualization/plot_graph.py +218 -218
- {NREL_erad-0.0.0a0.dist-info → nrel_erad-1.0.0.dist-info}/METADATA +65 -61
- nrel_erad-1.0.0.dist-info/RECORD +42 -0
- {NREL_erad-0.0.0a0.dist-info → nrel_erad-1.0.0.dist-info}/WHEEL +1 -2
- {NREL_erad-0.0.0a0.dist-info → nrel_erad-1.0.0.dist-info/licenses}/LICENSE.txt +28 -28
- NREL_erad-0.0.0a0.dist-info/RECORD +0 -42
- NREL_erad-0.0.0a0.dist-info/top_level.txt +0 -1
erad/db/inject_earthquake.py
CHANGED
@@ -1,52 +1,52 @@
|
|
1
|
-
""" Module for injecting scenario data into graph database. """
|
2
|
-
|
3
|
-
|
4
|
-
from neo4j import GraphDatabase
|
5
|
-
import shapely
|
6
|
-
|
7
|
-
from erad.db import disaster_input_model
|
8
|
-
from erad.scenarios import earthquake_scenario
|
9
|
-
from erad.db.assets.distribution_lines import (
|
10
|
-
_update_distribution_overhead_lines,
|
11
|
-
_update_distribution_underground_lines
|
12
|
-
)
|
13
|
-
|
14
|
-
from erad.db.assets.critical_infras import (
|
15
|
-
_update_critical_infra_based_on_grid_access_fast
|
16
|
-
)
|
17
|
-
|
18
|
-
|
19
|
-
def inject_point_earthquake(
|
20
|
-
scenario_input: disaster_input_model.PointEarthquake,
|
21
|
-
driver: GraphDatabase.driver,
|
22
|
-
critical_infras: None
|
23
|
-
):
|
24
|
-
"""Function to update the survival probability of different assets
|
25
|
-
due to user input based earthquake scenario.
|
26
|
-
|
27
|
-
Args:
|
28
|
-
scenario_input (scenario_model.PointEarthquake): Should be an instance
|
29
|
-
of `scenario_model.PointEarthquake` data model.
|
30
|
-
driver (GraphDatabase.driver): Instance of `GraphDatabase.driver`
|
31
|
-
instance
|
32
|
-
"""
|
33
|
-
if not critical_infras:
|
34
|
-
critical_infras = []
|
35
|
-
|
36
|
-
|
37
|
-
scenario = earthquake_scenario.EarthquakeScenario(
|
38
|
-
shapely.geometry.Point(
|
39
|
-
scenario_input.longitude, scenario_input.latitude
|
40
|
-
),
|
41
|
-
scenario_input.probability_model,
|
42
|
-
scenario_input.timestamp,
|
43
|
-
Magnitude=scenario_input.magnitude,
|
44
|
-
Depth=scenario_input.depth,
|
45
|
-
)
|
46
|
-
|
47
|
-
_update_distribution_overhead_lines(scenario, driver, scenario_input.timestamp)
|
48
|
-
_update_distribution_underground_lines(scenario, driver, scenario_input.timestamp)
|
49
|
-
if len(critical_infras):
|
50
|
-
_update_critical_infra_based_on_grid_access_fast(critical_infras, driver)
|
51
|
-
|
52
|
-
|
1
|
+
""" Module for injecting scenario data into graph database. """
|
2
|
+
|
3
|
+
|
4
|
+
from neo4j import GraphDatabase
|
5
|
+
import shapely
|
6
|
+
|
7
|
+
from erad.db import disaster_input_model
|
8
|
+
from erad.scenarios import earthquake_scenario
|
9
|
+
from erad.db.assets.distribution_lines import (
|
10
|
+
_update_distribution_overhead_lines,
|
11
|
+
_update_distribution_underground_lines
|
12
|
+
)
|
13
|
+
|
14
|
+
from erad.db.assets.critical_infras import (
|
15
|
+
_update_critical_infra_based_on_grid_access_fast
|
16
|
+
)
|
17
|
+
|
18
|
+
|
19
|
+
def inject_point_earthquake(
|
20
|
+
scenario_input: disaster_input_model.PointEarthquake,
|
21
|
+
driver: GraphDatabase.driver,
|
22
|
+
critical_infras: None
|
23
|
+
):
|
24
|
+
"""Function to update the survival probability of different assets
|
25
|
+
due to user input based earthquake scenario.
|
26
|
+
|
27
|
+
Args:
|
28
|
+
scenario_input (scenario_model.PointEarthquake): Should be an instance
|
29
|
+
of `scenario_model.PointEarthquake` data model.
|
30
|
+
driver (GraphDatabase.driver): Instance of `GraphDatabase.driver`
|
31
|
+
instance
|
32
|
+
"""
|
33
|
+
if not critical_infras:
|
34
|
+
critical_infras = []
|
35
|
+
|
36
|
+
|
37
|
+
scenario = earthquake_scenario.EarthquakeScenario(
|
38
|
+
shapely.geometry.Point(
|
39
|
+
scenario_input.longitude, scenario_input.latitude
|
40
|
+
),
|
41
|
+
scenario_input.probability_model,
|
42
|
+
scenario_input.timestamp,
|
43
|
+
Magnitude=scenario_input.magnitude,
|
44
|
+
Depth=scenario_input.depth,
|
45
|
+
)
|
46
|
+
|
47
|
+
_update_distribution_overhead_lines(scenario, driver, scenario_input.timestamp)
|
48
|
+
_update_distribution_underground_lines(scenario, driver, scenario_input.timestamp)
|
49
|
+
if len(critical_infras):
|
50
|
+
_update_critical_infra_based_on_grid_access_fast(critical_infras, driver)
|
51
|
+
|
52
|
+
|
erad/db/inject_flooding.py
CHANGED
@@ -1,53 +1,53 @@
|
|
1
|
-
""" Module for injecting flooding scenario data into graph database. """
|
2
|
-
|
3
|
-
|
4
|
-
from neo4j import GraphDatabase
|
5
|
-
from shapely import MultiPolygon, Polygon
|
6
|
-
|
7
|
-
from erad.db import disaster_input_model
|
8
|
-
from erad.scenarios import flood_scenario
|
9
|
-
from erad.db.assets.distribution_lines import (
|
10
|
-
_update_distribution_overhead_lines,
|
11
|
-
_update_distribution_underground_lines
|
12
|
-
)
|
13
|
-
|
14
|
-
from erad.db.assets.critical_infras import (
|
15
|
-
_update_critical_infra_based_on_grid_access_fast
|
16
|
-
)
|
17
|
-
|
18
|
-
|
19
|
-
def inject_polygon_flooding(
|
20
|
-
scenario_input: disaster_input_model.PolygonFlooding,
|
21
|
-
driver: GraphDatabase.driver,
|
22
|
-
critical_infras: None
|
23
|
-
):
|
24
|
-
"""Function to update the survival probability of different assets
|
25
|
-
due to user input based flooding scenario.
|
26
|
-
|
27
|
-
Args:
|
28
|
-
scenario_input (scenario_model.PolygonFlooding): Should be an instance
|
29
|
-
of `scenario_model.PolygonFlooding` data model.
|
30
|
-
driver (GraphDatabase.driver): Instance of `GraphDatabase.driver`
|
31
|
-
instance
|
32
|
-
"""
|
33
|
-
if not critical_infras:
|
34
|
-
critical_infras = []
|
35
|
-
|
36
|
-
|
37
|
-
scenario = flood_scenario.FlooadScenario(
|
38
|
-
MultiPolygon(
|
39
|
-
[Polygon(el) for el in scenario_input.polygon]
|
40
|
-
),
|
41
|
-
scenario_input.probability_model,
|
42
|
-
scenario_input.timestamp,
|
43
|
-
file_flow=scenario_input.file_flow,
|
44
|
-
file_levels=scenario_input.file_levels,
|
45
|
-
file_gaugues=scenario_input.file_gaugues
|
46
|
-
)
|
47
|
-
|
48
|
-
_update_distribution_underground_lines(scenario, driver, scenario_input.timestamp)
|
49
|
-
_update_distribution_overhead_lines(scenario, driver, scenario_input.timestamp)
|
50
|
-
if len(critical_infras):
|
51
|
-
_update_critical_infra_based_on_grid_access_fast(critical_infras, driver)
|
52
|
-
|
53
|
-
|
1
|
+
""" Module for injecting flooding scenario data into graph database. """
|
2
|
+
|
3
|
+
|
4
|
+
from neo4j import GraphDatabase
|
5
|
+
from shapely import MultiPolygon, Polygon
|
6
|
+
|
7
|
+
from erad.db import disaster_input_model
|
8
|
+
from erad.scenarios import flood_scenario
|
9
|
+
from erad.db.assets.distribution_lines import (
|
10
|
+
_update_distribution_overhead_lines,
|
11
|
+
_update_distribution_underground_lines
|
12
|
+
)
|
13
|
+
|
14
|
+
from erad.db.assets.critical_infras import (
|
15
|
+
_update_critical_infra_based_on_grid_access_fast
|
16
|
+
)
|
17
|
+
|
18
|
+
|
19
|
+
def inject_polygon_flooding(
|
20
|
+
scenario_input: disaster_input_model.PolygonFlooding,
|
21
|
+
driver: GraphDatabase.driver,
|
22
|
+
critical_infras: None
|
23
|
+
):
|
24
|
+
"""Function to update the survival probability of different assets
|
25
|
+
due to user input based flooding scenario.
|
26
|
+
|
27
|
+
Args:
|
28
|
+
scenario_input (scenario_model.PolygonFlooding): Should be an instance
|
29
|
+
of `scenario_model.PolygonFlooding` data model.
|
30
|
+
driver (GraphDatabase.driver): Instance of `GraphDatabase.driver`
|
31
|
+
instance
|
32
|
+
"""
|
33
|
+
if not critical_infras:
|
34
|
+
critical_infras = []
|
35
|
+
|
36
|
+
|
37
|
+
scenario = flood_scenario.FlooadScenario(
|
38
|
+
MultiPolygon(
|
39
|
+
[Polygon(el) for el in scenario_input.polygon]
|
40
|
+
),
|
41
|
+
scenario_input.probability_model,
|
42
|
+
scenario_input.timestamp,
|
43
|
+
file_flow=scenario_input.file_flow,
|
44
|
+
file_levels=scenario_input.file_levels,
|
45
|
+
file_gaugues=scenario_input.file_gaugues
|
46
|
+
)
|
47
|
+
|
48
|
+
_update_distribution_underground_lines(scenario, driver, scenario_input.timestamp)
|
49
|
+
_update_distribution_overhead_lines(scenario, driver, scenario_input.timestamp)
|
50
|
+
if len(critical_infras):
|
51
|
+
_update_critical_infra_based_on_grid_access_fast(critical_infras, driver)
|
52
|
+
|
53
|
+
|
erad/db/neo4j_.py
CHANGED
@@ -1,162 +1,162 @@
|
|
1
|
-
"""Module contains class and utility functions to manage
|
2
|
-
interactions with Neo4J database.
|
3
|
-
"""
|
4
|
-
|
5
|
-
# standard libraries
|
6
|
-
import os
|
7
|
-
import logging
|
8
|
-
from typing import Union
|
9
|
-
|
10
|
-
|
11
|
-
# third-party libraries
|
12
|
-
from dotenv import load_dotenv
|
13
|
-
from neo4j import GraphDatabase, basic_auth
|
14
|
-
|
15
|
-
# internal imports
|
16
|
-
from erad.db.credential_model import Neo4jConnectionModel
|
17
|
-
|
18
|
-
|
19
|
-
load_dotenv()
|
20
|
-
NEO4J_URL = os.getenv("NEO4J_URL")
|
21
|
-
NEO4J_USERNAME = os.getenv("NEO4J_USERNAME")
|
22
|
-
NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD")
|
23
|
-
|
24
|
-
logger = logging.getLogger(__name__)
|
25
|
-
|
26
|
-
|
27
|
-
class Neo4J:
|
28
|
-
"""Class for managing interaction with Neo4J database.
|
29
|
-
|
30
|
-
Attributes:
|
31
|
-
neo4j_url (str): URL for connecting to Neo4j
|
32
|
-
neo4j_username (str): Username for Neo4j database
|
33
|
-
neo4j_password (str): Password for Neo4j database
|
34
|
-
use_env (bool): True if above info are to be collected
|
35
|
-
from env file.
|
36
|
-
driver (GraphDatabase.driver): Neo4J driver instance
|
37
|
-
"""
|
38
|
-
|
39
|
-
def __init__(
|
40
|
-
self,
|
41
|
-
neo4j_url: Union[str, None] = None,
|
42
|
-
neo4j_username: Union[str, None] = None,
|
43
|
-
neo4j_password: Union[str, None] = None,
|
44
|
-
) -> None:
|
45
|
-
"""Constructor for Neo4J class.
|
46
|
-
|
47
|
-
Args:
|
48
|
-
neo4j_url (str): URL for connecting to Neo4j
|
49
|
-
neo4j_username (str): Username for Neo4j database
|
50
|
-
neo4j_password (str): Password for Neo4j database
|
51
|
-
"""
|
52
|
-
|
53
|
-
self.neo4j_url = NEO4J_URL if NEO4J_URL else neo4j_url
|
54
|
-
self.neo4j_username = (
|
55
|
-
NEO4J_USERNAME if NEO4J_USERNAME else neo4j_username
|
56
|
-
)
|
57
|
-
self.neo4j_password = (
|
58
|
-
NEO4J_PASSWORD if NEO4J_PASSWORD else neo4j_password
|
59
|
-
)
|
60
|
-
|
61
|
-
connection = Neo4jConnectionModel(
|
62
|
-
neo4j_url=self.neo4j_url,
|
63
|
-
neo4j_username=self.neo4j_username,
|
64
|
-
neo4j_password=self.neo4j_password,
|
65
|
-
)
|
66
|
-
|
67
|
-
self.driver = GraphDatabase.driver(
|
68
|
-
connection.neo4j_url,
|
69
|
-
auth=basic_auth(
|
70
|
-
connection.neo4j_username, connection.neo4j_password
|
71
|
-
),
|
72
|
-
)
|
73
|
-
|
74
|
-
logger.debug(
|
75
|
-
f"Connected to {connection.neo4j_url} database successfully"
|
76
|
-
)
|
77
|
-
|
78
|
-
@staticmethod
|
79
|
-
def rename_labels(label):
|
80
|
-
"""Method to replace the invalid character."""
|
81
|
-
invalid_chars = ["-", ":", "(", ")", "."]
|
82
|
-
for invalid_char in invalid_chars:
|
83
|
-
if invalid_char in label:
|
84
|
-
label = label.replace(invalid_char, "__")
|
85
|
-
return label
|
86
|
-
|
87
|
-
# def add_node(
|
88
|
-
# self,
|
89
|
-
# labels: Union[List, None] = None,
|
90
|
-
# properties: Union[Dict, None] = None,
|
91
|
-
# ) -> None:
|
92
|
-
# """Method to add node to the Neo4j database.
|
93
|
-
|
94
|
-
# Args:
|
95
|
-
# labels (Union[List, None]): List of labels to be used for node
|
96
|
-
# properties (Union[Dict, None]): Properties to be used for the node
|
97
|
-
# """
|
98
|
-
# if labels is None:
|
99
|
-
# labels = []
|
100
|
-
|
101
|
-
# if properties is None:
|
102
|
-
# properties = {}
|
103
|
-
|
104
|
-
# labels = ":".join([self.rename_labels(label) for label in labels])
|
105
|
-
# cypher_query = "CREATE (:" + labels + " $properties)"
|
106
|
-
|
107
|
-
# with self.driver.session() as session:
|
108
|
-
# session.write_transaction(
|
109
|
-
# lambda tx: tx.run(cypher_query, properties=properties)
|
110
|
-
# )
|
111
|
-
|
112
|
-
# def add_relationship(
|
113
|
-
# self,
|
114
|
-
# from_node_label: str,
|
115
|
-
# to_node_label: str,
|
116
|
-
# from_node: str,
|
117
|
-
# to_node: str,
|
118
|
-
# relationship_label: str,
|
119
|
-
# relationship_properties: Union[Dict, None] = None,
|
120
|
-
# ) -> None:
|
121
|
-
# """Method to create relationship in graph database.
|
122
|
-
|
123
|
-
# Args:
|
124
|
-
# from_node_label (str): Node label for from node
|
125
|
-
# to_node_label (str): Node label for to node
|
126
|
-
# from_node (str): From node name
|
127
|
-
# to_node (str): To node name
|
128
|
-
# relationship_label (str): Relationship label name
|
129
|
-
# relationship_properties (Union[Dict, None]): Properties to be used
|
130
|
-
# for relationship
|
131
|
-
# """
|
132
|
-
|
133
|
-
# if relationship_properties is None:
|
134
|
-
# relationship_properties = {}
|
135
|
-
|
136
|
-
# cypher_query = (
|
137
|
-
# f"MATCH (a:{from_node_label}),(b:{to_node_label}) WHERE a.name = '{from_node}'"
|
138
|
-
# + f" AND b.name = '{to_node}' CREATE (a)-[:"
|
139
|
-
# + self.rename_labels(relationship_label)
|
140
|
-
# + " $properties]->(b)"
|
141
|
-
# )
|
142
|
-
|
143
|
-
# with self.driver.session() as session:
|
144
|
-
# session.write_transaction(
|
145
|
-
# lambda tx: tx.run(
|
146
|
-
# cypher_query, properties=relationship_properties
|
147
|
-
# )
|
148
|
-
# )
|
149
|
-
|
150
|
-
# def read_query(self, cypher_query: str) -> List:
|
151
|
-
# """Executes a Cypher read query."""
|
152
|
-
|
153
|
-
# with self.driver.session() as session:
|
154
|
-
# result = session.read_transaction(
|
155
|
-
# lambda tx: tx.run(cypher_query).data()
|
156
|
-
# )
|
157
|
-
|
158
|
-
# return result
|
159
|
-
|
160
|
-
def close_driver(self):
|
161
|
-
"""Method to close the driver."""
|
162
|
-
self.driver.close()
|
1
|
+
"""Module contains class and utility functions to manage
|
2
|
+
interactions with Neo4J database.
|
3
|
+
"""
|
4
|
+
|
5
|
+
# standard libraries
|
6
|
+
import os
|
7
|
+
import logging
|
8
|
+
from typing import Union
|
9
|
+
|
10
|
+
|
11
|
+
# third-party libraries
|
12
|
+
from dotenv import load_dotenv
|
13
|
+
from neo4j import GraphDatabase, basic_auth
|
14
|
+
|
15
|
+
# internal imports
|
16
|
+
from erad.db.credential_model import Neo4jConnectionModel
|
17
|
+
|
18
|
+
|
19
|
+
load_dotenv()
|
20
|
+
NEO4J_URL = os.getenv("NEO4J_URL")
|
21
|
+
NEO4J_USERNAME = os.getenv("NEO4J_USERNAME")
|
22
|
+
NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD")
|
23
|
+
|
24
|
+
logger = logging.getLogger(__name__)
|
25
|
+
|
26
|
+
|
27
|
+
class Neo4J:
|
28
|
+
"""Class for managing interaction with Neo4J database.
|
29
|
+
|
30
|
+
Attributes:
|
31
|
+
neo4j_url (str): URL for connecting to Neo4j
|
32
|
+
neo4j_username (str): Username for Neo4j database
|
33
|
+
neo4j_password (str): Password for Neo4j database
|
34
|
+
use_env (bool): True if above info are to be collected
|
35
|
+
from env file.
|
36
|
+
driver (GraphDatabase.driver): Neo4J driver instance
|
37
|
+
"""
|
38
|
+
|
39
|
+
def __init__(
|
40
|
+
self,
|
41
|
+
neo4j_url: Union[str, None] = None,
|
42
|
+
neo4j_username: Union[str, None] = None,
|
43
|
+
neo4j_password: Union[str, None] = None,
|
44
|
+
) -> None:
|
45
|
+
"""Constructor for Neo4J class.
|
46
|
+
|
47
|
+
Args:
|
48
|
+
neo4j_url (str): URL for connecting to Neo4j
|
49
|
+
neo4j_username (str): Username for Neo4j database
|
50
|
+
neo4j_password (str): Password for Neo4j database
|
51
|
+
"""
|
52
|
+
|
53
|
+
self.neo4j_url = NEO4J_URL if NEO4J_URL else neo4j_url
|
54
|
+
self.neo4j_username = (
|
55
|
+
NEO4J_USERNAME if NEO4J_USERNAME else neo4j_username
|
56
|
+
)
|
57
|
+
self.neo4j_password = (
|
58
|
+
NEO4J_PASSWORD if NEO4J_PASSWORD else neo4j_password
|
59
|
+
)
|
60
|
+
|
61
|
+
connection = Neo4jConnectionModel(
|
62
|
+
neo4j_url=self.neo4j_url,
|
63
|
+
neo4j_username=self.neo4j_username,
|
64
|
+
neo4j_password=self.neo4j_password,
|
65
|
+
)
|
66
|
+
|
67
|
+
self.driver = GraphDatabase.driver(
|
68
|
+
connection.neo4j_url,
|
69
|
+
auth=basic_auth(
|
70
|
+
connection.neo4j_username, connection.neo4j_password
|
71
|
+
),
|
72
|
+
)
|
73
|
+
|
74
|
+
logger.debug(
|
75
|
+
f"Connected to {connection.neo4j_url} database successfully"
|
76
|
+
)
|
77
|
+
|
78
|
+
@staticmethod
|
79
|
+
def rename_labels(label):
|
80
|
+
"""Method to replace the invalid character."""
|
81
|
+
invalid_chars = ["-", ":", "(", ")", "."]
|
82
|
+
for invalid_char in invalid_chars:
|
83
|
+
if invalid_char in label:
|
84
|
+
label = label.replace(invalid_char, "__")
|
85
|
+
return label
|
86
|
+
|
87
|
+
# def add_node(
|
88
|
+
# self,
|
89
|
+
# labels: Union[List, None] = None,
|
90
|
+
# properties: Union[Dict, None] = None,
|
91
|
+
# ) -> None:
|
92
|
+
# """Method to add node to the Neo4j database.
|
93
|
+
|
94
|
+
# Args:
|
95
|
+
# labels (Union[List, None]): List of labels to be used for node
|
96
|
+
# properties (Union[Dict, None]): Properties to be used for the node
|
97
|
+
# """
|
98
|
+
# if labels is None:
|
99
|
+
# labels = []
|
100
|
+
|
101
|
+
# if properties is None:
|
102
|
+
# properties = {}
|
103
|
+
|
104
|
+
# labels = ":".join([self.rename_labels(label) for label in labels])
|
105
|
+
# cypher_query = "CREATE (:" + labels + " $properties)"
|
106
|
+
|
107
|
+
# with self.driver.session() as session:
|
108
|
+
# session.write_transaction(
|
109
|
+
# lambda tx: tx.run(cypher_query, properties=properties)
|
110
|
+
# )
|
111
|
+
|
112
|
+
# def add_relationship(
|
113
|
+
# self,
|
114
|
+
# from_node_label: str,
|
115
|
+
# to_node_label: str,
|
116
|
+
# from_node: str,
|
117
|
+
# to_node: str,
|
118
|
+
# relationship_label: str,
|
119
|
+
# relationship_properties: Union[Dict, None] = None,
|
120
|
+
# ) -> None:
|
121
|
+
# """Method to create relationship in graph database.
|
122
|
+
|
123
|
+
# Args:
|
124
|
+
# from_node_label (str): Node label for from node
|
125
|
+
# to_node_label (str): Node label for to node
|
126
|
+
# from_node (str): From node name
|
127
|
+
# to_node (str): To node name
|
128
|
+
# relationship_label (str): Relationship label name
|
129
|
+
# relationship_properties (Union[Dict, None]): Properties to be used
|
130
|
+
# for relationship
|
131
|
+
# """
|
132
|
+
|
133
|
+
# if relationship_properties is None:
|
134
|
+
# relationship_properties = {}
|
135
|
+
|
136
|
+
# cypher_query = (
|
137
|
+
# f"MATCH (a:{from_node_label}),(b:{to_node_label}) WHERE a.name = '{from_node}'"
|
138
|
+
# + f" AND b.name = '{to_node}' CREATE (a)-[:"
|
139
|
+
# + self.rename_labels(relationship_label)
|
140
|
+
# + " $properties]->(b)"
|
141
|
+
# )
|
142
|
+
|
143
|
+
# with self.driver.session() as session:
|
144
|
+
# session.write_transaction(
|
145
|
+
# lambda tx: tx.run(
|
146
|
+
# cypher_query, properties=relationship_properties
|
147
|
+
# )
|
148
|
+
# )
|
149
|
+
|
150
|
+
# def read_query(self, cypher_query: str) -> List:
|
151
|
+
# """Executes a Cypher read query."""
|
152
|
+
|
153
|
+
# with self.driver.session() as session:
|
154
|
+
# result = session.read_transaction(
|
155
|
+
# lambda tx: tx.run(cypher_query).data()
|
156
|
+
# )
|
157
|
+
|
158
|
+
# return result
|
159
|
+
|
160
|
+
def close_driver(self):
|
161
|
+
"""Method to close the driver."""
|
162
|
+
self.driver.close()
|
erad/db/utils.py
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
""" This module contains utility functions
|
2
|
-
utilized throughout the modules and subpackages
|
3
|
-
contained within db subpackage."""
|
4
|
-
|
5
|
-
from neo4j import GraphDatabase
|
6
|
-
|
7
|
-
def _run_read_query(driver:GraphDatabase.driver, cypher_query: str):
|
8
|
-
""" Runs a cypher query and returns result. """
|
9
|
-
|
10
|
-
with driver.session() as session:
|
11
|
-
result = session.read_transaction(
|
12
|
-
lambda tx: tx.run(cypher_query).data()
|
13
|
-
)
|
1
|
+
""" This module contains utility functions
|
2
|
+
utilized throughout the modules and subpackages
|
3
|
+
contained within db subpackage."""
|
4
|
+
|
5
|
+
from neo4j import GraphDatabase
|
6
|
+
|
7
|
+
def _run_read_query(driver:GraphDatabase.driver, cypher_query: str):
|
8
|
+
""" Runs a cypher query and returns result. """
|
9
|
+
|
10
|
+
with driver.session() as session:
|
11
|
+
result = session.read_transaction(
|
12
|
+
lambda tx: tx.run(cypher_query).data()
|
13
|
+
)
|
14
14
|
return result
|