Files
inse-internal-external-scripts/analysis/statistics_analysis.py
T
2023-10-09 09:58:24 +02:00

550 lines
20 KiB
Python

from os import name
from elasticsearch import Elasticsearch
from datetime import datetime
# uni-server
es = Elasticsearch({'host':'inse.nes.uni-due.de', 'port': 443, 'path_prefix':'elastic', 'scheme':'https'}, basic_auth=('elastic', 'RLldUaJgbGeoqd3KCX3p'), request_timeout=60, max_retries=5, retry_on_timeout=True)
stat_dict = {}
index_name = "inse_stats"
def get_max_count():
print("Max_Count")
max_dict = {}
max_dict["count_projects_bmbf"] = int(es.count(index='projects_bmbf').get('count'))
max_dict["count_projects_eu"] = int(es.count(index='projects_eu').get('count'))
max_dict["count_projects_dfg"] = int(es.count(index='projects_dfg').get('count'))
max_dict["count_projects_all"] = max_dict["count_projects_bmbf"] + max_dict["count_projects_dfg"] + max_dict["count_projects_eu"]
max_dict["count_wos"] = int(es.count(index='wos_index_1.0').get('count'))
max_dict["count_startups"] = int(es.count(index='startups').get('count'))
max_dict["count_akutell"] = int(es.count(index='aktuell').get('count'))
max_dict["count_epo"] = int(es.count(index='epo_index_1.0').get('count'))
max_dict["count_ude_persons"] = int(es.count(index='ude_persons_1.0').get('count'))
max_dict["count_ude_publications"] = int(es.count(index='due_index_1.0').get('count'))
stat_dict["max_count"] = max_dict
def get_stat_persons():
print("Persons")
# get all persons
aggs = {
"lsf_id": {
"terms": {
"size": 100000,
"field": "lsf_id.keyword"
}
}
}
docs_aggs = es.search(index="ude_persons_1.0", aggs=aggs, size=0)
ids = docs_aggs.get("aggregations").get("lsf_id").get("buckets")
current_year = int(datetime.now().year)
all_names = []
for id in ids:
current_person = {}
lsf_id = id.get("key")
print(" - " + lsf_id)
query = {
"match": {
"lsf_id": lsf_id
}
}
hit = es.search(index="ude_persons_1.0", query=query, size=1).get("hits").get("hits")[0].get("_source")
surname = hit.get("surname")
firstname = hit.get("firstname")
name = hit.get("name")
# filter out names with unallowed chars like "/" or "+"
#if any(c not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234 -öäüÖÜÄÁáĆćÉéÍíĹĺŃńÓóŐőŔশÚúŰűÝýŹź' for c in firstname):
# continue
#if any(c not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234 -öäüÖÜÄÁáĆćÉéÍíĹĺŃńÓóŐőŔশÚúŰűÝýŹź' for c in surname):
# continue
if any(c in '()!%&/+/,;.' for c in firstname):
continue
if any(c in '()!%&/+/,;.' for c in surname):
continue
# check requirements for following analysis
if firstname == "" or surname == "":
es.index(index=index_name, id=lsf_id, document=current_person)
continue
if name in all_names:
continue
else:
all_names.append(name)
current_person["name"] = name
current_person["surname"] = surname
current_person["firstname"] = firstname
current_person["lsf_id"] = lsf_id
# ude_publications
query = {
"nested": {
"path": "authors",
"query": {
"match": {
"authors.lsf_id": lsf_id
}
}
}
}
current_person["ude_publication_hits"] = es.search(index="due_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# ude_publications_last_year
query = {
"bool": {
"must": [
{
"nested": {
"path": "authors",
"query": {
"match": {
"authors.lsf_id": lsf_id
}
}
}
},
{
"range": {
"year.keyword": {
"gte": current_year - 1,
"lte": current_year - 1
}
}
}
]
}
}
current_person["ude_publication_hits_last_year"] = es.search(index="due_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# akutell
query = {
"multi_match": {
"query": firstname + " " + surname,
"fields": ["*"],
"type" : "phrase"
}
}
current_person["akutell_hits"] = es.search(index="aktuell", query=query, size=0).get("hits").get("total").get("value")
# akutell_last_year
query = {
"bool": {
"must": [
{
"multi_match": {
"query": firstname + " " + surname,
"fields": ["*"],
"type" : "phrase"
}
},
{
"range": {
"date_year.keyword": {
"gte": current_year - 1,
"lte": current_year - 1
}
}
}
]
}
}
current_person["akutell_hits_last_year"] = es.search(index="aktuell", query=query, size=0).get("hits").get("total").get("value")
# projects_dfg
query = {
"multi_match": {
"query": firstname + " " + surname,
"fields": ["*"],
"type" : "phrase"
}
}
current_person["projects_dfg_hits"] = es.search(index="projects_dfg", query=query, size=0).get("hits").get("total").get("value")
# wos_publications
query = {
"bool": {
"must": [
{
"nested": {
"path": "Author Full Names",
"query": {
"query_string": {
"default_field": "Author Full Names.author",
"query": firstname + " AND " + surname
}
}
}
}
],
"should": [
{
"query_string": {
"default_field": "Email Addresses",
"query": "@uni-due.de"
}
},
{
"match_phrase": {
"Affiliation": "University of Duisburg Essen"
}
}
],
"minimum_should_match": 1
}
}
current_person["wos_publication_hits"] = es.search(index="wos_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# wos_publications_last_year
query = {
"bool": {
"must": [
{
"nested": {
"path": "Author Full Names",
"query": {
"query_string": {
"default_field": "Author Full Names.author",
"query": firstname + " AND " + surname
}
}
}
},
{
"range": {
"Publication Year.keyword": {
"gte": current_year - 1,
"lte": current_year - 1
}
}
}
],
"should": [
{
"query_string": {
"default_field": "Email Addresses",
"query": "@uni-due.de"
}
},
{
"match_phrase": {
"Affiliation": "University of Duisburg Essen"
}
}
],
"minimum_should_match": 1
}
}
current_person["wos_publication_hits_last_year"] = es.search(index="wos_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# patents
query = {
"bool": {
"must": [
{
"match_phrase": {
"inventors_name_text": surname + ", " + firstname
}
},
{
"match_phrase": {
"applicants_name_text": "Universität Duisburg-Essen"
}
}
]
}
}
current_person["patent_hits"] = es.search(index="epo_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# patents_last_year
query = {
"bool": {
"must": [
{
"match_phrase": {
"inventors_name_text": surname + ", " + firstname
}
},
{
"match_phrase": {
"applicants_name_text": "Universität Duisburg-Essen"
}
},
{
"range": {
"date_publication_year": {
"gte": current_year - 1,
"lte": current_year - 1
}
}
}
]
}
}
current_person["patent_hits_last_year"] = es.search(index="epo_index_1.0", query=query, size=0).get("hits").get("total").get("value")
es.index(index=index_name, id=lsf_id, document=current_person)
def upload():
print("Uploading")
es.index(index=index_name, id="stats", document=stat_dict)
def check_elastic():
index_name = "inse_stats"
if es.indices.exists(index=index_name):
es.indices.delete(index=index_name)
if not es.indices.exists(index=index_name):
settings = {
"analysis": {
"analyzer": {
"searchabilityAnalyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
}
es.indices.create(index=index_name, settings=settings)
def get_stat_affiliation():
print("Affiliations")
# get all affiliations
aggs = {
"aff": {
"terms": {
"size": 100000,
"field": "affiliation_lower.keyword"
}
}
}
docs_aggs = es.search(index="ude_persons_1.0", aggs=aggs, size=0)
affis = docs_aggs.get("aggregations").get("aff").get("buckets")
for aff in affis:
current_affiliation = {}
aff_name = aff.get("key")
print(" - " + aff_name)
query = {
"match_phrase": {
"path.keyword": aff_name
}
}
current_affiliation["affiliation_name"] = aff_name
current_affiliation["affiliation_hits"] = es.search(index="projects_dfg", query=query, size=0).get("hits").get("total").get("value")
es.index(index=index_name, document=current_affiliation)
def get_best_10():
print("Best_10")
best_10 = {}
query = {
"match_all" : {}
}
### best_10_ever
# ude_publication_hits
sort = {
"ude_publication_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_ude_publication_hits = {}
counter = 1
for hit in hits:
best_10_ude_publication_hits[str(counter)] = {"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]}
counter += 1
best_10["best_10_ude_publication_hits"] = best_10_ude_publication_hits
# akutell_hits
sort = {
"akutell_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_akutell_hits = {}
counter = 1
for hit in hits:
best_10_akutell_hits[str(counter)] = {"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]}
counter += 1
best_10["best_10_akutell_hits"] = best_10_akutell_hits
# projects_dfg_hits
sort = {
"projects_dfg_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_projects_dfg_hits = {}
counter = 1
for hit in hits:
best_10_projects_dfg_hits[str(counter)] = {"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]}
counter += 1
best_10["best_10_projects_dfg_hits"] = best_10_projects_dfg_hits
# projects_affiliation_dfg_hits
sort = {
"affiliation_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_projects_affiliation_dfg_hits = {}
counter = 1
for hit in hits:
best_10_projects_affiliation_dfg_hits[str(counter)] = {"name": hit.get("_source").get("affiliation_name"), "number": hit.get("sort")[0]}
counter += 1
best_10["best_10_projects_affiliation_dfg_hits"] = best_10_projects_affiliation_dfg_hits
# wos_publication_hits
sort = {
"wos_publication_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_wos_publication_hits = {}
counter = 1
for hit in hits:
best_10_wos_publication_hits[str(counter)] = {"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]}
counter += 1
best_10["best_10_wos_publication_hits"] = best_10_wos_publication_hits
# patent_hits
sort = {
"patent_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_patent_hits = {}
counter = 1
for hit in hits:
best_10_patent_hits[str(counter)] = {"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]}
counter += 1
best_10["best_10_patent_hits"] = best_10_patent_hits
### best_10_last year
# ude_publication_hits_last_year
sort = {
"ude_publication_hits_last_year": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_ude_publication_hits_last_year = {}
counter = 1
for hit in hits:
best_10_ude_publication_hits_last_year[str(counter)] = {"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]}
counter += 1
best_10["best_10_ude_publication_hits_last_year"] = best_10_ude_publication_hits_last_year
# akutell_hits_last_year
sort = {
"akutell_hits_last_year": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_akutell_hits_last_year = {}
counter = 1
for hit in hits:
best_10_akutell_hits_last_year[str(counter)] = {"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]}
counter += 1
best_10["best_10_akutell_hits_last_year"] = best_10_akutell_hits_last_year
# wos_publication_hits_last_year
sort = {
"wos_publication_hits_last_year": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_wos_publication_hits_last_year = {}
counter = 1
for hit in hits:
best_10_wos_publication_hits_last_year[str(counter)] = {"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]}
counter += 1
best_10["best_10_wos_publication_hits_last_year"] = best_10_wos_publication_hits_last_year
# patent_hits_last_year
sort = {
"patent_hits_last_year": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=10).get("hits").get("hits")
best_10_patent_hits_last_year = {}
counter = 1
for hit in hits:
best_10_patent_hits_last_year[str(counter)] = {"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]}
counter += 1
best_10["best_10_patent_hits_last_year"] = best_10_patent_hits_last_year
stat_dict["best_10"] = best_10
if __name__ == '__main__':
print("Starting")
check_elastic()
get_max_count()
get_stat_persons()
get_stat_affiliation()
get_best_10()
upload()
print("Done")