Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ecf7a6f905 | ||
|
|
7b912196e9 | ||
|
|
0dd2023224 | ||
|
|
d4880b417b | ||
|
|
a7ced052dd | ||
|
|
9ac618ebec | ||
|
|
de6339586f | ||
|
|
97c9e2eec3 | ||
|
|
167913aa85 | ||
|
|
18c9785325 | ||
|
|
f4e0888ce2 | ||
|
|
d3266ee50e | ||
|
|
410bcdec4b |
@@ -1,6 +1,8 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
from types import SimpleNamespace
|
||||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from typing import Any, Literal
|
||||
|
||||
@@ -38,6 +40,14 @@ from .model import (
|
||||
TopicWordCloud,
|
||||
TopicWordCloudWord,
|
||||
)
|
||||
from inse.projects.eu_cordis_projects_client import (
|
||||
get_eu_project_ids_by_organization_filters,
|
||||
)
|
||||
from inse.projects.model import (
|
||||
EuCordisProjectType,
|
||||
ProjectType,
|
||||
ProjectsFilteringProperties,
|
||||
)
|
||||
|
||||
TOP_AUTHOR_LIMIT = 10
|
||||
TOPIC_HEATMAP_LIMIT = 25
|
||||
@@ -49,6 +59,30 @@ DashboardPeriod = tuple[int | None, int | None]
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def get_eu_organization_project_contributions(
|
||||
organization_names: list[str],
|
||||
projects_filtering_properties: ProjectsFilteringProperties,
|
||||
) -> SimpleNamespace:
|
||||
project_ids_by_type = await get_eu_project_ids_by_organization_filters(
|
||||
project_types=list(EuCordisProjectType),
|
||||
organization_names=organization_names,
|
||||
)
|
||||
project_ids = {
|
||||
str(project_id)
|
||||
for ids in project_ids_by_type.values()
|
||||
for project_id in ids
|
||||
}
|
||||
return SimpleNamespace(
|
||||
elements=[
|
||||
SimpleNamespace(project_id=project_id)
|
||||
for project_id in sorted(project_ids)
|
||||
],
|
||||
total=len(project_ids),
|
||||
page=projects_filtering_properties.page,
|
||||
size=projects_filtering_properties.size,
|
||||
)
|
||||
|
||||
|
||||
ACADEMIC_TITLE_RE = re.compile(
|
||||
r"^(?:(?:professor(?:in)?|prof|dr)(?:\.-[a-z]+)?\.?|rer\.|nat\.|med\.|ing\.)\s+",
|
||||
re.IGNORECASE,
|
||||
@@ -940,6 +974,7 @@ async def get_top_authors(
|
||||
from_year = datetime.now().year - 4
|
||||
to_year = datetime.now().year
|
||||
period = _dashboard_period(from_year, to_year)
|
||||
aliases = await _institution_aliases(normalized_id, organization_name)
|
||||
authors = await _safe_institution_authors(normalized_id)
|
||||
works = _top_work_authors(authors, period)
|
||||
projects = await _safe_top_authors(
|
||||
@@ -953,8 +988,9 @@ async def get_top_authors(
|
||||
)
|
||||
eu_projects = await _safe_top_authors(
|
||||
"eu_projects",
|
||||
_top_eu_project_publication_authors(),
|
||||
_top_eu_project_publication_authors(normalized_id, aliases, authors),
|
||||
)
|
||||
|
||||
patents = await _safe_top_authors(
|
||||
"patents",
|
||||
_top_patent_authors(
|
||||
@@ -963,10 +999,12 @@ async def get_top_authors(
|
||||
to_year=period[1],
|
||||
),
|
||||
)
|
||||
|
||||
news = await _safe_top_authors(
|
||||
"news",
|
||||
_top_news_authors(authors),
|
||||
)
|
||||
|
||||
return TopAuthorStatistics(
|
||||
institution_id=normalized_id,
|
||||
works=works,
|
||||
@@ -1921,52 +1959,168 @@ async def _top_patent_authors(
|
||||
]
|
||||
|
||||
|
||||
async def _top_eu_project_publication_authors() -> list[TopAuthor]:
|
||||
response = await elasticsearch.search(
|
||||
index=ESIndex.CORDIS_EU_FP7_PUBLICATIONS,
|
||||
async def _top_eu_project_publication_authors(
|
||||
institution_id: str,
|
||||
organization_names: list[str],
|
||||
institution_authors: list[dict[str, Any]],
|
||||
) -> list[TopAuthor]:
|
||||
institution_author_ids = {
|
||||
author_id
|
||||
for author in institution_authors
|
||||
if (author_id := _normalize_author_id(author.get("id")))
|
||||
}
|
||||
if not institution_author_ids:
|
||||
return []
|
||||
|
||||
project_page = await get_eu_organization_project_contributions(
|
||||
organization_names=organization_names,
|
||||
projects_filtering_properties=ProjectsFilteringProperties(
|
||||
project_type=ProjectType.EU,
|
||||
page=0,
|
||||
size=10000,
|
||||
),
|
||||
)
|
||||
|
||||
project_ids = [project.project_id for project in project_page.elements]
|
||||
|
||||
if not project_ids:
|
||||
return []
|
||||
|
||||
publication_response = await elasticsearch.search(
|
||||
index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD,
|
||||
body={
|
||||
"size": 0,
|
||||
"aggs": {
|
||||
"authors": {
|
||||
"terms": {
|
||||
"script": {
|
||||
"lang": "painless",
|
||||
"source": _cordis_publication_authors_script(),
|
||||
},
|
||||
"size": TOP_AUTHOR_LIMIT,
|
||||
"order": {"_count": "desc"},
|
||||
}
|
||||
"size": 10000,
|
||||
"_source": ["projectID", "qaProcessedDoi"],
|
||||
"query": {
|
||||
"terms": {
|
||||
"projectID": project_ids,
|
||||
}
|
||||
},
|
||||
},
|
||||
cluster=ESCluster.INSE,
|
||||
)
|
||||
|
||||
doi_project_ids = _cordis_publication_doi_project_ids(
|
||||
publication_response,
|
||||
set(project_ids),
|
||||
)
|
||||
if not doi_project_ids:
|
||||
return []
|
||||
|
||||
works_response = await elasticsearch.search(
|
||||
index="openalex-works",
|
||||
body={
|
||||
"size": 10000,
|
||||
"_source": [
|
||||
"doi",
|
||||
"authorships.author.id",
|
||||
"authorships.author.display_name",
|
||||
"authorships.institutions.id",
|
||||
],
|
||||
"query": {
|
||||
"bool": {
|
||||
"filter": [
|
||||
{"terms": {"doi": sorted(doi_project_ids)}},
|
||||
_institution_works_query(institution_id),
|
||||
]
|
||||
}
|
||||
},
|
||||
},
|
||||
cluster=ESCluster.NES,
|
||||
)
|
||||
|
||||
return _openalex_work_authors_by_eu_projects(
|
||||
works_response,
|
||||
doi_project_ids,
|
||||
institution_id,
|
||||
institution_author_ids,
|
||||
)
|
||||
|
||||
|
||||
def _cordis_publication_doi_project_ids(
|
||||
response: dict[str, Any],
|
||||
allowed_project_ids: set[str],
|
||||
) -> dict[str, set[str]]:
|
||||
doi_project_ids: dict[str, set[str]] = defaultdict(set)
|
||||
for hit in response.get("hits", {}).get("hits", []):
|
||||
source = hit.get("_source", {})
|
||||
project_id = str(source.get("projectID") or "").strip()
|
||||
if not project_id or project_id not in allowed_project_ids:
|
||||
continue
|
||||
doi = _normalize_doi(source.get("qaProcessedDoi"))
|
||||
if doi:
|
||||
doi_project_ids[doi].add(project_id)
|
||||
return dict(doi_project_ids)
|
||||
|
||||
|
||||
def _normalize_doi(value: Any) -> str | None:
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
return None
|
||||
doi = value.strip().lower()
|
||||
if doi.startswith("https://doi.org/"):
|
||||
return doi
|
||||
if doi.startswith("http://doi.org/"):
|
||||
return "https://" + doi.removeprefix("http://")
|
||||
if doi.startswith("doi.org/"):
|
||||
return f"https://{doi}"
|
||||
return f"https://doi.org/{doi}"
|
||||
|
||||
|
||||
def _openalex_work_authors_by_eu_projects(
|
||||
response: dict[str, Any],
|
||||
doi_project_ids: dict[str, set[str]],
|
||||
institution_id: str,
|
||||
institution_author_ids: set[str],
|
||||
) -> list[TopAuthor]:
|
||||
# Count unique CORDIS projects per author, not publications. Multiple works
|
||||
# (and therefore multiple DOIs) can belong to the same project ID.
|
||||
author_project_ids: dict[str, set[str]] = defaultdict(set)
|
||||
author_names: dict[str, str] = {}
|
||||
|
||||
for hit in response.get("hits", {}).get("hits", []):
|
||||
source = hit.get("_source", {})
|
||||
project_ids = doi_project_ids.get(_normalize_doi(source.get("doi")) or "")
|
||||
if not project_ids:
|
||||
continue
|
||||
|
||||
for authorship in source.get("authorships") or []:
|
||||
if not any(
|
||||
institution.get("id") == institution_id
|
||||
for institution in authorship.get("institutions") or []
|
||||
):
|
||||
continue
|
||||
author = authorship.get("author") or {}
|
||||
author_id = _normalize_author_id(author.get("id"))
|
||||
author_name = str(author.get("display_name") or "").strip()
|
||||
if (
|
||||
not author_id
|
||||
or author_id not in institution_author_ids
|
||||
or not author_name
|
||||
):
|
||||
continue
|
||||
author_project_ids[author_id].update(project_ids)
|
||||
author_names.setdefault(author_id, author_name)
|
||||
|
||||
ranked_authors = sorted(
|
||||
author_project_ids.items(),
|
||||
key=lambda item: (-len(item[1]), author_names.get(item[0], ""), item[0]),
|
||||
)
|
||||
|
||||
return [
|
||||
TopAuthor(
|
||||
name=str(bucket["key"]),
|
||||
count=int(bucket.get("doc_count", 0)),
|
||||
id=_short_author_id(author_id),
|
||||
name=author_names[author_id],
|
||||
count=len(project_ids),
|
||||
)
|
||||
for bucket in (
|
||||
response.get("aggregations", {}).get("authors", {}).get("buckets", [])
|
||||
)
|
||||
if str(bucket.get("key", "")).strip()
|
||||
for author_id, project_ids in ranked_authors[:TOP_AUTHOR_LIMIT]
|
||||
]
|
||||
|
||||
|
||||
def _cordis_publication_authors_script() -> str:
|
||||
return (
|
||||
"def names = new HashSet(); "
|
||||
"def source = params['_source']; "
|
||||
"def value = source.containsKey('authors') ? source['authors'] "
|
||||
" : (source.containsKey('Authors') ? source['Authors'] : null); "
|
||||
"if (value instanceof String && value.length() > 0) { "
|
||||
" for (name in value.splitOnToken(';')) { "
|
||||
" def trimmed = name.trim(); "
|
||||
" if (trimmed.length() > 0) { names.add(trimmed); } "
|
||||
" } "
|
||||
"} "
|
||||
"return new ArrayList(names);"
|
||||
)
|
||||
def _normalize_author_id(value: Any) -> str | None:
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
return None
|
||||
short_id = value.replace("https://openalex.org/", "").strip().upper()
|
||||
return f"https://openalex.org/{short_id}"
|
||||
|
||||
|
||||
async def _top_news_authors(authors: list[dict[str, Any]]) -> list[TopAuthor]:
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Innovation Search Engine API
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
|
||||
Reference in New Issue
Block a user