summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2020-07-21 16:32:05 +0200
committerPaweł Redman <pawel.redman@gmail.com>2020-07-21 16:32:05 +0200
commit663122dabf0498ae9522b0f53939e21ab64d052b (patch)
tree254b1d6964c710c7be3624040452c2ed5b902efc
parentc5c6b4f2f2640319a51924349055678e2b924514 (diff)
Implement a proper progress bar and better messages.
-rw-r--r--demo.py5
-rw-r--r--otsu2018.py31
2 files changed, 28 insertions, 8 deletions
diff --git a/demo.py b/demo.py
index 86b1c61..10e6251 100644
--- a/demo.py
+++ b/demo.py
@@ -1,5 +1,6 @@
import os
import matplotlib.pyplot as plt
+
from colour import (
SpectralShape, COLOURCHECKER_SDS, ILLUMINANT_SDS, sd_to_XYZ)
@@ -8,7 +9,7 @@ from otsu2018 import load_Otsu2018_spectra, Clustering
if __name__ == '__main__':
print('Loading spectral data...')
- sds = load_Otsu2018_spectra('CommonData/spectrum_m.csv', every_nth=50)
+ sds = load_Otsu2018_spectra('CommonData/spectrum_m.csv', every_nth=1)
shape = SpectralShape(380, 730, 10)
print('Initializing the clustering...')
@@ -16,7 +17,7 @@ if __name__ == '__main__':
print('Clustering...')
before = clustering.root.total_reconstruction_error()
- clustering.optimise(2)
+ clustering.optimise(8)
after = clustering.root.total_reconstruction_error()
print('Error before: %g' % before)
diff --git a/otsu2018.py b/otsu2018.py
index 7d3c1eb..e188950 100644
--- a/otsu2018.py
+++ b/otsu2018.py
@@ -1,5 +1,7 @@
import numpy as np
import matplotlib.pyplot as plt
+import tqdm
+import time
from colour import (SpectralDistribution, STANDARD_OBSERVER_CMFS,
ILLUMINANT_SDS, sd_to_XYZ, XYZ_to_xy)
@@ -55,7 +57,7 @@ class PartitionAxis:
self.direction = direction
def __str__(self):
- return '%s = %g' % ('yx'[self.direction], self.origin)
+ return '%s=%s' % ('yx'[self.direction], repr(self.origin))
class Colours:
@@ -351,9 +353,11 @@ class Node:
"""
if self.best_partition is not None:
+ print('%s: already optimised' % self)
return self.best_partition
best_error = None
+ bar = tqdm.trange(2 * len(self.colours), leave=False)
for direction in [0, 1]:
for i in range(len(self.colours)):
@@ -369,7 +373,9 @@ class Node:
self.best_partition = (error, axis, partition)
delta = error - self.reconstruction_error()
- print('%10s %3d %10s %g' % (self, i, axis, delta))
+ bar.update()
+
+ bar.close()
if self.best_partition is None:
raise ClusteringError('no partitions are possible')
@@ -511,15 +517,25 @@ class Clustering:
Maximum number of splits. If the dataset is too small, this number
might not be reached.
"""
+
+ t0 = time.time()
+ def elapsed():
+ delta = time.time() - t0
+ return '%dm%.3fs' % (delta // 60, delta % 60)
+
for repeat in range(repeats):
+ print('=== Iteration %d of %d ===' % (repeat + 1, repeats))
+
best_total_error = None
total_error = self.root.total_reconstruction_error()
- for leaf in self.root.leaves:
+ for i, leaf in enumerate(self.root.leaves):
+ print('(%s) Optimising %s...' % (elapsed(), leaf))
+
try:
error, axis, partition = leaf.find_best_partition()
except ClusteringError:
- print('%s has no partitions' % leaf)
+ print('...no partitions are possible.')
continue
new_total_error = (total_error - leaf.reconstruction_error()
@@ -532,12 +548,15 @@ class Clustering:
best_partition = partition
if best_total_error is None:
- print('WARNING: only %d splits were possible' % repeat)
+ print('WARNING: only %d splits were possible.' % repeat)
break
- print('==== Splitting %s along %s ====' % (best_leaf, best_axis))
+ print('\nSplit %s into %s and %s along %s.\n'
+ % (best_leaf, *best_partition, best_axis))
best_leaf.split(best_partition, best_axis)
+ print('Finished in %s.' % elapsed())
+
def write_python_dataset(self, path):
"""
Write the clustering into a Python dataset compatible with Colour's