{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example to write and read files with arrays from disk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The package can write arrays and designs to a custom file format. There is a text based format (specified by `oapackage.ATEXT`) and a binary format (specified by `oapackage.ABINARY`). See the online documentation for details on the file formats." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import tempfile\n", "\n", "import oapackage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a list of two example arrays." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[array_link: 18, 4, array_link: 18, 4]\n" ] } ], "source": [ "lst = [oapackage.exampleArray(32), oapackage.exampleArray(33)]\n", "print(lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the two arrays to a file on disk." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "filename = tempfile.mktemp(\".oa\")\n", "_ = oapackage.writearrayfile(filename, lst, oapackage.ATEXT)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display information about the file written." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "file C:\\Users\\EENDEB~1\\AppData\\Local\\Temp\\tmpxc4dnsl6.oa: 18 rows, 4 columns, 2 arrays, mode text, nbits 0\n" ] } ], "source": [ "oapackage.oainfo(filename)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The arrays can be read from disk again using the command `readarrayfile`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array_link: 18, 4, array_link: 18, 4)\n" ] } ], "source": [ "lst2 = oapackage.readarrayfile(filename)\n", "print(lst2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first array in the list that was read from disk is:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array:\n", " 0 0 0 0\n", " 0 0 0 0\n", " 1 1 1 1\n", " 1 1 1 1\n", " 1 1 -1 -1\n", " 1 1 -1 -1\n", " 1 -1 1 -1\n", " 1 -1 1 -1\n", " 1 -1 -1 1\n", " 1 -1 -1 1\n", " -1 1 1 -1\n", " -1 1 1 -1\n", " -1 1 -1 1\n", " -1 1 -1 1\n", " -1 -1 1 1\n", " -1 -1 1 1\n", " -1 -1 -1 -1\n", " -1 -1 -1 -1\n" ] } ], "source": [ "lst2[0].showarray()" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }