409 lines
14 KiB
Python
409 lines
14 KiB
Python
import datetime
|
|
import logging
|
|
from typing import Any, Dict, List, Optional, Set
|
|
|
|
from inse.projects import projects_search_client
|
|
from inse.projects.projects_search_client import (
|
|
get_eu_organization_project_contributions,
|
|
)
|
|
from inse.projects.model import (
|
|
BundProjectPage,
|
|
CordisEuProjectPage,
|
|
DfgProjectPage,
|
|
EuOrganizationProjectSummaryPage,
|
|
FundingProjectPage,
|
|
InstitutionFunderSummary,
|
|
ProjectType,
|
|
ProjectsFilteringProperties,
|
|
ProjectsSortBy,
|
|
SortDirection,
|
|
CordisEuProject
|
|
)
|
|
from inse.models.dfg_project_details import DfgProjectDetails
|
|
from inse.models.bund_project_details import BundProjectDetails
|
|
from inse.institutions.institutions_search_client import (
|
|
InstitutionsSearchClient,
|
|
)
|
|
from inse.exceptions import NotFoundException
|
|
from inse.persons.logic import get_author_name_by_id
|
|
from inse.projects.logic import get_dfg_author_names_by_ids, get_dfg_institution_names_by_ids
|
|
from inse.works.logic import get_publication_dois_of_author
|
|
from inse.models.projects_eu_cordis.horizon2020.h2020_project_details import H2020ProjectDetails
|
|
from inse.models.projects_eu_cordis.horizon_europe.heu_project_details import HEUProjectDetails
|
|
from inse.models.projects_eu_cordis.fp7.fp7_project_details import Fp7ProjectDetails
|
|
from typing import Union
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def search_all_projects(
|
|
page: int,
|
|
size: int,
|
|
sort_by: ProjectsSortBy,
|
|
sort_direction: SortDirection,
|
|
selected_project_types: Optional[Set[ProjectType]] = None,
|
|
title: Optional[str] = None,
|
|
) -> FundingProjectPage:
|
|
return await projects_search_client.search_all_projects(
|
|
projects_filtering_properties=ProjectsFilteringProperties(
|
|
project_type=ProjectType.EU, # placeholder, shall be ignored in search_all_projects
|
|
page=page,
|
|
size=size,
|
|
sort_by=sort_by,
|
|
sort_direction=sort_direction,
|
|
query=title,
|
|
),
|
|
selected_project_types=selected_project_types,
|
|
)
|
|
|
|
|
|
async def search_bund_projects(
|
|
institutions_search_client: InstitutionsSearchClient,
|
|
filtering_properties: ProjectsFilteringProperties,
|
|
) -> BundProjectPage:
|
|
if filtering_properties.project_type != "bund":
|
|
raise ValueError(
|
|
f"Invalid project type: {filtering_properties.project_type}. Expected 'bund'."
|
|
)
|
|
|
|
logger.debug(
|
|
f"search_bund_projects called with filtering_properties: {filtering_properties}"
|
|
)
|
|
|
|
return await get_bund_projects_by_institution_id(
|
|
institutions_search_client=institutions_search_client,
|
|
filtering_properties=filtering_properties,
|
|
)
|
|
|
|
|
|
async def get_bund_projects_by_institution_id(
|
|
institutions_search_client: InstitutionsSearchClient,
|
|
filtering_properties: ProjectsFilteringProperties,
|
|
) -> BundProjectPage:
|
|
if filtering_properties.raw_oa_institution_id is None:
|
|
raise ValueError("raw_oa_institution_id must be provided")
|
|
|
|
logger.info(
|
|
f"Fetching bund projects for institution_id={filtering_properties.raw_oa_institution_id}, page={filtering_properties.page}, size={filtering_properties.size}"
|
|
)
|
|
institution_aliases = (
|
|
await institutions_search_client.get_institution_aliases_by_oa_institution_id(
|
|
filtering_properties.raw_oa_institution_id
|
|
)
|
|
)
|
|
logger.debug(
|
|
f"get_bund_projects_by_institution_id: Found institution aliases for institution_id={filtering_properties.raw_oa_institution_id}: {institution_aliases}"
|
|
)
|
|
response = await projects_search_client.search_bund_projects(
|
|
institution_names=institution_aliases,
|
|
filter_properties=filtering_properties,
|
|
)
|
|
logger.debug(f"get_bund_projects_by_institution_id response: {response}")
|
|
return response
|
|
|
|
|
|
async def search_cordis_eu_projects(
|
|
institutions_search_client: InstitutionsSearchClient,
|
|
projects_filtering_properties: ProjectsFilteringProperties,
|
|
) -> EuOrganizationProjectSummaryPage:
|
|
if projects_filtering_properties.project_type != "eu":
|
|
raise ValueError(
|
|
f"Invalid project type: {projects_filtering_properties.project_type}. Expected 'eu'."
|
|
)
|
|
|
|
return await get_cordis_eu_projects_by_institution_id(
|
|
institutions_search_client=institutions_search_client,
|
|
projects_filtering_properties=projects_filtering_properties,
|
|
)
|
|
|
|
|
|
async def get_cordis_eu_projects_by_institution_id(
|
|
institutions_search_client: InstitutionsSearchClient,
|
|
projects_filtering_properties: ProjectsFilteringProperties,
|
|
) -> EuOrganizationProjectSummaryPage:
|
|
if not projects_filtering_properties.raw_oa_institution_id:
|
|
raise ValueError(
|
|
"raw_oa_institution_id must be provided to fetch CORDIS EU projects by institution ID"
|
|
)
|
|
|
|
logger.info(
|
|
f"Fetching CORDIS EU projects for institution_id={projects_filtering_properties.raw_oa_institution_id}, page={projects_filtering_properties.page}, size={projects_filtering_properties.size}"
|
|
)
|
|
institution_aliases = (
|
|
await institutions_search_client.get_institution_aliases_by_oa_institution_id(
|
|
projects_filtering_properties.raw_oa_institution_id
|
|
)
|
|
)
|
|
logger.debug(
|
|
f"Found institution aliases for institution_id={projects_filtering_properties.raw_oa_institution_id}: {institution_aliases}"
|
|
)
|
|
|
|
page = await get_eu_organization_project_contributions(
|
|
organization_names=institution_aliases,
|
|
projects_filtering_properties=projects_filtering_properties,
|
|
)
|
|
|
|
logger.debug(f"get_cordis_eu_projects_by_institution_id response: {page}")
|
|
|
|
return page
|
|
|
|
async def get_dfg_projects_by_author_id(
|
|
author_id: Optional[str],
|
|
author_filter_ids: Optional[List[str]],
|
|
institution_ids: Optional[List[str]],
|
|
topics: Optional[List[str]],
|
|
title: Optional[str],
|
|
funding_years: Optional[List[str]],
|
|
page: int,
|
|
size: int,
|
|
sort_by: str = "topic",
|
|
sort_direction: str = "asc",
|
|
) -> DfgProjectPage:
|
|
logger.debug(
|
|
"Fetching DFG projects for author_id=%s, author_filter_ids=%s, "
|
|
"institution_ids=%s, topics=%s, title=%s, funding_years=%s, "
|
|
"page=%s, size=%s, sort_by=%s, sort_direction=%s",
|
|
author_id,
|
|
author_filter_ids,
|
|
institution_ids,
|
|
topics,
|
|
title,
|
|
funding_years,
|
|
page,
|
|
size,
|
|
sort_by,
|
|
sort_direction,
|
|
)
|
|
filter_names = []
|
|
author_name = (
|
|
await get_author_name_by_id(author_id)
|
|
if author_id
|
|
else None
|
|
)
|
|
if author_id and not author_name:
|
|
logger.debug("No author name found for author_id=%s", author_id)
|
|
|
|
filter_names = (
|
|
await get_dfg_author_names_by_ids(author_ids = author_filter_ids)
|
|
if author_filter_ids
|
|
else []
|
|
)
|
|
if author_filter_ids and not filter_names:
|
|
logger.debug(
|
|
"No author names found for author_filter_ids=%s",
|
|
author_filter_ids,
|
|
)
|
|
|
|
if author_name:
|
|
filter_names.append(author_name)
|
|
|
|
institution_names = (
|
|
await get_dfg_institution_names_by_ids(institution_ids)
|
|
if institution_ids
|
|
else []
|
|
)
|
|
|
|
if institution_ids and not institution_names:
|
|
logger.debug(
|
|
"No institutions found for institution_ids=%s",
|
|
institution_ids,
|
|
)
|
|
|
|
return await projects_search_client.search_dfg_projects(
|
|
page=page,
|
|
size=size,
|
|
names=filter_names or [],
|
|
institution_names=institution_names,
|
|
topics=topics,
|
|
title=title,
|
|
funding_years=funding_years,
|
|
sort_by=sort_by,
|
|
sort_direction=sort_direction,
|
|
)
|
|
async def get_institution_cordis_eu_projects_funding_over_time(
|
|
institutions_search_client: InstitutionsSearchClient,
|
|
raw_oa_institution_id: str,
|
|
) -> InstitutionFunderSummary:
|
|
institution_aliases = (
|
|
await institutions_search_client.get_institution_aliases_by_oa_institution_id(
|
|
raw_oa_institution_id
|
|
)
|
|
)
|
|
page = await get_eu_organization_project_contributions(
|
|
organization_names=institution_aliases,
|
|
projects_filtering_properties=ProjectsFilteringProperties(
|
|
project_type=ProjectType.EU,
|
|
page=0,
|
|
size=10000,
|
|
),
|
|
)
|
|
|
|
return _calculate_funding_over_time(
|
|
project_type=ProjectType.EU,
|
|
projects=page.elements,
|
|
funding_start_attr="start_date",
|
|
funding_end_attr="end_date",
|
|
funding_amount_attr="ec_contribution",
|
|
)
|
|
|
|
|
|
async def get_institution_bund_projects_funding_over_time(
|
|
institutions_search_client: InstitutionsSearchClient,
|
|
raw_oa_institution_id: str,
|
|
) -> InstitutionFunderSummary:
|
|
logger.info(
|
|
f"Fetching funding over time for institution_id={raw_oa_institution_id}"
|
|
)
|
|
institution_aliases = (
|
|
await institutions_search_client.get_institution_aliases_by_oa_institution_id(
|
|
raw_oa_institution_id
|
|
)
|
|
)
|
|
logger.debug(
|
|
f"Found institution aliases for institution_id={raw_oa_institution_id}: {institution_aliases}"
|
|
)
|
|
response = await projects_search_client.search_bund_projects(
|
|
filter_properties=ProjectsFilteringProperties(
|
|
project_type=ProjectType.BUND,
|
|
page=0,
|
|
size=10000,
|
|
),
|
|
institution_names=institution_aliases,
|
|
)
|
|
|
|
return _calculate_funding_over_time(
|
|
project_type=ProjectType.BUND,
|
|
projects=response.elements,
|
|
funding_start_attr="funding_start",
|
|
funding_end_attr="funding_end",
|
|
funding_amount_attr="grant_amount",
|
|
)
|
|
|
|
|
|
def _calculate_funding_over_time(
|
|
project_type: ProjectType,
|
|
projects: List[Any],
|
|
funding_start_attr: str,
|
|
funding_end_attr: str,
|
|
funding_amount_attr: str,
|
|
) -> InstitutionFunderSummary:
|
|
funding_per_year: Dict[int, float] = {}
|
|
|
|
for project in projects:
|
|
funding_start = getattr(project, funding_start_attr)
|
|
funding_end = getattr(project, funding_end_attr)
|
|
funding_amount = getattr(project, funding_amount_attr)
|
|
if not funding_start or not funding_end or not funding_amount:
|
|
continue
|
|
|
|
if hasattr(funding_start, "date"):
|
|
funding_start = funding_start.date()
|
|
if hasattr(funding_end, "date"):
|
|
funding_end = funding_end.date()
|
|
|
|
delta = (funding_end - funding_start).days + 1
|
|
if delta <= 0:
|
|
continue
|
|
|
|
amount_per_day = funding_amount / delta
|
|
start_year = funding_start.year
|
|
end_year = funding_end.year
|
|
|
|
for year in range(start_year, end_year + 1):
|
|
if year == start_year:
|
|
year_start = funding_start
|
|
else:
|
|
year_start = datetime.date(year, 1, 1)
|
|
|
|
if year == end_year:
|
|
year_end = funding_end
|
|
else:
|
|
year_end = datetime.date(year, 12, 31)
|
|
|
|
days_in_year = (year_end - year_start).days + 1
|
|
proportional_share = days_in_year * amount_per_day
|
|
|
|
funding_per_year[year] = (
|
|
funding_per_year.get(year, 0.0) + proportional_share
|
|
)
|
|
|
|
sorted_funding_per_year: Dict[int, float] = {}
|
|
|
|
if funding_per_year:
|
|
min_year = min(funding_per_year.keys())
|
|
max_year = max(funding_per_year.keys())
|
|
|
|
for year in range(min_year, max_year + 1):
|
|
sorted_funding_per_year[year] = funding_per_year.get(year, 0.0)
|
|
|
|
total_sum = sum(sorted_funding_per_year.values())
|
|
annual_average_over_period = total_sum / len(sorted_funding_per_year)
|
|
else:
|
|
annual_average_over_period = 0.0
|
|
|
|
return InstitutionFunderSummary(
|
|
type=project_type,
|
|
funding_per_year=sorted_funding_per_year,
|
|
annual_average_over_period=annual_average_over_period,
|
|
)
|
|
|
|
|
|
async def get_single_dfg_project(
|
|
project_id: str,
|
|
) -> DfgProjectDetails:
|
|
logger.debug(f"Fetching single DFG project for id={project_id}")
|
|
|
|
project = await projects_search_client.get_dfg_project_by_id(project_id)
|
|
if not project:
|
|
raise NotFoundException(f"DFG project with id {project_id} not found")
|
|
return project
|
|
|
|
|
|
async def get_single_bund_project(
|
|
project_id: str,
|
|
) -> BundProjectDetails:
|
|
logger.debug(f"Fetching single BUND project for id={project_id}")
|
|
|
|
project = await projects_search_client.get_bund_project_by_id(project_id)
|
|
if not project:
|
|
raise NotFoundException(f"BUND project with id {project_id} not found")
|
|
return project
|
|
|
|
|
|
async def get_single_eu_cordis_project(
|
|
project_id: str,
|
|
) -> Optional[Union[Fp7ProjectDetails, H2020ProjectDetails, HEUProjectDetails]]:
|
|
logger.debug(f"Fetching single EU Cordis project for id={project_id}")
|
|
|
|
project = await projects_search_client.get_eu_cordis_projects_by_id(project_id)
|
|
if not project:
|
|
raise NotFoundException(f"EU project with id {project_id} not found")
|
|
return project
|
|
|
|
|
|
async def search_cordis_eu_projects_for_author_id(
|
|
raw_oa_author_id: str,
|
|
) -> CordisEuProjectPage:
|
|
|
|
# Direct search not possible -> multi stage search
|
|
# 1. Exisiting author has publications with DOIs
|
|
# 2. CORDIS EU projects have associated publications with DOIs
|
|
# 3. Matching DOIs of author publications with DOIs of CORDIS EU project publications -> projects with associated publications of the author
|
|
|
|
dois = await get_publication_dois_of_author(raw_oa_author_id=raw_oa_author_id)
|
|
|
|
logger.debug(f"Found DOIs for author_id={raw_oa_author_id}: {dois}")
|
|
|
|
project_ids = await projects_search_client.get_cordis_eu_project_ids_by_dois(dois=dois)
|
|
|
|
logger.debug(
|
|
f"Found project IDs {project_ids} for author_id={raw_oa_author_id} and dois={dois}"
|
|
)
|
|
|
|
funding_project_page = await projects_search_client.get_cordis_eu_projects_by_project_ids(
|
|
project_ids=project_ids
|
|
)
|
|
|
|
return funding_project_page
|
|
|