summaryrefslogtreecommitdiff
path: root/clustering.py
blob: d8b1ed9da52166b852d03464eef563033c7d459e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import matplotlib.pyplot as plt
import tqdm

from colour import SpectralShape, COLOURCHECKER_SDS, sd_to_XYZ

from otsu2018 import load_Otsu2018_spectra, Otsu2018Tree


if __name__ == '__main__':
    print('Loading spectral data...')
    sds = load_Otsu2018_spectra('CommonData/spectrum_m.csv', every_nth=100)
    shape = SpectralShape(380, 730, 10)

    print('Initializing the tree...')
    tree = Otsu2018Tree(sds, shape)

    print('Clustering...')
    before = tree.total_reconstruction_error()
    tree.optimise(progress_bar=tqdm.tqdm)
    after = tree.total_reconstruction_error()

    print('Error before: %g' % before)
    print('Error after:  %g' % after)

    print('Saving the dataset...')    
    if not os.path.exists('datasets'):
        os.makedirs('datasets')
    data = tree.to_dataset()
    data.to_file('datasets/otsu2018.npz')
    data.to_Python_file('datasets/otsu2018.py')

    print('Plotting...')
    tree.visualise()

    plt.figure()

    examples = COLOURCHECKER_SDS['ColorChecker N Ohta'].items()
    for i, (name, sd) in enumerate(examples):
        plt.subplot(2, 3, 1 + i)
        plt.title(name)

        plt.plot(sd.wavelengths, sd.values, label='Original')

        XYZ = sd_to_XYZ(sd) / 100
        recovered_sd = tree.reconstruct(XYZ)
        plt.plot(recovered_sd.wavelengths, recovered_sd.values,
                 label='Recovered')

        plt.legend()

        if i + 1 == 6:
            break

    plt.show()