recordheppubli.py 8.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
""" store_tools.recordheppubli

"""
from filters import CLEAN_COLLABORATION
from pandas import DataFrame
from .recordhep import RecordHep
from store_tools.pluginauthors import PluginAuthors
from store_tools.pluginpublicationinfo import PluginPublicationInfo


class RecordHepPubli(RecordHep, PluginAuthors, PluginPublicationInfo):
    """Article, preprint and proceeding from inspirehep.net version 2.

14
    Schema for publication is documented here:
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
    https://inspire-schemas.readthedocs.io/en/latest/schemas/

    """

    def __init__(self, recjson):

        super().__init__(recjson)

        self._last_fmt_author = "Last, First"
        self._process_authors()
        self._process_publication_info()

    def _process_authors(self):
        """Convert authors information into DataFrame:

        Authors and their affiliations are stored in DataFrame with the
        following structure:

            +---------------+--------------------------------+
            | column        |                                |
            +===============+================================+
            | affiliation   | value separated by "|"         |
            +---------------+--------------------------------+
            | first_name    | first name                     |
            +---------------+--------------------------------+
            | fmt_name      | formated name                  |
            +---------------+--------------------------------+
            | full_name     | Last, First                    |
            +---------------+--------------------------------+
            | last_name     | family name                    |
            +---------------+--------------------------------+
46
            | role          | equal to dir. for phd director |
47 48 49
            +---------------+--------------------------------+

        Note:
50 51 52
            After running this method, the attribute ``df_authors`` is defined.
            It contains one entry with empty strings when the file ``authors``
            is not defined.
53 54 55 56 57 58 59 60 61

        """
        authors = self.get("authors", None)

        if authors is None:
            cols = ["affiliation",
                    "first_name",
                    "fmt_name",
                    "full_name",
62 63
                    "last_name",
                    "role"]
64
            self.df_authors = DataFrame([[""] * len(cols)], columns=cols)
65 66 67 68 69 70 71 72 73
            return

        data = []
        for author in authors:

            affiliations = []
            if "affiliations" in author:
                affiliations = [elt["value"] for elt in author["affiliations"]]

74 75 76
            role = \
                (author["inspire_roles"] if "inspire_roles" in author else [])

77
            full_name = author["full_name"]
78 79 80
            idx = full_name.find(",")
            last_name = full_name[:idx]
            first_name = full_name[idx + 1:].strip()
81 82 83 84 85

            dct = {"affiliation": "|".join(affiliations),
                   "first_name": first_name.strip(),
                   "fmt_name": full_name,
                   "full_name": full_name,
86 87
                   "last_name": last_name.strip(),
                   "role": ", ".join(role)}
88 89 90 91 92 93 94 95 96 97

            data.append(dct)

        df = DataFrame(data)

        # protection against duplicated entries, e.g. twice the first author
        if "full_name" in df.columns:
            df = df.drop_duplicates("full_name")

        # replace
98
        self.df_authors = df
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    def _process_publication_info(self):
        """Convert publication_info into DataFrame:

            Note:
                * the field is a list when there are erratum
                * in some case the subfield year is a list (cds 1951625)

        publication information are stored in DataFrame with the
        following structure:

            +------------+--------------------------------+
            | column     |                                |
            +============+================================+
            | title      | abbreviation of the publisher  |
            +------------+--------------------------------+
            | volume     | volume                         |
            +------------+--------------------------------+
            | year       | year of publication            |
            +------------+--------------------------------+
            | pagination | page number or ranges          |
            +------------+--------------------------------+

        Note:
123 124 125
            * After running this method, the attribute ``df_info``
              is defined. It contains one entry with empty strings
              when the ``publication_info`` field does not exist.
126 127 128 129 130 131 132 133 134 135 136 137 138

            * In order to deal with erratum entry are sorter by year
              and volume.

        """
        data = self.get("publication_info", None)

        if data is None:
            cols = ["title",
                    "volume",
                    "year",
                    "pagination"]

139
            self.df_info = DataFrame([[""] * len(cols)], columns=cols)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

            return

        df = (DataFrame(data)
              .astype({"year": str})
              .rename(columns={"artid": "pagination",
                               "journal_title": "title",
                               "journal_volume": "volume"}))

        columns = df.columns

        # erratum -- sort by year and volume
        if set(["year", "volume"]).issubset(columns):
            df = df.sort_values(["year", "volume"])

        elif "year" in columns:
            df = df.sort_values("year")

        # replace
159
        self.df_info = df
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

    def collaboration(self):
        """The collaboration(s) signing the publication.

        Returns:
            str:
                * collaborations are separated by a comma.
                * The filter CLEAN_COLLABORATION is applied.
                * empty string when not defined

        """
        collaborations = self.get("collaborations", None)

        if collaborations is None:
            return ""

        lst = []

        for elt in collaborations:
            val = elt["value"]
            val = (val if val.endswith("ollaboration")
                   else f"{val} Collaboration")
            lst.append(val)

        return CLEAN_COLLABORATION(", ".join(lst))

    def paper_url(self):
        """The URL of the document.

        Returns:
            str:
                * the string is empty when no URLs are found.
                * first URL is selected when there is more than one

        """
        documents = self.get("documents", None)
        return ("" if documents is None else documents[0]["url"])

    def preprint_number(self):
        """The ArXiv preprint number.

        Returns:
            str:
                * numbers are separated by a comma.
                * empty string when it is not defined.

        """
        lst = self.get("arxiv_eprints", None)

        if lst is None:
            return ""

        lst = [f"arXiv:{elt['value']}" for elt in lst]
        return ", ".join(lst)

    def report_number(self):
        """The report number(s) associated to the publication.

        Returns:
            str:
                - Numbers are separated by a comma
                - Number are sorted in alphabetic order.
                - Empty string when not defined.

        """
        lst = self.get("report_numbers", None)

        if lst is None:
            return ""

        lst = [elt["value"] for elt in lst]
        return ", ".join(lst)

    def submitted(self):
        """The date of submission.

        Returns:
            str:
                * format are"YYYY-MM", "YYYY-MM-DD", "DD MMM YYYY", *etc.*
                * Empty string when not defined.

        """
        val = self.get("preprint_date", None)
        return ("" if val is None else val)

    def title(self):
        """The title of the publication.

        Returns:
            str:
                * Empty string when not defined.
                * The filter CLEAN_SPACES is applied.
                * First one is selectec when ther is more than one

        """
        titles = self.get("titles", None)
        return ("" if titles is None else titles[0]["title"])