<jupyter_start><jupyter_text>Analyze spatial entropy for model realizations<jupyter_code># basic imports
import numpy as np                                        
import matplotlib.pyplot as plt     
import pickle
import pandas as pd
# adjust settings for matplotlib:

# Loading ipython widgets for interactive features:
import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed, interact_manual
from IPython.display import display<jupyter_output><empty_output><jupyter_text>Load DataWe first load the dataset from the generated `pickle` file:<jupyter_code># realizations = pickle.load(open("facies_realizations_ncut_64x64.pkl", 'rb'))
realizations = pickle.load(open("facies_realizations_ncut_f3_aniso_64x64_4.pkl", 'rb'))
df = pickle.load(open("data_frame_three_facies_2.pkl", 'rb'))

df.tail()<jupyter_output><empty_output><jupyter_text>Visualize realizationsAs a first step, we visualize some of the generated geostatistical fields:<jupyter_code>plt.figure(figsize=(12,6))
for i in range(1, 9):
    plt.subplot(2, 4, i)
    plt.imshow(realizations[i])<jupyter_output><empty_output><jupyter_text>What do we observe? For comparison, here also the measurement locations:<jupyter_code>plt.figure(figsize=(12,6))
for i in range(1, 9):
    plt.subplot(2, 4, i)
    plt.imshow(realizations[i])
    plt.scatter(data_points[:,0]-0.5, data_points[:,1]-0.5, c='w')<jupyter_output><empty_output><jupyter_text>...and for a bit more intuitive interpretation, here the possibility to flip through some examples:<jupyter_code>def plot_realization(i):
    plt.figure(figsize=(12,8))
    plt.imshow(realizations[i][::-1,:])
    plt.scatter(data_points[:,0]-0.5, data_points[:,1]-0.5, c='w') #c=df["Facies"])

interact(plot_realization, i=widgets.IntSlider(min=1,max=99,step=1,value=1));<jupyter_output><empty_output><jupyter_text>Spatial analysis using entropyWe now calculate the entropy of each location in space. Note the difference to many of the other examples we have seen this week: we here consider the outcome in each cell as a random variable, estimate probabilities for each outcome based on frequencies, and calculate the entropy:$H(\vec{x}) = - \sum_i p_i(\vec{x}) \log p_i(\vec{x})$We could do this calculation on our own - but for convenience, it is all coded up in a python module ().<jupyter_code>import sys
sys.path.append("../../../hspace/")
import hspace.measures
import importlib
importlib.reload(hspace.measures)
h_sec = hspace.measures.EntropySection(realizations.swapaxes(1,2))
h_sec.plot_entropy();
data_points = df.values[:,2:4]/1000*64
plt.imshow(np.sum(realizations, axis=0)[::-1,:])
plt.scatter(data_points[:,0]-0.5, data_points[:,1]-0.5, c=df["Facies"])
# plt.scatter(data_points[:,0], data_points[:,1], c='w')
# df.values[:,2:4]/1000*64
# df.plot.scatter(x="X", y="Y", c="Facies", colormap='viridis')
im = plt.imshow(h_sec.h[::-1,:], cmap='gray_r')
plt.scatter(data_points[:,0]-0.5, data_points[:,1]-0.5, c=df["Facies"])
plt.colorbar(im)
im = plt.imshow(h_sec.h[::-1,:], cmap='gray_r')
# plt.scatter(data_points[:,0]-0.5, data_points[:,1]-0.5, c=df["Facies"])
plt.colorbar(im)
h_sec.plot_entropy();
m = np.meshgrid(np.arange(40,41), np.arange(30,40), indexing='xy')
pos = np.vstack([m[0].ravel(), m[1].ravel()]).T
pos = np.array([[40,33],[50,35],[42,45]])
h_sec.calc_cond_entropy_section(pos=pos);
h_sec.plot_cond_entropy_and_MI()
m = np.meshgrid(np.arange(2,4), np.arange(4,6), indexing='xy')
realizations.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16106804478897502, 'fraction_numerical': 0.03273040482342808, 'mean_word_length': 6.397027600849257, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '572084', 'license': 'MIT', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1290, 'n_words': 263, 'path': 'geoinfotheory/notebooks/ITgeo_03_entropy.ipynb', 'repo_name': 'cgre-aachen/teaching', 'size': '3483'}