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_biorxiv.py 5.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
  1. ##############################################################################################
  2. ########### Cleaning and Preprocessing the bioRxiv publications related to COVID-19 ##########
  3. ##############################################################################################
  4. # The publications' data were collected from bioRxiv API
  5. # (https://api.biorxiv.org/covid19/help) related to COVID-19.
  6. ########################################################################
  7. # Uncomment to install the library.
  8. # %pip install pylatexenc
  9. ########################################################################
  10. ########################################################################
  11. # Importing the required libraries.
  12. import re, numpy as np
  13. from pylatexenc.latex2text import LatexNodes2Text
  14. from preprocess import Preprocess
  15. ########################################################################
  16. class ProcessBiorxiv(Preprocess):
  17. # Cleaning and preprocessing the dataframe.
  18. def _preprocess(self):
  19. # Removing unnecessary columns.
  20. self._dataframe.drop(axis=1,
  21. columns=["rel_num_authors", "version", "license", "type"],
  22. inplace=True)
  23. # Renaming the columns.
  24. columns = {"rel_title": "title", "rel_doi": "doi", "rel_link": "id", "rel_abs": "abstract",
  25. "rel_authors": "author_affil", "rel_date": "publication_date", "rel_site": "source",
  26. "category": "subject_areas"}
  27. self._dataframe.rename(columns=columns, inplace=True)
  28. # Defining the "None" value for the "NaN" values.
  29. self._dataframe.replace({np.nan: None, "none": None, "none.": None, "None": None},
  30. inplace=True)
  31. # Normalizing the feature "id".
  32. self._dataframe.id = self._dataframe.id.apply(lambda x: x.split("/")[-1])
  33. # Normalizing the features "title" and "abstract".
  34. self._dataframe.loc[:, ["title", "abstract"]] = self._dataframe.loc[:, ["title", "abstract"]
  35. ].apply(lambda x: x.apply(lambda y: re.sub("/r/", "",
  36. re.sub("@PER@CENT@", "%", re.sub(r"\^", "",
  37. LatexNodes2Text().latex_to_text(re.sub(r"\s+", " ", re.sub("\\\\?%", "@PER@CENT@",
  38. re.sub("\\\\", "\n", re.sub(r"\\href\{(.+)\}\{(.+)\}", "\g<2> (\g<1>)",
  39. y)))).strip())))) if y else None))
  40. # Normalizing the feature "subject_areas".
  41. self._dataframe.subject_areas = self._dataframe.subject_areas.apply(
  42. lambda x: tuple([x]) if x else None)
  43. # Changing the type of feature "author_affil".
  44. self._dataframe.author_affil = self._dataframe.author_affil.apply(lambda x: eval(x) if x else None)
  45. # Normalizing the feature "author_affil".
  46. self._dataframe.author_affil[self._dataframe.author_affil.notnull()] = [
  47. [{"name": re.sub(r"\s+", " ", LatexNodes2Text().latex_to_text(
  48. re.sub(r"^\"(.+)\"$", "\g<1>", re.sub("^-\s", "", author["author_name"])))),
  49. "affiliation": re.sub(r"\s+", " ", LatexNodes2Text().latex_to_text(
  50. re.sub(r"^\"(.+)\"$", "\g<1>", re.sub("Affiliation:", "",
  51. re.sub(r"[0-9]+\.\s", "", author["author_inst"]), flags=re.IGNORECASE))))}
  52. for author in authors] if len(authors) > 0 else None
  53. for authors in self._dataframe.author_affil[self._dataframe.author_affil.notnull()]]
  54. # Removing the invalid authors and affiliations.
  55. invalid_authors = ["Revision Created", "Revision Converted", "Newly Submitted Revision",
  56. "Final Decision"]
  57. for idx, authors in self._dataframe.author_affil[
  58. self._dataframe.author_affil.notnull()].iteritems():
  59. if authors:
  60. for author in list(authors):
  61. if author["name"].strip() in invalid_authors:
  62. authors.remove(author)
  63. elif not author["affiliation"] or author["affiliation"].lower().replace(
  64. ".", "") == "none":
  65. author["affiliation"] = None
  66. self._dataframe.author_affil[idx] = tuple(authors)
  67. # Creating the authors' and affiliations' IDs.
  68. self._dataframe.author_affil[self._dataframe.author_affil.notnull()] = [tuple([
  69. {"id": str(hash("{} - {}".format(author["name"], self._dataframe.source[idx]))) \
  70. if author["name"] else None,
  71. "name": author["name"] if author["name"] else None,
  72. "affil_id": str(hash("{} - {}".format(author["affiliation"], self._dataframe.source[idx]))) \
  73. if author["affiliation"] else None,
  74. "affiliation": author["affiliation"] if author["affiliation"] else None, "country": None}
  75. for author in authors]) for idx, authors in self._dataframe.author_affil[
  76. self._dataframe.author_affil.notnull()].iteritems()]
  77. # Defining the "None" value for the "NaN" values.
  78. self._dataframe.replace(
  79. {np.nan: None, "none": None, "none.": None, "None": None}, inplace=True)
  80. # Removing the duplicated records by features "title" and "doi".
  81. self._dataframe = self._dataframe.sort_values(
  82. by=["title", "publication_date"]).drop_duplicates(["title", "doi"], "last")
Tip!

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

Comments

Loading...