Skip to content

Commit c1c76cd

Browse files
committed
Merge branch 'master' of https://github.com/ml4ai/delphi
2 parents 31cb7d4 + fbb7fc3 commit c1c76cd

File tree

10 files changed

+66
-68
lines changed

10 files changed

+66
-68
lines changed

apt.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
default-jre
12
python3-dev
23
graphviz
34
libgraphviz-dev

data/postBuild

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
jupyter contrib nbextension install --user
2+
jupyter nbextension enable --py widgetsnbextension
3+
jupyter nbextension enable toc2/main
4+
5+
# Notebooks w/ extensions that auto-run code must be "trusted" to work the first
6+
# time
7+
jupyter trust notebooks/*.ipynb

delphi/AnalysisGraph.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def infer_transition_model(
114114
if adjective_data is None:
115115
adjective_data = adjectiveData
116116

117-
gb = pd.read_csv(adjectiveData, delim_whitespace=True).groupby(
117+
gb = pd.read_csv(adjective_data, delim_whitespace=True).groupby(
118118
"adjective"
119119
)
120120

delphi/assembly.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import datetime
22
from delphi.paths import concept_to_indicator_mapping, data_dir
3-
from .utils import exists, flatMap, flatten
3+
from .utils import exists, flatMap, flatten, get_data_from_url
44
from .random_variables import Delta, Indicator
55
from typing import *
66
from indra.statements import Influence, Concept
@@ -229,8 +229,9 @@ def get_data(filename: str) -> pd.DataFrame:
229229
return df
230230

231231

232-
def get_mean_precipitation(year: int, cycles_output=data_dir + "/weather.dat"):
233-
df = pd.read_table(cycles_output)
232+
def get_mean_precipitation(year: int):
233+
url="http://vision.cs.arizona.edu/adarsh/export/demos/data/weather.dat"
234+
df = pd.read_table(get_data_from_url(url))
234235
df.columns = df.columns.str.strip()
235236
df.columns = [c + f" ({df.iloc[0][c].strip()})" for c in df.columns]
236237
df.drop([0], axis=0, inplace=True)
@@ -278,11 +279,12 @@ def process_variable_name(x: str):
278279
return " ".join(xs[0:2])
279280

280281

281-
def construct_concept_to_indicator_mapping(n: int = 2) -> Dict[str, List[str]]:
282+
def construct_concept_to_indicator_mapping(n: int = 2,
283+
mapping=concept_to_indicator_mapping) -> Dict[str, List[str]]:
282284
""" Create a dictionary mapping high-level concepts to low-level indicators """
283285

284286
df = pd.read_table(
285-
concept_to_indicator_mapping,
287+
mapping,
286288
usecols=[1, 3, 4],
287289
names=["Concept Grounding", "Indicator Grounding", "Score"],
288290
)

delphi/export.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
import pickle
1111
from .execution import construct_default_initial_state
1212
from datetime import datetime
13+
import platform
14+
15+
operating_system = platform.system()
16+
17+
if operating_system == "Darwin":
18+
font = "Gill Sans"
19+
elif operating_system == "Windows":
20+
font = "Candara"
21+
else:
22+
font = "Ubuntu"
1323

1424
# ==========================================================================
1525
# Export
@@ -33,7 +43,7 @@ def to_agraph(G, *args, **kwargs) -> AGraph:
3343
"dpi": 227,
3444
"fontsize": 20,
3545
"rankdir": kwargs.get("rankdir", "TB"),
36-
"fontname": "Gill Sans",
46+
"fontname": font,
3747
}
3848
)
3949

@@ -42,7 +52,7 @@ def to_agraph(G, *args, **kwargs) -> AGraph:
4252
"shape": "rectangle",
4353
"color": "#650021",
4454
"style": "rounded",
45-
"fontname": "Gill Sans",
55+
"fontname": font,
4656
}
4757
)
4858

delphi/parameterization.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,17 @@
77

88

99
def parameterize(
10-
G: AnalysisGraph, time: datetime, data: Optional[pd.DataFrame] = None
10+
G: AnalysisGraph, time: datetime, data = south_sudan_data
1111
) -> AnalysisGraph:
1212
""" Parameterize the analysis graph.
1313
1414
Args:
1515
G
1616
time
17-
data
17+
datafile
1818
"""
1919

20-
if data is not None:
21-
G.data = data
22-
else:
23-
if G.data is None:
24-
G.data = get_data(south_sudan_data)
25-
else:
26-
pass
20+
G.data = get_data(data)
2721

2822
nodes_with_indicators = [
2923
n for n in G.nodes(data=True) if n[1]["indicators"] is not None

delphi/program_analysis/scopes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def to_agraph(self):
8282
elif operating_system == "Windows":
8383
font = "Consolas"
8484
else:
85-
font = "Courier"
85+
font = "Ubuntu Mono"
8686

8787
A.node_attr["fontname"] = font
8888
A.graph_attr["fontname"] = font

delphi/quantification.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def map_concepts_to_indicators(
11-
G: AnalysisGraph, n: int = 1, manual_mapping: Optional[dict] = None
11+
G: AnalysisGraph, n: int = 1, mapping_file: Optional[str] = None
1212
) -> AnalysisGraph:
1313
""" Add indicators to the analysis graph.
1414
@@ -17,14 +17,11 @@ def map_concepts_to_indicators(
1717
n
1818
manual_mapping
1919
"""
20-
mapping = construct_concept_to_indicator_mapping(n=n)
20+
mapping = construct_concept_to_indicator_mapping(n, mapping_file)
2121

2222
for n in G.nodes(data=True):
2323
n[1]["indicators"] = get_indicators(
2424
n[0].lower().replace(" ", "_"), mapping
2525
)
26-
if manual_mapping is not None:
27-
if n[0] in manual_mapping:
28-
n[1]["indicators"] = manual_mapping[n[0]]
2926

3027
return G

delphi/utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ def download_file(url: str, filename: str):
281281
request.urlretrieve(url, filename, reporthook)
282282

283283

284+
def get_data_from_url(url: str):
285+
return request.urlopen(url)
286+
287+
284288
def _change_directory(destination_directory):
285289
cwd = os.getcwd()
286290
os.chdir(destination_directory)

notebooks/PI Meeting 2018 Demo.ipynb

+28-45
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@
6565
"metadata": {},
6666
"outputs": [],
6767
"source": [
68-
"import urllib.request\n",
68+
"from delphi.utils import get_data_from_url\n",
6969
"import pickle\n",
7070
"url = 'http://vision.cs.arizona.edu/adarsh/export/demos/data/pi_mtg_demo.pkl'\n",
71-
"with urllib.request.urlopen(url) as f:\n",
72-
" sts = pickle.load(f) "
71+
"sts = pickle.load(get_data_from_url(url)) "
7372
]
7473
},
7574
{
@@ -195,6 +194,16 @@
195194
"## Mapping concepts to indicators"
196195
]
197196
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": null,
200+
"metadata": {},
201+
"outputs": [],
202+
"source": [
203+
"%load_ext autoreload\n",
204+
"%autoreload 2"
205+
]
206+
},
198207
{
199208
"cell_type": "code",
200209
"execution_count": null,
@@ -206,7 +215,9 @@
206215
"outputs": [],
207216
"source": [
208217
"from delphi.quantification import map_concepts_to_indicators\n",
209-
"map_concepts_to_indicators(G, 2)\n",
218+
"url = 'http://vision.cs.arizona.edu/adarsh/export/demos/data/concept_to_indicator_mapping.txt'\n",
219+
"mapping = get_data_from_url(url)\n",
220+
"map_concepts_to_indicators(G, 2, mapping)\n",
210221
"visualize(G, indicators=True)"
211222
]
212223
},
@@ -233,6 +244,7 @@
233244
"cell_type": "code",
234245
"execution_count": null,
235246
"metadata": {
247+
"scrolled": false,
236248
"slideshow": {
237249
"slide_type": "subslide"
238250
}
@@ -242,7 +254,8 @@
242254
"from datetime import datetime\n",
243255
"from delphi.parameterization import parameterize\n",
244256
"date = datetime(2014, 1, 1)\n",
245-
"parameterize(G, datetime(2014, 1,1))\n",
257+
"url = 'http://vision.cs.arizona.edu/adarsh/export/demos/data/south_sudan_data.csv'\n",
258+
"parameterize(G, datetime(2014, 1,1), get_data_from_url(url))\n",
246259
"visualize(G, indicators=True, indicator_values = True,\n",
247260
" graph_label=f'Causal Analysis Graph for South Sudan, {date.year}')"
248261
]
@@ -258,15 +271,6 @@
258271
"## Infer transition model"
259272
]
260273
},
261-
{
262-
"cell_type": "code",
263-
"execution_count": null,
264-
"metadata": {},
265-
"outputs": [],
266-
"source": [
267-
"jt.display_image('images/delphi_model.png')"
268-
]
269-
},
270274
{
271275
"cell_type": "code",
272276
"execution_count": null,
@@ -277,7 +281,8 @@
277281
},
278282
"outputs": [],
279283
"source": [
280-
"%time G.infer_transition_model(res=10000)"
284+
"url = 'http://vision.cs.arizona.edu/adarsh/export/demos/data/adjectiveData.tsv'\n",
285+
"G.infer_transition_model(get_data_from_url(url), 10000)"
281286
]
282287
},
283288
{
@@ -317,16 +322,6 @@
317322
"## Execute model"
318323
]
319324
},
320-
{
321-
"cell_type": "code",
322-
"execution_count": null,
323-
"metadata": {},
324-
"outputs": [],
325-
"source": [
326-
"%load_ext autoreload\n",
327-
"%autoreload 2"
328-
]
329-
},
330325
{
331326
"cell_type": "code",
332327
"execution_count": null,
@@ -422,25 +417,7 @@
422417
"outputs": [],
423418
"source": [
424419
"url = 'http://vision.cs.arizona.edu/adarsh/export/demos/data/pi_mtg_demo_unfiltered.pkl'\n",
425-
"with urllib.request.urlopen(url) as f:\n",
426-
" sts = pickle.load(f) "
427-
]
428-
},
429-
{
430-
"cell_type": "code",
431-
"execution_count": null,
432-
"metadata": {
433-
"slideshow": {
434-
"slide_type": "subslide"
435-
}
436-
},
437-
"outputs": [],
438-
"source": [
439-
"from delphi.subgraphs import *\n",
440-
"G = AnalysisGraph.from_statements(sts)\n",
441-
"merge_nodes(G, 'food_security', 'food_insecurity', same_polarity=False)\n",
442-
"G = get_subgraph_for_concept(G, 'food_insecurity', depth_limit=1)\n",
443-
"visualize(G, nodes_to_highlight='food_insecurity')"
420+
"sts=pickle.load(get_data_from_url(url))"
444421
]
445422
},
446423
{
@@ -453,6 +430,7 @@
453430
},
454431
"outputs": [],
455432
"source": [
433+
"from delphi.subgraphs import get_subgraph_for_concept_pair\n",
456434
"G = AnalysisGraph.from_statements(sts)\n",
457435
"merge_nodes(G, 'food_security', 'food_insecurity', same_polarity=False)\n",
458436
"G = get_subgraph_for_concept_pair(G, 'drought', 'food_insecurity', cutoff=2)\n",
@@ -798,7 +776,12 @@
798776
"title_cell": "Table of Contents",
799777
"title_sidebar": "Contents",
800778
"toc_cell": false,
801-
"toc_position": {},
779+
"toc_position": {
780+
"height": "calc(100% - 180px)",
781+
"left": "10px",
782+
"top": "150px",
783+
"width": "165px"
784+
},
802785
"toc_section_display": true,
803786
"toc_window_display": true
804787
}

0 commit comments

Comments
 (0)