<jupyter_start><jupyter_text>Simple Evolutionary Exploration  WalkthroughThis notebook contains instructions on how to use the SEE module, along with several examples. These instructions will cover the following parts: * [Import Image Files](Import_Image_Files)* [Manual Search](Manual_Search)* [Genetic Algorithm Search](Genetic_Algorithm_Search)* [Reading the Results](Reading_the_Results) ---- Import Image Files First import the following packages:<jupyter_code>import random
random.seed(2)
%matplotlib inline
import matplotlib.pylab as plt
import imageio

from see import JupyterGUI
data = JupyterGUI.pickimage()<jupyter_output><empty_output><jupyter_text>Create Workflow<jupyter_code>from see import base_classes 
from see.Segmentors import segmentor
from see.ColorSpace import colorspace
from see.Workflow import workflow
from see.Segment_Fitness import segment_fitness

workflow.addalgos([colorspace, segmentor, segment_fitness])
wf = workflow()
print(wf)
from see.base_classes import pipedata
individual = segmentor()
d = pipedata()
d.img = data.img
d.gmask = data.gmask
individual.runAlgo(d)<jupyter_output><empty_output><jupyter_text>---- Genetic Algorithm Search First import image files, as well as the following packages:<jupyter_code>from see import GeneticSearch<jupyter_output><empty_output><jupyter_text>To run the genetic algorithm, we need to initialize an instance of an evolver. The original image and ground truth segmentation image are inputs to it, along with an integer value for population size. This value sets how many indivudals are in our population. For this example, we'll set this number to be equal to 10.<jupyter_code>mydata = base_classes.pipedata()
mydata.img = data.img
mydata.gmask = data.gmask
my_evolver = GeneticSearch.Evolver(workflow, mydata, pop_size=10)<jupyter_output><empty_output><jupyter_text>Now that the evolver has been initialized, we can run the genetic algorithm for a specified number of generations (or iterations). Here we will set this number equal to 5.<jupyter_code># warnings may appear when this runs
population = my_evolver.run(ngen=2)<jupyter_output><empty_output><jupyter_text>---- Reading the Results After the genetic algorithm is complete, we can retrieve the individuals that resulted in the lowest (best) fitness values by printing `my_evolver.hof`. These individuals are sorted according to fitness value, so to get the overal best individual, we can simply look at the first individual in the list.<jupyter_code>params = my_evolver.hof[0]

print('Best Individual:\n', params)<jupyter_output><empty_output><jupyter_text>We can see the mask this individual generates by evaluating it, then plotting the result:<jupyter_code>seg = workflow(params)
data = seg.pipe(data)

plt.figure(figsize=(20, 10))
plt.subplot(121)
plt.imshow(data.inputimage)
plt.title("Original Image")
plt.axis('off')

plt.subplot(122)
plt.imshow(data.mask)
plt.title("Segmentation")
plt.axis('off')

plt.tight_layout
plt.show()<jupyter_output><empty_output><jupyter_text>We can also use `FitnessFunction` to calculate the final fitness value for this algorithm:<jupyter_code>print('Fitness Value: ', segment_fitness().evaluate(data.mask, data.gmask)[0])<jupyter_output><empty_output><jupyter_text>If this value is satisfactory, we can then get usable code to run this algorithm anywhere, including outside this notebook. The `print_best_algorithm_code` function does this using the given individual:<jupyter_code>dir(base_classes)
ex = base_classes.print_best_algorithm_code(my_evolver.hof[0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08354359405663023, 'fraction_numerical': 0.005606952621250351, 'mean_word_length': 7.146118721461187, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '60720', 'license': 'MIT', 'n_tokens_mistral': 1101, 'n_tokens_neox': 1046, 'n_words': 347, 'path': 'Pipe_Example.ipynb', 'repo_name': 'dottydot7/see-segment', 'size': '3567'}