<jupyter_start><jupyter_text>Visualization with ``EOCube``<jupyter_code>%load_ext autoreload
%autoreload 2

import numpy as np
import pandas as pd
from pathlib import Path
import rasterio
import seaborn as sns
import matplotlib.pyplot as plt

# from eobox.raster import MultiRasterIO
from eobox import sampledata
from eobox.raster import cube
from eobox.raster import gdalutils

from eobox.raster.utils import cleanup_df_values_for_given_dtype
from eobox.raster.utils import dtype_checker_df

print(cube.__file__)
print(sampledata.__file__)<jupyter_output>/home/ben/Devel/Packages/eo-box/raster/eobox/raster/cube.py
/home/ben/Devel/Packages/eo-box/sampledata/eobox/sampledata/__init__.py<jupyter_text>Sample dataset<jupyter_code>def get_sampledata(year):
    dataset = sampledata.get_dataset("lsts")
    layers_paths = [Path(p) for p in dataset["raster_files"]]
    layers_df = pd.Series([p.stem for p in layers_paths]).str.split("_", expand=True) \
    .rename({0: "sceneid", 1:"band"}, axis=1)

    layers_df["date"] = pd.to_datetime(layers_df.sceneid.str[9:16], format="%Y%j")
    layers_df["uname"] = layers_df.sceneid.str[:3] + "_" + layers_df.date.dt.strftime("%Y-%m-%d") + "_" + layers_df.band.str[::] 
    layers_df["path"] = layers_paths

    layers_df = layers_df.sort_values(["date", "band"])
    layers_df = layers_df.reset_index(drop=True)

    layers_df_year = layers_df[(layers_df.date >= str(year)) & (layers_df.date < str(year+1))]
    layers_df_year = layers_df_year.reset_index(drop=True)
    return layers_df_year<jupyter_output><empty_output><jupyter_text>The sample data we are loading here contains 23 scenes each of which consists of three bands (*b3*, *b4*, *b5*) and a QA (quality assessment) band (here *fmask*).*fmask* has the following categories:      0 - clear land      1 - clear water      2 - cloud      3 - snow      4 - shadow    255 - NoData<jupyter_code>df_layers = get_sampledata(2008)
display(df_layers.head(4))
print(df_layers.band.value_counts())
print(df_layers.date.dt.strftime("%Y-%m-%d").unique())<jupyter_output><empty_output><jupyter_text>**Initialize an ``EOCubeSceneCollection``**<jupyter_code>df_layers=df_layers
chunksize=2**5
variables=["b3", "b4", "b5"]
qa="fmask"
qa_valid=[0, 1]<jupyter_output><empty_output><jupyter_text>**Get a chunk and read the data**<jupyter_code>scoll = cube.EOCubeSceneCollection(df_layers=df_layers, 
                                   chunksize=chunksize, 
                                   variables=variables, 
                                   qa=qa, 
                                   qa_valid=qa_valid 
                                  )
display(scoll.df_layers.head())

scoll_chunk = scoll.get_chunk(1)
scoll_chunk.read_data()
scoll_chunk.data.shape<jupyter_output><empty_output><jupyter_text>Plotting raster  Single layer Continuous<jupyter_code>fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 6))

aximg = scoll_chunk.plot_raster(0, spatial_bounds=False, ax=axes[0])
axes[0].set_title(scoll_chunk.df_layers.uname[0])

aximg = scoll_chunk.plot_raster(1, spatial_bounds=False, ax=axes[1])
axes[1].set_title(scoll_chunk.df_layers.uname[1])

aximg = scoll_chunk.plot_raster(2, spatial_bounds=False, ax=axes[2])
axes[2].set_title(scoll_chunk.df_layers.uname[2])

plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Categorical**ToDo** RGBsThere is a helper for getting the indices of commonly required for RGB plots:<jupyter_code># three bands, one date
ilocs = scoll_chunk.get_df_ilocs(band=["b5", "b4", "b3"], 
                                 date="2008-06-30")
print(ilocs)
display(scoll_chunk.df_layers.iloc[ilocs])
# one band, three dates
ilocs = scoll_chunk.get_df_ilocs(band="b5", 
                                 date=['2008-04-19', '2008-07-08', '2008-09-26'])
print(ilocs)
display(scoll_chunk.df_layers.iloc[ilocs])<jupyter_output>[34, 33, 32]<jupyter_text>Plot false color RGB (SWIR-1, NIR, RED) of tree days. Each with its own robust color stretch.<jupyter_code>fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 6))

ilocs = scoll_chunk.get_df_ilocs(band=["b5", "b4", "b3"], 
                                 date="2008-04-19")
print(ilocs)
scoll_chunk.plot_raster_rgb(ilocs, 
                            spatial_bounds=False, 
                            robust=True,
                            ax=axes[0])
axes[0].set_title("2008-04-19")


ilocs = scoll_chunk.get_df_ilocs(band=["b5", "b4", "b3"], 
                                 date="2008-07-08")
print(ilocs)
scoll_chunk.plot_raster_rgb(ilocs, 
                            spatial_bounds=False, 
                            robust=True,
                            ax=axes[1])
axes[1].set_title("2008-07-08")

ilocs = scoll_chunk.get_df_ilocs(band=["b5", "b4", "b3"], 
                                 date="2008-09-26")
print(ilocs)
scoll_chunk.plot_raster_rgb(ilocs, 
                            spatial_bounds=False, 
                            robust=True,
                            ax=axes[2])
axes[2].set_title("2008-09-26")

plt.tight_layout()<jupyter_output>[2, 1, 0]
[38, 37, 36]
[74, 73, 72]<jupyter_text>Plot false color RGB (SWIR-1, NIR, RED) of tree days. All with the same color stretch derived from the first image.<jupyter_code>master_ilocs = scoll_chunk.get_df_ilocs(band=["b5", "b4", "b3"], 
                                        date="2008-04-19")
master_array = scoll_chunk.data[:, :, master_ilocs]

vmin, vmax = scoll_chunk.robust_data_range(master_array, robust=True)
print(vmin)
print(vmax)
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 6))

ilocs = scoll_chunk.get_df_ilocs(band=["b5", "b4", "b3"], 
                                 date="2008-04-19")
scoll_chunk.plot_raster_rgb(ilocs, 
                            spatial_bounds=False, 
                            vmin=vmin, vmax=vmax,
                            ax=axes[0])
axes[0].set_title("2008-04-19")


ilocs = scoll_chunk.get_df_ilocs(band=["b5", "b4", "b3"], 
                                 date="2008-07-08")
scoll_chunk.plot_raster_rgb(ilocs, 
                            spatial_bounds=False, 
                            vmin=vmin, vmax=vmax,
                            ax=axes[1])
axes[1].set_title("2008-07-08")

ilocs = scoll_chunk.get_df_ilocs(band=["b5", "b4", "b3"], 
                                 date="2008-09-26")
scoll_chunk.plot_raster_rgb(ilocs, 
                            spatial_bounds=False, 
                            vmin=vmin, vmax=vmax,
                            ax=axes[2])
axes[2].set_title("2008-09-26")

plt.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13395164968830775, 'fraction_numerical': 0.039227611372966396, 'mean_word_length': 2.880825958702065, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '438545', 'license': 'MIT', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2338, 'n_words': 372, 'path': 'examples/raster/cube_viz.ipynb', 'repo_name': 'luyang1210/EO', 'size': '6577'}