Skip to content
Snippets Groups Projects
minimalbeispiel.ipynb 17.5 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c9328cd1",
   "metadata": {},
   "source": [
    "# FAIRe Qualitäts-KPIs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ee8746d",
   "metadata": {},
   "source": [
    "## Einführung\n",
    "FAIRe Qualitäts-KPIs schaffen eine transparente und nachvollziehbare Entscheidungsgrundlage. In dieser Lerneinheit sollen sie die Methodik erlernen, indem sie LEGO-Autos zusammenbauen, deren Quality KPIs bestimmen und miteinander vergleichen.\n",
    "\n",
    "### Werkzeug für den Zusammenbau\n",
    "Der Zusammenbau der Autos erfolgt virtuell. Als Werkzeug steht Ihnen das Modul `classes` zur Verfügung. In diesem sind unterschiedliche Klassen und Methoden implementiert (Objektorientierung). Sie erzeugen jeweils Objekte der Klassen. Ein Objekt ist dabei die virtuelle Abbildung eines realen Bauteiles. Die Eigenschaften des Bauteiles werden über die Eigenschaften des Objektes abgebildet.\n",
    "\n",
    "### Berechnung der Quality KPIs\n",
    "KPIs (Key Performance Indikatoren) sind Kenngrößen des Systems. Sie werden über Berechnungsvorschriften bestimmt. Diese Berechnungsvorschriften sind von Ihnen als python-Funktionen im Modul `calculation_rules` zu implementieren.\n",
    "\n",
    "### Datenblätter\n",
    "Für den Zusammenbau stehen Ihnen verschiedene Bauteile in unterschiedlichen Ausführungen zur Verfügung. Sie nutzen die Datenblätter, die als `json-Dateien` zur Verfügung gestellt werden, um Ihre Autos zusammenzubauen."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1c702114",
   "metadata": {},
   "source": [
    "## Eigene Module und Minimalbeispiel\n",
    "Für die Ausarbeitung nutzen wir zwei eigene Module (Python-Dateien), die im Ordner `functions` gespeichert sind.\n",
    "Das Modul `calculation_rules` erweitern Sie während der Ausarbeitung. Um die Änderungen zu nutzen, müssen Sie das Notebook neu starten.\n",
    "Im Modul `classes` befinden sich die komplette Klassen und Funktionen zur Verwendung.\n",
    "\n",
    "Mit einem Minimalbeispiel wird Ihnen gezeigt, wie sie die Module nutzen. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "670118b3",
   "metadata": {},
   "source": [
    "#### Versuchsaufbau:\n",
    "Die folgende Abbildung zeigt den Versuchsaufbau des Minimalbeispiels in LeoCAD. Die transparenten Bauteile sind dabei nicht Teil des hier erläuterten Zusammenbaus, können aber mit dem zur Verfügung gestellten Baukasten ergänzt werden:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f969ed9",
   "metadata": {},
   "source": [
    "![Tretroller in LeoCAD](figures/leocad_screenshot_cut.png)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6d3310be",
   "metadata": {},
   "source": [
    "### Modul classes\n",
    "Enthält `LegoComponent, LegoAssembly, AggregationLayer, KPIEncoder` und die Funktion `print_assembly_tree`.  \n",
    "`LegoComponent` bildet einzelne Komponenten ab, während `LegoAssembly` zusammengesetzte Aggregationsebenen abdeckt, also Bauteil, Baugruppe und System. Zur Unterscheidung dient die Klasse `AggregationLayer`, diese ist für `LegoComponent` immer `Component` (Komponente), muss für `LegoAssembly`  entsprechend auf `SYSTEM` (System) , `ASSEMBLY` (Baugruppe) oder `SUBASSEMBLY` (Bauteil) gesetzt werden.\n",
    "\n",
    "Wir bauen aus Achse, Rahmen und Reifen einen Tretroller zusammen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b2778dee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import modules\n",
    "import json\n",
    "import pprint\n",
    "from functions import calculation_rules\n",
    "\n",
    "# Importing all modules one by one to provide an overview\n",
    "# The next commented line would provide the same result in one line\n",
    "# from functions.classes import *\n",
    "from functions.classes import LegoComponent\n",
    "from functions.classes import LegoAssembly\n",
    "from functions.classes import AggregationLayer\n",
    "from functions.classes import KPIEncoder\n",
    "from functions.classes import print_assembly_tree\n",
    "\n",
    "# When you are writing code yourself later, you might want to copy\n",
    "# these imports to avoid rerunning the full notebook after restart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0b1f9aff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'item number': 32073, 'item description': 'Axle 5 studs', 'category': 'axle', 'price [Euro]': 0.001, 'mass [g]': 0.66, 'delivery time [days]': 3, 'data source': 'https://www.bricklink.com/v2/catalog/catalogitem.page?P=32073#T=S&O={%22iconly%22:0}', 'dimension [studs]': 5}\n",
      "front axle\n",
      "{'category': 'axle',\n",
      " 'color': 'grey',\n",
      " 'data source': 'https://www.bricklink.com/v2/catalog/catalogitem.page?P=32073#T=S&O={%22iconly%22:0}',\n",
      " 'delivery time [days]': 3,\n",
      " 'dimension [studs]': 5,\n",
      " 'item description': 'Axle 5 studs',\n",
      " 'item number': 32073,\n",
      " 'label': 'back axle',\n",
      " 'mass [g]': 0.66,\n",
      " 'price [Euro]': 0.001}\n"
     ]
    }
   ],
   "source": [
    "# Create the wheels and axles as single components first\n",
    "# Look up the specific item you want from the provided json files,\n",
    "# we can load the full file into a dict\n",
    "with open(\"datasheets/axles.json\") as json_file:\n",
    "    axles = json.load(json_file)\n",
    "# Pick a specific axle via its 'item number'\n",
    "print(axles[\"32073\"])\n",
    "\n",
    "# Create the component with the dict:\n",
    "front_axle = LegoComponent(\"front axle\", axles[\"32073\"])\n",
    "# Both label and the data dict are optional parameters.\n",
    "# You can view, add or edit properties:\n",
    "print(front_axle.properties[\"label\"])\n",
    "front_axle.properties[\"color\"] = \"grey\"\n",
    "\n",
    "\n",
    "# Create the second axle\n",
    "back_axle = LegoComponent()\n",
    "back_axle.properties[\"label\"] = \"back axle\"\n",
    "back_axle.properties.update(axles[\"32073\"])\n",
    "# Do not use = here, otherwise you'd overwrite existing properties.\n",
    "# Instead use update to have all entries added to properties\n",
    "back_axle.properties[\"color\"] = \"grey\"\n",
    "# Viewing dicts in one line is not easy to read,\n",
    "# a better output comes with pretty print (pprint):\n",
    "pprint.pprint(back_axle.properties)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b4a8e8c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'category': 'wheel',\n",
      " 'data source': 'https://www.bricklink.com/v2/catalog/catalogitem.page?P=2903c02#T=C',\n",
      " 'delivery time [days]': 5,\n",
      " 'diameter [mm]': 81.6,\n",
      " 'item description': 'wheel 81,6',\n",
      " 'item number': '2903c02',\n",
      " 'label': 'front wheel',\n",
      " 'mass [g]': 30.0,\n",
      " 'paint': 'glossy',\n",
      " 'price [Euro]': 1.31,\n",
      " 'related items': 2902,\n",
      " 'surface': 'rough'}\n"
     ]
    }
   ],
   "source": [
    "# Now wheels\n",
    "with open(\"datasheets/wheels.json\") as json_file:\n",
    "    wheels = json.load(json_file)\n",
    "# Adding the color here already as dict, and the surface as key-value argument.\n",
    "# Multiple of both parameters are supported, but only in this order.\n",
    "# front_wheel = LegoComponent(\n",
    "# 'front wheel', wheels['2903c02'], {'color':'yellow'},winter='true')\n",
    "\n",
    "front_wheel = LegoComponent(\n",
    "    \"front wheel\", wheels[\"2903c02\"], surface=\"rough\", paint=\"glossy\"\n",
    ")\n",
    "pprint.pprint(front_wheel.properties)\n",
    "\n",
    "# We included a clone function to both Lego classes, so you can easily\n",
    "# create duplicate objects. Passing the new label is optional\n",
    "back_wheel = front_wheel.clone(\"back wheel\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "25bd06c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "front axle\n"
     ]
    }
   ],
   "source": [
    "# Create subassemblies and add the wheels and axles\n",
    "\n",
    "# The AggregationLayer must be used, passing the label\n",
    "# or additional properties is optional\n",
    "front_wheel_assembly = LegoAssembly(\n",
    "    AggregationLayer.SUBASSEMBLY,\n",
    "    \"front wheel assembly\",\n",
    "    assembly_method=\"stick together like lego blocks\",\n",
    ")\n",
    "# Add LegoComponents to the LegoAssembly, single or as list\n",
    "front_wheel_assembly.add([front_wheel, front_axle])\n",
    "# You can access the components of an assembly like this:\n",
    "print(front_wheel_assembly.components[1].properties[\"label\"])\n",
    "\n",
    "# Assemblies can be cloned as well (including their children),\n",
    "# but don't forget to adjust labels or you might be stuck with\n",
    "# a 'front wheel' in your 'back wheel assembly'\n",
    "\n",
    "# Stick together back wheel parts\n",
    "back_wheel_assembly = LegoAssembly(\n",
    "    AggregationLayer.SUBASSEMBLY, \"back wheel assembly\"\n",
    "    )\n",
    "back_wheel_assembly.add([back_wheel, back_axle])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2b6648e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create frame component and assemble the system\n",
    "\n",
    "with open(\"datasheets/frame.json\") as json_file:\n",
    "    frame = json.load(json_file)\n",
    "scooter_frame = LegoComponent(\"scooter frame\", frame[\"3703\"], {\"color\": \"red\"})\n",
    "\n",
    "# The scooter is our system level assembly, you can choose the AggregationLayer\n",
    "# But the hiercarchy (SYSTEM > ASSEMBLY > SUBASSEMBLY) must be in order.\n",
    "# Components can be added to all LegoAssembly objects\n",
    "\n",
    "scooter = LegoAssembly(\n",
    "    AggregationLayer.SYSTEM,\n",
    "    \"scooter\",\n",
    "    manufacturer=\"FST\",\n",
    "    comment=\"Faster! Harder! Scooter!\",\n",
    ")\n",
    "# add frame and subassemblies\n",
    "scooter.add([scooter_frame, front_wheel_assembly, back_wheel_assembly])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "71324895",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LegoComponent scooter frame [a90b291e-696c-41c7-b9b8-43d34724b460]]\n",
      "[LegoComponent scooter frame [a90b291e-696c-41c7-b9b8-43d34724b460], LegoComponent front wheel [b03ce5b5-9c6c-4315-a67b-902c729c3d7b], LegoComponent front axle [64771daf-733a-4a2a-8520-9fa94259e50e], LegoComponent back wheel [8fcb7db9-6fec-4e70-b274-0c4fe403550b], LegoComponent back axle [2ce57b96-64b8-48b1-aac8-b0731adaa93b]]\n"
     ]
    }
   ],
   "source": [
    "# Look at the assembly\n",
    "\n",
    "# We can get all LegoComponents from this assembly.\n",
    "# If parameter 'max_depth' is set to 0, only direct children will be listed:\n",
    "print(scooter.get_component_list(max_depth=0))\n",
    "# If it is set to -1 (default), all LegoComponents are listed:\n",
    "print(scooter.get_component_list())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "001f1c77",
   "metadata": {},
   "source": [
    "### Modul calculation_rules\n",
    "\n",
    "Um für unser System \"Tretroller\" einen KPI zu erzeugen, wird die Masse des Systems betrachtet. Die Masse der einzelnen Komponenten ist in den Datenblättern unter `mass [g]` enthalten."
   "execution_count": 7,
   "id": "7b60d0fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You called the test function.\n"
     ]
    }
   ],
   "source": [
    "# test the import\n",
    "calculation_rules.test_function()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fe4edad6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gesamtmasse:  67.19 g\n"
     ]
    }
   ],
    "total_mass = calculation_rules.kpi_mass(scooter)\n",
    "print(\"Gesamtmasse: \", total_mass, \"g\")\n",
    "# Add KPI to system\n",
    "scooter.properties[\"mass [g]\"] = total_mass\n",
    "\n",
    "# We can also add the mass to the subassemblies.\n",
    "# children() returns a dict with a list of added\n",
    "# components and assemblies.\n",
    "for a in scooter.children()[\"assemblies\"]:\n",
    "    a_mass = 0\n",
    "    for c in a.get_component_list(-1):\n",
    "        a_mass += c.properties[\"mass [g]\"]\n",
    "    a.properties[\"mass [g]\"] = a_mass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c26b36c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[LegoComponent scooter frame [a90b291e-696c-41c7-b9b8-43d34724b460],\n",
       " LegoComponent front wheel [b03ce5b5-9c6c-4315-a67b-902c729c3d7b],\n",
       " LegoComponent front axle [64771daf-733a-4a2a-8520-9fa94259e50e],\n",
       " LegoComponent back wheel [8fcb7db9-6fec-4e70-b274-0c4fe403550b],\n",
       " LegoComponent back axle [2ce57b96-64b8-48b1-aac8-b0731adaa93b]]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scooter.get_component_list(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4d56419f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LegoAssembly scooter [86fdda66-adb2-48c0-b6f8-f6c59d488662]\n",
      "├── LegoAssembly front wheel assembly [e02a87d2-cb0b-4695-b237-9ca17f9611da]\n",
      "│   ├── LegoComponent front wheel [b03ce5b5-9c6c-4315-a67b-902c729c3d7b]\n",
      "│   └── LegoComponent front axle [64771daf-733a-4a2a-8520-9fa94259e50e]\n",
      "├── LegoAssembly back wheel assembly [db5db4c5-36c3-4b90-88eb-299c46a30049]\n",
      "│   ├── LegoComponent back wheel [8fcb7db9-6fec-4e70-b274-0c4fe403550b]\n",
      "│   └── LegoComponent back axle [2ce57b96-64b8-48b1-aac8-b0731adaa93b]\n",
      "└── LegoComponent scooter frame [a90b291e-696c-41c7-b9b8-43d34724b460]\n"
     ]
    }
   ],
   "source": [
    "# Look at the full assembly with KPI\n",
    "\n",
    "# Print the full assembly with all levels\n",
    "print_assembly_tree(scooter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b31416d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dump to json file with to_dict() and KPIEncoder\n",
    "with open(\"scooter.json\", \"w\") as fp:\n",
    "    json.dump(scooter.to_dict(), fp, cls=KPIEncoder, indent=4)\n",
    "# full view is too big for Juypter (try it)\n",
    "# pprint.pprint(scooter.to_dict())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "53793ae8",
   "metadata": {},
   "source": [
    "In dieser exportierten json-Datei ('scooter.json') sind die Werte maschinen- und menschenlesbar hinterlegt.\n",
    "Zusammen mit der Berechnungsvorschrift in `calculation_rules` ist auch die Entstehung des KPI nachvollziehbar und wiederverwendbar dokumentiert und damit 'FAIR'."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "92c77051",
   "metadata": {},
   "source": [
    "#### Zusätzliche Details"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b91fed73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Child: LegoAssembly front wheel assembly [e02a87d2-cb0b-4695-b237-9ca17f9611da]\n",
      "Parent: LegoAssembly scooter [86fdda66-adb2-48c0-b6f8-f6c59d488662]\n",
      "Top parent:  LegoAssembly scooter [86fdda66-adb2-48c0-b6f8-f6c59d488662]\n"
     ]
    }
   ],
   "source": [
    "# Each child knows its parent\n",
    "first_child = scooter.children()[\"assemblies\"][0]\n",
    "print(\"Child:\", first_child)\n",
    "print(\"Parent:\", first_child.parent)\n",
    "# Also we can access the \"top\" parent\n",
    "latest_child = first_child.children()[\"components\"][0]\n",
    "print(\"Top parent: \", latest_child.get_root_assembly())\n",
    "\n",
    "# Each part created has a unique identifier (the long number in [])\n",
    "# Don't try to add the identical part again."
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": ".venv",
   "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.9.13"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  },
  "vscode": {
   "interpreter": {
    "hash": "386d359a8531ffdc4805ead3a16e7983e89a5ab7bba0cbec0e7ad9597b7a2b64"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}