From b95f715085c48d32000f64bb9097b515fdf9f131 Mon Sep 17 00:00:00 2001 From: RUCT Date: Fri, 13 Dec 2024 10:05:36 +0100 Subject: [PATCH] made samplers and moved DOE into it --- .../test_of_categorical.ipynb | 1086 ----------------- .../{ => samplers}/doe/LICENSE_dexpy.md | 0 .../{ => samplers}/doe/__init__.py | 0 .../{ => samplers}/doe/doe_transform.py | 0 .../{ => samplers}/doe/doe_utils.py | 0 .../{ => samplers}/doe/optimal_design.py | 0 6 files changed, 1086 deletions(-) delete mode 100644 ProcessOptimizer/doe/various_dev_tests/test_of_categorical.ipynb rename ProcessOptimizer/{ => samplers}/doe/LICENSE_dexpy.md (100%) rename ProcessOptimizer/{ => samplers}/doe/__init__.py (100%) rename ProcessOptimizer/{ => samplers}/doe/doe_transform.py (100%) rename ProcessOptimizer/{ => samplers}/doe/doe_utils.py (100%) rename ProcessOptimizer/{ => samplers}/doe/optimal_design.py (100%) diff --git a/ProcessOptimizer/doe/various_dev_tests/test_of_categorical.ipynb b/ProcessOptimizer/doe/various_dev_tests/test_of_categorical.ipynb deleted file mode 100644 index 6af43058..00000000 --- a/ProcessOptimizer/doe/various_dev_tests/test_of_categorical.ipynb +++ /dev/null @@ -1,1086 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import ProcessOptimizer\n", - "from ProcessOptimizer.space import Integer, Real, Space, Categorical\n", - "from ProcessOptimizer.doe.optimal_design import get_optimal_DOE\n", - "from ProcessOptimizer.doe.doe_transform import doe_to_real_space\n", - "import numpy as np" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.0.2\n" - ] - } - ], - "source": [ - "print(ProcessOptimizer.__version__)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "from ProcessOptimizer.doe.doe_utils import generate_replicas_and_sort\n", - "\n", - "design_points = np.array([[1, 2], [3, 4]])\n", - "result = generate_replicas_and_sort(\n", - " design_points, 2, \"random_but_group_replicates\"\n", - " )" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[3 4]\n", - " [3 4]\n", - " [1 2]\n", - " [1 2]]\n" - ] - } - ], - "source": [ - "print(result)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "assert result[0].all() == result[2].all()" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "result[2][0]" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([1, 2], dtype=object)" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "result[2]" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "factor_space = Space(dimensions=[Integer(10, 40, name='int_var1'),\n", - " Integer(-40, 520, name='int_var2'),\n", - " Real(0.4, 117.7, name='real_var1'),\n", - " Categorical(['A', 'B'], name='cat_var1'),\n", - " # Categorical(['X', 'Y'], name='cat_var2'),\n", - " ])" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "int_var1\n", - "int_var2\n", - "real_var1\n", - "cat_var1\n", - "2\n" - ] - } - ], - "source": [ - "for factor in factor_space.dimensions:\n", - " print(factor.name)\n", - " if isinstance(factor, Categorical):\n", - " print(len(factor.categories))" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "design, factor_names = get_optimal_DOE(factor_space, 28, \n", - " design_type='response',\n", - " model=None,\n", - " replicates=1,\n", - " sorting='randomized',\n", - " res=31)\n", - "\n", - "\n", - "\n", - "#print(design)" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[['25' '520' '59.05' 'A']\n", - " ['40' '520' '0.4' 'A']\n", - " ['10' '520' '0.4' 'A']\n", - " ['40' '-40' '117.7' 'B']\n", - " ['40' '520' '117.7' 'B']\n", - " ['40' '-40' '0.4' 'B']\n", - " ['10' '-40' '55.14' 'A']\n", - " ['10' '520' '117.7' 'A']\n", - " ['25' '-40' '59.05' 'B']\n", - " ['24' '520' '0.4' 'B']\n", - " ['10' '-40' '0.4' 'B']\n", - " ['24' '-40' '0.4' 'A']\n", - " ['40' '-40' '117.7' 'A']\n", - " ['10' '520' '55.14' 'B']\n", - " ['10' '-40' '117.7' 'A']\n", - " ['23' '221' '117.7' 'B']\n", - " ['10' '520' '117.7' 'B']\n", - " ['40' '-40' '117.7' 'A']\n", - " ['40' '-40' '0.4' 'A']\n", - " ['40' '520' '117.7' 'B']\n", - " ['10' '-40' '117.7' 'B']\n", - " ['40' '259' '51.23' 'A']\n", - " ['10' '259' '0.4' 'B']\n", - " ['40' '520' '117.7' 'A']\n", - " ['10' '221' '0.4' 'A']\n", - " ['40' '221' '51.23' 'B']\n", - " ['40' '520' '0.4' 'B']\n", - " ['23' '259' '117.7' 'A']]\n", - "['int_var1', 'int_var2', 'real_var1', 'cat_var1']\n" - ] - } - ], - "source": [ - "print(design)\n", - "print(factor_names)" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'~ ul_indicator + ul_indicator2 + ul_indicator4 + antibody + ul_indicator:ul_indicator2 + ul_indicator:ul_indicator4 + ul_indicator:antibody + ul_indicator2:ul_indicator4 + ul_indicator2:antibody + ul_indicator4:antibody + pow(ul_indicator, 2) + pow(ul_indicator2, 2) + pow(ul_indicator4, 2)'" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "model = '(ul_indicator+ul_indicator2+ul_indicator4+antibody)**2+pow(ul_indicator, 2)+pow(ul_indicator2, 2)+pow(ul_indicator4, 2)'\n", - "\n", - "from patsy import ModelDesc\n", - "desc = ModelDesc.from_formula(model)\n", - "desc.describe()" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [], - "source": [ - "cat_var_levels = [None, None, 2]\n", - "include_powers = False" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [], - "source": [ - "if include_powers is True and any(level for level in cat_var_levels):\n", - " print(\"something is up\")" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "int_var1 is integer\n", - "int_var2 is integer\n", - "real_var1 is real\n", - "cat_var1 is categorical\n", - "2\n" - ] - } - ], - "source": [ - "for factor in factor_space.dimensions:\n", - " if isinstance(factor, Integer):\n", - " print(factor.name, \"is integer\")\n", - " elif isinstance(factor, Real):\n", - " print(factor.name, \"is real\")\n", - " elif isinstance(factor, Categorical):\n", - " print(factor.name, \"is categorical\")\n", - " print(len(factor.categories))\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "space = Space([Real(-100, 100, name='x1'), Real(0, 1, name='x2'), Categorical(['A', 'B'], name='x3')])\n", - "design = np.array([[0, 0, 0], [1, 1, 1]])\n", - "result = doe_to_real_space(design, space)\n", - "assert result[0] == [-100, 0, 'A']\n", - "assert result[1] == [100, 1, 'B']" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "def test_categorical_doe_to_real_space_rounding():\n", - " from ProcessOptimizer.space import Categorical\n", - " space = Space([Real(-100, 100, name='x1'), Real(0, 1, name='x2'),\n", - " Categorical(['A', 'B'], name='x3')])\n", - " design = np.array([[0, 0, 0.3], [1, 1, 0.8]])\n", - " result = doe_to_real_space(design, space)\n", - " assert result[0] == [-100, 0, 'A']\n", - " assert result[1] == [100, 1, 'B']\n", - "\n", - "\n", - "\n", - "def test_categorical_doe_to_real_space_edge():\n", - " from ProcessOptimizer.space import Categorical\n", - " space = Space([Real(-100, 100, name='x1'), Real(0, 1, name='x2'),\n", - " Categorical(['A', 'B'], name='x3')])\n", - " design = np.array([[0, 0, -42], [1, 1, 31]])\n", - " result = doe_to_real_space(design, space)\n", - " assert result[0] == [-100, 0, 'A']\n", - " assert result[1] == [100, 1, 'B']\n", - "\n", - "test_categorical_doe_to_real_space_edge()\n", - "test_categorical_doe_to_real_space_rounding()" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Went here\n" - ] - } - ], - "source": [ - "from ProcessOptimizer.doe.doe_utils import generate_replicas_and_sort\n", - "import numpy as np\n", - "\n", - "def test_generate_replicas_and_sort_random_but_group_replicates():\n", - " design_points = np.array([[1, 2], [3, 4]])\n", - " result = generate_replicas_and_sort(design_points, 2, \"random_but_group_replicates\")\n", - " assert result.shape == (4, 2)\n", - " assert result[1].all() == result[2].all()\n", - "\n", - "out = test_generate_replicas_and_sort_random_but_group_replicates()" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [], - "source": [ - "from ProcessOptimizer.doe.doe_utils import sanitize_names_for_patsy\n", - "\n", - "def test_sanitize_names_for_patsy():\n", - " factor_names = [\n", - " \"Factor 1\",\n", - " \"Factor-2\",\n", - " \"Factor+3\",\n", - " \"Factor*4\",\n", - " \"Factor/5\",\n", - " \"Factor:6\",\n", - " \"Factor^7\",\n", - " \"Factor=8\",\n", - " \"Factor~9\",\n", - " \"Factor$10\",\n", - " \"Factor(11)\",\n", - " \"Factor[12]\",\n", - " \"Factor{13}\",\n", - " ]\n", - " expected = [\n", - " \"Factor_1\",\n", - " \"Factor_2\",\n", - " \"Factor_3\",\n", - " \"Factor_4\",\n", - " \"Factor_5\",\n", - " \"Factor_6\",\n", - " \"Factor_7\",\n", - " \"Factor_8\",\n", - " \"Factor_9\",\n", - " \"Factor10\",\n", - " \"Factor11\",\n", - " \"Factor12\",\n", - " \"Factor13\",\n", - " ]\n", - " result = sanitize_names_for_patsy(factor_names)\n", - " assert result == expected\n", - "\n", - "out = test_sanitize_names_for_patsy()" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Space([Integer(low=10, high=40),\n", - " Integer(low=-40, high=520),\n", - " Real(low=0.4, high=117.7, prior='uniform', transform='normalize'),\n", - " Categorical(categories=('A', 'B'), prior=None)])\n" - ] - } - ], - "source": [ - "space = ProcessOptimizer.space.normalize_dimensions(factor_space)\n", - "print(space)" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[ 1. 1. 1. ]\n", - " [ 1. 1. -1. ]\n", - " [-1. 1. 1. ]\n", - " [-1. 1. -1. ]\n", - " [ 0.4 -1. 1. ]\n", - " [ 0. -1. -1. ]\n", - " [-1. -1. -1. ]\n", - " [ 1. -1. -1. ]\n", - " [-1. -1. 1. ]\n", - " [ 0. 0. -1. ]\n", - " [ 1. -0.2 1. ]\n", - " [ 0.09840003 -0.32569928 -1. ]]\n" - ] - } - ], - "source": [ - "from ProcessOptimizer.doe.optimal_design import build_optimal_design\n", - "\n", - "def optimal_design_space():\n", - " return Space([Real(20, 100, name='x1'), Real(0, 1, name='x2'),\n", - " Categorical(['A', 'B'], name='x3')])\n", - "\n", - "\n", - "def test_build_optimal_design_with_categorical():\n", - " factor_names = ['x1', 'x2', 'x3']\n", - " space = optimal_design_space()\n", - "\n", - " result = build_optimal_design(factor_names, run_count=12, space=space)\n", - "\n", - " print(result)\n", - "\n", - " assert result.shape == (12, 3)\n", - " assert np.all(np.isin(result[:, 2], [-1, 1]))\n", - "\n", - "test_build_optimal_design_with_categorical()" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[np.float64(-100.0), np.float64(0.0), 'A'], [np.float64(100.0), np.float64(1.0), 'B']]\n", - "[[np.float64(-100.0) np.float64(0.0) 'A']\n", - " [np.float64(100.0) np.float64(1.0) 'B']\n", - " [np.float64(-100.0) np.float64(0.0) 'A']\n", - " [np.float64(100.0) np.float64(1.0) 'B']]\n", - "(4, 3)\n" - ] - } - ], - "source": [ - "def test_categorical_doe_to_real_space():\n", - " space = Space([Real(-100, 100, name='x1'), Real(0, 1, name='x2'),\n", - " Categorical(['A', 'B'], name='x3')])\n", - " design = np.array([[0, 0, 0], [1, 1, 1]])\n", - " result = doe_to_real_space(design, space)\n", - "\n", - " assert result[0] == [-100, 0, 'A']\n", - " assert result[1] == [100, 1, 'B']\n", - " return result\n", - "\n", - "\n", - "\n", - "converted = test_categorical_doe_to_real_space()\n", - "print(converted)\n", - "\n", - "from ProcessOptimizer.doe.doe_utils import generate_replicas_and_sort\n", - "\n", - "\n", - "result = generate_replicas_and_sort(converted, 2, False)\n", - "print(result)\n", - "\n", - "\n", - "print(result.shape)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "def try_convert_to_float(s):\n", - " \"\"\"\n", - " Try to convert a string to a float. If it fails, return the string.\n", - "\n", - " :param s: The string to convert\n", - " :type s: str\n", - "\n", - " :return: The float or the string\n", - " :rtype: float or str\n", - " \"\"\"\n", - " try:\n", - " return float(s)\n", - " except Exception:\n", - " return s" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[1 2 'A']\n", - " [3 4 'B']]\n", - "[[1 2 'A']\n", - " [3 4 'B']\n", - " [1 2 'A']\n", - " [3 4 'B']]\n", - "[[1 2 'A']\n", - " [3 4 'B']\n", - " [1 2 'A']\n", - " [3 4 'B']]\n" - ] - } - ], - "source": [ - "from ProcessOptimizer.doe.doe_utils import generate_replicas_and_sort\n", - "\n", - "def test_generate_replicas_and_sort_no_sorting():\n", - " design_points = np.array([[1, 2, 'A'], [3, 4, 'B']], dtype=object)\n", - " #print(design_points)\n", - " result = generate_replicas_and_sort(design_points, 2, False)\n", - " print(result)\n", - " expected = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])\n", - " #np.testing.assert_array_equal(result, expected)\n", - " return result\n", - "\n", - "result = test_generate_replicas_and_sort_no_sorting()\n", - "\n", - "print(result)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "ename": "ValueError", - "evalue": "could not convert string to float: 'invalid'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[28], line 16\u001b[0m\n\u001b[0;32m 13\u001b[0m vectorized_conversion \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mvectorize(try_convert_to_float)\n\u001b[0;32m 15\u001b[0m \u001b[38;5;66;03m# Apply the conversion function to the entire array\u001b[39;00m\n\u001b[1;32m---> 16\u001b[0m converted_data \u001b[38;5;241m=\u001b[39m \u001b[43mvectorized_conversion\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 18\u001b[0m \u001b[38;5;28mprint\u001b[39m(converted_data)\n", - "File \u001b[1;32mc:\\Users\\RUCT\\ProcessOptimizer\\.venv\\lib\\site-packages\\numpy\\lib\\_function_base_impl.py:2397\u001b[0m, in \u001b[0;36mvectorize.__call__\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 2394\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_init_stage_2(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m 2395\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\n\u001b[1;32m-> 2397\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call_as_normal(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n", - "File \u001b[1;32mc:\\Users\\RUCT\\ProcessOptimizer\\.venv\\lib\\site-packages\\numpy\\lib\\_function_base_impl.py:2390\u001b[0m, in \u001b[0;36mvectorize._call_as_normal\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 2387\u001b[0m vargs \u001b[38;5;241m=\u001b[39m [args[_i] \u001b[38;5;28;01mfor\u001b[39;00m _i \u001b[38;5;129;01min\u001b[39;00m inds]\n\u001b[0;32m 2388\u001b[0m vargs\u001b[38;5;241m.\u001b[39mextend([kwargs[_n] \u001b[38;5;28;01mfor\u001b[39;00m _n \u001b[38;5;129;01min\u001b[39;00m names])\n\u001b[1;32m-> 2390\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_vectorize_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfunc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvargs\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[1;32mc:\\Users\\RUCT\\ProcessOptimizer\\.venv\\lib\\site-packages\\numpy\\lib\\_function_base_impl.py:2483\u001b[0m, in \u001b[0;36mvectorize._vectorize_call\u001b[1;34m(self, func, args)\u001b[0m\n\u001b[0;32m 2480\u001b[0m outputs \u001b[38;5;241m=\u001b[39m ufunc(\u001b[38;5;241m*\u001b[39minputs)\n\u001b[0;32m 2482\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m ufunc\u001b[38;5;241m.\u001b[39mnout \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m-> 2483\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[43masanyarray\u001b[49m\u001b[43m(\u001b[49m\u001b[43moutputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43motypes\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2484\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 2485\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mtuple\u001b[39m([asanyarray(x, dtype\u001b[38;5;241m=\u001b[39mt)\n\u001b[0;32m 2486\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m x, t \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(outputs, otypes)])\n", - "\u001b[1;31mValueError\u001b[0m: could not convert string to float: 'invalid'" - ] - } - ], - "source": [ - "data = np.array([['1.5', '2.7', '3.2'],\n", - " ['4.1', '5.3', '6.0'],\n", - " ['7.2', 'invalid', '9.9']])\n", - "\n", - "# Function to convert strings to floats or NaN\n", - "def try_convert_to_float(s):\n", - " try:\n", - " return float(s)\n", - " except ValueError:\n", - " return np.nan\n", - "\n", - "# Vectorize the conversion function\n", - "vectorized_conversion = np.vectorize(try_convert_to_float)\n", - "\n", - "# Apply the conversion function to the entire array\n", - "converted_data = vectorized_conversion(data)\n", - "\n", - "print(converted_data)" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.5\n", - "\n", - "\n", - "2.7\n", - "\n", - "\n", - "3.2\n", - "\n", - "\n", - "4.1\n", - "\n", - "\n", - "5.3\n", - "\n", - "\n", - "6.0\n", - "\n", - "\n", - "7.2\n", - "\n", - "\n", - "invalid\n", - "failed conversion\n", - "\n", - "\n", - "9.9\n", - "\n", - "\n", - "[[1.5 2.7 3.2]\n", - " [4.1 5.3 6.0]\n", - " [7.2 'invalid' 9.9]]\n" - ] - } - ], - "source": [ - "data = np.array([['1.5', '2.7', '3.2'],\n", - " ['4.1', '5.3', '6.0'],\n", - " ['7.2', 'invalid', '9.9']], dtype=object)\n", - "\n", - "# Function to convert strings to floats or NaN\n", - "def try_convert_to_float(s):\n", - " try:\n", - " float_s = float(s)\n", - " #return np.float64(float_s)\n", - " return float_s\n", - " except ValueError:\n", - " print('failed conversion')\n", - " return s\n", - " \n", - "for i in range(data.shape[0]):\n", - " for j in range(data.shape[1]):\n", - " print(data[i, j])\n", - " converted = try_convert_to_float(data[i, j])\n", - " print(type(converted))\n", - " data[i, j] = converted\n", - " print(type(data[i, j]))\n", - "\n", - "print(data)" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[np.float64(0.20000000000000018), np.int64(9), np.str_('foo')], [np.float64(0.6000000000000001), np.int64(18), np.str_('bar')]]\n" - ] - } - ], - "source": [ - "from ProcessOptimizer import Optimizer\n", - "\n", - "opt = Optimizer([(-2.0, 2.0), (0,21), ('foo', 'bar')], \"ET\", acq_optimizer=\"sampling\")\n", - "\n", - "next2 = opt.ask(2)\n", - "\n", - "print(next2)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "space = Space([Real(20, 100, name='x1'), Real(0, 1, name='x2'),\n", - " Categorical(['A', 'B'], name='x3')])\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[ 0. 5. ]\n", - " [ 0. -5. ]\n", - " [10. 5. ]\n", - " [ 0. -1.66666667]\n", - " [ 6.66666667 3.33333333]\n", - " [10. 1.66666667]\n", - " [10. -5. ]\n", - " [ 6.66666667 -5. ]\n", - " [ 1.66666667 1.66666667]\n", - " [ 3.33333333 5. ]\n", - " [ 3.33333333 -3.33333333]\n", - " [ 8.33333333 -1.66666667]]\n" - ] - } - ], - "source": [ - "def sample_space():\n", - " return Space([Real(0, 10, name='x1'), Real(-5, 5, name='x2')])\n", - "\n", - "def test_get_optimal_DOE_without_categorical():\n", - " space = sample_space()\n", - " result, factor_names = get_optimal_DOE(space, 12, design_type='optimization', res=7)\n", - " print(result)\n", - " assert result.shape == (12, 2)\n", - " assert np.all(result[:, 0] >= 0) and np.all(result[:, 0] <= 10)\n", - " assert np.all(result[:, 1] >= -5) and np.all(result[:, 1] <= 5)\n", - " assert factor_names == ['x1', 'x2']\n", - "\n", - "test_get_optimal_DOE_without_categorical()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[np.float64(100.0) np.float64(0.0) 'B']\n", - " [np.float64(60.0) np.float64(0.0) 'A']\n", - " [np.float64(100.0) np.float64(1.0) 'A']\n", - " [np.float64(20.0) np.float64(1.0) 'A']\n", - " [np.float64(20.0) np.float64(0.0) 'B']\n", - " [np.float64(60.0) np.float64(1.0) 'B']]\n" - ] - } - ], - "source": [ - "def optimal_design_space():\n", - " return Space([Real(20, 100, name='x1'), Real(0, 1, name='x2'),\n", - " Categorical(['A', 'B'], name='x3')])\n", - "\n", - "def test_get_optimal_DOE():\n", - " space = optimal_design_space()\n", - "\n", - " for design_type in ['linear', 'screening', 'response', 'optimization']:\n", - "\n", - " design, factor_names = get_optimal_DOE(space, 16, design_type=design_type, res=5)\n", - "\n", - " assert design.shape == (16, 3)\n", - " assert np.all(design[:, 0] >= 20) and np.all(design[:, 0] <= 100)\n", - " assert np.all(design[:, 1] >= 0) and np.all(design[:, 1] <= 1)\n", - " assert [entry in ['A', 'B'] for entry in design[:, 2]]\n", - " assert factor_names == ['x1', 'x2', 'x3']\n", - "\n", - "test_get_optimal_DOE()\n", - "\n", - "\n", - "def test_custom_model():\n", - " space = optimal_design_space()\n", - "\n", - " custom_model = \"x1 + x2 + x3 + x1:x2 + pow(x1, 2)\"\n", - "\n", - " design, factor_names = get_optimal_DOE(space, 6, res=5, model=custom_model)\n", - "\n", - " assert design.shape == (6, 3)\n", - " assert np.all(design[:, 0] >= 20) and np.all(design[:, 0] <= 100)\n", - " assert np.all(design[:, 1] >= 0) and np.all(design[:, 1] <= 1)\n", - " assert [entry in [\"A\", \"B\"] for entry in design[:, 2]]\n", - " assert factor_names == [\"x1\", \"x2\", \"x3\"]\n", - "\n", - "test_custom_model()" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Space([Integer(low=10, high=40),\n", - " Categorical(categories=('A', 'B'), prior=None),\n", - " Categorical(categories=('A', 'B', 'c', 'd'), prior=None),\n", - " Categorical(categories=('A', 'B', 'c', 'd'), prior=None)])\n" - ] - } - ], - "source": [ - "print(factor_space)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def sample_space():\n", - " return Space([Real(0, 10, name='x1'), Real(-5, 5, name='x2')])\n", - "\n", - "def test_doe_to_real_space_basic(sample_space):\n", - "\n", - " design = np.array([[0, 0], [1, 1]])\n", - " result = doe_to_real_space(design, sample_space)\n", - " assert np.asarray(result).shape == (2, 2)\n", - " assert np.allclose(result[0], [0, -5])\n", - " assert np.allclose(result[1], [10, 5])\n", - "\n", - "test_doe_to_real_space_basic(sample_space())\n", - "\n", - "def test_doe_to_real_space_scaler(sample_space):\n", - " design = np.array([[0.25, 0.75], [0.75, 0.25]])\n", - " corner_points = [[0, 0], [1, 1]]\n", - " result = doe_to_real_space(design, sample_space, corner_points=corner_points)\n", - " assert np.allclose(result[0], [2.5, 2.5])\n", - " assert np.allclose(result[1], [7.5, -2.5])\n", - "\n", - "test_doe_to_real_space_scaler(sample_space())\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[-1, -1, -1, -1, -1]\n", - "[1, 1, 1, 1, 1]\n" - ] - } - ], - "source": [ - "factor_names = ['fw', 'bw', 'x1', 'x2', 'x3']\n", - "\n", - "corner_neg = [-1] * len(factor_names)\n", - "corner_pos = [1] * len(factor_names)\n", - "\n", - "print(corner_neg)\n", - "print(corner_pos)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[-1. 1. -1. ]\n", - " [-1. 1. 1. ]\n", - " [-1. -1. -1. ]\n", - " [ 1. 0. 1. ]\n", - " [-1. -1. 1. ]\n", - " [-1. 0. 0. ]\n", - " [ 0. -1. 0. ]\n", - " [ 1. 1. -1. ]\n", - " [ 0. 0.03058772 -1. ]\n", - " [ 1. -1. -1. ]\n", - " [ 1. -1. 1. ]\n", - " [ 1. 1. 1. ]]\n" - ] - }, - { - "ename": "AssertionError", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[3], line 13\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(result[:, :\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(result[:, :\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m 11\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(np\u001b[38;5;241m.\u001b[39misin(result[:, \u001b[38;5;241m2\u001b[39m], [\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1\u001b[39m]))\n\u001b[1;32m---> 13\u001b[0m \u001b[43mtest_build_optimal_design_vanilla\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", - "Cell \u001b[1;32mIn[3], line 10\u001b[0m, in \u001b[0;36mtest_build_optimal_design_vanilla\u001b[1;34m()\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m result\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;241m==\u001b[39m (\u001b[38;5;241m12\u001b[39m, \u001b[38;5;241m3\u001b[39m)\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(result)\n\u001b[1;32m---> 10\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(result[:, :\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(result[:, :\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m 11\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(np\u001b[38;5;241m.\u001b[39misin(result[:, \u001b[38;5;241m2\u001b[39m], [\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1\u001b[39m]))\n", - "\u001b[1;31mAssertionError\u001b[0m: " - ] - } - ], - "source": [ - "from ProcessOptimizer.doe.optimal_design import build_optimal_design\n", - "\n", - "def test_build_optimal_design_vanilla():\n", - " factor_names = ['x1', 'x2', 'x3']\n", - "\n", - " result = build_optimal_design(factor_names, run_count=12)\n", - "\n", - " assert result.shape == (12, 3)\n", - " print(result)\n", - " assert np.all(result[:, :2] >= 0) and np.all(result[:, :2] <= 1)\n", - " assert np.all(np.isin(result[:, 2], [-1, 1]))\n", - "\n", - "test_build_optimal_design_vanilla()" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array(1)" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.where(0.3 < 0, -1, 1)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from sklearn.preprocessing import MinMaxScaler\n", - "\n", - "scaler = MinMaxScaler(feature_range=(0., 1.))\n", - "\n", - "\n", - "\n", - "design = np.array([[0.25, 0.75], [0.75, 0.25]])" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['c:\\\\Users\\\\RUCT\\\\OneDrive - Novo Nordisk\\\\Python Scripts\\\\ProcessOptimizer\\\\ProcessOptimizer\\\\doe\\\\various_dev_tests', 'c:\\\\Users\\\\RUCT\\\\AppData\\\\Local\\\\anaconda3\\\\python311.zip', 'c:\\\\Users\\\\RUCT\\\\AppData\\\\Local\\\\anaconda3\\\\DLLs', 'c:\\\\Users\\\\RUCT\\\\AppData\\\\Local\\\\anaconda3\\\\Lib', 'c:\\\\Users\\\\RUCT\\\\AppData\\\\Local\\\\anaconda3', '', 'c:\\\\Users\\\\RUCT\\\\AppData\\\\Local\\\\anaconda3\\\\Lib\\\\site-packages', 'c:\\\\users\\\\ruct\\\\onedrive - novo nordisk\\\\python scripts\\\\my_process_optimizer\\\\processoptimizer', 'c:\\\\Users\\\\RUCT\\\\AppData\\\\Local\\\\anaconda3\\\\Lib\\\\site-packages\\\\win32', 'c:\\\\Users\\\\RUCT\\\\AppData\\\\Local\\\\anaconda3\\\\Lib\\\\site-packages\\\\win32\\\\lib', 'c:\\\\Users\\\\RUCT\\\\AppData\\\\Local\\\\anaconda3\\\\Lib\\\\site-packages\\\\Pythonwin', 'c:\\\\Users\\\\RUCT\\\\AppData\\\\Local\\\\anaconda3\\\\Lib\\\\site-packages\\\\setuptools\\\\_vendor']\n" - ] - } - ], - "source": [ - "import sys\n", - "\n", - "print(sys.path)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from ProcessOptimizer.doe.optimal_design import bootstrap, make_model" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "space = None" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'patsy' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[22], line 17\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m result\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;241m==\u001b[39m design\u001b[38;5;241m.\u001b[39mshape\n\u001b[0;32m 15\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(result \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(result \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m---> 17\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43mtest_optimize_design\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", - "Cell \u001b[1;32mIn[22], line 12\u001b[0m, in \u001b[0;36mtest_optimize_design\u001b[1;34m()\u001b[0m\n\u001b[0;32m 8\u001b[0m X \u001b[38;5;241m=\u001b[39m patsy\u001b[38;5;241m.\u001b[39mdmatrix(model, {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mx1\u001b[39m\u001b[38;5;124m\"\u001b[39m: [\u001b[38;5;241m0.5\u001b[39m, \u001b[38;5;241m0.5\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m0.45\u001b[39m], \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mx2\u001b[39m\u001b[38;5;124m\"\u001b[39m: [\u001b[38;5;241m0.5\u001b[39m, \u001b[38;5;241m0.5\u001b[39m, \u001b[38;5;241m0.45\u001b[39m], \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mx3\u001b[39m\u001b[38;5;124m\"\u001b[39m: [\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m0.45\u001b[39m]})\n\u001b[0;32m 10\u001b[0m code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpatsy.dmatrix(\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx1 + x2 + x3\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m{\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx1\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m: [design_point[\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx1\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m]], \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx2\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m: [design_point[\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx2\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m]], \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx3\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m: [design_point[\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx3\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m]]})[0]\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m---> 12\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43moptimize_design\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdesign\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfactor_names\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcode\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 14\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m result\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;241m==\u001b[39m design\u001b[38;5;241m.\u001b[39mshape\n\u001b[0;32m 15\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(result \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m np\u001b[38;5;241m.\u001b[39mall(result \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m)\n", - "File \u001b[1;32m~\\ProcessOptimizer\\ProcessOptimizer\\doe\\optimal_design.py:383\u001b[0m, in \u001b[0;36moptimize_design\u001b[1;34m(X, design, factor_names, code, **kwargs)\u001b[0m\n\u001b[0;32m 380\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m s \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m0\u001b[39m, steps):\n\u001b[0;32m 382\u001b[0m design_point[f] \u001b[38;5;241m=\u001b[39m low \u001b[38;5;241m+\u001b[39m ((high \u001b[38;5;241m-\u001b[39m low) \u001b[38;5;241m/\u001b[39m (steps \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m)) \u001b[38;5;241m*\u001b[39m s\n\u001b[1;32m--> 383\u001b[0m new_point \u001b[38;5;241m=\u001b[39m \u001b[43mexpand_point\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdesign_point\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcode\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 385\u001b[0m change_in_d \u001b[38;5;241m=\u001b[39m delta(X, XtXi, i, new_point)\n\u001b[0;32m 387\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m change_in_d \u001b[38;5;241m-\u001b[39m best_change \u001b[38;5;241m>\u001b[39m np\u001b[38;5;241m.\u001b[39mfinfo(\u001b[38;5;28mfloat\u001b[39m)\u001b[38;5;241m.\u001b[39meps:\n", - "File \u001b[1;32m~\\ProcessOptimizer\\ProcessOptimizer\\doe\\optimal_design.py:131\u001b[0m, in \u001b[0;36mexpand_point\u001b[1;34m(design_point, code)\u001b[0m\n\u001b[0;32m 123\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexpand_point\u001b[39m(design_point, code):\n\u001b[0;32m 124\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Converts a point in factor space to conform with the X matrix.\u001b[39;00m\n\u001b[0;32m 125\u001b[0m \n\u001b[0;32m 126\u001b[0m \u001b[38;5;124;03m This function is from https://github.com/statease/dexpy\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 129\u001b[0m \u001b[38;5;124;03m License link: https://github.com/statease/dexpy/blob/master/LICENSE\u001b[39;00m\n\u001b[0;32m 130\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[1;32m--> 131\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39marray(\u001b[38;5;28;43meval\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdesign_point\u001b[49m\u001b[43m)\u001b[49m)\n", - "File \u001b[1;32m:1\u001b[0m\n", - "\u001b[1;31mNameError\u001b[0m: name 'patsy' is not defined" - ] - } - ], - "source": [ - "from ProcessOptimizer.doe.optimal_design import optimize_design\n", - "import patsy\n", - "\n", - "def test_optimize_design():\n", - " factor_names = ['x1', 'x2', 'x3']\n", - " design = np.array([[0.5, 0.5, -1], [0.5, 0.5, 1], [-0.45, 0.45, 0.45]])\n", - " model = \"x1 + x2 + x3\"\n", - " X = patsy.dmatrix(model, {\"x1\": [0.5, 0.5, -0.45], \"x2\": [0.5, 0.5, 0.45], \"x3\": [-1, 1, 0.45]})\n", - " \n", - " code = \"patsy.dmatrix('x1 + x2 + x3', {'x1': [design_point['x1']], 'x2': [design_point['x2']], 'x3': [design_point['x3']]})[0]\"\n", - " \n", - " result = optimize_design(X, design, factor_names, code)\n", - " \n", - " assert result.shape == design.shape\n", - " assert np.all(result >= -1) and np.all(result <= 1)\n", - "\n", - "out = test_optimize_design()" - ] - }, - { - "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.9.1" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/ProcessOptimizer/doe/LICENSE_dexpy.md b/ProcessOptimizer/samplers/doe/LICENSE_dexpy.md similarity index 100% rename from ProcessOptimizer/doe/LICENSE_dexpy.md rename to ProcessOptimizer/samplers/doe/LICENSE_dexpy.md diff --git a/ProcessOptimizer/doe/__init__.py b/ProcessOptimizer/samplers/doe/__init__.py similarity index 100% rename from ProcessOptimizer/doe/__init__.py rename to ProcessOptimizer/samplers/doe/__init__.py diff --git a/ProcessOptimizer/doe/doe_transform.py b/ProcessOptimizer/samplers/doe/doe_transform.py similarity index 100% rename from ProcessOptimizer/doe/doe_transform.py rename to ProcessOptimizer/samplers/doe/doe_transform.py diff --git a/ProcessOptimizer/doe/doe_utils.py b/ProcessOptimizer/samplers/doe/doe_utils.py similarity index 100% rename from ProcessOptimizer/doe/doe_utils.py rename to ProcessOptimizer/samplers/doe/doe_utils.py diff --git a/ProcessOptimizer/doe/optimal_design.py b/ProcessOptimizer/samplers/doe/optimal_design.py similarity index 100% rename from ProcessOptimizer/doe/optimal_design.py rename to ProcessOptimizer/samplers/doe/optimal_design.py