{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0862ce82", "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "try:\n", " import IPython\n", "except:\n", " %pip install IPython\n", " import IPython \n", "from IPython.display import display, IFrame, HTML, Javascript\n", "from IPython.core.display import display, HTML\n", "\n", "HTML(\"\"\"\"\"\")" ] }, { "cell_type": "markdown", "id": "11e94730", "metadata": {}, "source": [ "# Transforming Collections Data to Linked Art\n", "# Harvard Art Museum" ] }, { "cell_type": "markdown", "id": "c0e616a9", "metadata": {}, "source": [ "## Input Data\n", "The notebook uses data from the Harvard Art Museum, using an API" ] }, { "cell_type": "code", "execution_count": 2, "id": "ecc024dc", "metadata": {}, "outputs": [], "source": [ "import requests\n", "try:\n", " import json\n", "except:\n", " !pip install json\n", " import json \n", "\n", " \n", "\n", "apidocs = \"https://github.com/harvardartmuseums/api-docs\"\n", "\n", "keyfile = \"data/ruskin/input/key.txt\"\n", "key = open(keyfile, mode='r', encoding='utf-8-sig').read()\n", "uri = \"https://api.harvardartmuseums.org/object?person=28419&apikey=\" + key\n", "\n", "for page in range(1, 11):\n", " response = requests.get(uri + \"&page=\" + str(page))\n", " json_data = response.json()\n", " \n", " text_file = open( \"./data/harvard/input/\" + str(page) + \".json\", \"wt\")\n", " n = text_file.write(json.dumps(json_data, indent=2))\n", " text_file.close()" ] }, { "cell_type": "code", "execution_count": 3, "id": "302a5ef7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0
idid
accession_numberobjectnumber
accession_dateaccessionyear
classificationclassification
titletitle
alt_title
notescommentary
date_createddated
date_created_earliest
date_created_latest
created_periodperiod
created_dynastycentury
created_inscriptions
created_notes
creatorpeople
physical_mediummedium
physical_style
physical_techniquetechnique
physical_descriptiondescription
physical_dimensionsdimensions
created_provenanceprovenance
credit_linecreditline
collectiondivision
current_status
current_ownerHarvard Museum of Art
image_urlprimaryimageurl
homepageurl
\n", "
" ], "text/plain": [ " 0\n", "id id\n", "accession_number objectnumber\n", "accession_date accessionyear\n", "classification classification\n", "title title\n", "alt_title \n", "notes commentary\n", "date_created dated\n", "date_created_earliest \n", "date_created_latest \n", "created_period period\n", "created_dynasty century\n", "created_inscriptions \n", "created_notes \n", "creator people\n", "physical_medium medium\n", "physical_style \n", "physical_technique technique\n", "physical_description description\n", "physical_dimensions dimensions\n", "created_provenance provenance\n", "credit_line creditline\n", "collection division\n", "current_status \n", "current_owner Harvard Museum of Art\n", "image_url primaryimageurl\n", "homepage url" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "try:\n", " import pandas as pd\n", "except:\n", " !pip install pandas\n", " import pandas as pd\n", " \n", "\n", "baseURI = \"https://www.harvardartmuseums.org/collections/object/\"\n", "outputdir = \"data/harvard/output\"\n", "\n", "mapp = {\n", " \"id\":\"id\",\n", " \"accession_number\":\"objectnumber\",\n", " \"accession_date\": \"accessionyear\",\n", " \"classification\" : \"\",\n", " \"title\": \"title\",\n", " \"alt_title\": \"\",\n", " \"notes\": \"commentary\",\n", " \"date_created\":\"dated\",\n", " \"date_created_earliest\": \"\",\n", " \"date_created_latest\": \"\",\n", " \"created_period\":\"period\",\n", " \"created_dynasty\":\"century\",\n", " \"created_inscriptions\":\"\",\n", " \"created_notes\": \"\",\n", " \"creator\":\"people\",\n", " \"physical_medium\": \"medium\",\n", " \"physical_style\": \"\",\n", " \"physical_technique\": \"technique\",\n", " \"physical_description\": \"description\",\n", " \"physical_dimensions\": \"dimensions\",\n", " \"created_provenance\": \"provenance\" ,\n", " \"credit_line\": \"creditline\",\n", " \"collection\" : \"division\",\n", " \"classification\": \"classification\",\n", " \"current_status\" : \"\",\n", " \"current_owner\": \"Harvard Museum of Art\",\n", " \"image_url\":\"primaryimageurl\",\n", " \"homepage\" : \"url\"\n", "}\n", "\n", "# display transposed dataframe of data mapping\n", "display(pd.DataFrame(mapp, index=[0]).T)\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "2ef7973b", "metadata": {}, "outputs": [], "source": [ "try:\n", " import cromulent\n", "except:\n", " !pip install cromulent\n", " import cromulent\n", " \n", "from cromulent.model import factory\n", "from lib import linkedart as la\n", "\n", "\n", "\n", "def createObjPropHarvard(obj,mapp):\n", " objProp = {}\n", " csv_keys = list(obj.keys())\n", " for key in csv_keys:\n", " for prop in mapp:\n", " if key == mapp[prop]:\n", " if prop == \"creator\":\n", " objProp[\"creator\"] = \"\"\n", " for person in obj[key]:\n", " \n", " if \"personid\" in person and person[\"personid\"] == 28419 and person[\"role\"] == \"Artist\": \n", " objProp[prop] = [{\n", " \"id\": str(person[\"personid\"]),\n", " \"name\": person[\"displayname\"],\n", " }] \n", " else:\n", " objProp[prop] = obj[key]\n", " objProp[\"current_owner\"] = {\"name\":\"Harvard Art Museum\",\n", " \"location\":\"Cambridge, MA\",\n", " \"type\": \"http://vocab.getty.edu/aat/300312281\" ,\n", " \"type_label\": \"\"}\n", " return objProp \n", "\n", "# list to hold file names for use with jsonld visualisation dropdown\n", "selectOptions = []\n", "selectOptions = [('Please select an artwork', '')]\n", "\n", "\n", "\n", "for x in range(1,11):\n", " \n", " with open(\"./data/harvard/input/\" + str(x) + \".json\") as json_file:\n", " data = json.load(json_file)\n", " for obj in data[\"records\"]:\n", " objProp=createObjPropHarvard(obj,mapp)\n", " if objProp[\"creator\"] != \"\":\n", " \n", " \n", " id = str(objProp[\"id\"])\n", " filename = id + \".json\"\n", " object_uri = baseURI + id\n", " \n", " # create obj description\n", " objLA = la.createObjDesc(objProp,la.objTypes,object_uri)\n", " \n", " selectOptions.append( ( objProp[\"title\"] + \" (\" + filename + \")\" , filename))\n", " # write to file \n", " text_file = open(outputdir + \"/\" + filename, \"wt\")\n", " n = text_file.write(factory.toString(objLA, compact=False))\n", " text_file.close()\n", " " ] }, { "cell_type": "code", "execution_count": 5, "id": "a6e42b21", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "91583a5c316e411292bd4d78edcadc35", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Dropdown(options=(('Please select an artwork', ''), ('Pass of Faido (262409.json)', '262409.json'), ('Château …" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "try:\n", " import ipywidgets\n", "except:\n", " %pip install ipywidgets\n", " import ipywidgets\n", "\n", "from ipywidgets import Layout, FileUpload \n", "from IPython.display import display, IFrame, HTML, Image\n", "from IPython.core.display import Javascript \n", " \n", "import os\n", "\n", "try:\n", " import json\n", "except:\n", " %pip install json\n", " import json \n", " \n", " \n", "def dropdown_eventhandler(change):\n", " with open('./src/js/visld.js', 'r') as _jscript:\n", " code = _jscript.read() + \"var file = '\" + outputdir + \"/\" + change.new + \"';var selector = '#vis';visjsonld(file, selector); \"\n", " display(Javascript(code))\n", " \n", " with open( outputdir + \"/\" + change.new) as json_file:\n", " \n", " artwork = json.load(json_file)\n", " if (\"representation\" in artwork):\n", " image = artwork[\"representation\"][0][\"id\"]\n", " display(Javascript(\"document.getElementById('artwork').src = '\" + image + \"';\"))\n", " else:\n", " display(Javascript(\"document.getElementById('artwork').src = '';\"))\n", " \n", "\n", "selectObject = ipywidgets.Dropdown(options=selectOptions)\n", "selectObject.observe(dropdown_eventhandler, names='value')\n", "\n", "display(selectObject)" ] }, { "cell_type": "markdown", "id": "2ccb7ff2", "metadata": {}, "source": [ "
\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "85c23191", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }