pranavvmurthy26 commited on
Commit
a7e2173
·
verified ·
1 Parent(s): eda5512

Create pubmed.py

Browse files
Files changed (1) hide show
  1. pubmed.py +43 -0
pubmed.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # libraries
2
+ from datasets import load_dataset
3
+ import os
4
+ import json
5
+
6
+ # load all of pubmed -- note that this dataset is 360GB+, so I'm loading this in "streaming mode" -- https://huggingface.co/docs/datasets/stream
7
+ # https://huggingface.co/datasets/pubmed
8
+ full_pubmed = load_dataset('/home/sagemaker-user/pubmed_dataset_src/', streaming=True)
9
+
10
+ # make directory if it doesn't exist
11
+ if not os.path.isdir('pubmed_portions'):
12
+ os.mkdir('pubmed_portions')
13
+
14
+ # download pubmed in portions
15
+ portion_size = 1000 # 1 mil entries per portion
16
+ pubmed_portion = []
17
+ counter = 0
18
+
19
+ # note that I'm only taking the pmid, year, abstract text, abstract title, and authors. There is some more information that you can see by either visiting (https://huggingface.co/datasets/pubmed) or printing one of the entries in the dataset
20
+ for idx, entry in enumerate(full_pubmed['train']):
21
+ try:
22
+ # print(entry)
23
+ pmid = entry['MedlineCitation']['PMID']
24
+ year = entry['MedlineCitation']['DateCompleted']['Year']
25
+ abstract_text = entry['MedlineCitation']['Article']['Abstract']['AbstractText']
26
+ abstract_title = entry['MedlineCitation']['Article']['ArticleTitle']
27
+ abstract_authors_list = entry['MedlineCitation']['Article']['AuthorList']['Author']['LastName']
28
+
29
+ if len(abstract_text) > 20 and len(abstract_title) > 5 and len(abstract_authors_list) > 0: # check that the abstract text, title, or authors are not empty
30
+ pubmed_portion.append({'pmid':pmid, 'year':year, 'abstract_text':abstract_text, 'abstract_title':abstract_title, 'abstract_authors_list':abstract_authors_list})
31
+
32
+ if idx % 10000 == 0:
33
+ print('idx', idx)
34
+
35
+ if idx % portion_size == 0 and len(pubmed_portion)>0:
36
+ # save the pubmed portion
37
+ with open(f'pubmed_portions/pubmed_portion_{counter}.json', 'w') as fp:
38
+ json.dump(pubmed_portion, fp)
39
+ # reset the list
40
+ pubmed_portion = []
41
+ counter += 1
42
+ except: # any issue with the article
43
+ pass