{ "cells": [ { "cell_type": "markdown", "id": "9beeccb7", "metadata": {}, "source": [ "# WolfArray - Data type\n", "\n", "A `WolfArray` can store any type of data, including:\n", "\n", "- `int` (integer8, integer16, integer32)\n", "- `float` (floating point number, 32-bit or 64-bit)\n", "- `bool` (boolean/logical)\n", "\n", "WOLF types are defined as follows (for compatibility with Fortran and VB6):\n", "\n", "- WOLF_ARRAY_HILLSHAPE = -1\n", "- WOLF_ARRAY_FULL_SINGLE = 1\n", "- WOLF_ARRAY_FULL_DOUBLE = 2\n", "- WOLF_ARRAY_SYM_DOUBLE = 12\n", "- WOLF_ARRAY_FULL_LOGICAL = 4\n", "- WOLF_ARRAY_CSR_DOUBLE = 5\n", "- WOLF_ARRAY_FULL_INTEGER = 6\n", "- WOLF_ARRAY_FULL_SINGLE_3D = 7\n", "- WOLF_ARRAY_FULL_INTEGER8 = 8\n", "- WOLF_ARRAY_FULL_UINTEGER8 = 88\n", "\n", "Equivalent Numpy types can be obtained using the `dtype` attribute." ] }, { "cell_type": "code", "execution_count": 1, "id": "c5c3a6c3", "metadata": {}, "outputs": [], "source": [ "import _add_path\n", "from wolfhece.wolf_array import WolfArray, WOLF_ARRAY_FULL_SINGLE, WOLF_ARRAY_FULL_INTEGER8, WOLF_ARRAY_FULL_DOUBLE, header_wolf\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "b2453ffe", "metadata": {}, "outputs": [], "source": [ "h = header_wolf()\n", "h.shape = (10,10)\n", "\n", "w_single = WolfArray(srcheader=h, whichtype=WOLF_ARRAY_FULL_SINGLE)\n", "w_double = WolfArray(srcheader=h, whichtype=WOLF_ARRAY_FULL_DOUBLE)\n", "w_integer8 = WolfArray(srcheader=h, whichtype=WOLF_ARRAY_FULL_INTEGER8)\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "e841c53e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " float32\n", " float64\n", " int8\n" ] } ], "source": [ "# check the dtype of the array and the WolfArray object\n", "print(w_single.dtype, w_single.array.dtype)\n", "print(w_double.dtype, w_double.array.dtype)\n", "print(w_integer8.dtype, w_integer8.array.dtype)" ] }, { "cell_type": "code", "execution_count": 4, "id": "a76bd160", "metadata": {}, "outputs": [], "source": [ "# Modify some values in the array\n", "w_integer8.array [0,0] = 10\n", "w_integer8.array [-1,-1] = 11" ] }, { "cell_type": "code", "execution_count": 10, "id": "08fbfccb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " float32\n", "10.0 \n", "11.0 \n", "1.0 \n" ] } ], "source": [ "# convert the array to a different type (all wolfhece versions)\n", "w3_single = WolfArray(srcheader=w_integer8.get_header(), whichtype=WOLF_ARRAY_FULL_SINGLE)\n", "\n", "w3_single.set_array_from_numpy(w_integer8.array.astype(w3_single.dtype))\n", "\n", "print(w3_single.dtype, w3_single.array.dtype)\n", "print(w3_single.array [0,0], type(w3_single.array [0,0]))\n", "print(w3_single.array [-1,-1], type(w3_single.array [-1,-1]))\n", "print(w3_single.array [0,1], type(w3_single.array [0,1]))" ] }, { "cell_type": "code", "execution_count": 14, "id": "bba69eac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.2.5\n" ] } ], "source": [ "# convert the array to a different type (wolfhece 2.2.6 and above)\n", "from wolfhece.apps.version import WolfVersion\n", "print(WolfVersion().get_version())\n", "\n", "if WolfVersion().get_version() >= '2.2.6':\n", " w4_single = WolfArray(mold=w_integer8, whichtype=WOLF_ARRAY_FULL_SINGLE)\n", " print(w4_single.dtype, w4_single.array.dtype)\n", " print(w4_single.array [0,0], type(w4_single.array [0,0]))\n", " print(w4_single.array [-1,-1], type(w4_single.array [-1,-1]))\n", " print(w4_single.array [0,1], type(w4_single.array [0,1]))" ] }, { "cell_type": "code", "execution_count": null, "id": "c589b7c6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-24 \n" ] } ], "source": [ "w3_single.array [2,2] = 1000.0 # set a value in the new array\n", "w4_int8 = WolfArray(mold=w3_single, whichtype=WOLF_ARRAY_FULL_INTEGER8) # Try to convert to int8\n", "\n", "print(w4_int8.array [2,2], type(w4_int8.array [2,2])) # 1000.0 should be converted to 1000 but the type is int8 so 1000 is not possible" ] }, { "cell_type": "code", "execution_count": null, "id": "eb66a6e5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.int8(-24)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check the conversion of a \"BAD\" float to int8\n", "import numpy as np\n", "\n", "np.float32(1000.0).astype(np.int8) # 1000.0 should be converted to 1000 but the type is int8 so 1000 is not possible" ] } ], "metadata": { "kernelspec": { "display_name": "python3.10", "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.11" } }, "nbformat": 4, "nbformat_minor": 5 }