From 55f8829a471b31b56b16116a210903702f7473bb Mon Sep 17 00:00:00 2001 From: Samuel-WM Date: Wed, 10 Sep 2025 00:09:34 -0400 Subject: [PATCH 1/7] replicated errors --- issue_173.ipynb | 242 ++++++++++++++++++++++++++++++++++++++++++++++++ issue_266.ipynb | 123 ++++++++++++++++++++++++ 2 files changed, 365 insertions(+) create mode 100644 issue_173.ipynb create mode 100644 issue_266.ipynb diff --git a/issue_173.ipynb b/issue_173.ipynb new file mode 100644 index 0000000..73c4a72 --- /dev/null +++ b/issue_173.ipynb @@ -0,0 +1,242 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "bf283f7f", + "metadata": {}, + "source": [ + "# Reproducing the error" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "bca48984", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "GPU available: False, used: False\n", + "TPU available: False, using: 0 TPU cores\n", + "HPU available: False, using: 0 HPUs\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n", + " | Name | Type | Params | Mode \n", + "-------------------------------------------------------\n", + "0 | metamodel | SubtypeMetamodel | 3.7 K | train\n", + "-------------------------------------------------------\n", + "3.7 K Trainable params\n", + "0 Non-trainable params\n", + "3.7 K Total params\n", + "0.015 Total estimated model params size (MB)\n", + "11 Modules in train mode\n", + "0 Modules in eval mode\n", + "`Trainer.fit` stopped: `max_epochs=1` reached.\n", + "GPU available: False, used: False\n", + "TPU available: False, using: 0 TPU cores\n", + "HPU available: False, using: 0 HPUs\n", + "\n", + " | Name | Type | Params | Mode \n", + "-------------------------------------------------------\n", + "0 | metamodel | SubtypeMetamodel | 3.7 K | train\n", + "-------------------------------------------------------\n", + "3.7 K Trainable params\n", + "0 Non-trainable params\n", + "3.7 K Total params\n", + "0.015 Total estimated model params size (MB)\n", + "11 Modules in train mode\n", + "0 Modules in eval mode\n", + "`Trainer.fit` stopped: `max_epochs=1` reached.\n", + "GPU available: False, used: False\n", + "TPU available: False, using: 0 TPU cores\n", + "HPU available: False, using: 0 HPUs\n", + "\n", + " | Name | Type | Params | Mode \n", + "-------------------------------------------------------\n", + "0 | metamodel | SubtypeMetamodel | 3.7 K | train\n", + "-------------------------------------------------------\n", + "3.7 K Trainable params\n", + "0 Non-trainable params\n", + "3.7 K Total params\n", + "0.015 Total estimated model params size (MB)\n", + "11 Modules in train mode\n", + "0 Modules in eval mode\n", + "`Trainer.fit` stopped: `max_epochs=1` reached.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", + "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", + "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", + "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", + "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", + "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", + "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", + "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", + "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n" + ] + }, + { + "ename": "AssertionError", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAssertionError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 36\u001b[39m\n\u001b[32m 33\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m mses_individual.shape == (n_bootstraps, n_samples)\n\u001b[32m 35\u001b[39m rho_squared = ccn.predict_correlation(C)\n\u001b[32m---> \u001b[39m\u001b[32m36\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m rho_squared.shape == (n_samples, n_features, n_features)\n\u001b[32m 37\u001b[39m rho_squared = ccn.predict_correlation(C, individual_preds=\u001b[38;5;28;01mFalse\u001b[39;00m, squared=\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[32m 38\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m rho_squared.shape == (n_samples, n_features, n_features)\n", + "\u001b[31mAssertionError\u001b[39m: " + ] + } + ], + "source": [ + "import numpy as np\n", + "from contextualized.easy import ContextualizedCorrelationNetworks, ContextualizedMarkovNetworks, ContextualizedBayesianNetworks\n", + "\n", + "n_samples = 100\n", + "n_contexts = 5\n", + "n_features = 10\n", + "n_bootstraps = 3\n", + "C = np.random.uniform(-1, 1, size=(n_samples, n_contexts))\n", + "X = np.random.normal(0, 1, size=(n_samples, n_features))\n", + "\n", + "ccn = ContextualizedCorrelationNetworks(n_bootstraps=n_bootstraps)\n", + "ccn.fit(C, X, max_epochs=1)\n", + "\n", + "networks = ccn.predict_networks(C)\n", + "assert networks.shape == (n_samples, n_features, n_features)\n", + "networks = ccn.predict_networks(C, individual_preds=False)\n", + "assert networks.shape == (n_samples, n_features, n_features)\n", + "networks_individual = ccn.predict_networks(C, individual_preds=True)\n", + "assert networks_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", + "\n", + "y_pred = ccn.predict(C, X)\n", + "assert y_pred.shape == (n_samples, n_features, n_features)\n", + "y_pred_avg = ccn.predict(C, X, individual_preds=False)\n", + "assert y_pred_avg.shape == (n_samples, n_features, n_features)\n", + "y_pred_individual = ccn.predict(C, X, individual_preds=True)\n", + "assert y_pred_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", + "\n", + "mses = ccn.measure_mses(C, X)\n", + "assert mses.shape == (n_samples,)\n", + "mses_avg = ccn.measure_mses(C, X, individual_preds=False)\n", + "assert mses_avg.shape == (n_samples,)\n", + "mses_individual = ccn.measure_mses(C, X, individual_preds=True)\n", + "assert mses_individual.shape == (n_bootstraps, n_samples)\n", + "\n", + "rho_squared = ccn.predict_correlation(C)\n", + "assert rho_squared.shape == (n_samples, n_features, n_features)\n", + "rho_squared = ccn.predict_correlation(C, individual_preds=False, squared=True)\n", + "assert rho_squared.shape == (n_samples, n_features, n_features)\n", + "rho_squared_individual = ccn.predict_correlation(C, individual_preds=True, squared=True)\n", + "assert rho_squared_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", + "\n", + "rho = ccn.predict_correlation(C, squared=False)\n", + "assert rho.shape == (n_samples, n_features, n_features)\n", + "rho = ccn.predict_correlation(C, individual_preds=False, squared=False)\n", + "assert rho.shape == (n_samples, n_features, n_features)\n", + "rho_individual = ccn.predict_correlation(C, individual_preds=True, squared=False)\n", + "assert rho_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", + "\n", + "mkn = ContextualizedMarkovNetworks(n_bootstraps=n_bootstraps)\n", + "mkn.fit(C, X, max_epochs=1)\n", + "networks = mkn.predict_networks(C)\n", + "assert networks.shape == (n_samples, n_features, n_features)\n", + "networks = mkn.predict_networks(C, individual_preds=False)\n", + "assert networks.shape == (n_samples, n_features, n_features)\n", + "networks_individual = mkn.predict_networks(C, individual_preds=True)\n", + "assert networks_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", + "mses_avg = mkn.measure_mses(C, X, individual_preds=False)\n", + "assert mses_avg.shape == (n_samples,)\n", + "mses_individual = mkn.measure_mses(C, X, individual_preds=True)\n", + "assert mses_individual.shape == (n_bootstraps, n_samples)\n", + "\n", + "mkn = ContextualizedMarkovNetworks(n_bootstraps=n_bootstraps)\n", + "mkn.fit(C, X, max_epochs=1)\n", + "\n", + "networks = mkn.predict_networks(C)\n", + "assert networks.shape == (n_samples, n_features, n_features)\n", + "networks = mkn.predict_networks(C, individual_preds=False)\n", + "assert networks.shape == (n_samples, n_features, n_features)\n", + "networks_individual = mkn.predict_networks(C, individual_preds=True)\n", + "assert networks_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", + "\n", + "y_pred = mkn.predict(C, X)\n", + "assert y_pred.shape == (n_samples, n_features)\n", + "y_pred_avg = mkn.predict(C, X, individual_preds=False)\n", + "assert y_pred_avg.shape == (n_samples, n_features)\n", + "y_pred_individual = mkn.predict(C, X, individual_preds=True)\n", + "assert y_pred_individual.shape == (n_bootstraps, n_samples, n_features)\n", + "\n", + "mses = mkn.measure_mses(C, X)\n", + "assert mses.shape == (n_samples,)\n", + "mses_avg = mkn.measure_mses(C, X, individual_preds=False)\n", + "assert mses_avg.shape == (n_samples,)\n", + "mses_individual = mkn.measure_mses(C, X, individual_preds=True)\n", + "assert mses_individual.shape == (n_bootstraps, n_samples)\n", + "\n", + "precisions = mkn.predict_precisions(C)\n", + "assert precisions.shape == (n_samples, n_features, n_features)\n", + "precision_avg = mkn.predict_precisions(C, individual_preds=False)\n", + "assert precision_avg.shape == (n_samples, n_features, n_features)\n", + "precision_individual = mkn.predict_precisions(C, individual_preds=True)\n", + "assert precision_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", + "\n", + "cbn = ContextualizedBayesianNetworks(n_bootstraps=n_bootstraps)\n", + "cbn.fit(C, X, max_epochs=1)\n", + "\n", + "networks = cbn.predict_networks(C)\n", + "assert networks.shape == (n_samples, n_features, n_features)\n", + "networks = cbn.predict_networks(C, individual_preds=False)\n", + "assert networks.shape == (n_samples, n_features, n_features)\n", + "networks_individual = cbn.predict_networks(C, individual_preds=True)\n", + "assert networks_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", + "\n", + "# Broken, related to issue #266 \n", + "# y_pred = cbn.predict(C, X)\n", + "# assert y_pred.shape == (n_samples, n_features)\n", + "# y_pred_avg = cbn.predict(C, X, individual_preds=False)\n", + "# assert y_pred_avg.shape == (n_samples, n_features)\n", + "# y_pred_individual = cbn.predict(C, X, individual_preds=True)\n", + "# assert y_pred_individual.shape == (n_bootstraps, n_samples, n_features)\n", + "\n", + "mses = cbn.measure_mses(C, X)\n", + "assert mses.shape == (n_samples,)\n", + "mses_avg = cbn.measure_mses(C, X, individual_preds=False)\n", + "assert mses_avg.shape == (n_samples,)\n", + "mses_individual = cbn.measure_mses(C, X, individual_preds=True)\n", + "assert mses_individual.shape == (n_bootstraps, n_samples)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "contextualized", + "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.11.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/issue_266.ipynb b/issue_266.ipynb new file mode 100644 index 0000000..884c7ac --- /dev/null +++ b/issue_266.ipynb @@ -0,0 +1,123 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "cd5cda1a", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "GPU available: False, used: False\n", + "TPU available: False, using: 0 TPU cores\n", + "HPU available: False, using: 0 HPUs\n", + "\n", + " | Name | Type | Params | Mode \n", + "------------------------------------------------\n", + "0 | encoder | MLP | 1.9 K | train\n", + "1 | explainer | Explainer | 1.6 K | train\n", + "------------------------------------------------\n", + "3.5 K Trainable params\n", + "0 Non-trainable params\n", + "3.5 K Total params\n", + "0.014 Total estimated model params size (MB)\n", + "10 Modules in train mode\n", + "0 Modules in eval mode\n", + "`Trainer.fit` stopped: `max_epochs=1` reached.\n", + "GPU available: False, used: False\n", + "TPU available: False, using: 0 TPU cores\n", + "HPU available: False, using: 0 HPUs\n", + "\n", + " | Name | Type | Params | Mode \n", + "------------------------------------------------\n", + "0 | encoder | MLP | 1.9 K | train\n", + "1 | explainer | Explainer | 1.6 K | train\n", + "------------------------------------------------\n", + "3.5 K Trainable params\n", + "0 Non-trainable params\n", + "3.5 K Total params\n", + "0.014 Total estimated model params size (MB)\n", + "10 Modules in train mode\n", + "0 Modules in eval mode\n", + "`Trainer.fit` stopped: `max_epochs=1` reached.\n", + "GPU available: False, used: False\n", + "TPU available: False, using: 0 TPU cores\n", + "HPU available: False, using: 0 HPUs\n", + "\n", + " | Name | Type | Params | Mode \n", + "------------------------------------------------\n", + "0 | encoder | MLP | 1.9 K | train\n", + "1 | explainer | Explainer | 1.6 K | train\n", + "------------------------------------------------\n", + "3.5 K Trainable params\n", + "0 Non-trainable params\n", + "3.5 K Total params\n", + "0.014 Total estimated model params size (MB)\n", + "10 Modules in train mode\n", + "0 Modules in eval mode\n", + "`Trainer.fit` stopped: `max_epochs=1` reached.\n" + ] + }, + { + "ename": "AttributeError", + "evalue": "'GraphTrainer' object has no attribute 'predict_y'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 14\u001b[39m\n\u001b[32m 11\u001b[39m cbn = ContextualizedBayesianNetworks(n_bootstraps=n_bootstraps)\n\u001b[32m 12\u001b[39m cbn.fit(C, X, max_epochs=\u001b[32m1\u001b[39m)\n\u001b[32m---> \u001b[39m\u001b[32m14\u001b[39m y_pred = \u001b[43mcbn\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpredict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mC\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mX\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Problematic line\u001b[39;00m\n\u001b[32m 16\u001b[39m \u001b[38;5;66;03m# Extra tests\u001b[39;00m\n\u001b[32m 17\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m y_pred.shape == (n_samples, n_features)\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\samue\\OneDrive\\Documents\\open_source\\contexualized\\Contextualized\\contextualized\\easy\\wrappers\\SKLearnWrapper.py:434\u001b[39m, in \u001b[36mSKLearnWrapper.predict\u001b[39m\u001b[34m(self, C, X, individual_preds, **kwargs)\u001b[39m\n\u001b[32m 431\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mmodels\u001b[39m\u001b[33m\"\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m.models \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[32m 432\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[33m\"\u001b[39m\u001b[33mTrying to predict with a model that hasn\u001b[39m\u001b[33m'\u001b[39m\u001b[33mt been trained yet.\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 433\u001b[39m predictions = np.array(\n\u001b[32m--> \u001b[39m\u001b[32m434\u001b[39m \u001b[43m[\u001b[49m\n\u001b[32m 435\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mtrainers\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpredict_y\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 436\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mmodels\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 437\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mmodels\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43mdataloader\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 438\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_maybe_scale_C\u001b[49m\u001b[43m(\u001b[49m\u001b[43mC\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 439\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_maybe_scale_X\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 440\u001b[39m \u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mzeros\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mC\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43my_dim\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 441\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 442\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 443\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 444\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mrange\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mmodels\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 445\u001b[39m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m,\n\u001b[32m 446\u001b[39m dtype=\u001b[38;5;28mfloat\u001b[39m,\n\u001b[32m 447\u001b[39m )\n\u001b[32m 448\u001b[39m \u001b[38;5;66;03m# aggregation across bootstraps\u001b[39;00m\n\u001b[32m 449\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m individual_preds:\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\samue\\OneDrive\\Documents\\open_source\\contexualized\\Contextualized\\contextualized\\easy\\wrappers\\SKLearnWrapper.py:435\u001b[39m, in \u001b[36m\u001b[39m\u001b[34m(.0)\u001b[39m\n\u001b[32m 431\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mmodels\u001b[39m\u001b[33m\"\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m.models \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[32m 432\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[33m\"\u001b[39m\u001b[33mTrying to predict with a model that hasn\u001b[39m\u001b[33m'\u001b[39m\u001b[33mt been trained yet.\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 433\u001b[39m predictions = np.array(\n\u001b[32m 434\u001b[39m [\n\u001b[32m--> \u001b[39m\u001b[32m435\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mtrainers\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpredict_y\u001b[49m(\n\u001b[32m 436\u001b[39m \u001b[38;5;28mself\u001b[39m.models[i],\n\u001b[32m 437\u001b[39m \u001b[38;5;28mself\u001b[39m.models[i].dataloader(\n\u001b[32m 438\u001b[39m \u001b[38;5;28mself\u001b[39m._maybe_scale_C(C),\n\u001b[32m 439\u001b[39m \u001b[38;5;28mself\u001b[39m._maybe_scale_X(X),\n\u001b[32m 440\u001b[39m np.zeros((\u001b[38;5;28mlen\u001b[39m(C), \u001b[38;5;28mself\u001b[39m.y_dim)),\n\u001b[32m 441\u001b[39m ),\n\u001b[32m 442\u001b[39m **kwargs,\n\u001b[32m 443\u001b[39m )\n\u001b[32m 444\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m.models))\n\u001b[32m 445\u001b[39m ],\n\u001b[32m 446\u001b[39m dtype=\u001b[38;5;28mfloat\u001b[39m,\n\u001b[32m 447\u001b[39m )\n\u001b[32m 448\u001b[39m \u001b[38;5;66;03m# aggregation across bootstraps\u001b[39;00m\n\u001b[32m 449\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m individual_preds:\n", + "\u001b[31mAttributeError\u001b[39m: 'GraphTrainer' object has no attribute 'predict_y'" + ] + } + ], + "source": [ + "import numpy as np\n", + "from contextualized.easy import ContextualizedBayesianNetworks\n", + "\n", + "n_samples = 100\n", + "n_contexts = 5\n", + "n_features = 10\n", + "n_bootstraps = 3\n", + "C = np.random.uniform(-1, 1, size=(n_samples, n_contexts))\n", + "X = np.random.normal(0, 1, size=(n_samples, n_features))\n", + "\n", + "cbn = ContextualizedBayesianNetworks(n_bootstraps=n_bootstraps)\n", + "cbn.fit(C, X, max_epochs=1)\n", + "\n", + "y_pred = cbn.predict(C, X) # Problematic line\n", + "\n", + "# Extra tests\n", + "assert y_pred.shape == (n_samples, n_features)\n", + "y_pred_avg = cbn.predict(C, X, individual_preds=False)\n", + "assert y_pred_avg.shape == (n_samples, n_features)\n", + "y_pred_individual = cbn.predict(C, X, individual_preds=True)\n", + "assert y_pred_individual.shape == (n_bootstraps, n_samples, n_features)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "contextualized", + "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.11.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From d4fec374ae9cb2c55809e4ff11b932d18936ff40 Mon Sep 17 00:00:00 2001 From: Samuel-WM Date: Mon, 15 Sep 2025 11:25:22 -0400 Subject: [PATCH 2/7] added helper function for reconstruction --- contextualized/easy/ContextualizedNetworks.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/contextualized/easy/ContextualizedNetworks.py b/contextualized/easy/ContextualizedNetworks.py index a989d46..f3c2d5f 100644 --- a/contextualized/easy/ContextualizedNetworks.py +++ b/contextualized/easy/ContextualizedNetworks.py @@ -433,6 +433,38 @@ def predict_networks( C, uses_y=False, project_to_dag=project_to_dag, **kwargs ) return betas + + def _reconstruct_from_betas(self, betas: np.ndarray, X_arr: np.ndarray) -> np.ndarray: + """ + Given betas of shape (n_samples, n_features, n_features) + and X_arr of shape (n_samples, n_features) + produce a reconstruction: X_hat[i] = betas[i] @ X_arr[i]. + Diagonals zeroed to avoid self edges. + """ + + n_samples, n_features = X_arr.shape + + B = betas.copy() + idx = np.arange(n_features) + B[:, idx, idx] = 0.0 # remove self-edges + # one-shot reconstruction using parents' observed values + X_hat = np.einsum("bij,bi->bj", B, X_arr, optimize=True) + return X_hat + + + def predict( + self, + C: np.ndarray, + X: np.ndarray, + project_to_dag: bool = True, + **kwargs, + ) -> np.ndarray: + ... + + + + + def measure_mses( self, C: np.ndarray, X: np.ndarray, individual_preds: bool = False, **kwargs From 98030f6ad6f3693a22ef20367e367a5d11795672 Mon Sep 17 00:00:00 2001 From: Samuel-WM Date: Wed, 17 Sep 2025 15:52:28 -0400 Subject: [PATCH 3/7] added finalized predict and reconstruction methods. All initial have passed --- contextualized/easy/ContextualizedNetworks.py | 53 ++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/contextualized/easy/ContextualizedNetworks.py b/contextualized/easy/ContextualizedNetworks.py index f3c2d5f..df02561 100644 --- a/contextualized/easy/ContextualizedNetworks.py +++ b/contextualized/easy/ContextualizedNetworks.py @@ -435,34 +435,63 @@ def predict_networks( return betas def _reconstruct_from_betas(self, betas: np.ndarray, X_arr: np.ndarray) -> np.ndarray: - """ - Given betas of shape (n_samples, n_features, n_features) - and X_arr of shape (n_samples, n_features) - produce a reconstruction: X_hat[i] = betas[i] @ X_arr[i]. - Diagonals zeroed to avoid self edges. - """ n_samples, n_features = X_arr.shape - B = betas.copy() + B = np.array(betas, copy=True) + if B.ndim == 2: + B = np.broadcast_to(B[None, :, :], (n_samples, n_features, n_features)).copy() + elif B.ndim != 3: + raise ValueError(f"Expected betas 2D or 3D, got shape {B.shape}") + + # zero diagonal idx = np.arange(n_features) - B[:, idx, idx] = 0.0 # remove self-edges - # one-shot reconstruction using parents' observed values - X_hat = np.einsum("bij,bi->bj", B, X_arr, optimize=True) + B[:, idx, idx] = 0.0 + + X_hat = dag_pred_np(X_arr, B) return X_hat + def predict( self, C: np.ndarray, X: np.ndarray, project_to_dag: bool = True, + individual_preds: bool = False, **kwargs, ) -> np.ndarray: - ... + X_scaled = self._maybe_scale_X(X) + betas = self.predict_networks( + C, project_to_dag=project_to_dag, individual_preds=individual_preds, **kwargs + ) - + # unify iterable over bootstraps + is_bootstrap_stack = isinstance(betas, np.ndarray) and betas.ndim == 4 + if isinstance(betas, list) or is_bootstrap_stack: + if is_bootstrap_stack: + betas_iter = (betas[k] for k in range(betas.shape[0])) + else: + betas_iter = betas + + reconstructions = [self._reconstruct_from_betas(b, X_scaled) for b in betas_iter] + recon_stack = np.stack(reconstructions, axis=0) # (B, N, F) + + if self.normalize and self.scalers["X"] is not None: + recon_stack = np.stack( + [self.scalers["X"].inverse_transform(recon_stack[k]) for k in range(recon_stack.shape[0])], + axis=0, + ) + + if individual_preds: + return recon_stack # (B, N, F) + return self._nanrobust_mean(recon_stack, axis=0) # (N, F) + + reconstructed_scaled = self._reconstruct_from_betas(betas, X_scaled) + if self.normalize and self.scalers["X"] is not None: + return self.scalers["X"].inverse_transform(reconstructed_scaled) + return reconstructed_scaled From 18f491be1b135df18e83a68558bd45224ec6af00 Mon Sep 17 00:00:00 2001 From: Samuel-WM Date: Wed, 17 Sep 2025 16:30:53 -0400 Subject: [PATCH 4/7] A quick fix for problem 173 where individual_preds was switched from True to False in the method signature of predict_precisions and predict_correlation --- contextualized/easy/ContextualizedNetworks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contextualized/easy/ContextualizedNetworks.py b/contextualized/easy/ContextualizedNetworks.py index df02561..3464d61 100644 --- a/contextualized/easy/ContextualizedNetworks.py +++ b/contextualized/easy/ContextualizedNetworks.py @@ -107,7 +107,7 @@ def __init__(self, **kwargs): ) def predict_correlation( - self, C: np.ndarray, individual_preds: bool = True, squared: bool = True + self, C: np.ndarray, individual_preds: bool = False, squared: bool = True ) -> Union[np.ndarray, List[np.ndarray]]: """Predicts context-specific correlations between features. @@ -182,7 +182,7 @@ def __init__(self, **kwargs): super().__init__(ContextualizedMarkovGraph, [], [], MarkovTrainer, **kwargs) def predict_precisions( - self, C: np.ndarray, individual_preds: bool = True + self, C: np.ndarray, individual_preds: bool = False ) -> Union[np.ndarray, List[np.ndarray]]: """Predicts context-specific precision matrices. Can be converted to context-specific Markov networks by binarizing the networks and setting all non-zero entries to 1. From 3acfa53e76369b004fd831f518b0767b964e6776 Mon Sep 17 00:00:00 2001 From: Samuel-WM Date: Fri, 19 Sep 2025 10:37:15 -0400 Subject: [PATCH 5/7] updated easy.tests.py to respond to the method signature changes that were made --- contextualized/easy/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contextualized/easy/tests.py b/contextualized/easy/tests.py index b22339d..f3121a4 100644 --- a/contextualized/easy/tests.py +++ b/contextualized/easy/tests.py @@ -113,11 +113,11 @@ def test_correlation(self): encoder_type="ngam", num_archetypes=16 ) self._quicktest(model, self.C, self.X, max_epochs=10, learning_rate=1e-3) - rho = model.predict_correlation(self.C, squared=False) + rho = model.predict_correlation(self.C, individual_preds=True, squared=False) assert rho.shape == (1, self.n_samples, self.x_dim, self.x_dim) rho = model.predict_correlation(self.C, individual_preds=False, squared=False) assert rho.shape == (self.n_samples, self.x_dim, self.x_dim), rho.shape - rho_squared = model.predict_correlation(self.C, squared=True) + rho_squared = model.predict_correlation(self.C, individual_preds= True, squared=True) assert np.min(rho_squared) >= 0 assert rho_squared.shape == (1, self.n_samples, self.x_dim, self.x_dim) From 272bdea02da7d37103d0ab2270a397271e63739a Mon Sep 17 00:00:00 2001 From: Samuel-WM Date: Fri, 19 Sep 2025 11:02:02 -0400 Subject: [PATCH 6/7] format changes and pylint badge --- contextualized/easy/ContextualizedNetworks.py | 28 ++++++++++++------- contextualized/easy/tests.py | 4 ++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/contextualized/easy/ContextualizedNetworks.py b/contextualized/easy/ContextualizedNetworks.py index 3464d61..01cea6e 100644 --- a/contextualized/easy/ContextualizedNetworks.py +++ b/contextualized/easy/ContextualizedNetworks.py @@ -433,14 +433,18 @@ def predict_networks( C, uses_y=False, project_to_dag=project_to_dag, **kwargs ) return betas - - def _reconstruct_from_betas(self, betas: np.ndarray, X_arr: np.ndarray) -> np.ndarray: + + def _reconstruct_from_betas( + self, betas: np.ndarray, X_arr: np.ndarray + ) -> np.ndarray: n_samples, n_features = X_arr.shape B = np.array(betas, copy=True) if B.ndim == 2: - B = np.broadcast_to(B[None, :, :], (n_samples, n_features, n_features)).copy() + B = np.broadcast_to( + B[None, :, :], (n_samples, n_features, n_features) + ).copy() elif B.ndim != 3: raise ValueError(f"Expected betas 2D or 3D, got shape {B.shape}") @@ -451,8 +455,6 @@ def _reconstruct_from_betas(self, betas: np.ndarray, X_arr: np.ndarray) -> np.nd X_hat = dag_pred_np(X_arr, B) return X_hat - - def predict( self, C: np.ndarray, @@ -464,7 +466,10 @@ def predict( X_scaled = self._maybe_scale_X(X) betas = self.predict_networks( - C, project_to_dag=project_to_dag, individual_preds=individual_preds, **kwargs + C, + project_to_dag=project_to_dag, + individual_preds=individual_preds, + **kwargs, ) # unify iterable over bootstraps @@ -475,12 +480,17 @@ def predict( else: betas_iter = betas - reconstructions = [self._reconstruct_from_betas(b, X_scaled) for b in betas_iter] + reconstructions = [ + self._reconstruct_from_betas(b, X_scaled) for b in betas_iter + ] recon_stack = np.stack(reconstructions, axis=0) # (B, N, F) if self.normalize and self.scalers["X"] is not None: recon_stack = np.stack( - [self.scalers["X"].inverse_transform(recon_stack[k]) for k in range(recon_stack.shape[0])], + [ + self.scalers["X"].inverse_transform(recon_stack[k]) + for k in range(recon_stack.shape[0]) + ], axis=0, ) @@ -493,8 +503,6 @@ def predict( return self.scalers["X"].inverse_transform(reconstructed_scaled) return reconstructed_scaled - - def measure_mses( self, C: np.ndarray, X: np.ndarray, individual_preds: bool = False, **kwargs ) -> Union[np.ndarray, List[np.ndarray]]: diff --git a/contextualized/easy/tests.py b/contextualized/easy/tests.py index f3121a4..f7f54c4 100644 --- a/contextualized/easy/tests.py +++ b/contextualized/easy/tests.py @@ -117,7 +117,9 @@ def test_correlation(self): assert rho.shape == (1, self.n_samples, self.x_dim, self.x_dim) rho = model.predict_correlation(self.C, individual_preds=False, squared=False) assert rho.shape == (self.n_samples, self.x_dim, self.x_dim), rho.shape - rho_squared = model.predict_correlation(self.C, individual_preds= True, squared=True) + rho_squared = model.predict_correlation( + self.C, individual_preds=True, squared=True + ) assert np.min(rho_squared) >= 0 assert rho_squared.shape == (1, self.n_samples, self.x_dim, self.x_dim) From 1e3a847aa34069a36d76a2fec13ea46435595258 Mon Sep 17 00:00:00 2001 From: Samuel-WM Date: Fri, 19 Sep 2025 11:58:02 -0400 Subject: [PATCH 7/7] added docstrings --- contextualized/easy/ContextualizedNetworks.py | 23 ++ issue_173.ipynb | 242 ------------------ issue_266.ipynb | 123 --------- 3 files changed, 23 insertions(+), 365 deletions(-) delete mode 100644 issue_173.ipynb delete mode 100644 issue_266.ipynb diff --git a/contextualized/easy/ContextualizedNetworks.py b/contextualized/easy/ContextualizedNetworks.py index 01cea6e..604da99 100644 --- a/contextualized/easy/ContextualizedNetworks.py +++ b/contextualized/easy/ContextualizedNetworks.py @@ -437,6 +437,16 @@ def predict_networks( def _reconstruct_from_betas( self, betas: np.ndarray, X_arr: np.ndarray ) -> np.ndarray: + + """Reconstructs features from predicted betas. + + Args: + betas (np.ndarray): Coefficient matrices, shape (F, F) or (N, F, F). + X_arr (np.ndarray): Input data, shape (N, F). + + Returns: + np.ndarray: Reconstructed data, shape (N, F). + """ n_samples, n_features = X_arr.shape @@ -463,6 +473,19 @@ def predict( individual_preds: bool = False, **kwargs, ) -> np.ndarray: + + """Predicts reconstructed data from context and features. + + Args: + C (np.ndarray): Contextual features, shape (N, K). + X (np.ndarray): Input data, shape (N, F). + project_to_dag (bool, optional): If True, enforce DAG structure. Defaults to True. + individual_preds (bool, optional): If True, return per-bootstrap predictions. Defaults to False. + **kwargs: Additional keyword arguments. + + Returns: + np.ndarray: Reconstructed predictions, shape (N, F), or (B, N, F) if individual_preds is True. + """ X_scaled = self._maybe_scale_X(X) betas = self.predict_networks( diff --git a/issue_173.ipynb b/issue_173.ipynb deleted file mode 100644 index 73c4a72..0000000 --- a/issue_173.ipynb +++ /dev/null @@ -1,242 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "bf283f7f", - "metadata": {}, - "source": [ - "# Reproducing the error" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "bca48984", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "GPU available: False, used: False\n", - "TPU available: False, using: 0 TPU cores\n", - "HPU available: False, using: 0 HPUs\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "\n", - " | Name | Type | Params | Mode \n", - "-------------------------------------------------------\n", - "0 | metamodel | SubtypeMetamodel | 3.7 K | train\n", - "-------------------------------------------------------\n", - "3.7 K Trainable params\n", - "0 Non-trainable params\n", - "3.7 K Total params\n", - "0.015 Total estimated model params size (MB)\n", - "11 Modules in train mode\n", - "0 Modules in eval mode\n", - "`Trainer.fit` stopped: `max_epochs=1` reached.\n", - "GPU available: False, used: False\n", - "TPU available: False, using: 0 TPU cores\n", - "HPU available: False, using: 0 HPUs\n", - "\n", - " | Name | Type | Params | Mode \n", - "-------------------------------------------------------\n", - "0 | metamodel | SubtypeMetamodel | 3.7 K | train\n", - "-------------------------------------------------------\n", - "3.7 K Trainable params\n", - "0 Non-trainable params\n", - "3.7 K Total params\n", - "0.015 Total estimated model params size (MB)\n", - "11 Modules in train mode\n", - "0 Modules in eval mode\n", - "`Trainer.fit` stopped: `max_epochs=1` reached.\n", - "GPU available: False, used: False\n", - "TPU available: False, using: 0 TPU cores\n", - "HPU available: False, using: 0 HPUs\n", - "\n", - " | Name | Type | Params | Mode \n", - "-------------------------------------------------------\n", - "0 | metamodel | SubtypeMetamodel | 3.7 K | train\n", - "-------------------------------------------------------\n", - "3.7 K Trainable params\n", - "0 Non-trainable params\n", - "3.7 K Total params\n", - "0.015 Total estimated model params size (MB)\n", - "11 Modules in train mode\n", - "0 Modules in eval mode\n", - "`Trainer.fit` stopped: `max_epochs=1` reached.\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", - "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", - "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", - "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", - "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", - "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", - "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", - "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n", - "Passed a Y, but this is self-correlation between X featuers. Ignoring Y.\n" - ] - }, - { - "ename": "AssertionError", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mAssertionError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 36\u001b[39m\n\u001b[32m 33\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m mses_individual.shape == (n_bootstraps, n_samples)\n\u001b[32m 35\u001b[39m rho_squared = ccn.predict_correlation(C)\n\u001b[32m---> \u001b[39m\u001b[32m36\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m rho_squared.shape == (n_samples, n_features, n_features)\n\u001b[32m 37\u001b[39m rho_squared = ccn.predict_correlation(C, individual_preds=\u001b[38;5;28;01mFalse\u001b[39;00m, squared=\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[32m 38\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m rho_squared.shape == (n_samples, n_features, n_features)\n", - "\u001b[31mAssertionError\u001b[39m: " - ] - } - ], - "source": [ - "import numpy as np\n", - "from contextualized.easy import ContextualizedCorrelationNetworks, ContextualizedMarkovNetworks, ContextualizedBayesianNetworks\n", - "\n", - "n_samples = 100\n", - "n_contexts = 5\n", - "n_features = 10\n", - "n_bootstraps = 3\n", - "C = np.random.uniform(-1, 1, size=(n_samples, n_contexts))\n", - "X = np.random.normal(0, 1, size=(n_samples, n_features))\n", - "\n", - "ccn = ContextualizedCorrelationNetworks(n_bootstraps=n_bootstraps)\n", - "ccn.fit(C, X, max_epochs=1)\n", - "\n", - "networks = ccn.predict_networks(C)\n", - "assert networks.shape == (n_samples, n_features, n_features)\n", - "networks = ccn.predict_networks(C, individual_preds=False)\n", - "assert networks.shape == (n_samples, n_features, n_features)\n", - "networks_individual = ccn.predict_networks(C, individual_preds=True)\n", - "assert networks_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", - "\n", - "y_pred = ccn.predict(C, X)\n", - "assert y_pred.shape == (n_samples, n_features, n_features)\n", - "y_pred_avg = ccn.predict(C, X, individual_preds=False)\n", - "assert y_pred_avg.shape == (n_samples, n_features, n_features)\n", - "y_pred_individual = ccn.predict(C, X, individual_preds=True)\n", - "assert y_pred_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", - "\n", - "mses = ccn.measure_mses(C, X)\n", - "assert mses.shape == (n_samples,)\n", - "mses_avg = ccn.measure_mses(C, X, individual_preds=False)\n", - "assert mses_avg.shape == (n_samples,)\n", - "mses_individual = ccn.measure_mses(C, X, individual_preds=True)\n", - "assert mses_individual.shape == (n_bootstraps, n_samples)\n", - "\n", - "rho_squared = ccn.predict_correlation(C)\n", - "assert rho_squared.shape == (n_samples, n_features, n_features)\n", - "rho_squared = ccn.predict_correlation(C, individual_preds=False, squared=True)\n", - "assert rho_squared.shape == (n_samples, n_features, n_features)\n", - "rho_squared_individual = ccn.predict_correlation(C, individual_preds=True, squared=True)\n", - "assert rho_squared_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", - "\n", - "rho = ccn.predict_correlation(C, squared=False)\n", - "assert rho.shape == (n_samples, n_features, n_features)\n", - "rho = ccn.predict_correlation(C, individual_preds=False, squared=False)\n", - "assert rho.shape == (n_samples, n_features, n_features)\n", - "rho_individual = ccn.predict_correlation(C, individual_preds=True, squared=False)\n", - "assert rho_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", - "\n", - "mkn = ContextualizedMarkovNetworks(n_bootstraps=n_bootstraps)\n", - "mkn.fit(C, X, max_epochs=1)\n", - "networks = mkn.predict_networks(C)\n", - "assert networks.shape == (n_samples, n_features, n_features)\n", - "networks = mkn.predict_networks(C, individual_preds=False)\n", - "assert networks.shape == (n_samples, n_features, n_features)\n", - "networks_individual = mkn.predict_networks(C, individual_preds=True)\n", - "assert networks_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", - "mses_avg = mkn.measure_mses(C, X, individual_preds=False)\n", - "assert mses_avg.shape == (n_samples,)\n", - "mses_individual = mkn.measure_mses(C, X, individual_preds=True)\n", - "assert mses_individual.shape == (n_bootstraps, n_samples)\n", - "\n", - "mkn = ContextualizedMarkovNetworks(n_bootstraps=n_bootstraps)\n", - "mkn.fit(C, X, max_epochs=1)\n", - "\n", - "networks = mkn.predict_networks(C)\n", - "assert networks.shape == (n_samples, n_features, n_features)\n", - "networks = mkn.predict_networks(C, individual_preds=False)\n", - "assert networks.shape == (n_samples, n_features, n_features)\n", - "networks_individual = mkn.predict_networks(C, individual_preds=True)\n", - "assert networks_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", - "\n", - "y_pred = mkn.predict(C, X)\n", - "assert y_pred.shape == (n_samples, n_features)\n", - "y_pred_avg = mkn.predict(C, X, individual_preds=False)\n", - "assert y_pred_avg.shape == (n_samples, n_features)\n", - "y_pred_individual = mkn.predict(C, X, individual_preds=True)\n", - "assert y_pred_individual.shape == (n_bootstraps, n_samples, n_features)\n", - "\n", - "mses = mkn.measure_mses(C, X)\n", - "assert mses.shape == (n_samples,)\n", - "mses_avg = mkn.measure_mses(C, X, individual_preds=False)\n", - "assert mses_avg.shape == (n_samples,)\n", - "mses_individual = mkn.measure_mses(C, X, individual_preds=True)\n", - "assert mses_individual.shape == (n_bootstraps, n_samples)\n", - "\n", - "precisions = mkn.predict_precisions(C)\n", - "assert precisions.shape == (n_samples, n_features, n_features)\n", - "precision_avg = mkn.predict_precisions(C, individual_preds=False)\n", - "assert precision_avg.shape == (n_samples, n_features, n_features)\n", - "precision_individual = mkn.predict_precisions(C, individual_preds=True)\n", - "assert precision_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", - "\n", - "cbn = ContextualizedBayesianNetworks(n_bootstraps=n_bootstraps)\n", - "cbn.fit(C, X, max_epochs=1)\n", - "\n", - "networks = cbn.predict_networks(C)\n", - "assert networks.shape == (n_samples, n_features, n_features)\n", - "networks = cbn.predict_networks(C, individual_preds=False)\n", - "assert networks.shape == (n_samples, n_features, n_features)\n", - "networks_individual = cbn.predict_networks(C, individual_preds=True)\n", - "assert networks_individual.shape == (n_bootstraps, n_samples, n_features, n_features)\n", - "\n", - "# Broken, related to issue #266 \n", - "# y_pred = cbn.predict(C, X)\n", - "# assert y_pred.shape == (n_samples, n_features)\n", - "# y_pred_avg = cbn.predict(C, X, individual_preds=False)\n", - "# assert y_pred_avg.shape == (n_samples, n_features)\n", - "# y_pred_individual = cbn.predict(C, X, individual_preds=True)\n", - "# assert y_pred_individual.shape == (n_bootstraps, n_samples, n_features)\n", - "\n", - "mses = cbn.measure_mses(C, X)\n", - "assert mses.shape == (n_samples,)\n", - "mses_avg = cbn.measure_mses(C, X, individual_preds=False)\n", - "assert mses_avg.shape == (n_samples,)\n", - "mses_individual = cbn.measure_mses(C, X, individual_preds=True)\n", - "assert mses_individual.shape == (n_bootstraps, n_samples)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "contextualized", - "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.11.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/issue_266.ipynb b/issue_266.ipynb deleted file mode 100644 index 884c7ac..0000000 --- a/issue_266.ipynb +++ /dev/null @@ -1,123 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 3, - "id": "cd5cda1a", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "GPU available: False, used: False\n", - "TPU available: False, using: 0 TPU cores\n", - "HPU available: False, using: 0 HPUs\n", - "\n", - " | Name | Type | Params | Mode \n", - "------------------------------------------------\n", - "0 | encoder | MLP | 1.9 K | train\n", - "1 | explainer | Explainer | 1.6 K | train\n", - "------------------------------------------------\n", - "3.5 K Trainable params\n", - "0 Non-trainable params\n", - "3.5 K Total params\n", - "0.014 Total estimated model params size (MB)\n", - "10 Modules in train mode\n", - "0 Modules in eval mode\n", - "`Trainer.fit` stopped: `max_epochs=1` reached.\n", - "GPU available: False, used: False\n", - "TPU available: False, using: 0 TPU cores\n", - "HPU available: False, using: 0 HPUs\n", - "\n", - " | Name | Type | Params | Mode \n", - "------------------------------------------------\n", - "0 | encoder | MLP | 1.9 K | train\n", - "1 | explainer | Explainer | 1.6 K | train\n", - "------------------------------------------------\n", - "3.5 K Trainable params\n", - "0 Non-trainable params\n", - "3.5 K Total params\n", - "0.014 Total estimated model params size (MB)\n", - "10 Modules in train mode\n", - "0 Modules in eval mode\n", - "`Trainer.fit` stopped: `max_epochs=1` reached.\n", - "GPU available: False, used: False\n", - "TPU available: False, using: 0 TPU cores\n", - "HPU available: False, using: 0 HPUs\n", - "\n", - " | Name | Type | Params | Mode \n", - "------------------------------------------------\n", - "0 | encoder | MLP | 1.9 K | train\n", - "1 | explainer | Explainer | 1.6 K | train\n", - "------------------------------------------------\n", - "3.5 K Trainable params\n", - "0 Non-trainable params\n", - "3.5 K Total params\n", - "0.014 Total estimated model params size (MB)\n", - "10 Modules in train mode\n", - "0 Modules in eval mode\n", - "`Trainer.fit` stopped: `max_epochs=1` reached.\n" - ] - }, - { - "ename": "AttributeError", - "evalue": "'GraphTrainer' object has no attribute 'predict_y'", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 14\u001b[39m\n\u001b[32m 11\u001b[39m cbn = ContextualizedBayesianNetworks(n_bootstraps=n_bootstraps)\n\u001b[32m 12\u001b[39m cbn.fit(C, X, max_epochs=\u001b[32m1\u001b[39m)\n\u001b[32m---> \u001b[39m\u001b[32m14\u001b[39m y_pred = \u001b[43mcbn\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpredict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mC\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mX\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Problematic line\u001b[39;00m\n\u001b[32m 16\u001b[39m \u001b[38;5;66;03m# Extra tests\u001b[39;00m\n\u001b[32m 17\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m y_pred.shape == (n_samples, n_features)\n", - "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\samue\\OneDrive\\Documents\\open_source\\contexualized\\Contextualized\\contextualized\\easy\\wrappers\\SKLearnWrapper.py:434\u001b[39m, in \u001b[36mSKLearnWrapper.predict\u001b[39m\u001b[34m(self, C, X, individual_preds, **kwargs)\u001b[39m\n\u001b[32m 431\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mmodels\u001b[39m\u001b[33m\"\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m.models \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[32m 432\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[33m\"\u001b[39m\u001b[33mTrying to predict with a model that hasn\u001b[39m\u001b[33m'\u001b[39m\u001b[33mt been trained yet.\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 433\u001b[39m predictions = np.array(\n\u001b[32m--> \u001b[39m\u001b[32m434\u001b[39m \u001b[43m[\u001b[49m\n\u001b[32m 435\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mtrainers\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpredict_y\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 436\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mmodels\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 437\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mmodels\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43mdataloader\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 438\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_maybe_scale_C\u001b[49m\u001b[43m(\u001b[49m\u001b[43mC\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 439\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_maybe_scale_X\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 440\u001b[39m \u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mzeros\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mC\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43my_dim\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 441\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 442\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 443\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 444\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mrange\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mmodels\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 445\u001b[39m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m,\n\u001b[32m 446\u001b[39m dtype=\u001b[38;5;28mfloat\u001b[39m,\n\u001b[32m 447\u001b[39m )\n\u001b[32m 448\u001b[39m \u001b[38;5;66;03m# aggregation across bootstraps\u001b[39;00m\n\u001b[32m 449\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m individual_preds:\n", - "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\samue\\OneDrive\\Documents\\open_source\\contexualized\\Contextualized\\contextualized\\easy\\wrappers\\SKLearnWrapper.py:435\u001b[39m, in \u001b[36m\u001b[39m\u001b[34m(.0)\u001b[39m\n\u001b[32m 431\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33mmodels\u001b[39m\u001b[33m\"\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m.models \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[32m 432\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[33m\"\u001b[39m\u001b[33mTrying to predict with a model that hasn\u001b[39m\u001b[33m'\u001b[39m\u001b[33mt been trained yet.\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 433\u001b[39m predictions = np.array(\n\u001b[32m 434\u001b[39m [\n\u001b[32m--> \u001b[39m\u001b[32m435\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mtrainers\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpredict_y\u001b[49m(\n\u001b[32m 436\u001b[39m \u001b[38;5;28mself\u001b[39m.models[i],\n\u001b[32m 437\u001b[39m \u001b[38;5;28mself\u001b[39m.models[i].dataloader(\n\u001b[32m 438\u001b[39m \u001b[38;5;28mself\u001b[39m._maybe_scale_C(C),\n\u001b[32m 439\u001b[39m \u001b[38;5;28mself\u001b[39m._maybe_scale_X(X),\n\u001b[32m 440\u001b[39m np.zeros((\u001b[38;5;28mlen\u001b[39m(C), \u001b[38;5;28mself\u001b[39m.y_dim)),\n\u001b[32m 441\u001b[39m ),\n\u001b[32m 442\u001b[39m **kwargs,\n\u001b[32m 443\u001b[39m )\n\u001b[32m 444\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m.models))\n\u001b[32m 445\u001b[39m ],\n\u001b[32m 446\u001b[39m dtype=\u001b[38;5;28mfloat\u001b[39m,\n\u001b[32m 447\u001b[39m )\n\u001b[32m 448\u001b[39m \u001b[38;5;66;03m# aggregation across bootstraps\u001b[39;00m\n\u001b[32m 449\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m individual_preds:\n", - "\u001b[31mAttributeError\u001b[39m: 'GraphTrainer' object has no attribute 'predict_y'" - ] - } - ], - "source": [ - "import numpy as np\n", - "from contextualized.easy import ContextualizedBayesianNetworks\n", - "\n", - "n_samples = 100\n", - "n_contexts = 5\n", - "n_features = 10\n", - "n_bootstraps = 3\n", - "C = np.random.uniform(-1, 1, size=(n_samples, n_contexts))\n", - "X = np.random.normal(0, 1, size=(n_samples, n_features))\n", - "\n", - "cbn = ContextualizedBayesianNetworks(n_bootstraps=n_bootstraps)\n", - "cbn.fit(C, X, max_epochs=1)\n", - "\n", - "y_pred = cbn.predict(C, X) # Problematic line\n", - "\n", - "# Extra tests\n", - "assert y_pred.shape == (n_samples, n_features)\n", - "y_pred_avg = cbn.predict(C, X, individual_preds=False)\n", - "assert y_pred_avg.shape == (n_samples, n_features)\n", - "y_pred_individual = cbn.predict(C, X, individual_preds=True)\n", - "assert y_pred_individual.shape == (n_bootstraps, n_samples, n_features)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "contextualized", - "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.11.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}