{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example script for Python interface to Orthogonal Array package" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pieter Eendebak pieter.eendebak@gmail.com" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "oapackage version: 2.6.0\n" ] } ], "source": [ "import numpy as np\n", "\n", "import oapackage\n", "\n", "print(f\"oapackage version: {oapackage.version()}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load an example array." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array:\n", " 0 0\n", " 0 0\n", " 0 1\n", " 0 1\n", " 1 0\n", " 1 0\n", " 1 1\n", " 1 1\n" ] } ], "source": [ "array = oapackage.exampleArray(0)\n", "array.showarray()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate properties of the array such as the D-efficiency for the main effects model, the generalized word length pattern and the rank." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "D-efficiency 1.000000, rank 2\n", "Generalized wordlength pattern: (1.0, 0.0, 0.0)\n" ] } ], "source": [ "print(\"D-efficiency %f, rank %d\" % (array.Defficiency(), array.rank()))\n", "print(f\"Generalized wordlength pattern: {str(array.GWLP())}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the generalized word length pattern for another example array." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generalized wordlength pattern: 1.00,0.02273,0.03926,0.5434,0.8244,2.217,1.043,0.126,0.002066\n" ] } ], "source": [ "array = oapackage.exampleArray(11)\n", "print(f\"Generalized wordlength pattern: {oapackage.oahelper.gwlp2str(array.GWLP())}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `array_link` object can be indexed as a normal array." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array_link: 5, 1" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array[0:5, 2:3]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array:\n", " 1 1\n", " 0 1\n", " 1 0\n", " 0 0\n", " 1 1\n" ] } ], "source": [ "array[0:5, 2:4].showarray()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(array[0, 2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can convert between Numpy arrays and `array_link` objects. Note that an `array_link` is always integer valued." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2003200102\n", "0322120001\n", "3030110033\n", "0311131120\n", "2200100200\n", "2112333121\n", "3110300211\n", "3302313313\n", "2331031302\n", "1212232322\n", "3003330322\n", "0312311331\n", "3222200313\n", "2001123302\n", "2222111302\n", "0020322112\n", "0101223211\n", "0223300100\n", "1232030100\n", "0320032113\n" ] } ], "source": [ "X = (4 * np.random.rand(20, 10)).astype(int)\n", "array = oapackage.array_link(X)\n", "array.showarraycompact()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert from `array_link` back to Numpy array." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "X2 = np.array(array)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.8" } }, "nbformat": 4, "nbformat_minor": 4 }