{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Part Section to Layers\n", "Splits part section file into smaller layer files." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import os\n", "import pickle\n", "import pyarrow as pa\n", "import pyarrow.parquet as pq\n", "\n", "from tqdm import tqdm " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "CHECK_SHAPE = False\n", "\n", "layer_folder = \"layer\"\n", "layer_table_folder = \"layer_table\"\n", "\n", "# config_folder = \"base\"\n", "# config_folder = \"block\"\n", "# config_folder = \"overhang_no_supports\"\n", "config_folder = \"overhang_with_supports\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "non_array = [str, int, np.uint8, np.uint16, np.float64]\n", "for layer_file in tqdm(os.listdir(f\"{layer_folder}/{config_folder}\")):\n", " # print(layer_file)\n", " with open(f\"{layer_folder}/{config_folder}/{layer_file}\", \"rb\") as f:\n", " layer = pickle.load(f)\n", "\n", " pydict = {}\n", " layer_number = layer[\"layer_number\"]\n", " for key, value in layer.items():\n", " if (type(value) in non_array):\n", " pydict[key] = [value]\n", " elif (isinstance(value, np.ndarray)):\n", " # print(key, value.shape, type(value.shape))\n", " pydict[f\"{key}_shape\"] = [value.shape]\n", " pydict[key] = [value.flatten()]\n", " else:\n", " print(key, type(value))\n", " # print(pydict)\n", " table = pa.Table.from_pydict(pydict)\n", " pq.write_table(table, f\"{layer_table_folder}/{config_folder}/{layer_number}.parquet\")\n", " # print(table)\n", "\n", " if CHECK_SHAPE:\n", " radiant_temps = layer[\"radiant_temp\"]\n", " radiant_temps_shape = radiant_temps.shape\n", " radiant_temps_flat = radiant_temps.flatten()\n", " radiant_temps_arrow = pa.array(radiant_temps_flat)\n", "\n", " radiant_temps_reshaped = radiant_temps_flat.reshape(radiant_temps_shape)\n", "\n", " # print(radiant_temps_reshaped)\n", " arrow_radiant_temps_flat = table[\"radiant_temp\"]\n", " # print(\"called\")\n", " # print(arrow_radiant_temps_flat[0])\n", " arrow_radiant_temps_shape = table[\"radiant_temp_shape\"][0]\n", "\n", " # print(arrow_radiant_temps_flat)\n", " arrow_radiant_temps_reshaped = arrow_radiant_temps_flat.reshape(arrow_radiant_temps_shape)\n", "\n", " plt.imshow(radiant_temps[100])\n", " plt.show()\n", "\n", " plt.imshow(radiant_temps_reshaped[100])\n", " plt.show()\n", "\n", " plt.imshow(arrow_radiant_temps_reshaped[100])\n", " plt.show()\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "layer_number = 102\n", "table = pq.read_table(f\"{layer_table_folder}/{config_folder}/{layer_number}.parquet\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "layer = table.to_pydict()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "folder_layer_range 101-110\n", "part OverhangPart\n", "part_section OVERHANG-05deg\n", "process LPBFthermography\n", "source NIST\n", "supports wSup\n", "layer_number 102\n", "(3, 664)\n", "contact_email jarred.heigel@nist.gov\n", "file_name 20180801_OverhangStudy_Layer102.mat\n", "hatch_spacing 100\n", "laser_power 195\n", "layer_thickness 20\n", "material IN625\n", "(664, 126, 360)\n", "(1, 664)\n", "(2, 1)\n", "s_hvariable__a 2.655\n", "s_hvariable__b -800.7\n", "s_hvariable__c 1940000.0\n", "scan_speed 800\n", "website nist.gov/el/lpbf-thermography/3D-part-builds/OverhangPart-IN625\n" ] } ], "source": [ "non_array = [str, int, float]\n", "converted_layer = {}\n", "for key, value in layer.items():\n", " layer_value = value[0]\n", " # print(key, type(layer_value))\n", " if (type(layer_value) in non_array):\n", " print(key, layer_value)\n", " converted_layer[key] = layer_value\n", " elif(isinstance(value, list) and \"shape\" not in key):\n", " shape = layer[f\"{key}_shape\"][0]\n", " flattened_array = np.array(layer_value)\n", " array = flattened_array.reshape(shape)\n", " print(array.shape)\n", " converted_layer[key] = array\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "folder_layer_range \n", "part \n", "part_section \n", "process \n", "source \n", "supports \n", "layer_number \n", "build_time \n", "(3, 664)\n", "contact_email \n", "file_name \n", "hatch_spacing \n", "laser_power \n", "layer_thickness \n", "material \n", "radiant_temp \n", "(664, 126, 360)\n", "raw_frame_number \n", "(1, 664)\n", "resolution \n", "(2, 1)\n", "s_hvariable__a \n", "s_hvariable__b \n", "s_hvariable__c \n", "scan_speed \n", "website \n" ] } ], "source": [ "for key, value in converted_layer.items():\n", " print(key, type(value))\n", " if(isinstance(value, np.ndarray)):\n", " print(value.shape)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"called\", table)\n", "arrow_radiant_temps_flat = np.array(table[\"radiant_temp\"][0].as_py())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# arrow_radiant_temps_shape = [int(size) for size in table[\"radiant_temp_shape\"][0]]\n", "arrow_radiant_temps_shape = tuple(table[\"radiant_temp_shape\"][0].as_py())\n", "print(arrow_radiant_temps_shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arrow_radiant_temps_reshaped = arrow_radiant_temps_flat.reshape(arrow_radiant_temps_shape)\n", "print(arrow_radiant_temps_reshaped[100])\n", "plt.imshow(arrow_radiant_temps_reshaped[100])\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for layer in layers:\n", " layer_number = layer[\"layer_number\"]\n", " with open(f\"{layers_folder}/{config_folder}/{layer_number}.pkl\", \"wb\") as f:\n", " pickle.dump(layer, f)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "layers[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "layer_number = 1\n", "with open(f\"{layers_folder}/{config_folder}/{layer_number}.pkl\", \"rb\") as f:\n", " layer = pickle.load(f)\n", "\n", "print(layer)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }