Skip to content
Snippets Groups Projects
minimalbeispiel.ipynb 13.3 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c9328cd1",
   "metadata": {},
   "source": [
    "# FAIRe Qualitäts-KPI"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ee8746d",
   "metadata": {},
   "source": [
    "## Einführung\n",
    "FAIR Quality 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. "
   ]
  },
  {
   "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": null,
   "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": null,
   "id": "0b1f9aff",
   "metadata": {},
   "outputs": [],
   "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 the properties just any dict:\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": null,
   "id": "b4a8e8c8",
   "metadata": {},
   "outputs": [],
   "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": null,
   "id": "25bd06c5",
   "metadata": {},
   "outputs": [],
   "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": null,
   "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": null,
   "id": "71324895",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Look at the assembly\n",
    "\n",
    "# We can get all LegoComponents from this assembly.\n",
    "# Without parameter 'max_depth' only direct children will be listed.\n",
    "scooter.get_component_list(5)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "001f1c77",
   "metadata": {},
   "source": [
    "### Modul calculation_rules\n",
    "\n",
    "Um für unser System \"Tretroller\" ein KPI  für das Gesamtgewicht zu erzeugen, wurde in `functions.calculation_rules` die Funktion `kpi_sum` definiert. Zusammen mit den Hilfsfunktionen der Klasse können wir nun das KPI Gewicht für das System hinzufügen. Die Massen der einzelnen Komponenten sind in den Datenblättern unter `mass [g]` enthalten."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b60d0fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# test the import\n",
    "calculation_rules.test_function()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe4edad6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add mass to assemblies\n",
    "\n",
    "# List all components' mass\n",
    "combined_weight = 0\n",
    "for c in scooter.get_component_list(-1):\n",
    "    combined_weight += c.properties[\"mass [g]\"]\n",
    "print(\"Gesamtgewicht: \", combined_weight, \"g\")\n",
    "# Add KPI to system\n",
    "scooter.properties[\"mass [g]\"] = combined_weight\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": null,
   "id": "c26b36c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "scooter.get_component_list(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d56419f",
   "metadata": {},
   "outputs": [],
   "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": null,
   "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)\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": null,
   "id": "b91fed73",
   "metadata": {},
   "outputs": [],
   "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 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.11.0"
  },
  "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
}