{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# pyhasse.core" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Objectives\n", "\n", "pyhasse.core is the basic module for calculations within \n", "partially ordered sets.\n", "Beside others: It imports the dataset, which is to be analyzed." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import pathlib\n", "from pyhasse.core.csv_io import CSVReader\n", "from pyhasse.core.matrix import Matrix" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#TESTFILENAME = '/csvdata/zeta-test.csv'\n", "#TESTFILENAME = '/csvdata/acm-houses.csv'\n", "#TESTFILENAME = '/csvdata/chain-demo.csv'\n", "#TESTFILENAME = '/csvdata/chain-pollution.csv'\n", "#TESTFILENAME = '/csvdata/copeland_demo.csv'\n", "#TESTFILENAME = '/csvdata/fuzzy-demo.csv'\n", "#TESTFILENAME = '/csvdata/main19chemicals.csv'\n", "#TESTFILENAME = '/csvdata/pesticides-demo.txt'\n", "#TESTFILENAME = '/csvdata/spyout-demo.csv'\n", "#TESTFILENAME = '/csvdata/zeta-test.csv'\n", "TESTFILENAME = '/csvdata/check-K.csv'\n", "\n", "HERE = pathlib.Path('__file__').parent\n", "csv = CSVReader(fn=str(HERE) + TESTFILENAME, ndec=3)\n", "# calc a reduced system\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Check if all data are correct imported" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Objects" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']\n" ] } ], "source": [ "print(csv.obj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Attributes" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['q1', 'q2']\n" ] } ], "source": [ "print(csv.prop)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### All data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.0, 2.0]\n", "[3.0, 4.0]\n", "[4.0, 3.0]\n", "[3.0, 4.0]\n", "[1.0, 2.0]\n", "[5.0, 6.0]\n", "[6.0, 5.0]\n", "[1.0, 2.0]\n", "[6.0, 5.0]\n" ] } ], "source": [ "for row in csv.data:\n", " print(row)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic calculations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Equivalence classes" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# prepare dataset\n", "matrix = Matrix(csv.data, csv.obj, csv.prop, reduced=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The next step delivers the equivalence classes in terms of the object's pointers and their labels, additionally the characteristic of the structure induced by equivalences: K" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "K = sum Ni*(Ni-1)\n", "\n", "K = 0: There is no nontrivial equivalence class\n", "\n", "K > 0: Check the list below to detect the nontirivial equivalence classes" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "K = 0 <-- sum Ni*(Ni-1)\n" ] } ], "source": [ "k = 0\n", "for i in csv.eqm:\n", " k += (len(i))*(len(i)-1)\n", "print(f\"K = {k} <-- sum Ni*(Ni-1)\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Pointer object label represents equivalence class \n", "-----------------------------------------------------------\n", " 0 a [0, 4, 7] \n", " 1 b [1, 3] \n", " 2 c [2] \n", " 5 f [5] \n", " 6 g [6, 8] \n" ] } ], "source": [ "#eqm = csv.generate_eqcl(csv.obj,csv.eqm)\n", "eqm, rred = matrix.generate_equivalenceclasses(csv.data, csv.rows, csv.cols, prec=None)\n", "print(f'{\"Pointer\":>8} {\"object label represents\":^30} {\"equivalence class\":^20}')\n", "print(f'{59 * \"-\"}')\n", "for idx, value in enumerate(matrix.obj):\n", " print(f'{csv.obj.index(value):>8} {value:^30} {str(eqm[idx]):^20}') " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from pyhasse.core.order import Order\n", "precision = 4\n", "order = Order(csv.data,\n", " csv.rows,\n", " csv.cols)\n", "zeta =order.calc_relatmatrix(csv.data, csv.rows, csv.cols)\n", "zeta = order.calc_relatmatrix(\n", " datamatrix=matrix.data,\n", " rows=matrix.rows,\n", " cols=matrix.cols,\n", " prec=precision)\n", "covd_act, cov_act = order.calc_cov(zeta, matrix.rows)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "print(csv.eqm)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Zeta-Matrix" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 0, 0, 0, 0]\n", "[1, 1, 0, 0, 0]\n", "[1, 0, 1, 0, 0]\n", "[1, 1, 1, 1, 0]\n", "[1, 1, 1, 0, 1]\n" ] } ], "source": [ "for row in zeta:\n", " print(row)" ] }, { "cell_type": "code", "execution_count": null, "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.9.0" } }, "nbformat": 4, "nbformat_minor": 2 }