{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Performing a Combination\n", "\n", "We'll demonstrate how a combination works by combining everything we've learned so far.\n", "\n", "## Loading the Workspace\n", "\n", "To do so, we'll use a simple workspace to demonstrate functionality of combinations." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "import pyhf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open(\"data/2-bin_1-channel.json\") as serialized:\n", " spec = json.load(serialized)\n", "\n", "workspace = pyhf.Workspace(spec)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combine Workspaces\n", "\n", "Let's just try to combine naively right now." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "pyhf.Workspace.combine(workspace, workspace)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we can see, we can't just combine a workspace with itself if it has some channel names in common. We try very hard in `pyhf` to make sure a combination \"makes sense\".\n", "\n", "Let's go ahead and rename the channel (as well as the measurement). Then try to combine." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "other_workspace = workspace.rename(\n", " channels={\"singlechannel\": \"othersinglechannel\"},\n", " modifiers={\"uncorr_bkguncrt\": \"otheruncorr_bkguncrt\"},\n", " measurements={\"Measurement\": \"OtherMeasurement\"},\n", ")\n", "\n", "combined_workspace = pyhf.Workspace.combine(workspace, other_workspace)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And did we combine?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\" channels: {combined_workspace.channels}\")\n", "print(f\" nbins: {combined_workspace.channel_nbins}\")\n", "print(f\" samples: {combined_workspace.samples}\")\n", "print(f\" modifiers: {combined_workspace.modifiers}\")\n", "print(f\"measurements: {combined_workspace.measurement_names}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indeed. And at this point, we can just use all the same functionality we expect of pyhf, such as performing a fit:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = workspace.model()\n", "data = workspace.data(model)\n", "test_poi = 1.0\n", "\n", "pyhf.infer.hypotest(test_poi, data, model, test_stat=\"qtilde\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "other_model = other_workspace.model()\n", "other_data = other_workspace.data(other_model)\n", "\n", "pyhf.infer.hypotest(test_poi, other_data, other_model, test_stat=\"qtilde\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "combined_model = combined_workspace.model()\n", "combined_data = combined_workspace.data(combined_model)\n", "\n", "pyhf.infer.hypotest(test_poi, combined_data, combined_model, test_stat=\"qtilde\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.7" } }, "nbformat": 4, "nbformat_minor": 4 }