<jupyter_start><jupyter_text>Setup  Github SetupIf not already present this clones the repo into google colab so that I may work on it with a bash environment.<jupyter_code>from getpass import getpass
import os

# if not in a repo clone the desired one
if not os.path.isdir("/content/repo"):
    
    # get username and password
    user = "Boyne272"
    password = getpass('github password')
    os.environ['GITHUB_AUTH'] = user + ':' + password
    
    # clone the repo
    !git clone --quiet https://$GITHUB_AUTH@github.com/msc-acse/acse-9-independent-research-project-Boyne272.git repo

    # move into the repo
    %cd repo
    
    # swap to the wanted branch
    !git checkout Release
    !git pull origin Release --quiet

# set name and email
!git config --global user.name "<NAME>"
!git config --global user.email "<EMAIL>"

# remove the sample data if there
if os.path.isdir("/content/sample_data"):
    !rm -r /content/sample_data
    
# show where we are
!git show --summary<jupyter_output><empty_output><jupyter_text>Github SpaceUse the cells here to run github commands<jupyter_code>%cd /content/repo
# !git pull origin master
# !git pull origin merging
# !git checkout merging
# !git pull post_processing
# !git branch
!rm -r images/Results
!mv images/Dumb_TS_meme.jpg images/meme.jpg 
!git status
!git rm -r  images/Results/
!git commit -m "uploaded results notebook and removed old results images"

!git push origin Release<jupyter_output><empty_output><jupyter_text>Run TestsRun all pytests here, try to do as often as possible<jupyter_code>!pytest # last run 28th Aug<jupyter_output><empty_output><jupyter_text>Imports<jupyter_code># Ipython magic functions
%load_ext autoreload
%autoreload 2
%matplotlib inline
# imports
import torch
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd 

# custom imports
%cd /content/repo
from TSA.merging import AGNES
from TSA.merging import segment_group
from TSA.pre_post_processing import Segment_Analyser
from TSA.pre_post_processing import Image_processor
from TSA.kmeans import SLIC
from TSA.kmeans import MSLIC_wrapper
# set matplotlib preferences - report
import matplotlib as mpl
mpl.rcdefaults()
plt.rc('axes', titlesize=30, labelsize=25)
plt.rc('axes.formatter', limits=[-4, 4])
plt.rc('ytick', labelsize=12)
plt.rc('xtick', labelsize=12)
plt.rc('lines', linewidth=2, markersize=7)
plt.rc('figure', figsize=(9, 9))
# print(plt.rcParams) # all parameters<jupyter_output><empty_output><jupyter_text>Results  showing unblured and blured results<jupyter_code>butterfly_IP = Image_processor(path='/content/images/butterfly.tif')
butterfly_IP.plot()
# blur the image to reduce spotting effect
butterfly_IP.reset()
blur = butterfly_IP.gauss(sigma=3, key='blur')
# create the SLIC objects iterate it and plot
blured_SLIC = SLIC(blur, [25, 25], dist_metric_args=[1.])
blured_SLIC.iterate(10)

unblured_SLIC = SLIC(butterfly_IP.imgs['original'], [25, 25], dist_metric_args=[1.])
unblured_SLIC.iterate(10)
fig, ax = plt.subplots(figsize=[30, 30])
butterfly_IP.plot('original', ax=ax)
blured_SLIC.plot(option='edges', ax=ax)
plt.savefig(r'blured.tif')

fig, ax = plt.subplots(figsize=[30, 30])
butterfly_IP.plot('original', ax=ax)
unblured_SLIC.plot(option='edges', ax=ax)
plt.savefig(r'unblured.tif')<jupyter_output><empty_output><jupyter_text>Showing different bin grids<jupyter_code>big_SLIC = SLIC(butterfly_IP.imgs['blur'], [20, 20], dist_metric_args=[1.])
big_SLIC.iterate(10)

small_SLIC = SLIC(butterfly_IP.imgs['blur'], [40, 40], dist_metric_args=[1.])
small_SLIC.iterate(10)
fig, ax = plt.subplots(figsize=[30, 30])
butterfly_IP.plot('original', ax=ax)
small_SLIC.plot(option='edges', ax=ax)
ax.set(title='')
ax.axis('off')
plt.savefig(r'high_res.tif')

fig, ax = plt.subplots(figsize=[30, 30])
butterfly_IP.plot('original', ax=ax)
big_SLIC.plot(option='edges', ax=ax)
ax.set(title='')
ax.axis('off')
plt.savefig(r'low_res.tif')<jupyter_output><empty_output><jupyter_text>Color space comparison<jupyter_code>space_SLIC = SLIC(butterfly_IP.imgs['blur'], [25, 25], dist_metric_args=[2.])
space_SLIC.iterate(10)

color_SLIC = SLIC(butterfly_IP.imgs['blur'], [25, 25], dist_metric_args=[.5])
color_SLIC.iterate(10)
fig, ax = plt.subplots(figsize=[30, 30])
butterfly_IP.plot('original', ax=ax)
space_SLIC.plot(option='edges', ax=ax)
ax.set(title='')
ax.axis('off')
plt.savefig(r'space_bias.tif')

fig, ax = plt.subplots(figsize=[30, 30])
butterfly_IP.plot('original', ax=ax)
color_SLIC.plot(option='edges', ax=ax)
ax.set(title='')
ax.axis('off')
plt.savefig(r'color_bias.tif')<jupyter_output><empty_output><jupyter_text>SLIC and MSLIC Comparison<jupyter_code>IP_example_white = Image_processor(path='images/example_white.tif')
IP_example_polar = Image_processor(path='images/example_polar.tif')
# blur the image to reduce spotting effect
IP_example_white.reset()
IP_example_white.gauss(sigma=3, key='blur')

# blur the image to reduce spotting effect
IP_example_polar.reset()
IP_example_polar.gauss(sigma=3, key='blur')
# SLIC on both
white_SLIC = SLIC(IP_example_white.imgs['blur'], [25, 25], dist_metric_args=[1.])
white_SLIC.iterate(10)

polar_SLIC = SLIC(IP_example_polar.imgs['blur'], [25, 25], dist_metric_args=[1.])
polar_SLIC.iterate(10)
fig, ax = plt.subplots(figsize=[30, 30])
IP_example_white.plot('original', ax=ax)
white_SLIC.plot(option='edges', ax=ax)
ax.set(title='')
ax.axis('off')
plt.savefig(r'white_SLIC.tif')

fig, ax = plt.subplots(figsize=[30, 30])
IP_example_polar.plot('original', ax=ax)
polar_SLIC.plot(option='edges', ax=ax)
ax.set(title='')
ax.axis('off')
plt.savefig(r'polar_SLIC.tif')
# MSLIC with both
example_MSLIC = MSLIC_wrapper([IP_example_white.imgs['blur'],
                               IP_example_polar.imgs['blur']][::-1],
                              [25, 25],
                              dist_metric_args=[1.])
example_MSLIC.iterate(10)
fig, ax = plt.subplots(figsize=[30, 30])
IP_example_white.plot('original', ax=ax)
example_MSLIC.plot([0], option='edges', axs=[ax])
ax.set(title='')
ax.axis('off')
plt.savefig(r'white_MSLIC.tif')

fig, ax = plt.subplots(figsize=[30, 30])
IP_example_polar.plot('original', ax=ax)
example_MSLIC.plot([1], option='edges', axs=[ax])
ax.set(title='')
ax.axis('off')
plt.savefig(r'polar_MSLIC.tif')
###################################
fig, ax = plt.subplots(figsize=[30, 30])
IP_example_white.plot('original', ax=ax)
example_MSLIC.plot([0], option='edges', axs=[ax])
ax.set(title='')
ax.axis('off')
plt.savefig(r'white_MSLIC.tif')

fig, ax = plt.subplots(figsize=[30, 30])
IP_example_polar.plot('original', ax=ax)
example_MSLIC.plot([1], option='edges', axs=[ax])
ax.set(title='')
ax.axis('off')
plt.savefig(r'polar_MSLIC.tif')<jupyter_output><empty_output><jupyter_text>Number of Itertaions<jupyter_code># load the butterfly
butterfly_IP = Image_processor(path='/content/images/butterfly.tif')

# blur the image to reduce spotting effect
butterfly_IP.reset()
butterfly_IP.gauss(sigma=3, key='blur')

# create slic obj
butterfly_SLIC = SLIC(butterfly_IP.imgs['blur'], [25, 25], dist_metric_args=[1.])
butterfly_SLIC.iterate(10)

fig, ax = plt.subplots(figsize=[30, 30])
butterfly_IP.plot('original', ax=ax)
butterfly_SLIC.plot(option='edges', ax=ax)
plt.savefig(r'iterations_10.tif')
butterfly_SLIC.iterate(10)

fig, ax = plt.subplots(figsize=[30, 30])
butterfly_IP.plot('original', ax=ax)
butterfly_SLIC.plot(option='edges', ax=ax)
plt.savefig(r'iterations_20.tif')
butterfly_SLIC.iterate(10)

fig, ax = plt.subplots(figsize=[30, 30])
butterfly_IP.plot('original', ax=ax)
butterfly_SLIC.plot(option='edges', ax=ax)
plt.savefig(r'iterations_30.tif')<jupyter_output><empty_output><jupyter_text>Demonstraiting edge detection<jupyter_code># load image
butterfly_IP = Image_processor(path='images/butterfly.tif')

# blur the image to reduce spotting effect
butterfly_IP.reset()
butterfly_IP.gauss(sigma=3, key='blur')

# create the SLIC objects iterate it and plot
butterfly_SLIC = SLIC(butterfly_IP.imgs['blur'], [25, 25], dist_metric_args=[1.])
butterfly_SLIC.iterate(10)
# create the segments
butterfly_mask = butterfly_SLIC.get_segmentation()
butterfly_segs = segment_group(butterfly_mask)
butterfly_segs.enforce_size()
butterfly_segs.plot(back_img=butterfly_IP.imgs['original'])
# use scharr edge detection on original image
fig, ax = plt.subplots(figsize=[15, 15])
butterfly_IP.reset()
butterfly_IP.scharr()
butterfly_IP.plot(ax=ax)
ax.set(title='Three Channel Scharr Edge Detection')
ax.axis('off')
plt.savefig('scharr.tif')
# threshold this image
fig, ax = plt.subplots(figsize=[17, 17])
butterfly_IP.grey_scale()
butterfly_IP.threshold(0.05, key='scharr')
butterfly_IP.plot('scharr', ax=ax)
ax.set(title=r'Greyscale Theshold Image ($Threshold=0.05$)')
ax.axis('off')
plt.savefig('/content/repo/images/Results/binary_scharr.tif')
# define extraction function
def edge_extraction(Xs, Ys, scharr_img):
    return scharr_img[Ys, Xs].mean() / scharr_img.std()

# pass this to the group object and plot it
butterfly_segs.edge_confidence(edge_extraction, [butterfly_IP.imgs['scharr']])
# make the image
fig, ax = plt.subplots(figsize=[16, 16])
butterfly_IP.reset()
ax.imshow(butterfly_IP.grey_scale(), cmap='gray')
butterfly_segs.plot('edge_conf', ax=ax)
ax.axis('off')
plt.savefig('edge_confidence.tif')
# merge by edge confidence
butterfly_segs.merge_by_edge(edge_absent=.5)

# plot the result
fig, ax = plt.subplots(figsize=[16, 16])
butterfly_segs.plot('merged_edges', ax=ax, back_img=butterfly_IP.imgs['original'])
ax.axis('off')
ax.set(title=r'Edge Confidence Merging ($Threshold=0.5$)')
plt.savefig('images/Results/edge_conf_merged.tif')<jupyter_output><empty_output><jupyter_text>AGNES Clustering Figures<jupyter_code># generate dummy 2d data
np.random.seed(10)
features = np.random.normal(size=[500, 2])
features[50:, :] += 5
features[100:, :] += 5
features[150:, 0] += 5
features[250:300, 1] += 10
features[350:400, 0] += 10
features[-1, -1] += 20

plt.figure(figsize=[5, 5])
plt.scatter(*features.T)
plt.title("dummy data")
# iterate the clustering
obj = AGNES(features)
obj.iterate()
fig, axs = plt.subplots(1, 3, figsize=[27, 8])


# ------------------------------------

obj.cluster_distance_plot('dists', last_few=False, ax=axs[0])
axs[0].set(xlabel='Iteration', ylabel='Distance', title='Dummy Data Join Distances')
# plt.savefig('AGNES_distances.tif')


# ------------------------------------


obj.cluster_distance_plot('2nd', last_few=True, ax=axs[1])
axs[1].set(xlabel='Iteration', ylabel='Distance')

std =  0.4297
axs[1].hlines(std, 477, 487, 'r', label=r'$\sigma$')
axs[1].hlines(2*std, 477, 487, 'g', label=r'$2\sigma$')
axs[1].hlines(3*std, 477, 487, 'm', label=r'$3\sigma$')
axs[1].hlines(4*std, 477, 487, 'k', label=r'$4\sigma$')
axs[1].hlines(5*std, 477, 487, 'c', label=r'$5\sigma$')
labs = [r'2nd Derivative', r'+$\sigma$'] + [r'+$%i\sigma$'%i for i in range(2, 6)]
axs[1].legend(labels=labs, loc='upper left', fontsize=15)
axs[1].set(title='Dummy Data Second Derivative')
# plt.savefig('AGNES_derivative.tif')


# ------------------------------------

clusters = obj.cluster_by_derivative(n_std=1., plot=False)

for clust in np.unique(clusters):
    f = features[clusters==clust]
    axs[2].scatter(*f .T, label=clust)

# produce a legend with the unique colors from the scatter
axs[2].legend(loc="upper left", fontsize=15)
axs[2].set(title='Dummy Data AGNES Clustering', xlabel='X Distance',
              ylabel='Y Distance')

plt.savefig('AGNES_clustering.tif')
ax = axs[2]
sca
# fig, ax = plt.subplots(figsize=[10, 10])
# obj.cluster_distance_plot('2nd', last_few=True, ax=ax)
# ax.set(xlabel='Iteration', ylabel='Distance')

# std =  0.4297
# ax.hlines(std, 477, 487, 'r', label=r'$\sigma$')
# ax.hlines(2*std, 477, 487, 'g', label=r'$2\sigma$')
# ax.hlines(3*std, 477, 487, 'm', label=r'$3\sigma$')
# ax.hlines(4*std, 477, 487, 'k', label=r'$4\sigma$')
# ax.hlines(5*std, 477, 487, 'c', label=r'$5\sigma$')
# labs = [r'2nd Derivative', r'+$\sigma$'] + [r'+$%i\sigma$'%i for i in range(2, 6)]
# ax.legend(labels=labs, loc='upper left', fontsize=12)
# ax.set(title='Dummy Data Second Derivative')
# plt.savefig('AGNES_derivative.tif')
# obj.cluster_by_derivative(n_std=1., plot=True)
# plt.gca().set(title='Dummy Data AGNES Clustering', xlabel='X Distance',
#               ylabel='Y Distance')
# plt.savefig('AGNES_clustering.tif')<jupyter_output><empty_output><jupyter_text>Complexity Analysis<jupyter_code># load a large image made by putting 4 smaller images together
paths = ['images/California/white_1.tif', 'images/California/white_2.tif', 
         'images/Indonesia/white_1.tif', 'images/Indonesia/white_2.tif']
arrays = [Image_processor(path=p).imgs['original'] for p in paths]
comb_image = np.vstack([np.hstack(arrays[:2]), np.hstack(arrays[2:])])
IP_combo = Image_processor(img=comb_image)
IP_combo.plot()<jupyter_output><empty_output><jupyter_text>SLIC time analysis<jupyter_code>s = 400 # image size
img = comb_image[:s, :s]
g = 100
bin_grid = [g, g]
%%timeit
obj = SLIC(img, bin_grid)
obj.iterate(10)<jupyter_output><empty_output><jupyter_text>**Times varying grid size**size 400 used(2, 2) 1 loop, best of 3: 4.87 s per loop(4, 4) 1 loop, best of 3: 7.49 s per loop(5, 5) 1 loop, best of 3: 8.16 s per loop(8, 8) 1 loop, best of 3: 9.47 s per loop(10, 10) 1 loop, best of 3: 10.1 s per loop(16, 16) 1 loop, best of 3: 12.3 s per loop(20, 20) 1 loop, best of 3: 14 s per loop(25, 25) 1 loop, best of 3: 16.5 s per loop(40, 40) 1 loop, best of 3: 25.8 s per loop(50, 50) 1 loop, best of 3: 34.3 s per loop(100, 100) 1 loop, best of 3: 1min 45s per loop<jupyter_code>times = [4.87, 7.49, 8.16, 9.47, 10.1, 12.3, 14, 16.5, 25.8, 34.3, 60+45]
sizes = [2, 4, 5, 8, 10, 16, 20, 25, 40, 50, 100]
centroids = np.array(sizes)**2

plt.figure(figsize=[8,8])
plt.loglog(centroids, times, 'bo-', label='Times')


x = np.logspace(1, 4, 10)
y = .5 * x**0.5
plt.loglog(x, y, 'g--', label=r'$\mathcal{O}(10^\frac{1}{2})$)')


plt.gca().set(title='SLIC Grid Size Runtime', xlabel='Number of Grid Points',
              ylabel='Runtime [s]')
plt.legend(loc='upper left', fontsize=14)

plt.savefig('SLIC_grid_runtimes.png')<jupyter_output><empty_output><jupyter_text>**Times varying grid size**size 500 used(5,5) 1 loop, best of 3: 12.8 s per loop(10, 10) 1 loop, best of 3: 15.7 s per loop(20, 20) 1 loop, best of 3: 21 s per loop **Times varying image size**grid (10, 10) useds = 50, 1 loop, best of 3: 390 ms per loops = 100, 1 loop, best of 3: 884 ms per loops = 150, 1 loop, best of 3: 1.75 s per loops = 200, 1 loop, best of 3: 2.86 s per loops = 300, 1 loop, best of 3: 5.97 s per loops = 400, 1 loop, best of 3: 10.2 s per loop(without GPU 1 loop, best of 3: 10.4 s per loop)s = 600, 1 loop, best of 3: 22.5 s per loops = 800, 1 loop, best of 3: 39.9 s per loops = 1000, 1 loop, best of 3: 1min 2s per loops = 1200, 1 loop, best of 3: 1min 29s per loops = 1600, 1 loop, best of 3: 2min 37s per loop Runtime with GPU: 10.2 sRuntime without GPU: 10.4s<jupyter_code>times = [390e-3, 884e-3, 1.75, 2.86, 5.97, 10.2, 22.5, 39.9, 60 + 2, 60 + 29, 120 + 37]
sizes = [50, 100, 150, 200, 300, 400, 600, 800, 1000, 1200, 1600]
pixels = np.array(sizes)**2

plt.figure(figsize=[8,8])
plt.loglog(pixels, times, 'bo-', label='Times')

x = np.logspace(4, 7, 10)
y = 1e-5 * x**1
plt.loglog(x, y, 'r--', label=r'$\mathcal{O}(10^1)$)')


plt.gca().set(title='SLIC Image Size Runtime', xlabel='Number of Pixels',
              ylabel='Runtime [s]')
plt.legend(loc='upper left', fontsize=14)

plt.savefig('SLIC_size_runtimes.png')<jupyter_output><empty_output><jupyter_text>SLIC memory analysis<jupyter_code>!pip install memory_profiler
%load_ext memory_profiler
%memit [range(0, i) for i in range(1, 100)]
%%memit

s = 1200 # image size
img = comb_image[:s, :s]
bin_grid = [10, 10]

obj = SLIC(img, bin_grid)
obj.iterate(10)<jupyter_output><empty_output><jupyter_text>s = 50,s = 100,s = 400, peak memory: 811.61 MiB, increment: 0.08 MiBs = 1600, peak memory: 1022.53 MiB, increment: 0.01 MiB  Butterfly  over segmentation<jupyter_code>butterfly_IP = Image_processor(path='images/butterfly.tif')
butterfly_IP.plot()
# blur the image to reduce spotting effect
butterfly_IP.reset()
butterfly_IP.gauss(sigma=3, key='blur')

# create the SLIC object iterate it and plot
butterfly_SLIC = SLIC(butterfly_IP.imgs['blur'], [25, 25], dist_metric_args=[1.])
butterfly_SLIC.iterate(10)
butterfly_SLIC.plot()<jupyter_output><empty_output><jupyter_text>Create Segments and Define Extraction Functions<jupyter_code># extract mask
butterfly_mask = butterfly_SLIC.get_segmentation()

# create segment groups, enforce size and plot
butterfly_segs = segment_group(butterfly_mask)
butterfly_segs.enforce_size(min_size=50)

# plot the segments
butterfly_segs.plot(back_img=butterfly_IP.imgs['original'])
# obtain the edge detected image
butterfly_IP.reset()
butterfly_IP.scharr()
butterfly_IP.grey_scale()
butterfly_IP.threshold(0.05, key='scharr')
butterfly_IP.plot('scharr')
# define edge extraction function
def edge_extraction(Xs, Ys, scharr_img):
    return scharr_img[Ys, Xs].mean() / scharr_img.std()

# define color extracton function
def color_extraction(Xs, Ys, img1):
    """
    Takes in a single image and returns the average at the cordinates in
    each color channel
    """
    avgs1 = img1[Ys, Xs].mean(axis=0)
    return [*avgs1]
# older extraction functions


# extract color and texture features
# titles = ['red_avg', 'green_avg', 'blue_avg', 'lap_avg']
# def extraction(Xs, Ys, img_col, img_grad):
#     """
#     """
#     avgs_col = img_col[Ys, Xs].mean(axis=0)
#     avg_grad = (img_grad[Ys, Xs]**2).mean()
#     return [*avgs_col, avg_grad]


# extract color and std features
# titles = ['red_avg', 'green_avg', 'blue_avg',
#           'red_std', 'green_std', 'blue_std']
# def extraction(Xs, Ys, img_col):
#     """
#     """
#     avgs_col = img_col[Ys, Xs].mean(axis=0)
#     stds_col = img_col[Ys, Xs].std(axis=0)
#     return [*avgs_col, *stds_col*5]


# extract color and gabor features
# titles = ['red_avg', 'green_avg', 'blue_avg',
#           'gabor_avg', 'gabor_std']
# def extraction(Xs, Ys, img_col, img_gab):
#     """
#     """
#     avgs_col = img_col[Ys, Xs].mean(axis=0)
#     avgs_gab = (img_gab[Ys, Xs]**2).mean()
#     stds_gab = img_gab[Ys, Xs].std()
#     return [*avgs_col, avgs_gab*3, stds_gab]<jupyter_output><empty_output><jupyter_text>Merge by Clustering<jupyter_code># extract features
butterfly_feats = butterfly_segs.feature_extraction(color_extraction,
                                                    [butterfly_IP.imgs['blur']])

# inspect features
pd.DataFrame(butterfly_feats, columns=['red_avg', 'green_avg', 'blue_avg']).describe()
# normalise features if need be
# butterfly_feats = butterfly_feats - butterfly_feats.mean(axis=0)
# butterfly_feats = butterfly_feats / butterfly_feats.std(axis=0)

# inspect features
# pd.DataFrame(butterfly_feats, columns=titles).describe()
# do an AGNES clustering with these features
butterly_AGNES = AGNES(butterfly_feats)
butterly_AGNES.iterate()
butterly_AGNES.cluster_distance_plot('all')
# get the clustering and assign it
butterly_clusters = butterly_AGNES.cluster_by_derivative(n_std=2., plot=False)
butterfly_segs.assign_clusters(butterly_clusters)

# plot these clusters
butterfly_segs.plot('cluster_all', back_img=butterfly_IP.imgs['original'])
# implement cluster merging (no edge presence used)
butterfly_segs.merge_by_cluster()
butterfly_segs.plot('merged_edges', back_img=butterfly_IP.imgs['original'])<jupyter_output><empty_output><jupyter_text>Merging by Edge Confidence<jupyter_code># Identify edge confs
butterfly_segs.edge_confidence(edge_extraction, [butterfly_IP.imgs['scharr']])

# plot them
butterfly_segs.plot('edge_conf', back_img=butterfly_IP.imgs['original'])
# merge
butterfly_segs.merge_by_edge(edge_absent=.2)
butterfly_segs.plot('merged_edges', back_img=butterfly_IP.imgs['original'])<jupyter_output><empty_output><jupyter_text>repeate merging process again (if wanted)<jupyter_code># repeat the process for the new segmentation

# feature extraction
butterfly_feats = butterfly_segs.feature_extraction(extraction,
                                                    [butterfly_IP.imgs['blur']])

# clustering
butterly_AGNES = AGNES(butterfly_feats)
butterly_AGNES.iterate()

# get the clustering and assign it
butterly_clusters = butterly_AGNES.cluster_by_derivative(n_std=2., plot=False)
butterfly_segs.assign_clusters(butterly_clusters)
# plot these clusters
butterfly_segs.plot('cluster_all', back_img=butterfly_IP.imgs['original'])
# implement cluster merging (no edge presence used)
butterfly_segs.merge_by_cluster()
butterfly_IP.plot('original')
butterfly_segs.plot('merged_edges', ax=plt.gca())<jupyter_output><empty_output><jupyter_text>Segmentation Analysis<jupyter_code># extract the cluster mask
butterfly_cluster = butterfly_segs.get_cluster_mask()
plt.imshow(butterfly_cluster)

# extract the segments mask
butterfly_mask = butterfly_segs.mask
# butterfly_cluster = butterfly_SA.clusters
butterfly_SA = Segment_Analyser(butterfly_IP.imgs['original'],
                      butterfly_mask,
                      butterfly_cluster)
butterfly_SA.set_labels()
butterfly_SA.plot_clusters()
butterfly_SA.get_composition()
print('\n')
butterfly_SA.get_grain_count()
butterfly_SA.get_gsd('wing')
butterfly_SA.get_span('wing')<jupyter_output><empty_output><jupyter_text>Thin SectionsThis is the sequence used for each TS image, all that was changed was the load data section.  Texture, color cluster comparison  Load Images<jupyter_code>fig, axs = plt.subplots(1, 2, figsize=[20, 10])

# load data
IP_polar = Image_processor(path='images/Indonesia/polar_3.tif')
IP_white = Image_processor(path='images/Indonesia/white_3.tif')

# plot them
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    IP.plot(ax=ax)
    ax.axis('off')

# plt.savefig('images/Results/SS_both.tif')<jupyter_output><empty_output><jupyter_text>over segmentation<jupyter_code>fig, axs = plt.subplots(1, 2, figsize=[20, 10])

# guassian blur for color space
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    IP.reset()
    IP.gauss(sigma=3, key='gauss')
    IP.plot('gauss', ax=ax)
    ax.axis('off')
    
# plt.savefig('image/Results/SS_both_blured.tif')
fig, axs = plt.subplots(1, 2, figsize=[20, 10])
[ax.axis('off') for ax in axs.ravel()]

MSLIC_obj = MSLIC_wrapper([IP_polar.imgs['original'],
                           IP_white.imgs['original']],
                          bin_grid=[25, 25],
                          dist_metric_args=[.5])
MSLIC_obj.iterate(10)
MSLIC_obj.plot(axs=axs.ravel())<jupyter_output><empty_output><jupyter_text>create segment object<jupyter_code># extract the mask
mask = MSLIC_obj.get_segmentation()

# initalise segement object and remove small grains
segment_obj = segment_group(mask)

# plot the segmentations
fig, axs = plt.subplots(1, 2, figsize=[20, 10])
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    segment_obj.plot(option='default', back_img=IP.imgs['original'], ax=ax)
    ax.axis('off')
    
# plt.savefig()
# remove small grains
segment_obj.enforce_size(min_size=50)<jupyter_output><empty_output><jupyter_text>color based clustering<jupyter_code># define extraction function (extract only color features)
titles = ['red_nonpolar', 'green_nonpolar', 'blue_nonpolar',
          'red_polar', 'green_polar', 'blue_polar']
def extraction(Xs, Ys, img1, img2):
    """
    Takes in two images and returns the average at the cordinates in
    each color channel for both images
    """
    avgs1 = img1[Ys, Xs].mean(axis=0)
    avgs2 = img2[Ys, Xs].mean(axis=0)
    return [*avgs1, *avgs2]

# extract features
feats = segment_obj.feature_extraction(extract_func = extraction,
                                       func_vars = [IP_white.imgs['gauss'],
                                                    IP_polar.imgs['gauss']])
pd.DataFrame(feats, columns=titles).describe()
# cluster by AGNES
AGNES_obj = AGNES(feats)
AGNES_obj.iterate()
AGNES_obj.cluster_distance_plot('2nd')
# form the clustering
clustering = AGNES_obj.cluster_by_derivative(n_std=3., plot=False)
segment_obj.assign_clusters(clustering)

# plot all clusters
segment_obj.plot('cluster_all', back_img=IP_white.imgs['original'])

# plt.savefig('Gabon_1_clusterings.tif')<jupyter_output><empty_output><jupyter_text>Texture based clustering<jupyter_code>fig, axs = plt.subplots(1, 2, figsize=[20, 10])
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    IP.reset()
    IP.laplace(key='lap')
    IP.plot(key='lap', ax=ax)
fig, axs = plt.subplots(1, 2, figsize=[20, 10])
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    IP.reset()
    IP.grey_scale()
    IP.laplace(key='lap')
    IP.plot(key='lap', ax=ax)
fig, axs = plt.subplots(1, 2, figsize=[20, 10])
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    IP.reset()
    IP.grey_scale()
    IP.gabor_filters(2, 5, key='gab')
    IP.plot(key='gab', ax=ax)
# define extraction function (extract only color features)
# titles = ['lap_avg_nonpolar', 'lap_std_nonpolar',
#           'lap_avg_polar', 'lap_std_polar']

def extraction(Xs, Ys, img1, img2):
    """
    Takes in two images and returns the average at the cordinates in
    each color channel for both images
    """
    avg1 = img1[Ys, Xs].mean(axis=0)
    avg2 = img2[Ys, Xs].mean(axis=0)
    return [*avg1, *avg2]



# titles = ['red_std_nonpolar', 'green_std_nonpolar', 'blue_std_nonpolar',
#           'red_std_polar', 'green_std_polar', 'blue_std_polar']
def extraction_std(Xs, Ys, img1, img2):
    std1 = img1[Ys, Xs].std(axis=0)
    std2 = img2[Ys, Xs].std(axis=0)
    return [*std1, *std2]


titles= ['gab_avg_nonpolar', 'gab_std_nonpolar',
         'gab_avg_polar', 'gab_std_polar']
def extraction_single(Xs, Ys, img1, img2):
    avg1 = img1[Ys, Xs].mean()
    avg2 = img2[Ys, Xs].mean()
    std1 = img1[Ys, Xs].std()
    std2 = img2[Ys, Xs].std()
    return [avg1, avg2, std1, std2]

# extract features
feats = segment_obj.feature_extraction(extract_func = extraction_single,
                                       func_vars = [IP_white.imgs['lap'],
                                                    IP_polar.imgs['lap']])
pd.DataFrame(feats, columns=titles).describe()
# cluster by AGNES
AGNES_obj = AGNES(feats)
AGNES_obj.iterate()
AGNES_obj.cluster_distance_plot('2nd')
# form the clustering
clustering = AGNES_obj.cluster_by_derivative(n_std=3., plot=False)
segment_obj.assign_clusters(clustering)

# plot all clusters
segment_obj.plot('cluster_all', back_img=IP_white.imgs['original'])

# plt.savefig('Gabon_1_clusterings.tif')<jupyter_output><empty_output><jupyter_text>Porosity determination  Load Images<jupyter_code>fig, axs = plt.subplots(1, 2, figsize=[20, 10])

# load data
IP_polar = Image_processor(path='images/Thailand/polar_1.tif')
IP_white = Image_processor(path='images/Thailand/white_1.tif')

# plot them
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    IP.plot(ax=ax)
    ax.axis('off')

plt.savefig('proc_1.png')<jupyter_output><empty_output><jupyter_text>over segmentation<jupyter_code>fig, axs = plt.subplots(1, 2, figsize=[20, 10])

# guassian blur for color space
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    IP.reset()
    IP.gauss(sigma=3, key='gauss')
    IP.plot('gauss', ax=ax)
    ax.axis('off')
    
plt.savefig('proc_2.png')
fig, axs = plt.subplots(1, 2, figsize=[20, 10])
[ax.axis('off') for ax in axs.ravel()]

MSLIC_obj = MSLIC_wrapper([IP_polar.imgs['gauss'],
                           IP_white.imgs['gauss']],
                          bin_grid=[25, 25],
                          dist_metric_args=[.5])
MSLIC_obj.iterate(10)
MSLIC_obj.plot(axs=axs.ravel())

plt.savefig('proc_3.png')<jupyter_output><empty_output><jupyter_text>segment clustering<jupyter_code># extract the mask
mask = MSLIC_obj.get_segmentation()

# initalise segement object and remove small grains
segment_obj = segment_group(mask)

# plot the segmentations
fig, axs = plt.subplots(1, 2, figsize=[20, 10])
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    segment_obj.plot(option='default', back_img=IP.imgs['original'], ax=ax)
    ax.axis('off')
    
plt.savefig('proc_4.png')
# remove small grains
segment_obj.enforce_size(min_size=50)
# define extraction function (extract only color features)
titles = ['red_nonpolar', 'green_nonpolar', 'blue_nonpolar',
          'red_polar', 'green_polar', 'blue_polar']
def extraction(Xs, Ys, img1, img2):
    """
    Takes in two images and returns the average at the cordinates in
    each color channel for both images
    """
    avgs1 = img1[Ys, Xs].mean(axis=0)
    avgs2 = img2[Ys, Xs].mean(axis=0)
    return [*avgs1, *avgs2]

# extract features
feats = segment_obj.feature_extraction(extract_func = extraction,
                                       func_vars = [IP_white.imgs['gauss'],
                                                    IP_polar.imgs['gauss']])
pd.DataFrame(feats, columns=titles).describe()
# cluster by AGNES
AGNES_obj = AGNES(feats)
AGNES_obj.iterate()
AGNES_obj.cluster_distance_plot('2nd')
# form the clustering
clustering = AGNES_obj.cluster_by_derivative(n_std=3., plot=False)
segment_obj.assign_clusters(clustering)

# plot all clusters
segment_obj.plot('cluster_all', back_img=IP_white.imgs['original'])

plt.savefig('proc_5.png')<jupyter_output><empty_output><jupyter_text>edge detection<jupyter_code># edge detected image
fig, axs = plt.subplots(1, 2, figsize=[20, 10])
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    IP.reset()
    IP.grey_scale()
    IP.canny()
    IP.gauss_grey(sigma=3)
    IP.threshold(.15, 'edges')
    IP.plot('edges', ax=ax)
    ax.axis('off')
    
plt.savefig('proc_6.png')
# define the edge confidence funciton
def edge_conf(Xs, Ys, img1, img2):
    return img1[Ys, Xs].mean() + img2[Ys, Xs].mean()

# calculate edge condifences
segment_obj.edge_confidence(edge_conf, [IP_polar.imgs['edges'],
                                        IP_white.imgs['edges']])

# plot them
segment_obj.plot('edge_conf', back_img=IP_white.imgs['original'])

plt.savefig('proc_7.png')<jupyter_output><empty_output><jupyter_text>Segment Merging<jupyter_code># merge connected segments
segment_obj.merge_by_cluster(edge_present=1.)
# segment_obj.merge_by_edge(edge_absent=.25)

# plot the results
fig, axs = plt.subplots(1, 2, figsize=[20, 10])
for IP, ax in zip([IP_white, IP_polar], axs.ravel()):
    segment_obj.plot('merged_edges', back_img=IP.imgs['original'], ax=ax)
    ax.axis('off')
#     ax.set(title='Post Cluster Merging')
    
plt.savefig('proc_8.png')<jupyter_output><empty_output><jupyter_text>segment analysis<jupyter_code># extract mask and clustering
seg_mask = segment_obj.mask
clust_mask = segment_obj.get_cluster_mask()

# tx2_clusters = SA_obj.clusters # keep already clusters labels
SA_obj = Segment_Analyser(IP_white.imgs['original'],
                          seg_mask,
                          clust_mask)
SA_obj.set_labels()
SA_obj.plot_clusters()

plt.savefig('proc_9.png')

SA_obj.get_composition()
SA_obj.get_gsd('rock')
plt.savefig('proc_10.png')<jupyter_output><empty_output><jupyter_text>Save Segementations<jupyter_code># save this mask
# np.savetxt('masks/TX2_MSLIC_25_25.tif', MSLIC_obj.get_segmentation())
# save the masks
# np.savetxt('/content/masks/both_30_30_merged_6_channel_color.txt', segments.mask)
# np.save('/content/masks/both_30_30_merged_6_channel_color_groupings.npy', segments.seg_clusters)<jupyter_output><empty_output><jupyter_text>Save Results Indonesia 1Tabel of Compositions0.47 %	 silt66.52 %	 rock33.01 %	 poreIndonesia 2Tabel of Compositions63.77 %	 rock25.94 %	 pore10.3 %	 siltIndonesia 3Tabel of Compositions26.29 %	 pore14.83 %	 silt58.88 %	 rockGabon 1Tabel of Compositions11.92 %	 pore39.76 %	 silt32.54 %	 rock15.78 %	 silt/poreGabon 2Tabel of Compositions48.52 %	 silt13.91 %	 silt/pore37.57 %	 rockGabon 3Tabel of Compositions55.89 %	 silt30.03 %	 rock14.08 %	 poreNorway 1Tabel of Compositions13.16 %	 pore12.26 %	 silt74.58 %	 rockNorway 2Tabel of Compositions14.23 %	 pore29.21 %	 silt39.97 %	 rock16.59 %	 silt/poreNorway 3Tabel of Compositions44.19 %	 rock15.03 %	 pore30.77 %	 siltTexas 2Tabel of Compositions19.36 %	 pore80.64 %	 rockTexas 3Tabel of Compositions13.84 %	 pore9.57 %	 silt63.67 %	 rock12.91 %	 silt/poreTexas 4Tabel of Compositions14.96 %	 pore29.0 %	 silt56.05 %	 rockThailand 1Tabel of Compositions15.54 %	 pore72.69 %	 rock11.77 %	 siltThailand 2Tabel of Compositions65.8 %	 rock17.27 %	 silt16.93 %	 poreThailand 3Tabel of Compositions26.05 %	 pore66.76 %	 rock7.20 %	 siltCalifornia 1Tabel of Compositions28.54 %	 silt4.97 %	 pore66.49 %	 rockCalifornia 2Tabel of Compositions11.98 %	 silt75.48 %	 rock12.54 %	 poreCalifornia 3Tabel of Compositions15.19 %	 pore71.5 %	 rock13.31 %	 silt<jupyter_code>titles = ['Indonesia', 'Gabon   ', 'Norway   ', 'Texas   ', 'Thailand', 'California']
results = np.array([[33.01, 25.94, 26.29],
                    [11.92, 13.91, 14.08],
                    [13.16, 14.23, 15.03],
                    [19.36, 13.84, 14.96],
                    [15.54, 16.93, 26.05],
                    [4.97, 12.54, 15.19]])

actual = np.array([22.0, 20.7, 21.9, 21.0, 19.6, 15.1])

print('Region \t\t Avg \t\t Std \t\t Actual \t Diff')
print('-'*72)
for l, r, a in zip(titles, results, actual):
    avg = round(r.mean(),2)
    std = round(r.std(),2)
    print(l, '\t', avg, '\t\t', std, '\t\t', a, '\t\t', round((avg-a),2))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15347571189279732, 'fraction_numerical': 0.04121799473558267, 'mean_word_length': 4.904803956199223, 'pattern_counts': {'":': 0, '<': 143, '<?xml version=': 0, '>': 143, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '35', 'dir': 'jupyter-structured-clean-dedup', 'id': '538207', 'license': 'MIT', 'n_tokens_mistral': 14021, 'n_tokens_neox': 12726, 'n_words': 2605, 'path': 'notebooks/Results_Collection.ipynb', 'repo_name': 'Boyne272/Thin_Section_Analsis', 'size': '33456'}