{ "cells": [ { "cell_type": "markdown", "id": "df71c286-78ab-43f8-8867-99ce2eac544a", "metadata": {}, "source": [ "# Unit Test for Calculate_MDI" ] }, { "cell_type": "code", "execution_count": 1, "id": "5c09c4fd-2809-4442-aa54-a060188d861b", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/miniconda3/envs/mesa/lib/python3.11/site-packages/geopandas/_compat.py:106: UserWarning: The Shapely GEOS version (3.8.0-CAPI-1.13.1) is incompatible with the GEOS version PyGEOS was compiled with (3.10.4-CAPI-1.16.2). Conversions between both will be slow.\n", " warnings.warn(\n", "OMP: Info #276: omp_set_nested routine deprecated, please use omp_set_max_active_levels instead.\n", "/opt/miniconda3/envs/mesa/lib/python3.11/site-packages/spaghetti/network.py:41: FutureWarning: The next major release of pysal/spaghetti (2.0.0) will drop support for all ``libpysal.cg`` geometries. This change is a first step in refactoring ``spaghetti`` that is expected to result in dramatically reduced runtimes for network instantiation and operations. Users currently requiring network and point pattern input as ``libpysal.cg`` geometries should prepare for this simply by converting to ``shapely`` geometries.\n", " warnings.warn(dep_msg, FutureWarning, stacklevel=1)\n" ] } ], "source": [ "import unittest\n", "import numpy as np\n", "import pandas as pd\n", "import anndata as ad\n", "from unittest.mock import patch\n", "import os\n", "os.sys.path.append('../../../')\n", " \n", "from mesa import ecospatial as eco" ] }, { "cell_type": "code", "execution_count": 2, "id": "fa4d769c-53ea-4db7-9f53-e677adce6744", "metadata": {}, "outputs": [], "source": [ "class TestCalculateMDI(unittest.TestCase):\n", " def setUp(self):\n", " # Set up test data\n", " self.spatial_data = pd.DataFrame({\n", " 'x': 1000 * np.random.rand(5000),\n", " 'y': 1000 * np.random.rand(5000),\n", " 'library_key': ['sample_1'] * 2500 + ['sample_2'] * 2500,\n", " 'cluster_key': np.random.randint(0, 10, size=5000)\n", " })\n", " self.scales = [1., 2., 4., 8.]\n", " self.library_key = 'library_key'\n", " self.library_id = ['sample_1', 'sample_2']\n", " self.spatial_key = ['x', 'y']\n", " self.cluster_key = 'cluster_key'\n", " self.expected_index = self.library_id\n", " self.expected_columns = [scale for scale in self.scales] + ['Slope']\n", " \n", " def test_calculate_MDI_with_random_patch_false(self):\n", " # Call the function under test with random_patch=False\n", " result = eco.calculate_MDI(\n", " spatial_data=self.spatial_data,\n", " scales=self.scales,\n", " library_key=self.library_key,\n", " library_id=self.library_id,\n", " spatial_key=self.spatial_key,\n", " cluster_key=self.cluster_key,\n", " random_patch=False,\n", " plotfigs=False,\n", " savefigs=False,\n", " patch_kwargs={}, \n", " other_kwargs={}\n", " )\n", " # Check that result is a DataFrame\n", " self.assertIsInstance(result, pd.DataFrame)\n", " \n", " # Check that the DataFrame has the expected index and columns\n", " self.assertListEqual(list(result.columns), self.expected_columns)\n", " self.assertListEqual(list(result.index), self.expected_index)\n", "\n", " # Verify that 'Slope' column exists and is numeric\n", " self.assertIn('Slope', result.columns)\n", " self.assertTrue(np.issubdtype(result['Slope'].dtype, np.number))\n", "\n", " def test_calculate_MDI_with_random_patch_true(self):\n", " # Call the function under test with random_patch=True\n", " result_random = eco.calculate_MDI(\n", " spatial_data=self.spatial_data,\n", " scales=self.scales,\n", " library_key=self.library_key,\n", " library_id=self.library_id,\n", " spatial_key=self.spatial_key,\n", " cluster_key=self.cluster_key,\n", " random_patch=True,\n", " plotfigs=False,\n", " savefigs=False,\n", " patch_kwargs={}, \n", " other_kwargs={}\n", " )\n", " \n", " # Check the results as before\n", " self.assertIsInstance(result_random, pd.DataFrame)\n", " self.assertListEqual(list(result_random.columns), self.expected_columns)\n", " self.assertListEqual(list(result_random.index), self.expected_index)\n", "\n", " # Verify that 'Slope' column exists and is numeric\n", " self.assertIn('Slope', result_random.columns)\n", " self.assertTrue(np.issubdtype(result_random['Slope'].dtype, np.number))\n", " " ] }, { "cell_type": "code", "execution_count": 3, "id": "6e429722-44eb-4b93-9f8e-a99f5d394f29", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Processing region: sample_1 at scale 1.0\n", "0.000 per cent patches are empty\n", "sample_1 at scale 1.0 has 0 patches with zero diveristy\n", "sample_1 at scale 1.0 diversity is 3.320548430536853\n", "Processing region: sample_2 at scale 1.0\n", "0.000 per cent patches are empty\n", "sample_2 at scale 1.0 has 0 patches with zero diveristy\n", "sample_2 at scale 1.0 diversity is 3.318675527712075\n", "Processing region: sample_1 at scale 2.0\n", "0.000 per cent patches are empty\n", "sample_1 at scale 2.0 has 0 patches with zero diveristy\n", "sample_1 at scale 2.0 diversity is 3.3157615698139944\n", "Processing region: sample_2 at scale 2.0\n", "0.000 per cent patches are empty\n", "sample_2 at scale 2.0 has 0 patches with zero diveristy\n", "sample_2 at scale 2.0 diversity is 3.3098586262794134\n", "Processing region: sample_1 at scale 4.0\n", "0.000 per cent patches are empty\n", "sample_1 at scale 4.0 has 0 patches with zero diveristy\n", "sample_1 at scale 4.0 diversity is 3.2738724660974885\n", "Processing region: sample_2 at scale 4.0\n", "0.000 per cent patches are empty\n", "sample_2 at scale 4.0 has 0 patches with zero diveristy\n", "sample_2 at scale 4.0 diversity is 3.2712741280351403\n", "Processing region: sample_1 at scale 8.0\n", "0.000 per cent patches are empty\n", "sample_1 at scale 8.0 has 0 patches with zero diveristy\n", "sample_1 at scale 8.0 diversity is 3.1357805949294244\n", "Processing region: sample_2 at scale 8.0\n", "0.000 per cent patches are empty\n", "sample_2 at scale 8.0 has 0 patches with zero diveristy\n", "sample_2 at scale 8.0 diversity is 3.136249663683922\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "." ] }, { "name": "stdout", "output_type": "stream", "text": [ "[0.5, 0.5571428571428572, 0.6714285714285715, 0.9]\n", "Processing region: sample_1 at scale 1.0\n", "0.000 per cent patches are empty\n", "sample_1 at scale 1.0 has 0 patches with zero diveristy\n", "sample_1 at scale 1.0 diversity is 3.319397130221288\n", "Processing region: sample_2 at scale 1.0\n", "0.000 per cent patches are empty\n", "sample_2 at scale 1.0 has 0 patches with zero diveristy\n", "sample_2 at scale 1.0 diversity is 3.3198048602087438\n", "Processing region: sample_1 at scale 2.0\n", "0.000 per cent patches are empty\n", "sample_1 at scale 2.0 has 0 patches with zero diveristy\n", "sample_1 at scale 2.0 diversity is 3.311824991920736\n", "Processing region: sample_2 at scale 2.0\n", "0.000 per cent patches are empty\n", "sample_2 at scale 2.0 has 0 patches with zero diveristy\n", "sample_2 at scale 2.0 diversity is 3.3127017488287462\n", "Processing region: sample_1 at scale 4.0\n", "0.000 per cent patches are empty\n", "sample_1 at scale 4.0 has 0 patches with zero diveristy\n", "sample_1 at scale 4.0 diversity is 3.2742115498481206\n", "Processing region: sample_2 at scale 4.0\n", "0.000 per cent patches are empty\n", "sample_2 at scale 4.0 has 0 patches with zero diveristy\n", "sample_2 at scale 4.0 diversity is 3.280909902805943\n", "Processing region: sample_1 at scale 8.0\n", "0.000 per cent patches are empty\n", "sample_1 at scale 8.0 has 0 patches with zero diveristy\n", "sample_1 at scale 8.0 diversity is 3.1625338119527555\n", "Processing region: sample_2 at scale 8.0\n", "0.000 per cent patches are empty\n", "sample_2 at scale 8.0 has 0 patches with zero diveristy\n", "sample_2 at scale 8.0 diversity is 3.153896959644078\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ ".\n", "----------------------------------------------------------------------\n", "Ran 2 tests in 1.135s\n", "\n", "OK\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Run the tests in the notebook\n", "unittest.main(argv=['first-arg-is-ignored'], exit=False)" ] } ], "metadata": { "kernelspec": { "display_name": "Python3.11 (mesa)", "language": "python", "name": "python311_mesa" }, "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.11.3" } }, "nbformat": 4, "nbformat_minor": 5 }