-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdata_saver.py
948 lines (883 loc) · 38.2 KB
/
data_saver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
#!/usr/bin/python !/usr/bin/env python
# -*- coding: utf-8 -*
# Functions to extract knowledge from medical text. Everything related to
# reading, parsing and extraction needed for the knowledge base. Also,
# some wrappers for SemRep, MetaMap and Reverb.
import json
import os
import py2neo
import unicodecsv as csv2
import pymongo
from config import settings
from utilities import time_log
from data_extractor import chunk_document_collection
from multiprocessing import cpu_count, Pool
suppress_log_to_file = py2neo.watch('neo4j',
level='ERROR', out='./out/neo4j.log')
suppress_log_to_file2 = py2neo.watch('httpstream',
level='ERROR', out='./out/neo4j.log')
def save_json2(json_):
"""
Helper function to save enriched medical json to file.
Input:
- json_: dic,
json-style dictionary generated from the extractors in the
previous phase
"""
# Output file location from settings
outfile = settings['out']['json']['out_path']
with open(outfile, 'w+') as f:
json.dump(json_, f, indent=3)
def save_json(json_):
"""
Helper function to save enriched medical json to file
. Input:
- json_: dic,
json-style dictionary generated from the extractors in the
previous phase
"""
# Output file location from settings
outfile = settings['out']['json']['out_path']
if settings['pipeline']['in']['stream'] or settings['pipeline']['in']['parallel']:
print 'mpainei append'
if os.path.isfile(outfile):
with open(outfile, 'r') as f:
docs1 = json.load(f)[settings['out']['json']['json_doc_field']]
json_[settings['out']['json']['json_doc_field']] = json_[settings['out']['json']['json_doc_field']] + docs1
with open(outfile, 'w+') as f:
json.dump(json_, f, indent=3)
# with open (outfile, mode="r+") as file:
# file.seek(0,2)
# position = file.tell() -1
# file.seek(position)
# file.write( ",{}]".format(json.dumps(dictionary)) )
# with open(outfile, 'a+') as f:
# json1 = json.load(f)
def save_csv(json_):
"""
Helper function to save enriched medical json to file.
Input:
- json_: dic,
json-style dictionary generated from the extractors in the
previous phase
"""
# Output file location from settings
outfile = settings['out']['json']['out_path']
with open(outfile, 'w+') as f:
json.dump(json_, f, indent=3)
def save_neo4j(json_):
"""
Helper function to save enriched medical json to file.
Input:
- json_: dic,
json-style dictionary generated from the extractors in the
previous phase
"""
# Output file location from settings
outfile = settings['out']['json']['out_path']
with open(outfile, 'w+') as f:
json.dump(json_, f, indent=3)
def aggregate_mentions(entity_pmc_edges):
"""
Function to aggregate recurring entity:MENTIONED_IN:pmc relations.
Input:
- entity_pmc_edges: list,
list of dicts as generated by create_neo4j_ functions
Outpu:
- entity_pmc_edges: list,
list of dicts with aggregated values in identical ages
"""
uniques = {}
c = 0
for edge in entity_pmc_edges:
cur_key = str(edge[':START_ID'])+'_'+str(edge[':END_ID'])
flag = False
if cur_key in uniques:
uniques[cur_key]['score:float[]'] = uniques[cur_key]['score:float[]']+';'+edge['score:float[]']
uniques[cur_key]['sent_id:string[]'] = uniques[cur_key]['sent_id:string[]']+';'+edge['sent_id:string[]']
uniques[cur_key]['resource:string[]'] = uniques[cur_key]['resource:string[]']+';'+edge['resource:string[]']
flag = True
else:
uniques[cur_key] = edge
if flag:
c += 1
un_list = []
time_log('Aggregated %d mentions from %d in total' % (c, len(entity_pmc_edges)))
for k, v in uniques.iteritems():
un_list.append(v)
return un_list
def aggregate_relations(relations_edges):
"""
Function to aggregate recurring entity:SEMREP_RELATION:entity relations.
Input:
- relations_edges: list,
list of dicts as generated by create_neo4j_ functions
Outpu:
- relations_edges: list,
list of dicts with aggregated values in identical ages
"""
uniques = {}
c = 0
for edge in relations_edges:
cur_key = str(edge[':START_ID'])+'_'+str(edge[':TYPE'])+'_'+str(edge[':END_ID'])
flag = False
if cur_key in uniques:
if 'sent_id:string[]' in edge.keys():
if edge['sent_id:string[]'] in uniques[cur_key]['sent_id:string[]']:
continue
for field in edge.keys():
if not(field in [':START_ID', ':TYPE', ':END_ID']):
uniques[cur_key][field] = uniques[cur_key][field]+';'+edge[field]
flag = True
else:
uniques[cur_key] = edge
if flag:
c += 1
un_list = []
time_log('Aggregated %d relations from %d in total' % (c, len(relations_edges)))
for k, v in uniques.iteritems():
un_list.append(v)
return un_list
def create_neo4j_results(json_, key='harvester'):
"""
Helper function to call either the create_neo4j_harvester or the
create_neo4j_edges function, according to the type of input.
Input:
- json_: dic,
dictionary-json style generated from the parsers/extractors in the
previous stages
- key: str,
string for denoting which create_neo4j_ function to use
Output:
- results: dic,
json-style dictionary with keys 'nodes' and 'edges' containing
a list of the transformed nodes and edges to be created/updated in
neo4j. Each element in the list has a 'type' field denoting the type
of the node/edge and the 'value' field containg the nodes/edges
"""
if key == 'harvester':
results = create_neo4j_harvester(json_)
elif key == 'edges':
results = create_neo4j_edges(json_)
else:
time_log('Type %s of data not yet supported!' % key)
raise NotImplementedError
return results
def create_neo4j_edges(json_):
"""
Function that takes the edges file as provided and generates the nodes
and relationships entities needed for creating/updating the neo4j database.
Currently supporting:
- Nodes: ['Articles(PMC)', 'Entities(MetaMapConcepts)']
- Edges: ['Relations between Entities']
Input:
- json_: dic,
json-style dictionary generated from the parser in the
previous phase
Output:
- results: dic,
json-style dictionary with keys 'nodes' and 'edges' containing
a list of the transformed nodes and edges to be created/updated in
neo4j. Each element in the list has a 'type' field denoting the type
of the node/edge and the 'value' field containg the nodes/edges
"""
# docfield containing list of elements containing the relations
edgefield = settings['load']['edges']['itemfield']
# field containing the type of the node for the subject
sub_type = settings['load']['edges']['sub_type']
# field containing the source of the node for the subject
sub_source = settings['load']['edges']['sub_source']
# field containing the type of the node for the object
obj_type = settings['load']['edges']['obj_type']
# field containing the source of the node for the object
obj_source = settings['load']['edges']['obj_source']
results = {'nodes':[], 'edges':[{'type':'NEW', 'values':[]}]}
entities_nodes = []
articles_nodes = []
other_nodes_sub = []
other_nodes_obj = []
for edge in json_[edgefield]:
if sub_type == 'Entity':
if not(edge['s'] in entities_nodes):
entities_nodes.append(edge['s'])
elif sub_type == 'Article':
if not(edge['s'] in articles_nodes):
articles_nodes.append(edge['s'])
else:
if not(edge['s'] in other_nodes_sub):
other_nodes_sub.append(edge['s'])
if obj_type == 'Entity':
if not(edge['o'] in entities_nodes):
entities_nodes.append(edge['o'])
elif obj_type == 'Article':
if not(edge['o'] in articles_nodes):
articles_nodes.append(edge['o'])
else:
if not(edge['o'] in other_nodes_obj):
other_nodes_obj.append(edge['o'])
#sub_id_key = next((key for key in edge['s'].keys() if ':ID' in key), None)
#obj_id_key = next((key for key in edge['o'].keys() if ':ID' in key), None)
results['edges'][0]['values'].append({':START_ID':edge['s']['id:ID'], ':TYPE':edge['p'], 'resource:string[]':settings['neo4j']['resource'], ':END_ID':edge['o']['id:ID']})
if entities_nodes:
results['nodes'].append({'type': 'Entity', 'values': entities_nodes})
if articles_nodes:
results['nodes'].append({'type': 'Article', 'values': articles_nodes})
if other_nodes_sub:
results['nodes'].append({'type': sub_type, 'values': other_nodes_sub})
if other_nodes_obj:
results['nodes'].append({'type': obj_type, 'values': other_nodes_obj})
return results
def create_neo4j_harvester(json_):
"""
Function that takes the enriched json_ file and generates the nodes
and relationships entities needed for creating/updating the neo4j database.
Currently supporting:
- Nodes: ['Articles(PMC)', 'Entities(UMLS-Concepts)']
- Edges: ['Relations between Entities', 'Entity:MENTIONED_IN:Article']
Input:
- json_: dic,
json-style dictionary generated from the extractors in the
previous phase
Output:
- results: dic,
json-style dictionary with keys 'nodes' and 'edges' containing
a list of the transformed nodes and edges to be created/updated in
neo4j. Each element in the list has a 'type' field denoting the type
of the node/edge and the 'value' field containg the nodes/edges
"""
# docfield containing list of elements
out_outfield = settings['out']['json']['itemfield']
# textfield to read text from
out_textfield = settings['out']['json']['json_text_field']
# idfield where id of document is stored
out_idfield = settings['out']['json']['json_id_field']
# labelfield where the label is located
out_labelfield = settings['out']['json']['json_label_field']
# Sentence Prefix
sent_prefix = settings['load']['text']['sent_prefix']
if sent_prefix == 'None' or not(sent_prefix):
sent_prefix = ''
entities_nodes = []
unique_sent = {}
articles_nodes = []
entity_pmc_edges = []
relations_edges = []
unique_cuis = []
for doc in json_[out_outfield]:
pmid = doc[out_idfield]
for sent in doc['sents']:
cur_sent_id = str(pmid)+'_' + str(sent_prefix) + '_' + str(sent['sent_id'])
unique_sent[cur_sent_id] = sent['sent_text']
for ent in sent['entities']:
if ent['cuid']:
if not(ent['cuid'] in unique_cuis):
unique_cuis.append(ent['cuid'])
if (type(ent['sem_types']) == list and len(ent['sem_types']) > 1):
sem_types = ';'.join(ent['sem_types'])
elif (',' in ent['sem_types']):
sem_types = ';'.join(ent['sem_types'].split(','))
else:
sem_types = ent['sem_types']
#if not(ent['cuid']):
entities_nodes.append({'id:ID': ent['cuid'],
'label': ent['label'],
'sem_types:string[]': sem_types})
entity_pmc_edges.append({':START_ID': ent['cuid'],
'score:float[]': ent['score'],
'sent_id:string[]': cur_sent_id,
':TYPE':'MENTIONED_IN',
'resource:string[]':settings['neo4j']['resource'],
':END_ID': pmid})
for rel in sent['relations']:
if rel['subject__cui'] and rel['object__cui']:
relations_edges.append({':START_ID': rel['subject__cui'],
'subject_score:float[]': rel['subject__score'],
'subject_sem_type:string[]': rel['subject__sem_type'],
':TYPE': rel['predicate'].replace('(','__').replace(')','__'),
'pred_type:string[]': rel['predicate__type'],
'object_score:float[]': rel['object__score'],
'object_sem_type:string[]': rel['object__sem_type'],
'sent_id:string[]': cur_sent_id,
'negation:string[]': rel['negation'],
'resource:string[]':settings['neo4j']['resource'],
':END_ID': rel['object__cui']})
articles_nodes.append({'id:ID': doc[out_idfield],
'title': doc[out_labelfield],
'journal': doc['journal']})
entity_pmc_edges = aggregate_mentions(entity_pmc_edges)
relations_edges = aggregate_relations(relations_edges)
results = {'nodes': [{'type': 'Entity', 'values': entities_nodes}, {'type': 'Article', 'values': articles_nodes}],
'edges': [{'type': 'relation', 'values': relations_edges}, {'type': 'mention', 'values': entity_pmc_edges}]
}
return results
def create_neo4j_csv(results):
"""
Create csv's for use by the neo4j import tool. Relies on create_neo4j_ functions
output and transforms it to suitable format for automatic importing.
Input:
- results: dic,
json-style dictionary. Check create_neo4j_ function output for
details
Output:
- None just saves the documents in the allocated path as defined
in settings.yaml
"""
outpath = settings['out']['csv']['out_path']
entities_nodes = None
articles_nodes = None
relations_edges = None
entity_pmc_edges = None
other_nodes = []
other_edges = []
for nodes in results['nodes']:
if nodes['type'] == 'Entity':
entities_nodes = nodes['values']
elif nodes['type'] == 'Article':
articles_nodes = nodes['values']
else:
other_nodes.extend(nodes['values'])
for edges in results['edges']:
if edges['type'] == 'relation':
relations_edges = edges['values']
elif edges['type'] == 'mention':
entity_pmc_edges = edges['values']
elif edges['type'] == 'NEW':
other_edges.extend(edges['values'])
dic_ = {
'entities.csv': entities_nodes,
'articles.csv': articles_nodes,
'other_nodes.csv': other_nodes,
'entities_pmc.csv':entity_pmc_edges,
'relations.csv':relations_edges,
'other_edges.csv': other_edges
}
dic_fiels = {
'entities.csv': ['id:ID', 'label', 'sem_types:string[]'],
'articles.csv': ['id:ID', 'title', 'journal','sent_id:string[]'],
'other_nodes.csv': ['id:ID'],
'entities_pmc.csv':[':START_ID','score:float[]','sent_id:string[]', 'resource:string[]', ':END_ID'],
'relations.csv':[':START_ID','subject_score:float[]','subject_sem_type:string[]',':TYPE','pred_type:string[]',
'object_score:float[]','object_sem_type:string[]','sent_id:string[]','negation:string[]', 'resource:string[]', ':END_ID'],
'other_edges.csv':[':START_ID', ':TYPE', 'resource:string[]', ':END_ID']
}
for k, toCSV in dic_.iteritems():
if toCSV:
keys = toCSV[0].keys()
out = os.path.join(outpath, k)
with open(out, 'wb') as output_file:
time_log("Created file %s" % k)
dict_writer = csv2.DictWriter(output_file, fieldnames=dic_fiels[k], encoding='utf-8')
dict_writer.writeheader()
dict_writer.writerows(toCSV)
time_log('Created all documents needed')
def fix_on_create_nodes(node):
"""
Helper function to create the correct cypher string for
querying and merging a new node to the graph. This is used
when no node is matched and a new one has to be created.
Input:
- node: dic,
dictionary of a node generated from some create_neo4j_
function
Output:
- s: string,
part of cypher query, responsible handling the creation of anew node
"""
s = ' '
# Has at least one other attribute to create than id
if len(node.keys())>1:
s = 'ON CREATE SET '
for key, value in node.iteritems():
if (value) and (value != " "):
if 'ID' in key.split(':'):
continue
elif 'string[]' in key:
field = key.split(':')[0]
string_value = '['
for i in value.split(';'):
string_value += '"' + i + '"' + ','
string_value = string_value[:-1] + ']'
s += ' a.%s = %s,' % (field, string_value)
elif 'float[]' in key:
field = key.split(':')[0]
string_value = str([int(i) for i in value.split(';')])
s += ' a.%s = %s,' % (field, string_value)
else:
field = key.split(':')[0]
s += ' a.%s = "%s",' % (field, value.replace('"', "'"))
s = s[:-1]
# No attributes
return s
def create_merge_query(node, type_):
"""
Creating the whole merge and update cypher query for a node.
Input:
- node: dic,
dictionary of a node containing the attributes of the
node
- type_: str,
type of the node to be merged
Output:
- quer: str,
the complete cypher query ready to be run
"""
quer = """
MERGE (a:%s {id:"%s"})
%s""" % (type_, node["id:ID"], fix_on_create_nodes(node))
return quer
def populate_nodes(graph, nodes, type_):
"""
Function that actually calls the cypher query and populates the graph
with nodes of type_, merging on already existing nodes on their id_.
Input:
-graph: py2neo.Graph,
object representing the graph in neo4j. Using py2neo.
- nodes: list,
list of dics containing the attributes of each node
- type_: str,
type of the node to be merged
Output: None, populates the db.
"""
c = 0
total_rel = 0
time_log('~~~~~~ Will create nodes of type: %s ~~~~~~' % type_)
for ent in nodes:
c += 1
quer = create_merge_query(ent, type_)
f = graph.run(quer)
total_rel += f.stats()['nodes_created']
if c % 1000 == 0 and c > 999:
time_log("Process: %d -- %0.2f %%" % (c, 100*c/float(len(nodes))))
time_log('#%s : %d' % (type_, c))
time_log('Finally added %d new nodes!' % total_rel)
def create_edge_query(edge, sub_ent=settings['load']['edges']['sub_type'],
obj_ent=settings['load']['edges']['obj_type']):
"""
Takes as input an edge, in the form of a dictionary, and returns the
corresponding cypher query that:
1) First Matches the start-end nodes and the type of the edge
2) Merges the edge the following way:
- If the edge doesn't exist it creates it setting all attributes
of the edge according to its' values
- If the edge exists, it updates the attributes that are both in the
graph edge and the dictionary and creates the attributes that are not
found in the graph edge but are provided in the edge dictionary
Input:
- edge, dict
dictionary containing the edge properties
- sub_ent, str
string denoting what type is the subject node
- obj_ent, str
string denoting what type is the object node
Output:
- s, string
query string to perform
"""
s = """MATCH (a:%s {id:"%s"}), (b:%s {id:"%s"})
MERGE (a)-[r:%s]->(b)
ON MATCH SET """ % (sub_ent, edge[':START_ID'], obj_ent, edge[':END_ID'], edge[':TYPE'])
for key, value in edge.iteritems():
# Don't see why this check should be here???
# if (value):
if not(('START_ID' in key.split(':')) or ('END_ID' in key.split(':')) or ('TYPE' in key.split(':'))):
if 'string[]' in key:
field = key.split(':')[0]
string_value = '['
for i in value.split(';'):
string_value += '"' + i + '"' + ','
string_value = string_value[:-1] + ']'
elif 'float[]' in key:
field = key.split(':')[0]
# Dealing with empty or non-scored elements
tmp_s = []
for i in value.split(';'):
try:
tmp_s.append(int(i))
except ValueError:
tmp_s.append(0)
string_value = str(tmp_s)
else:
field = key.split(':')[0]
string_value = value.replace('"', "'")
s += ' r.%s = CASE WHEN NOT EXISTS(r.%s) THEN %s ELSE r.%s + %s END,' % (field, field, string_value, field, string_value)
s = s[:-1]
s += ' ON CREATE SET '
for key, value in edge.iteritems():
# Don't see why this check should be here???
# if (value):
if not(('START_ID' in key.split(':')) or ('END_ID' in key.split(':')) or ('TYPE' in key.split(':'))):
if 'string[]' in key:
field = key.split(':')[0]
string_value = '['
for i in value.split(';'):
string_value += '"' + i + '"' + ','
string_value = string_value[:-1] + ']'
elif 'float[]' in key:
field = key.split(':')[0]
# Dealing with empty or non-scored elements
tmp_s = []
for i in value.split(';'):
try:
tmp_s.append(int(i))
except ValueError:
tmp_s.append(0)
string_value = str(tmp_s)
else:
field = key.split(':')[0]
string_value = value.replace('"', "'")
s += ' r.%s = %s,' % (field, string_value)
s = s[:-1]
return s
def populate_relation_edges(graph, relations_edges):
"""
Function to create/merge the relation edges between existing entities.
Input:
- graph: py2neo.Graph,
object representing the graph in neo4j. Using py2neo.
- relations_edges: list,
list of dics containing the attributes of each relation
Output: None, populates the db.
"""
c = 0
total_rel = 0
for edge in relations_edges:
c += 1
quer = """
Match (a:Entity {id:"%s"}), (b:Entity {id:"%s"})
MATCH (a)-[r:%s]->(b)
WHERE "%s" in r.sent_id
Return r;
""" % (edge[':START_ID'], edge[':END_ID'], edge[':TYPE'], edge['sent_id:string[]'].split(';')[0])
f = graph.run(quer)
if len(f.data()) == 0 and edge[':START_ID'] and edge[':END_ID']:
quer = create_edge_query(edge, 'Entity', 'Entity')
# subj_s = '['
# for i in edge['subject_sem_type:string[]'].split(';'):
# subj_s += '"' + i + '"' + ','
# subj_s = subj_s[:-1] + ']'
# obj_s = '['
# for i in edge['object_sem_type:string[]'].split(';'):
# obj_s += '"' + i + '"' + ','
# obj_s = obj_s[:-1] + ']'
# sent_s = '['
# for i in edge['sent_id:string[]'].split(';'):
# sent_s += '"' + i + '"' + ','
# sent_s = sent_s[:-1] + ']'
# neg_s = '['
# for i in edge['negation:string[]'].split(';'):
# neg_s += '"' + i + '"' + ','
# neg_s = neg_s[:-1] + ']'
# sent_res = '['
# for i in edge['resource:string[]'].split(';'):
# sent_res += '"' + i + '"' + ','
# sent_res = sent_res[:-1] + ']'
# quer = """
# Match (a:Entity {id:"%s"}), (b:Entity {id:"%s"})
# MERGE (a)-[r:%s]->(b)
# ON MATCH SET r.subject_score = r.subject_score + %s, r.subject_sem_type = r.subject_sem_type + %s,
# r.object_score = r.object_score + %s, r.object_sem_type = r.object_sem_type + %s,
# r.sent_id = r.sent_id + %s, r.negation = r.negation + %s, r.resource = r.resource + %s
# ON CREATE SET r.subject_score = %s, r.subject_sem_type = %s,
# r.object_score = %s, r.object_sem_type = %s,
# r.sent_id = %s, r.negation = %s, r.resource = %s
# """ % (edge[':START_ID'], edge[':END_ID'], edge[':TYPE'],
# str([int(i) for i in edge['subject_score:float[]'].split(';')]), subj_s,
# str([int(i) for i in edge['object_score:float[]'].split(';')]), obj_s,
# sent_s, neg_s, sent_res, str([int(i) for i in edge['subject_score:float[]'].split(';')]), subj_s,
# str([int(i) for i in edge['object_score:float[]'].split(';')]), obj_s,
# sent_s, neg_s, sent_res)
# print quer
# print '~'*50
# print edge
# quer = """
# Match (a:Entity {id:"%s"}), (b:Entity {id:"%s"})
# MERGE (a)-[r:%s]->(b)
# ON MATCH SET r.object_score = CASE WHEN NOT EXISTS(r.object_score) THEN %s ELSE r.object_score + %s END
# """ % (edge[':START_ID'], edge[':END_ID'], edge[':TYPE'],
# str([int(i) for i in edge['object_score:float[]'].split(';')]), str([int(i) for i in edge['object_score:float[]'].split(';')]))
f = graph.run(quer)
total_rel += f.stats()['relationships_created']
if c % 1000 == 0 and c > 999:
time_log('Process: %d -- %0.2f %%' % (c, 100*c/float(len(relations_edges))))
time_log('#Relations :%d' % c)
time_log('Finally added %d new relations!' % total_rel)
def populate_mentioned_edges(graph, entity_pmc_edges):
"""
Function to create/merge the relation edges between existing entities.
Input:
- graph: py2neo.Graph,
object representing the graph in neo4j. Using py2neo.
- entity_pmc_edges: list,
list of dics containing the attributes of each relation
Output: None, populates the db.
"""
c = 0
total_rel = 0
for edge in entity_pmc_edges:
c += 1
quer = """
Match (a:Entity {id:"%s"}), (b:Article {id:"%s"})
MATCH (a)-[r:%s]->(b)
WHERE "%s" in r.sent_id
Return r;
""" % (edge[':START_ID'], edge[':END_ID'], edge[':TYPE'] , edge['sent_id:string[]'])
f = graph.run(quer)
if len(f.data()) == 0 and edge[':START_ID'] and edge[':END_ID']:
quer = create_edge_query(edge, 'Entity', 'Article')
# sent_s = '['
# for i in edge['sent_id:string[]'].split(';'):
# sent_s += '"' + i + '"' + ','
# sent_s = sent_s[:-1] + ']'
# sent_res = '['
# for i in edge['resource:string[]'].split(';'):
# sent_res += '"' + i + '"' + ','
# sent_res = sent_res[:-1] + ']'
# quer = """
# Match (a:Entity {id:"%s"}), (b:Article {id:"%s"})
# MERGE (a)-[r:MENTIONED_IN]->(b)
# ON MATCH SET r.score = r.score + %s, r.sent_id = r.sent_id + %s, r.resource = r.resource + %s
# ON CREATE SET r.score = %s, r.sent_id = %s, r.resource = %s
# """ % (edge[':START_ID'], edge[':END_ID'],
# str([int(i) for i in edge['score:float[]'].split(';')]), sent_s, sent_res,
# str([int(i) for i in edge['score:float[]'].split(';')]), sent_s, sent_res)
f = graph.run(quer)
total_rel += f.stats()['relationships_created']
if c % 1000 == 0 and c>999:
time_log("Process: %d -- %0.2f %%" % (c, 100*c/float(len(entity_pmc_edges))))
time_log('#Mentions: %d' % c)
time_log('Finally added %d new mentions!' % total_rel)
def populate_new_edges(graph, new_edges):
"""
Function to create/merge an unknwon type of edge.
Input:
- graph: py2neo.Graph,
object representing the graph in neo4j. Using py2neo.
- new_edges: list,
list of dics containing the attributes of each relation
Output: None, populates the db.
"""
c = 0
total_rel = 0
# field containing the type of the node for the subject
sub_type = settings['load']['edges']['sub_type']
# field containing the type of the node for the object
obj_type = settings['load']['edges']['obj_type']
for edge in new_edges:
c += 1
quer = """
Match (a:%s {id:"%s"}), (b:%s {id:"%s"})
MATCH (a)-[r:%s]->(b)
WHERE ("%s" in r.resource)
Return r;
""" % (sub_type, edge[':START_ID'], obj_type, edge[':END_ID'], edge[':TYPE'], settings['neo4j']['resource'])
f = graph.run(quer)
if len(f.data()) == 0 and edge[':START_ID'] and edge[':END_ID']:
quer = create_edge_query(edge, sub_type, obj_type)
# sent_res = '['
# for i in edge['resource:string[]'].split(';'):
# sent_res += '"' + i + '"' + ','
# sent_res = sent_res[:-1] + ']'
# quer = """
# MATCH (a:%s {id:"%s"}), (b:%s {id:"%s"})
# MERGE (a)-[r:%s]->(b)
# ON MATCH SET r.resource = r.resource + %s
# ON CREATE SET r.resource = %s
# """ % (sub_type, edge[':START_ID'], obj_type, edge[':END_ID'],
# edge[':TYPE'], sent_res, sent_res)
# print quer
f = graph.run(quer)
total_rel += f.stats()['relationships_created']
if c % 1000 == 0 and c > 999:
time_log("Process: %d -- %0.2f %%" % (c, 100*c/float(len(new_edges))))
time_log('#Edges: %d' % c)
time_log('Finally added %d new edges!' % total_rel)
def update_neo4j_parallel(results):
"""
Function to create/update a neo4j database according to the nodeg and edges
generated by the create_neo4j_ functions. Change settings.yaml values in
the neo4j group of variables to match your needs.
Input:
- results:
json-style dictionary. Check create_neo4j_ functions output for
details
Output: None, creates/merges the nodes to the wanted database
"""
found = False
for key in ['nodes', 'edges']:
for item in results[key]:
if item['values'] and item['type'] == 'Entity':
found = True
break
if found:
break
if not(found):
time_log('NO NODES/EDGES FOUND! MOVING ON!')
return 1
#c = raw_input()
#if c=='q':
# exit()
#else:
# return
try:
N_THREADS = int(settings['num_cores'])
except:
N_THREADS = cpu_count()
# results = {'nodes': [{'type': 'Entity', 'values': entities_nodes}, {'type': 'Article', 'values': articles_nodes}],
# 'edges': [{'type': 'relation', 'values': relations_edges}, {'type': 'mention', 'values': entity_pmc_edges}]
# }
par_res = [{'nodes': [{} for j in results['nodes']], 'edges': [{} for j in results['edges']]} for i in xrange(N_THREADS)]
# Create mini batches of the results
for i, nodes in enumerate(results['nodes']):
par_nodes = chunk_document_collection(nodes['values'], N_THREADS)
for batch_num in xrange(N_THREADS):
par_res[batch_num]['nodes'][i]['type'] = nodes['type']
par_res[batch_num]['nodes'][i]['values'] = par_nodes[batch_num]
for i, edges in enumerate(results['edges']):
par_edges = chunk_document_collection(edges['values'], N_THREADS)
for batch_num in xrange(N_THREADS):
par_res[batch_num]['edges'][i]['type'] = edges['type']
par_res[batch_num]['edges'][i]['values'] = par_edges[batch_num]
len_col = " | ".join([str(len(b)) for b in par_edges])
time_log('Will break the collection into batches of: %s %s edges!' % (len_col, edges['type']))
pool = Pool(N_THREADS, maxtasksperchild=1)
res = pool.map(update_neo4j_parallel_worker, par_res)
pool.close()
pool.join()
del pool
if sum(res) == N_THREADS:
time_log('Completed parallel update of Neo4j!')
else:
time_log('Something wrong with the parallel execution?')
time_log('Returned %d instead of %d' % (sum(res), N_THREADS))
return 1
def update_neo4j_parallel_worker(results):
"""
Just a worker interface for the different Neo4j update
executions.
Input:
- results:
json-style dictionary. Check create_neo4j_ functions output for
details
Output:
- res : dic,
Output: None, creates/merges the nodes to the wanted database
"""
# Update Neo4j as usual
from pprint import pprint
#pprint(results)
#print('~'*50)
update_neo4j(results)
# Return 1 for everything is ok
return 1
def update_neo4j(results):
"""
Function to create/update a neo4j database according to the nodeg and edges
generated by the create_neo4j_ functions. Change settings.yaml values in
the neo4j group of variables to match your needs.
Input:
- results:
json-style dictionary. Check create_neo4j_ functions output for
details
Output: None, creates/merges the nodes to the wanted database
"""
host = settings['neo4j']['host']
port = settings['neo4j']['port']
user = settings['neo4j']['user']
password = settings['neo4j']['password']
try:
graph = py2neo.Graph(host=host, port=port, user=user, password=password)
except Exception, e:
#time_log(e)
#time_log("Couldn't connect to db! Check settings!")
exit(2)
for nodes in results['nodes']:
populate_nodes(graph, nodes['values'], nodes['type'])
for edges in results['edges']:
if edges['type'] == 'relation':
time_log('~~~~~~ Will create Relations Between Entities ~~~~~~')
populate_relation_edges(graph, edges['values'])
elif edges['type'] == 'mention':
time_log('~~~~~~ Will create Mentioned In ~~~~~~')
populate_mentioned_edges(graph, edges['values'])
elif edges['type'] == 'NEW':
time_log('~~~~~~ Will create new-type of edges~~~~~~')
populate_new_edges(graph, edges['values'])
else:
time_log('Specific node type not handled! You have to update the code!')
raise NotImplementedError
def update_mongo_sentences(json_):
"""
Helper function to save the sentences found in the enriched articles in
mongodb. Connecting to a collection according to settings and then
creating/updating the articles with the sentences found in them.
Input:
- json_: dic,
json-style dictionary generated from the semrep extractor in the
previous phase. Must make sure that there is a field named as indicated
in json_['out']['json']['json_doc_field'], where the documents/articles
are stored and each document/article has a field sents, as expected
in the output of the semrep extractor.
Output:
None, just populates the database
"""
uri = settings['mongo_sentences']['uri']
db_name = settings['mongo_sentences']['db']
collection_name = settings['mongo_sentences']['collection']
client = pymongo.MongoClient(uri)
db = client[db_name]
collection = db[collection_name]
new = 0
upd = 0
docs = json_[settings['out']['json']['itemfield']]
for i, doc in enumerate(docs):
cursor = collection.find({'id': doc['id']})
sents = [{'sent_id': sent['sent_id'], 'text': sent['sent_text']} for sent in doc['sents']]
if cursor.count() == 0:
collection.insert_one({'id': doc['id'], 'sentences': sents})
new += 1
else:
for mongo_doc in cursor:
cur_sent = mongo_doc['sentences']
cur_ids = [s['sent_id'] for s in cur_sent]
new_sent = [s for s in sents if not(s['sent_id'] in cur_ids)]
if new_sent:
cur_sent.extend(new_sent)
mongo_doc['sentences'] = cur_sent
collection.replace_one({'id': doc['id']}, mongo_doc)
upd += 1
if i % 100 == 0 and i > 99:
time_log("Process: %d -- %0.2f %%" % (i, 100*i/float(len(docs))))
time_log('Finally updated %d -- inserted %d documents!' % (upd, new))
def save_mongo(json_):
"""
Helper function to save edges/documents to mongo.
Input:
- json_: dic,
json-style dictionary generated from the transformation modules in the
previous phase. Must make sure that there is a field named as indicated
in settings['out']['json']['json_doc_field'], where the edges/docs
are stored. Specifically for the articles, they are replaced if another
item with the same id is found in the collection.
Output:
None, just populates the database
"""
uri = settings['out']['mongo']['uri']
db_name = settings['out']['mongo']['db']
collection_name = settings['out']['mongo']['collection']
client = pymongo.MongoClient(uri)
db = client[db_name]
collection = db[collection_name]
# Output Idfield
idfield = settings['out']['json']['json_id_field']
docs = json_[settings['out']['json']['itemfield']]
for i, doc in enumerate(docs):
if idfield in doc:
result = collection.replace_one({'id': str(doc[idfield])}, doc, True)
elif 'p' in doc:
result = collection.insert_one(doc)
else:
time_log('Unknown type to persist to mongo')
raise NotImplementedError
if i % 100 == 0 and i > 99:
time_log("Process: %d -- %0.2f %%" % (i, 100*i/float(len(docs))))
return 1