From 3ca519782ce92bb18fade9bc0de78018ca389edb Mon Sep 17 00:00:00 2001 From: Vishal Johnson <vishal.johnson@physik.lmu.de> Date: Tue, 24 Oct 2023 08:18:52 +0000 Subject: [PATCH] Upload New File --- resolve_workshop_intro_blank.ipynb | 579 +++++++++++++++++++++++++++++ 1 file changed, 579 insertions(+) create mode 100644 resolve_workshop_intro_blank.ipynb diff --git a/resolve_workshop_intro_blank.ipynb b/resolve_workshop_intro_blank.ipynb new file mode 100644 index 0000000..5558cc9 --- /dev/null +++ b/resolve_workshop_intro_blank.ipynb @@ -0,0 +1,579 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "918290f8-e19c-4feb-bddd-043e531b88af", + "metadata": {}, + "source": [ + "# Intro to nIFTy" + ] + }, + { + "cell_type": "markdown", + "id": "3b1f3be6-76a8-42f9-a91c-bd2a6bc4df6b", + "metadata": {}, + "source": [ + "### Spaces, Fields, and Operators\n", + "\n", + "A space is the manifold over which fields are defined. We use the mechanisms of statistical field theory and probability theory to infer the field. A probability distribution over field configurations is what is inferred. Operators are defined over the fields." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ad813a2-2eaf-4777-920c-7b6bb22ffd8e", + "metadata": {}, + "outputs": [], + "source": [ + "# import the required packages\n", + "import numpy as np\n", + "import nifty8 as ift" + ] + }, + { + "cell_type": "markdown", + "id": "4c7b7e93-69a5-4e3f-b350-3bbbdb25e37d", + "metadata": {}, + "source": [ + "#### Spaces\n", + "An RGSpace is a regularly spaced grid of points. Its shape and distances can be defined, making it a metric space." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "caaa21e4-c79c-47d2-978d-c41d55c1be79", + "metadata": {}, + "outputs": [], + "source": [ + "# RGSpace\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cf6a098c-ac9b-446e-b8c6-5c3bbeaa9978", + "metadata": {}, + "outputs": [], + "source": [ + "# its properties\n", + "print('shape:', )\n", + "print('size:', )\n", + "print('distances', )\n", + "print('volume element:', )\n", + "print('total volume:', )" + ] + }, + { + "cell_type": "markdown", + "id": "94918a2d-a0cd-4bf6-a7ac-f7e75c092574", + "metadata": {}, + "source": [ + "RGSpaces have natural harmonic counterpart. Their properties are listed here." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79627d10-09e2-4603-99ce-26915d1d74c7", + "metadata": {}, + "outputs": [], + "source": [ + "# codomain and properties\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "311c195e-3643-4a54-8bc5-91ae348ad4c3", + "metadata": {}, + "outputs": [], + "source": [ + "# its properties\n", + "print('shape:', )\n", + "print('size:', )\n", + "print('distances', )\n", + "print('volume element:', )\n", + "print('total volume:', )" + ] + }, + { + "cell_type": "markdown", + "id": "7b5ba271-3218-4984-a016-9f761ff94460", + "metadata": {}, + "source": [ + "Notice the distances!" + ] + }, + { + "cell_type": "markdown", + "id": "dc57afbc-b953-4d1f-9c24-8ef624467b65", + "metadata": {}, + "source": [ + "An unstructured domain has no metric structure." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9368aa50-605f-4f68-8d07-2754442792f9", + "metadata": {}, + "outputs": [], + "source": [ + "# unstructured domain and properties\n" + ] + }, + { + "cell_type": "markdown", + "id": "acc47115-43e3-4e6d-9545-cc73d3e3d85f", + "metadata": {}, + "source": [ + "Spaces can be combined to form new spaces, there can be product as well as sum spaces. It is to be noted the relevant vector space is that of the field which lives on the space and not the space itself." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a6df7f4e-db0e-42aa-a7b4-177282ec9a2b", + "metadata": {}, + "outputs": [], + "source": [ + "# product domain\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "60e19649-7cb5-478e-8625-ee928b5ba63a", + "metadata": {}, + "outputs": [], + "source": [ + "# its properties\n", + "print('shape:', )\n", + "print('size:', )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "97a7c46d-54ba-4da1-8117-031d0bda9f38", + "metadata": {}, + "outputs": [], + "source": [ + "# sum domain, multidomain\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0cb103ef-9221-4341-ab8f-7fa32624c8a9", + "metadata": {}, + "outputs": [], + "source": [ + "# its properties\n", + "print('size:', )" + ] + }, + { + "cell_type": "markdown", + "id": "097acdeb-2cfc-469a-a3d7-eaa0a9439ea0", + "metadata": {}, + "source": [ + "#### Fields\n", + "Fields live on spaces." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cb958810-78ec-48f7-accf-fe2604c5c856", + "metadata": {}, + "outputs": [], + "source": [ + "# create an array of values for the field and create a field out of it\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2a206afa-a079-479e-86ec-2465f0ad29a6", + "metadata": {}, + "outputs": [], + "source": [ + "# randomly sample the fields, sampled from gaussian\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7513bdea", + "metadata": {}, + "outputs": [], + "source": [ + "# some properties of fields\n", + "print('sum:\\n', )\n", + "print()\n", + "print('integrate:\\n', )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3c5169c1-3c51-45bb-99e3-4ee12233deed", + "metadata": {}, + "outputs": [], + "source": [ + "# prod field properties\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b761c526", + "metadata": {}, + "outputs": [], + "source": [ + "# sum field properties\n", + "\n", + "\n", + "print('domain:', )\n", + "print()\n", + "print('val:', )" + ] + }, + { + "cell_type": "markdown", + "id": "0433b60a-71a3-4313-90eb-c5e80b4033f0", + "metadata": {}, + "source": [ + "#### Operators\n", + "There also exist operators into and between spaces. We begin by defining a mask operator." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e4217581-594e-4397-a813-562ef2915e8a", + "metadata": {}, + "outputs": [], + "source": [ + "# first create a mask field\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b0248b09-122b-4931-9bba-063e7405a264", + "metadata": {}, + "outputs": [], + "source": [ + "# its properties\n", + "print('domain:', )\n", + "print()\n", + "print('target:', )" + ] + }, + { + "cell_type": "markdown", + "id": "053e10b5-7f9c-40c9-a77e-e97b3c5d8f5f", + "metadata": {}, + "source": [ + "Notice the target domain!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "be7ddf49-ca20-49c8-970c-399416e8e40c", + "metadata": {}, + "outputs": [], + "source": [ + "# here is an example of its working\n", + "\n", + "\n", + "print('input:', )\n", + "print()\n", + "print('output:', )" + ] + }, + { + "cell_type": "markdown", + "id": "e21e9571", + "metadata": {}, + "source": [ + "What are the values inserted when information is missing for the mask operator adjoint?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fde5a3d7", + "metadata": {}, + "outputs": [], + "source": [ + "# adjoint\n" + ] + }, + { + "cell_type": "markdown", + "id": "73cf87a4-55bc-4a38-9db4-74b18fcaaad2", + "metadata": {}, + "source": [ + "Now consider a mapping between harmonic to non-harmonic space." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6f197335-d441-4ec2-86b4-8f1100da1321", + "metadata": {}, + "outputs": [], + "source": [ + "# operator mapping harmonic to non-harmonic domain\n", + "\n", + "\n", + "print('domain:', )\n", + "print()\n", + "print('target:', )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ee9769e-e4f0-4741-86b3-bbd56a188549", + "metadata": {}, + "outputs": [], + "source": [ + "# another method of creating fields\n", + "\n", + "print('sampled harmonic field:\\n', )\n", + "print()\n", + "print('corresponding real field:\\n', )" + ] + }, + { + "cell_type": "markdown", + "id": "38c96141-33f0-46fd-a893-6508b0af8897", + "metadata": {}, + "source": [ + "### Correlated Field Paradigm\n", + "A physics inspired paradigm to perform inference. Natural properties such as smoothnes of fields is inherently included." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0c9d46ef-4334-4958-b557-52619ecb8336", + "metadata": {}, + "outputs": [], + "source": [ + "# define the spaces\n" + ] + }, + { + "cell_type": "markdown", + "id": "b8bc212a-f0fa-4ee4-aeb7-81546d08e01e", + "metadata": {}, + "source": [ + "Power space is a space for representations of the harmonic space fields where the assumptions of isotropy is used so that there are fewer parameters than the whole field. Only the absolute distances count separately." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a11b5fb3-dd0e-4e05-895c-d22df7520312", + "metadata": {}, + "outputs": [], + "source": [ + "# define power space\n", + "\n", + "\n", + "# and print its properties\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "903e688f-997e-4494-95a5-6f70d0aaa935", + "metadata": {}, + "outputs": [], + "source": [ + "# now add a power spectrum\n", + "\n", + "\n", + "# and make into field\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa3d3410-dbd2-48d9-a8c0-9a7aa837fd03", + "metadata": {}, + "outputs": [], + "source": [ + "# power distributor maps from the power space to the harmonic space\n", + "\n", + "\n", + "# sqrt used as power spectrum in covariance and not field\n", + "\n", + "\n", + "print('domain:', )\n", + "print('target:', )" + ] + }, + { + "cell_type": "markdown", + "id": "7d51317d-fd01-4259-9a08-4e7df0af1a3a", + "metadata": {}, + "source": [ + "We now consider some realisations of fields with the given power spectrum. In order to do this we need to create an excitation field in the harmonic domain. Then this domain is sampled and we look at a few realisations." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ee8c61e4-abfe-409f-8eeb-31901d9bf86b", + "metadata": {}, + "outputs": [], + "source": [ + "# building the fixed spectrum model\n", + "\n", + "\n", + "print('domain:', )\n", + "print()\n", + "print('target:', )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a80abe47-e2fb-4f88-b24f-7407e5196409", + "metadata": {}, + "outputs": [], + "source": [ + "# helper function to look at samples\n", + "def make_samples(model, num=9, ps=None, ncol=None):\n", + " pl = ift.Plot()\n", + " \n", + " for _ in range(num):\n", + " latent_sample = ift.from_random(model.domain)\n", + " pl.add(model(latent_sample))\n", + " \n", + " if ps:\n", + " pl.add(ps.force(latent_sample))\n", + "\n", + " if ncol:\n", + " pl.output(nx=ncol)\n", + " else:\n", + " pl.output()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c0c8e5b1-3546-4d09-b319-dcddd65f7129", + "metadata": {}, + "outputs": [], + "source": [ + "# now we look at samples\n" + ] + }, + { + "cell_type": "markdown", + "id": "f6e8d695-de64-4db5-a18a-8a856df444fb", + "metadata": {}, + "source": [ + "We now use a correlated field model to generate samples." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cd860dfe-8fae-4356-a25e-dff0553f0a5c", + "metadata": {}, + "outputs": [], + "source": [ + "# arguments for correlated field model\n", + "args = {\n", + " 'offset_mean': 0,\n", + " 'offset_std': (1e-3, 1e-6),\n", + "\n", + " # Amplitude of field fluctuations\n", + " 'fluctuations': (1., 0.8), # 1.0, 1e-2\n", + "\n", + " # Exponent of power law power spectrum component\n", + " 'loglogavgslope': (-3., 1.), # -6.0, 1\n", + "\n", + " # Amplitude of integrated Wiener process power spectrum component\n", + " 'flexibility': (2.0, 1.0), # 1.0, 0.5\n", + "\n", + " # How ragged the integrated Wiener process component is\n", + " 'asperity': (0.2, 0.1) # 0.1, 0.5\n", + "}\n", + "cf_model = ift.SimpleCorrelatedField(position_space, **args)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b8e87437-03bc-48de-86f1-93170b21775d", + "metadata": {}, + "outputs": [], + "source": [ + "# properties of cf model\n", + "print('domain:', )\n", + "print()\n", + "print('target:', )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8e600703-03c6-4e89-8db8-29a09a9c2fa2", + "metadata": {}, + "outputs": [], + "source": [ + "# make some samples of power spectra\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3de132ae-ba7d-4746-b941-5e44d7198d27", + "metadata": {}, + "outputs": [], + "source": [ + "# now make samples of correlated field model\n" + ] + }, + { + "cell_type": "markdown", + "id": "b0187e73-907f-4f47-a35a-e50726a49614", + "metadata": {}, + "source": [ + "By eye it seems that the correlated field model is really quite expressive! The point of inference is to now learn the optimal parameters of the correlated field model given some data." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} -- GitLab