Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a function to calculate cited items per year #17

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/bibx/_entities/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def citation_pairs(self) -> Iterable[Tuple[Article, Article]]:
else:
yield article, reference

@property
def primary_year(self) -> int:
"""
Returns the year of the first article in the collection.

:return: an integer.
"""
return min([article.year for article in self.articles])

def published_by_year(self) -> Dict[int, int]:
"""
Returns a dictionary where the key is the year of publication and the value is the number of articles
Expand All @@ -48,9 +57,8 @@ def published_by_year(self) -> Dict[int, int]:
:return: a dictionary with the number of articles published each year.
"""
current_year = datetime.date.today().year
primary_year = min([article.year for article in self.articles])
years = {}
for year in range(primary_year, current_year + 1):
for year in range(self.primary_year, current_year + 1):
years[year] = 0

for article in self.articles:
Expand All @@ -73,3 +81,26 @@ def merge(self, other: "Collection") -> "Collection":
merged = self.articles[:]
merged.extend(a for a in other.articles if a.key not in keys)
return Collection(merged)

def cited_by_year(self) -> Dict[int, int]:
"""
Returns a dictionary where the key is the year of publication and the value is the number of
citations in that year. The dictionary starts from the oldest article to the current year consecutively.
If a year has no citations the value will be zero.

:return: a dictionary with the number of citations each year.
"""
current_year = datetime.date.today().year
cited_items_per_year = {}
for year in range(self.primary_year, current_year + 1):
cited_items_per_year[year] = 0

for article in self.articles:
if article.times_cited is None:
continue
if article.year in cited_items_per_year:
cited_items_per_year[article.year] += article.times_cited
else:
cited_items_per_year[article.year] = article.times_cited

return cited_items_per_year
279 changes: 151 additions & 128 deletions tests/entities/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,136 +1,145 @@
from bibx._entities.article import Article
from bibx._entities.collection import Collection

articles = [
Article(
authors=["A"],
year=2010,
title="Aa",
journal="Aaa",
volume="1",
page="10",
doi="1",
times_cited=0,
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["B"],
year=2000,
title="Bb",
journal="Abb",
volume="2",
page="11",
doi="12",
times_cited=2,
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["C"],
year=2021,
title="Cc",
journal="Acc",
volume="3",
page="12",
doi="13",
times_cited=12,
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["D"],
year=2022,
title="Dd",
journal="Add",
volume="4",
page="13",
doi="14",
times_cited=2,
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["E"],
year=2005,
title="Ee",
journal="Aee",
volume="5",
page="14",
doi="15",
times_cited=0,
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["F"],
year=2005,
title="Ff",
journal="Aff",
volume="6",
page="15",
doi="16",
times_cited=None,
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["J"],
year=2010,
title="Jj",
journal="Ajj",
volume="7",
page="16",
doi="17",
times_cited=1,
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["H"],
year=2000,
title="Hh",
journal="Ahh",
volume="8",
page="17",
doi="18",
times_cited=20,
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["I"],
year=2021,
title="Ii",
journal="Aii",
volume="9",
page="18",
doi="19",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
]


def test_published_by_year():
articles = [
Article(
authors=["A"],
year=2010,
title="Aa",
journal="Aaa",
volume="1",
page="10",
doi="1",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["B"],
year=2000,
title="Bb",
journal="Abb",
volume="2",
page="11",
doi="12",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["C"],
year=2021,
title="Cc",
journal="Acc",
volume="3",
page="12",
doi="13",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["D"],
year=2022,
title="Dd",
journal="Add",
volume="4",
page="13",
doi="14",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["E"],
year=2005,
title="Ee",
journal="Aee",
volume="5",
page="14",
doi="15",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["F"],
year=2005,
title="Ff",
journal="Aff",
volume="6",
page="15",
doi="16",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["J"],
year=2010,
title="Jj",
journal="Ajj",
volume="7",
page="16",
doi="17",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["H"],
year=2000,
title="Hh",
journal="Ahh",
volume="8",
page="17",
doi="18",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
Article(
authors=["I"],
year=2021,
title="Ii",
journal="Aii",
volume="9",
page="18",
doi="19",
references=[],
keywords=[],
sources=[],
extra={},
_label=None,
),
]
collection = Collection(articles=articles)

res = collection.published_by_year()
Expand All @@ -142,3 +151,17 @@ def test_published_by_year():
assert res.get(2021) == 2
assert res.get(2022) == 1
assert res.get(2023) == 0


def test_cited_by_year():
collection = Collection(articles=articles)

res = collection.cited_by_year()
assert res.get(2000) == 22
assert res.get(2001) == 0
assert res.get(2002) == 0
assert res.get(2005) == 0
assert res.get(2010) == 1
assert res.get(2021) == 12
assert res.get(2022) == 2
assert res.get(2023) == 0