Created
May 9, 2023 08:31
-
-
Save simonrolph/19d16503794239cd46cc03bb1c173614 to your computer and use it in GitHub Desktop.
generate_ro_card.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyMeook7kl4F+aRSDFmOQpAR", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/simonrolph/19d16503794239cd46cc03bb1c173614/generate_ro_card.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"id": "bcFAmIr0W2Td" | |
}, | |
"outputs": [], | |
"source": [ | |
"#load required libraries\n", | |
"import requests\n", | |
"import json" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Functions that take an identifier (eg. doi) and return an 'entity' which will form part of the RO-Card. Some lines are commented ot becuase this information was provided for some entities not others so it would error." | |
], | |
"metadata": { | |
"id": "TzjzXpKOXAqy" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# GET MANUSCRIPT\n", | |
"def convert_crossref_to_entity(doi):\n", | |
" #get response\n", | |
" response = requests.get(f\"https://api.crossref.org/works/{doi}\")\n", | |
" crossref_api_response = response.json()\n", | |
" \n", | |
" # Manuscript\n", | |
" # Extract relevant metadata from the CrossRef API response\n", | |
" title = crossref_api_response['message']['title'][0]\n", | |
" authors = crossref_api_response['message']['author']\n", | |
" #abstract = crossref_api_response['message']['abstract']\n", | |
" container_title = crossref_api_response['message']['container-title'][0]\n", | |
" volume = crossref_api_response['message']['volume']\n", | |
" #issue = crossref_api_response['message']['issue']\n", | |
" #page = crossref_api_response['message']['page']\n", | |
" date_parts = crossref_api_response['message']['issued']['date-parts'][0]\n", | |
" year = date_parts[0]\n", | |
" month = date_parts[1]\n", | |
" #day = date_parts[2] \n", | |
"\n", | |
" # Construct the RO-Crate JSON structure\n", | |
" ro_crate_json = {\n", | |
" \"@id\": f\"https://doi.org/{doi}\",\n", | |
" \"@type\": \"ScholarlyArticle\",\n", | |
" \"identifier\": doi,\n", | |
" \"name\": title,\n", | |
" \"datePublished\": f\"{year}-{month:02d}\",\n", | |
" \"author\": [{\"@type\": \"Person\", \"name\": f\"{author['given']} {author['family']}\"} for author in authors],\n", | |
" #\"description\": abstract,\n", | |
" \"isPartOf\": {\"@id\": container_title, \"volumeNumber\": volume}\n", | |
" }\n", | |
"\n", | |
" # Return the ro entity json\n", | |
" return ro_crate_json\n", | |
"\n", | |
"# example\n", | |
"# Retrieve the CrossRef API response for the DOI \"10.1371/journal.pbio.3001991\"\n", | |
"# doi = \"10.1371/journal.pbio.3001991\"\n", | |
"# ro_crate_article = convert_crossref_to_entity(doi)\n", | |
"# ro_crate_article" | |
], | |
"metadata": { | |
"id": "rNfBvHaEW8dD" | |
}, | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# GET SOFTWARE FROM ZENODO\n", | |
"def generate_software_ro_crate_zenodo(zenodo_repo_id):\n", | |
" \n", | |
" # Define the Zenodo API endpoint URLs\n", | |
" base_url = \"https://zenodo.org/api/\"\n", | |
" deposition_url = base_url + \"deposit/depositions\"\n", | |
" software_url = base_url + f\"records/{zenodo_repo_id}\"\n", | |
" \n", | |
" # Retrieve the software metadata from Zenodo\n", | |
" software_response = requests.get(software_url)\n", | |
" software_metadata = software_response.json()\n", | |
" \n", | |
" # Extract relevant metadata from the software metadata\n", | |
" title = software_metadata['metadata']['title']\n", | |
" description = software_metadata['metadata']['description']\n", | |
" authors = software_metadata['metadata']['creators']\n", | |
" date = software_metadata['metadata']['publication_date']\n", | |
" version = software_metadata['metadata']['version']\n", | |
" license = software_metadata['metadata']['license']['id']\n", | |
" keywords = software_metadata['metadata'].get('keywords', [])\n", | |
"\n", | |
" # Create the RO-Crate JSON structure\n", | |
" ro_crate_json = {\n", | |
" \"@id\": f\"https://zenodo.org/record/{zenodo_repo_id}\",\n", | |
" \"@type\": \"Software\",\n", | |
" \"identifier\": zenodo_repo_id,\n", | |
" \"name\": title,\n", | |
" \"version\": version,\n", | |
" \"description\": description,\n", | |
" \"datePublished\": date,\n", | |
" \"author\": [{\"@type\": \"Person\", \"name\": f\"{author['name']}\"} for author in authors],\n", | |
" \"keywords\": keywords,\n", | |
" \"license\": {\"@id\": f\"https://spdx.org/licenses/{license}.html\"}\n", | |
" }\n", | |
"\n", | |
" # Return the filename of the generated RO-Crate JSON file\n", | |
" return ro_crate_json\n", | |
"\n", | |
"# example\n", | |
"# zenodo_repo_id = 7886834\n", | |
"# ro_crate_software_zenodo = generate_software_ro_crate(zenodo_repo_id)\n", | |
"# ro_crate_software_zenodo" | |
], | |
"metadata": { | |
"id": "-rKtT4gAXL0B" | |
}, | |
"execution_count": 8, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# GET SOFTWARE FROM GITHUB\n", | |
"def generate_software_ro_crate_github(github_repo_owner, github_repo_name): \n", | |
" # Define the GitHub API endpoint URLs\n", | |
" base_url = \"https://api.github.com/\"\n", | |
" repo_url = base_url + f\"repos/{github_repo_owner}/{github_repo_name}\"\n", | |
" \n", | |
" # Retrieve the software metadata from GitHub\n", | |
" repo_response = requests.get(repo_url)\n", | |
" repo_metadata = repo_response.json()\n", | |
" \n", | |
" # Extract relevant metadata from the repo metadata\n", | |
" title = repo_metadata['name']\n", | |
" description = repo_metadata['description']\n", | |
" #authors = [{\"name\": contributor['login']} for contributor in repo_metadata['contributors']]\n", | |
" date = repo_metadata['created_at']\n", | |
" version = repo_metadata['default_branch']\n", | |
" license_url = repo_metadata['license']['url']\n", | |
" license_response = requests.get(license_url)\n", | |
" license_metadata = license_response.json()\n", | |
" license = license_metadata['spdx_id']\n", | |
" keywords = repo_metadata.get('topics', [])\n", | |
"\n", | |
" # Create the RO-Crate JSON structure\n", | |
" ro_crate_json = {\n", | |
" \"@id\": f\"https://github.com/{github_repo_owner}/{github_repo_name}\",\n", | |
" \"@type\": \"Software\",\n", | |
" \"identifier\": f\"{github_repo_owner}/{github_repo_name}\",\n", | |
" \"name\": title,\n", | |
" \"version\": version,\n", | |
" \"description\": description,\n", | |
" \"datePublished\": date,\n", | |
" #\"author\": [{\"@type\": \"Person\", \"name\": f\"{author['name']}\"} for author in authors],\n", | |
" \"keywords\": keywords,\n", | |
" \"license\": {\"@id\": f\"https://spdx.org/licenses/{license}.html\"}\n", | |
" }\n", | |
"\n", | |
" # Return the generated RO-Crate JSON file\n", | |
" return ro_crate_json\n", | |
"\n", | |
"# example\n", | |
"# github_repo_owner = \"trixi-framework\"\n", | |
"# github_repo_name = \"Trixi.jl\"\n", | |
"# ro_crate_software_github = generate_software_ro_crate(github_repo_owner, github_repo_name)\n", | |
"# ro_crate_software_github" | |
], | |
"metadata": { | |
"id": "tkRt5vtCXQgZ" | |
}, | |
"execution_count": 9, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Then we can call each of the functions and put it together into an RO-Card. It prints it." | |
], | |
"metadata": { | |
"id": "Z79clFxIXRGp" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# get entities\n", | |
"\n", | |
"#article\n", | |
"doi = \"10.1016/j.jcp.2021.110467\"\n", | |
"ro_crate_article = convert_crossref_to_entity(doi)\n", | |
"\n", | |
"#software zendodo\n", | |
"zenodo_repo_id = 7886834\n", | |
"ro_crate_software_zenodo = generate_software_ro_crate_zenodo(zenodo_repo_id)\n", | |
"\n", | |
"#software github\n", | |
"github_repo_owner = \"trixi-framework\"\n", | |
"github_repo_name = \"Trixi.jl\"\n", | |
"ro_crate_software_github = generate_software_ro_crate_github(github_repo_owner, github_repo_name)\n", | |
"\n", | |
"# RO-Card details\n", | |
"ro_card_name = \"Trixi Framework\"\n", | |
"ro_card_description = \"This is the description\"\n", | |
"\n", | |
"\n", | |
"# Build RO card from entities\n", | |
"ro_card = { \n", | |
" \"@context\": \"https://w3id.org/ro/crate/1.1/context\", \n", | |
" \"@graph\": [\n", | |
" {\n", | |
" \"@type\": \"CreativeWork\",\n", | |
" \"@id\": \"ro-crate-metadata.json\",\n", | |
" \"conformsTo\": {\"@id\": \"https://w3id.org/ro/crate/1.1\"},\n", | |
" \"about\": {\"@id\": \"./\"}\n", | |
" },\n", | |
" {\n", | |
" \"@id\": \"./\",\n", | |
" \"@type\": \"Dataset\",\n", | |
" \"name\" : ro_card_name,\n", | |
" \"description\" : ro_card_description, \n", | |
" },\n", | |
" \n", | |
" ro_crate_article,\n", | |
" ro_crate_software_zenodo,\n", | |
" ro_crate_software_github\n", | |
" \n", | |
" ]\n", | |
"}\n", | |
"print(json.dumps(ro_card, indent=2))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "p72XDguSYjzt", | |
"outputId": "dae7b84e-e952-487f-9363-f1a33922ede7" | |
}, | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"{\n", | |
" \"@context\": \"https://w3id.org/ro/crate/1.1/context\",\n", | |
" \"@graph\": [\n", | |
" {\n", | |
" \"@type\": \"CreativeWork\",\n", | |
" \"@id\": \"ro-crate-metadata.json\",\n", | |
" \"conformsTo\": {\n", | |
" \"@id\": \"https://w3id.org/ro/crate/1.1\"\n", | |
" },\n", | |
" \"about\": {\n", | |
" \"@id\": \"./\"\n", | |
" }\n", | |
" },\n", | |
" {\n", | |
" \"@id\": \"./\",\n", | |
" \"@type\": \"Dataset\",\n", | |
" \"name\": \"Trixi Framework\",\n", | |
" \"description\": \"This is the description\"\n", | |
" },\n", | |
" {\n", | |
" \"@id\": \"https://doi.org/10.1016/j.jcp.2021.110467\",\n", | |
" \"@type\": \"ScholarlyArticle\",\n", | |
" \"identifier\": \"10.1016/j.jcp.2021.110467\",\n", | |
" \"name\": \"A purely hyperbolic discontinuous Galerkin approach for self-gravitating gas dynamics\",\n", | |
" \"datePublished\": \"2021-10\",\n", | |
" \"author\": [\n", | |
" {\n", | |
" \"@type\": \"Person\",\n", | |
" \"name\": \"Michael Schlottke-Lakemper\"\n", | |
" },\n", | |
" {\n", | |
" \"@type\": \"Person\",\n", | |
" \"name\": \"Andrew R. Winters\"\n", | |
" },\n", | |
" {\n", | |
" \"@type\": \"Person\",\n", | |
" \"name\": \"Hendrik Ranocha\"\n", | |
" },\n", | |
" {\n", | |
" \"@type\": \"Person\",\n", | |
" \"name\": \"Gregor J. Gassner\"\n", | |
" }\n", | |
" ],\n", | |
" \"isPartOf\": {\n", | |
" \"@id\": \"Journal of Computational Physics\",\n", | |
" \"volumeNumber\": \"442\"\n", | |
" }\n", | |
" },\n", | |
" {\n", | |
" \"@id\": \"https://zenodo.org/record/7886834\",\n", | |
" \"@type\": \"Software\",\n", | |
" \"identifier\": 7886834,\n", | |
" \"name\": \"Trixi.jl\",\n", | |
" \"version\": \"v0.5.19\",\n", | |
" \"description\": \"Adaptive high-order numerical simulations of hyperbolic PDEs in Julia\",\n", | |
" \"datePublished\": \"2023-05-02\",\n", | |
" \"author\": [\n", | |
" {\n", | |
" \"@type\": \"Person\",\n", | |
" \"name\": \"Schlottke-Lakemper, Michael\"\n", | |
" },\n", | |
" {\n", | |
" \"@type\": \"Person\",\n", | |
" \"name\": \"Gassner, Gregor J.\"\n", | |
" },\n", | |
" {\n", | |
" \"@type\": \"Person\",\n", | |
" \"name\": \"Ranocha, Hendrik\"\n", | |
" },\n", | |
" {\n", | |
" \"@type\": \"Person\",\n", | |
" \"name\": \"Winters, Andrew R.\"\n", | |
" },\n", | |
" {\n", | |
" \"@type\": \"Person\",\n", | |
" \"name\": \"Chan, Jesse\"\n", | |
" }\n", | |
" ],\n", | |
" \"keywords\": [],\n", | |
" \"license\": {\n", | |
" \"@id\": \"https://spdx.org/licenses/MIT.html\"\n", | |
" }\n", | |
" },\n", | |
" {\n", | |
" \"@id\": \"https://github.com/trixi-framework/Trixi.jl\",\n", | |
" \"@type\": \"Software\",\n", | |
" \"identifier\": \"trixi-framework/Trixi.jl\",\n", | |
" \"name\": \"Trixi.jl\",\n", | |
" \"version\": \"main\",\n", | |
" \"description\": \"Trixi.jl: Adaptive high-order numerical simulations of hyperbolic PDEs in Julia\",\n", | |
" \"datePublished\": \"2020-08-18T06:42:03Z\",\n", | |
" \"keywords\": [\n", | |
" \"adaptive-mesh-refinement\",\n", | |
" \"amr\",\n", | |
" \"conservation-laws\",\n", | |
" \"discontinuous-galerkin\",\n", | |
" \"julia\",\n", | |
" \"numerical-simulation-framework\",\n", | |
" \"simulation\",\n", | |
" \"summation-by-parts\"\n", | |
" ],\n", | |
" \"license\": {\n", | |
" \"@id\": \"https://spdx.org/licenses/MIT.html\"\n", | |
" }\n", | |
" }\n", | |
" ]\n", | |
"}\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment