Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

preprocess_final.py 7.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. ##############################################################################################
  2. ###### Cleaning and Preprocessing the final dataset of publications related to COVID-19 ######
  3. ##############################################################################################
  4. ########################################################################
  5. # Importing the required libraries.
  6. import pandas as pd, numpy as np
  7. from preprocess import Preprocess
  8. from datetime import date
  9. ########################################################################
  10. class ProcessFinal(Preprocess):
  11. # Function to normalize the affiliations of the authors.
  12. @staticmethod
  13. def __normalize_affiliations(row):
  14. # Getting missing values within the feature "author_affil" from "affiliations" one.
  15. if row.affiliations and row.author_affil:
  16. for pos, author in enumerate(row.author_affil):
  17. for affil in row.affiliations:
  18. if affil["id"] and author["affil_id"] and affil["id"] in [af.strip()
  19. for af in author["affil_id"].split(",")]:
  20. row.author_affil[pos]["affil_id"] = affil["id"]
  21. row.author_affil[pos]["affiliation"] = affil["affiliation"]
  22. if affil["country"] and not author["country"]:
  23. row.author_affil[pos]["country"] = affil["country"]
  24. elif affil["country"] != author["country"]:
  25. row.author_affil[pos]["country"] = affil["country"]
  26. else:
  27. # Getting missing values within the feature "affiliations" from "author_affil" one.
  28. if row.author_affil:
  29. affils = set([(author["affil_id"], author["affiliation"], author["country"])
  30. for author in row.author_affil
  31. if author["affil_id"] or author["affiliation"]])
  32. if len(affils) > 0:
  33. keys = ["id", "affiliation", "country"]
  34. row.affiliations = tuple([dict(zip(keys, affil)) for affil in affils])
  35. else:
  36. row.affiliations = None
  37. return row
  38. # Function to normalize the name of the authors.
  39. @staticmethod
  40. def __normalize_name_authors(row):
  41. if row.authors and row.author_affil:
  42. for pos, item in enumerate(row.authors):
  43. for author in list(row.author_affil):
  44. if item["id"] == author["id"]:
  45. row.authors[pos]["name"] = author["name"]
  46. elif row.author_affil:
  47. authors = set([(author["id"], author["name"]) for author in row.author_affil
  48. if author["name"]])
  49. if len(authors) > 0:
  50. keys = ["id", "name"]
  51. row.authors = tuple([dict(zip(keys, author)) for author in authors])
  52. else:
  53. row.authors = None
  54. return row
  55. # Function to normalize the the authors and their affiliations.
  56. @staticmethod
  57. def __normalize_features(row):
  58. fields = {
  59. "authors": ["id", "name"],
  60. "affiliations": ["id", "affiliation", "country"],
  61. "affil": ["affil_id", "affiliation", "country"]
  62. }
  63. # Normalizing the authors.
  64. records = [tuple([item[f] for f in fields["authors"]]) for item in row.authors] \
  65. if row.authors else []
  66. if row.author_affil:
  67. records = set([*records, *[tuple([item[c] for c in fields["authors"]])
  68. for item in row.author_affil
  69. if item["id"] and item["name"]]])
  70. elif len(records) > 0 and not row.author_affil:
  71. row.author_affil = tuple([{**dict(zip(fields["authors"], auth)), "affil_id": None,
  72. "affiliation": None, "country": None} for auth in records])
  73. if len(records) > 0:
  74. row.authors = tuple([dict(zip(fields["authors"], auth)) for auth in records])
  75. # Normalizing the affiliations.
  76. if row.affiliations:
  77. records = [tuple([item[c] for c in fields["affiliations"]])
  78. for item in row.affiliations]
  79. if row.author_affil:
  80. records = set([*records, *[tuple([item[c] for c in fields["affil"]])
  81. for item in row.author_affil
  82. if item["affil_id"] or item["affiliation"]]])
  83. row.affiliations = tuple([dict(zip(fields["affiliations"], affil))
  84. for affil in records])
  85. return row
  86. # Cleaning and preprocessing the final dataset.
  87. def _preprocess(self):
  88. # Defining the "None" value for the "NaN" values.
  89. self._dataframe.replace({np.nan: None}, inplace=True)
  90. # Changing the type of features.
  91. self._dataframe.loc[:, ["auth_keywords", "index_terms", "affiliations",
  92. "subject_areas", "authors", "author_affil", "references"]] = \
  93. self._dataframe.loc[:, ["auth_keywords", "index_terms", "affiliations",
  94. "subject_areas", "authors", "author_affil", "references"]].apply(
  95. lambda x: x.apply(lambda y: eval(y) if y else None))
  96. self._dataframe.publication_date = pd.to_datetime(self._dataframe.publication_date)
  97. # Filtering the data from the start period of COVID-19 pandemic.
  98. self._dataframe = self._dataframe[
  99. self._dataframe.publication_date.dt.date >= date(2019, 12, 1)]
  100. # Creating the feature "period" from the feature "publication_date".
  101. self._dataframe.loc[self._dataframe.period.isnull(), "period"] = self._dataframe.loc[
  102. self._dataframe.period.isnull(), "publication_date"].apply(
  103. lambda x: x.strftime("%Y-%m"))
  104. # Defining the "zero" value for the articles without numbers of citation and references.
  105. self._dataframe.citation_num.loc[self._dataframe.citation_num.isnull()] = 0
  106. self._dataframe.ref_count.loc[self._dataframe.ref_count.isnull()] = 0
  107. # Applying the function "normalize_name_authors" to the data.
  108. self._dataframe[["authors", "author_affil"]] = self._dataframe[
  109. ["authors", "author_affil"]].apply(ProcessFinal.__normalize_name_authors, axis=1)
  110. # Applying the function "normalize_affiliations" to the data.
  111. self._dataframe[["affiliations", "author_affil"]] = self._dataframe[
  112. ["affiliations", "author_affil"]].apply(ProcessFinal.__normalize_affiliations, axis=1)
  113. # Applying the function "normalize_features" to the data.
  114. self._dataframe[["authors", "affiliations", "author_affil"]] = self._dataframe[
  115. ["authors", "affiliations", "author_affil"]].apply(
  116. ProcessFinal.__normalize_features, axis=1)
  117. # Normalizing the feature "id".
  118. self._dataframe.loc[
  119. self._dataframe.pubmed_id.notnull() & self._dataframe.id.isnull(), "id"] = \
  120. self._dataframe.pubmed_id[
  121. self._dataframe.pubmed_id.notnull() & self._dataframe.id.isnull()]
  122. # Removing the feature "pubmed_id".
  123. self._dataframe.drop(columns="pubmed_id", inplace=True)
  124. # Defining the "None" value for the "NaN" values.
  125. self._dataframe.replace({np.nan: None}, inplace=True)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...