-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.py
1480 lines (1316 loc) · 67.1 KB
/
test.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Produces a set of test plots using matplotlib, with just random data.
Each plot is tagged with appropriate metadata, and an ImageDict produced
which describes them and creates a web page.
.. moduleauthor:: Melissa Brooks https://github.com/melissaebrooks
(C) Crown copyright Met Office. All rights reserved.
Released under BSD 3-Clause License. See LICENSE for more details.
'''
# standard python modules:
import os
import shutil
import sys
import errno
import argparse
import copy
import random
import platform
import math
import pdb
from multiprocessing import Pool
from datetime import datetime
# common python modules:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# for timings:
DATE_START = datetime.now()
# ensure batch plotting works:
matplotlib.use('Agg')
# python2/3 specific modules:
PY3 = sys.version_info[0] == 3
if PY3:
import pickle
else:
import cPickle as pickle
# import ImageMetaTag, the module this routine is testing, so that the
# test is using the version of ImageMetaTag associated with the test script:
# (this would normally be added already, by installation, but for testing, we
# need to be testing the version we are making changes too!)
TEST_PATH = os.sep.join(os.path.abspath(sys.argv[0]).split(os.sep)[0:-1])
sys.path.insert(0, TEST_PATH)
# and the location of the dir for test rousoruces (stock problem images etc.)
TEST_RESOURCES = os.path.join(TEST_PATH, 'test_resources')
# Now, import ImageMetaTag to do things:
import ImageMetaTag as imt
LOGO_FILE = os.path.join(TEST_PATH, 'logo.png')
LOGO_SIZE = 60
LOGO_PADDING = 5
# set the detailed format to print to stdout:
DATE_FORMAT = "%Y-%m-%dT%H:%M:%S"
# and a more friendly format for the output webpages:
DATE_FORMAT_WWW = "%Y-%m-%d"
# before we do anything, report where ImageMetaTag is from:
print('ImageMetaTag from "{}"'.format(imt.__path__[0]))
def get_webdir(minimal=False):
'''
Works out the location to use as webdir in this test run. This is done in
a function only because it can be imported into simplest_img_dict.py to
keep it consistent. In a real application you can just specify a location
on your file system that is web-accessible.
'''
home = os.getenv('HOME')
webdir = None
if minimal:
imt_subdir = os.path.join('ImageMetaTagTest', 'minimal')
else:
imt_subdir = 'ImageMetaTagTest'
dirs_to_check = ['%s/public_html' % home, '%s/Public' % home]
for check_dir in dirs_to_check:
if os.path.isdir(check_dir):
webdir = os.path.join(check_dir, imt_subdir)
break
if not webdir:
msg = 'Cannot find appropriate web dir from: {}'
raise ValueError(msg.format(dirs_to_check))
# make it if it doesn't exist:
if not os.path.isdir(webdir):
mkdir_p(webdir)
return webdir
def define_imt_db(wipe=False, minimal=False):
'Defines the main database file to use to the image metadate:'
imt_db = '{}/imt.db'.format(get_webdir(minimal=minimal))
if wipe and os.path.isfile(imt_db):
os.remove(imt_db)
return imt_db
def get_user_and_email():
'''
guesses the users email address from /etc/aliases.
This is an example of the sort of traceability information would be
a very common tag to add to images.
'''
username = os.getenv('USER')
user_email = None
alias_file = '/etc/aliases'
if os.path.isfile(alias_file):
with open(alias_file, 'r') as open_file:
for line in open_file:
if line.startswith(username):
user_email = line.split()[-1]
break
user_and_email = 'user: %s' % username
if user_email:
user_and_email += ', email: %s' % user_email
return user_and_email
def make_random_data(n_random_data, seed=3):
'''
generates some data sets of random data, simulating rolling 2 D6 a number
of times. In order to produce predictable random data for testing, the
random seed needs to be initialised.
By default the seed is 3:
"Then, shalt thou count to three. No more. No less."
'''
random_data = []
# simulate rolling 2 6 sided dice:
np.random.seed(seed)
for i_rand in range(n_random_data):
# make some actual random data:
if i_rand < 6:
n_rolls = 6 ** (i_rand + 1)
else:
n_rolls = 6 ** (6) + 2 ** (i_rand + 1)
random_data.append(np.random.randint(1, 6, n_rolls) +
np.random.randint(1, 6, n_rolls))
return random_data
def plot_random_data(random_data, i_rand, plot_col, col_name, trims, borders,
compression_levels, dpis, img_savedir, img_format, plot_owner,
imt_db):
'plots a set of random data'
# to tag the images with the routine that created it:
this_routine = 'ImageMetaTag module: lib/ImageMetaTag/test.py'
imt_verbose = True
images_and_tags = {}
db_timeout = 5
n_rolls = 6 ** (i_rand + 1)
plt.plot(random_data[i_rand], color=plot_col, linestyle=':', marker='x')
plt.ylim([1, 12])
if plot_col == 'r':
plt.title(('Sum of two random integers between 1 and 6, with '
'an extremely long title.\n'))
else:
plt.title('Sum of two random integers between 1 and 6\n')
# make the subdirectories:
mkdir_p('{}/rolls'.format(img_savedir))
mkdir_p('{}/dists'.format(img_savedir))
data_source = 'Some random data'
img_count = 0
# save the figure, using different image-meta-tag options
# and tag the images.
for trim in trims:
if trim:
these_borders = borders
else:
these_borders = [0]
for border in these_borders:
for compression in compression_levels:
for dpi in dpis:
outfile = '%s/rolls/imt_%s_%s_compression_%s' \
% (img_savedir, n_rolls, plot_col, compression)
outfile = 'imt_{}_{}_comp{}_{}dpi'.format(n_rolls,
plot_col,
compression,
dpi)
outfile = os.path.join(img_savedir, 'rolls', outfile)
if trim:
outfile += '_trim_b%s' % border
trim_str = 'Image trimmed'
else:
trim_str = 'Image untrimmed'
outfile += '.{}'.format(img_format)
# image tags for the web page:
img_tags = {'number of rolls': '{} simulated rolls'.format(n_rolls),
'plot type': 'Line plots',
'image compression': 'Compression option {}'.format(compression),
'image trim': trim_str,
'border': '{} pixels'.format(border),
'plot color': col_name,
'expected dpi': '{} dpi'.format(dpi)}
# and other, more general tags showing the sort of
# thing that might be useful:
img_tags['data source'] = data_source
img_tags['plot owner'] = plot_owner
img_tags['plot created by'] = this_routine
img_tags['ImageMetaTag version'] = imt.__version__
if img_count > 1 and img_count < 10:
# only add this one after the database exists,
# to test expandning the database:
img_tags['SQL-char-name:in_tag'] = 'testing SQL chars'
# now save the file with imt.savefig
# (deleting any pre-existing file first):
if os.path.isfile(outfile):
os.remove(outfile)
imt.savefig(outfile, do_trim=trim, trim_border=border,
do_thumb=True, img_converter=compression,
img_tags=img_tags, keep_open=True,
verbose=imt_verbose,
db_file=imt_db, db_timeout=db_timeout,
db_add_strict=False,
dpi=dpi,
logo_file=LOGO_FILE, logo_width=LOGO_SIZE,
logo_padding=LOGO_PADDING, logo_pos=0)
# now store those tags
images_and_tags[outfile] = img_tags
# and check they are the same as those that come from reading
# the image metatadata from disk:
check_img_tags(outfile, img_tags)
img_count += 1
plt.close()
outfile = '%s/dist_%s_%s.%s' % (img_savedir, n_rolls, plot_col, img_format)
if PY3:
_count, _bins, _ignored = plt.hist(random_data[i_rand],
[x + 0.5 for x in range(13)],
color=plot_col, density=True)
else:
_count, _bins, _ignored = plt.hist(random_data[i_rand],
[x + 0.5 for x in range(13)],
color=plot_col, normed=True)
plt.xlim([1, 13])
plt.title('Distribution of %s random integers between 1 and 6\n' % n_rolls)
for trim in trims:
if trim:
these_borders = borders
else:
these_borders = [0]
for border in these_borders:
for compression in compression_levels:
for dpi in dpis:
outfile = 'imt_{}_{}_comp{}_{}dpi'.format(n_rolls,
plot_col,
compression,
dpi)
outfile = os.path.join(img_savedir, 'dists', outfile)
if trim:
outfile += '_trim_b%s' % border
trim_str = 'Image trimmed'
else:
trim_str = 'Image untrimmed'
outfile += '.{}'.format(img_format)
# tags to drive web page:
img_tags = {'number of rolls': '{} simulated rolls'.format(n_rolls),
'plot type': 'Histogram',
'image compression': 'Compression option {}'.format(compression),
'image trim': trim_str,
'border': '{} pixels'.format(border),
'plot color': col_name,
'expected dpi': '{} dpi'.format(dpi)}
# again, more general tags:
img_tags['data source'] = data_source
img_tags['plot owner'] = plot_owner
img_tags['plot created by'] = this_routine
img_tags['ImageMetaTag version'] = imt.__version__
img_tags['SQL-char-name:in_tag'] = 'testing SQL chars'
# now save the file with imt.savefig
# (deleting any pre-existing file first):
if os.path.isfile(outfile):
os.remove(outfile)
imt.savefig(outfile, do_trim=trim, trim_border=border,
do_thumb=True, img_converter=compression,
img_tags=img_tags, keep_open=True,
verbose=imt_verbose,
db_file=imt_db, db_timeout=db_timeout,
db_add_strict=False,
dpi=dpi,
logo_file=[LOGO_FILE, LOGO_FILE],
logo_height=LOGO_SIZE//2,
logo_padding=LOGO_PADDING, logo_pos=[1, 1])
# log tags:
images_and_tags[outfile] = img_tags
# and check:
check_img_tags(outfile, img_tags)
img_count += 1
plt.close()
# NOTE: in actual usage, it's easier to refer to the database when you
# need to get image metadata. In this test script we need to test the
# integrity of the database, so we need to pass back the images
# and tags we expect:
return images_and_tags
def mkdir_p(path):
"""
Routine to mimic mkdir -p behaviour
Note: os.makedirs throws up error if directory already exists
"""
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST:
pass
else:
raise
def print_simple_timer(dt_start, dt_end, label):
'prints out a simple timer message'
msg = '{}: started at {}, completed at {}, taking {}'
print(msg.format(label,
dt_start.strftime(DATE_FORMAT),
dt_end.strftime(DATE_FORMAT),
(dt_end - dt_start)))
def check_img_tags(img_file, in_tags):
'''
raises an error if the tags read from a file do not
match those written to it, or are unreadable
'''
(read_ok, read_tags) = imt.readmeta_from_image(img_file)
if not read_ok:
raise ValueError('Unable to read image tags for file "%s"' % img_file)
# now check the tags themselves:
if not set(in_tags).issubset(set(read_tags)):
msg = 'Image metadata tags read from file "%s"' % img_file
msg += 'do not match the expected tags.\n'
msg += ' Input tags:\n'
for key, val in list(in_tags.items()):
msg += ' "%s" : "%s"\n' % (key, val)
msg += ' Tags read from file:\n'
for key, val in list(read_tags.items()):
msg += ' "%s" : "%s"\n' % (key, val)
raise ValueError(msg)
def define_img_dict_in_tuple(in_tuple):
'''
Defines a simple ImageDict from a dictionary of images and metadata.
The input is a tuple so it can be parallelised.
'''
sub_dict = in_tuple[0]
tag_order = in_tuple[1]
skip_key_relist = in_tuple[2]
selector_animated = in_tuple[3]
animation_direction = in_tuple[4]
img_dict = None
# now assemble the ImageDict:
# This is the simple way, but it is possible to parallelise this step,
# which I might include below:
for img_file, img_info in sub_dict.items():
tmp_dict = imt.dict_heirachy_from_list(img_info, img_file, tag_order)
if not tmp_dict:
raise ValueError('Input dict does not contain the required keys')
if not img_dict:
img_dict = imt.ImageDict(tmp_dict,
selector_animated=selector_animated,
animation_direction=animation_direction)
else:
# the skip_key_relist option is useful to set to True for Large
# dictionaries. It stops the key lists being regenerated each time
# something is appended.
img_dict.append(imt.ImageDict(tmp_dict),
skip_key_relist=skip_key_relist)
return img_dict
def make_test_css(webdir):
'writes out a test.css file in a specified directory'
css_file = os.path.join(webdir, 'test.css')
css_content = '''body {
background-color: #d3eeed;
color: black;
}
body, div, dl, dt, dd, li, h1, h2 {
margin: 0;
padding: 0;
}
h3, h4, h5, h6, pre, form, fieldset, input {
margin: 0;
padding: 0;
}
textarea, p, blockquote, th, td {
margin: 0;
padding: 0;
}
fieldset, img {
border: 0 none;
}
body {
font: 12px Myriad,Helvetica,Tahoma,Arial,clean,sans-serif;
*font-size: 75%;
}
h1 {
font-size: 1.5em;
font-weight: normal;
line-height: 1em;
margin-top: 1em;
margin-bottom:0;
}
h2 {
font-size: 1.1667em;
font-weight: bold;
line-height: 1.286em;
margin-top: 1.929em;
margin-bottom:0.643em;
}
h3, h4, h5, h6 {
font-size: 1em;
font-weight: bold;
line-height: 1.5em;
margin-top: 1.5em;
margin-bottom: 0;
}
p {
font-size: 1em;
margin-top: 1.5em;
margin-bottom: 1.5em;
line-height: 1.5em;
}
pre, code {
font-size:115%;
*font-size:100%;
font-family: Courier, "Courier New";
background-color: #efefef;
border: 1px solid #ccc;
}
pre {
border-width: 1px 0;
padding: 1.5em;
}
table {
font-size:100%;
}
'''
with open(css_file, 'w') as file_obj:
file_obj.write(css_content)
return css_file
def test_key_sorting():
'''
Tests that the different variants of sorting in an ImageDict work
are working correctly. These are not all used in the type of web pages
used in the main tests so they are tested directly.
'''
# for each type of test, define the input keys and how they should be when
# they are sorted:
sort_tests = {}
# a straight alphabetical sort - the tuple here gives the
# (input, expected output) for the type of sort:
sort_tests['sort'] = (['aaa', 'zaa', 'aba', '257', 'bob'],
['257', 'aaa', 'aba', 'bob', 'zaa'])
# a reversed alphabetical sort:
sort_tests['reverse sort'] = (['aaa', 'zaa', 'aba', '257', 'bob'],
['zaa', 'bob', 'aba', 'aaa', '257'])
# T+ - used for forecast lead times, so sort by the value after the T+
# and anything with None gets alphabetically sorted at the end
sort_tests['T+'] = (['T+0', 'T+1', 't+10', 'T+2', 't-3',
'T+None', 't-None', 'None'],
['t-3', 'T+0', 'T+1', 'T+2', 't+10',
'None', 'T+None', 't-None'])
# here, we will use a list to sort by, and anything not in the list
# gets a simple sort:
sort_by_list = ['aaa', 'zaa']
sort_tests['sort_by_list'] = (['aaa', 'zaa', 'aba', '257', 'bob'],
['aaa', 'zaa', '257', 'aba', 'bob'])
# a set of numeric values common in meteorology:
sort_tests['numeric'] = (['10m', '50m', '4mm', '62 hPa', '2m', '16 km',
'Model level 7', 'Surface', '12mb', '341.434646',
'Eastern England',
'3.344E, 16.7N', '2.344E, 18.7N', '16.0 nm'],
['Surface', '16.0 nm', '4mm', '2m', '10m', '50m',
'16 km', '62 hPa', '12mb', 'Model level 7',
'2.344E, 18.7N', '3.344E, 16.7N', '341.434646',
'Eastern England',])
# these get used/modified while prepping the sort:
sort_tests_copy = copy.deepcopy(sort_tests)
test_order = sorted(sort_tests.keys())
n_sorts_to_test = len(test_order)
# make a dummy ImageDict
dummy_tag_order = []
dummy_tag_info = {}
for level in range(n_sorts_to_test):
lname = 'l{}'.format(level)
dummy_tag_order.append(lname)
dummy_tag_info[lname] = str(level)
tmp_dict = imt.dict_heirachy_from_list(dummy_tag_info,
'None', dummy_tag_order)
img_dict = imt.ImageDict(tmp_dict)
# now overide the keys of imd_dict with the keys we want to test,
# and make the list of how each level is to be sorted:
sort_methods = []
for level, test_name in enumerate(test_order):
img_dict.keys[level] = sort_tests[test_name][0]
if test_name == 'sort_by_list':
sort_methods.append(sort_by_list)
else:
sort_methods.append(test_name)
# now do the sort:
img_dict.sort_keys(sort_methods)
# now go through the sort methods and check the output is as expected:
failed = False
for level, test_name in enumerate(test_order):
if img_dict.keys[level] == sort_tests[test_name][1]:
pass
else:
msg = ('Sort method test "{}" has failed to correctly'
'sort its ImageDict keys:\n'
' Input list: {}\n'
' Sorted list: {}\n'
' Expected list: {}\n')
print(msg.format(test_name,
sort_tests_copy[test_name][0],
img_dict.keys[level],
sort_tests[test_name][1]))
failed = True
return not failed
def test_compare_img_tags(img_tags1, name1, img_tags2, name2):
'''
Tests a set of images and metadata tags.
* img_tags1 - dict of images, and their metadata tags
* name1 - name of img_tags1 for error reporting only
* img_tags2 - second dict of images, and their metadata tags
* name2 - name of img_tags2 for error reporting only
This is not a simple '==' test, as the database will have None when the
item is missing for fields that are defined for some images, but not
all, images.
'''
# firstly check the keys:
imgs1 = sorted(img_tags1.keys())
imgs2 = sorted(img_tags2.keys())
if imgs1 != imgs2:
in1_not2 = set(imgs1).difference(set(imgs2))
in2_not1 = set(imgs2).difference(set(imgs1))
msg = ('image names in img_tags1 differ to image names in img_tags2.\n'
' images in {0}(1), but not {1}(2): {2}\n'
' images in {1}(2), but not {0}(1): {3}')
raise ValueError(msg.format(name1, name2, in1_not2, in2_not1))
for img in imgs1:
if img_tags1[img] != img_tags2[img]:
# test that all of the keys for this img that are shared,
# are equal:
i_t_keys = set(img_tags1[img].keys())
d_t_keys = set(img_tags2[img].keys())
common_keys = i_t_keys.intersection(d_t_keys)
for key in common_keys:
if img_tags1[img][key] != img_tags2[img][key]:
msg = ('tags in "{}" for file "{}", tag "{}" ("{}") '
'different to that in "{}" ("{}")')
raise ValueError(msg.format(name1, img, key,
img_tags1[img][key],
name2, img_tags2[img][key]))
# in the current tests, the only difference should be that
# sometimes the 'SQL-char-name:in_tag' tag isn't present:
keys_diff = i_t_keys.symmetric_difference(d_t_keys)
if keys_diff != set(['SQL-char-name:in_tag']):
msg = 'Unexpected difference in keys in databases: {}'
raise ValueError(msg.format(keys_diff))
def __main__():
# parse the arguments, straight fail if there's a problem.
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Test routine for ImageMetaTag")
parser.add_argument('--skip-plotting', '-s', action='store_true',
dest='skip_plotting', default=False,
help='Skip making plots, if metadata is available')
parser.add_argument('--no-big-dict', action='store_true',
dest='no_biggus_dictus', default=False,
help='Skip the big dictionary test.')
parser.add_argument('--no-db-rebuild', action='store_true',
dest='no_db_rebuild', default=False,
help='Skip rebuilding an image database from disk')
parser.add_argument('--minimal', '-m', '-q', action='store_true',
dest='minimal', default=False,
help='Run only a minimal amount of testing')
args = parser.parse_args()
if args.minimal:
n_random_data = 1
compress_levels = [3]
trims = [True]
borders = [10]
dpis = [None, 72, 300]
# colours to plot with, and the names we want in the metadata/webpage
colours = [('g', 'Plotted in Green')]
else:
n_random_data = 3
compress_levels = [0, 1, 2, 3]
trims = [True, False]
borders = [5, 10, 100]
dpis = [72, 150]
# colours to plot with, and the names we want in the metadata/webpage
colours = [('r', 'Plotted in Red'),
('b', 'Plotted in Blue'),
('k', 'Plotted in Black'),
('g', 'Plotted in Green')]
random_data = make_random_data(n_random_data)
borders_str = ['{} pixels'.format(border) for border in borders]
# working directory to create images and webpage etc.
webdir = get_webdir(minimal=args.minimal)
# The img_savedir is relative to webdir:
img_savedir = os.path.join(webdir, 'images')
mkdir_p(img_savedir)
test_zlib_compression = True
# a database to store the image metadata, as we write them:
imt_db = define_imt_db(wipe=not args.skip_plotting, minimal=args.minimal)
if not os.path.isfile(LOGO_FILE):
raise ValueError('No logo file found at: %s' % LOGO_FILE)
# set up the images we want to create in the test, and the webpages:
img_format = 'png'
# this defines the order of the different tags in
# the ImageDict, and so how they are displayed on the webpage:
tagorder = ['data source',
'number of rolls',
'plot type',
'plot color',
'image trim',
'border',
'expected dpi',
'image compression']
selector_animated = 5 # animate the image compression
animation_direction = +1 # move forwards
sort_methods = ['sort', 'numeric', 'sort', 'sort', 'sort',
borders_str, 'numeric', 'sort']
plot_owner = 'Created by %s' % get_user_and_email()
# what are the full names of those tags:
tag_full_names = {'data source': 'Data Source',
'number of rolls': 'Number of rolls',
'plot type': 'Plot type',
'plot color': 'Plot color',
'image trim': 'Image trimmed?',
'border': 'Image border',
'image compression': 'Image compression',
'expected dpi': 'Expected DPI',
}
sel_widths = {'data source': '120px',
'number of rolls': '180px',
'plot type': '120px',
'plot color': '200px',
'image trim': '150px',
'border': '140px',
'expected dpi': '90px',
'image compression': '200px',
}
# if we want to present these, have them as an ordered list, by tagorder:
sel_names_list = [tag_full_names[x] for x in tagorder]
sel_widths_list = [sel_widths[x] for x in tagorder]
# this will become a large dict of images and their metadata:
images_and_tags = {}
if PY3:
metadata_pickle = '%s/meta.py3.p' % img_savedir
else:
metadata_pickle = '%s/meta.py2.p' % img_savedir
# do we want to, and can we actually, skip ploting:
skip_plot = (args.skip_plotting and
os.path.isfile(metadata_pickle) and
os.path.isfile(imt_db))
if skip_plot:
# load the metadata from the pickle
print('loading metadata from %s' % metadata_pickle)
with open(metadata_pickle, "rb") as open_pickle:
images_and_tags = pickle.load(open_pickle)
else:
# do the work of making all the plots:
if os.path.isfile(imt_db):
print('Deleting pre-exising image database "%s"' % imt_db)
os.remove(imt_db)
# now plot the random data in some different ways,
# tagging the image as we go:
for i_rnd in range(n_random_data):
for (plot_col, col_name) in colours:
new_imgs_tags = plot_random_data(random_data, i_rnd, plot_col,
col_name, trims, borders,
compress_levels, dpis,
img_savedir, img_format,
plot_owner, imt_db)
# now stick the new img ingo into the main dict:
for img_file, img_info in new_imgs_tags.items():
images_and_tags[img_file] = img_info
print_simple_timer(DATE_START, datetime.now(), 'plotting')
with open(metadata_pickle, "wb") as open_pickle:
pickle.dump(images_and_tags, open_pickle)
# the metadata saved in images_and_tags is references to absolute
# filepaths but it is much easier to work with a path that is relative
# to the webdir, where the database is stored.
# The database also does this automatically.
#
# In actual usage, the database would be the place to get the metadata,
# so there is usually no need to get the metadata back from a plotting
# process. In this test we need that information directly because we
# want to test the integrity of the database!
rel_images_and_tags = {}
for img_file, img_info in images_and_tags.items():
if img_file.startswith(webdir):
img_file = img_file[len(webdir)+1:]
rel_images_and_tags[img_file] = img_info
images_and_tags = rel_images_and_tags
# now assemble the ImageDict using the image metadata:
img_dict = None
# This is the simple way, but it is possible to parallelise
# this step as done below:
for img_file, img_info in images_and_tags.items():
tmp_dict = imt.dict_heirachy_from_list(img_info, img_file, tagorder)
if not img_dict:
img_dict = imt.ImageDict(tmp_dict,
selector_animated=selector_animated,
animation_direction=animation_direction,
level_names=sel_names_list,
selector_widths=sel_widths_list)
else:
img_dict.append(imt.ImageDict(tmp_dict,
level_names=sel_names_list))
# Database integrity and optimisation tests:
# Firstly, read the database. This simply loads ALL of the image metadata:
db_imgs, db_img_tags = imt.db.read(imt_db)
# check that the database table contains ONLY the SQLITE_IMG_INFO_TABLE
dbcn, dbcr = imt.db.open_db_file(imt_db)
# check for the required table:
table_names = imt.db.list_tables(dbcr)
dbcn.close()
if table_names != [imt.db.SQLITE_IMG_INFO_TABLE]:
msg = 'Database integrity failre: database contains "{}", not "{}"'
raise ValueError(msg.format(table_names,
[imt.db.SQLITE_IMG_INFO_TABLE]))
# In this test though, we also need to verfiy the integrity of the
# database, relative to the plotting/pickling process:
img_list = sorted(images_and_tags.keys())
db_imgs.sort()
if img_list != db_imgs:
msg = ('List of plots differ between database and '
'plotting/pickle versions')
raise ValueError(msg)
if not args.skip_plotting:
# if we have done the plotting then these should match. If not, then
# the database delete test later on will mess this up:
test_compare_img_tags(images_and_tags, 'plot dict',
db_img_tags, 'database dict')
# For memory optimisation of large image databases, we want to make sure
# the dictionary we get back is as small as possible in memory:
#
# these are the tags that we actually need to work with for the web page.
# Others are ignored:
required_tags = ['data source', 'number of rolls', 'plot type',
'plot color', 'image trim', 'border', 'expected dpi',
'image compression', 'SQL-char-name:in_tag']
db_imgs, db_img_tags = imt.db.read(imt_db, required_tags=required_tags)
# For more memory optimisation, this will return a list of all of the
# unique metadata strings, as there is usually a lot of duplication.
# The returned db_img_tags will then reference the strings witin
# that list, rahter than contain the duplicated strings. This saves a
# lot of memory for large databases of files.
tag_strings = []
db_imgs, db_img_tags = imt.db.read(imt_db, tag_strings=tag_strings)
# and this both filters out un-needed tags and uses the tag_strings
# list as a reference
tag_strings = []
db_imgs, db_img_tags = imt.db.read(imt_db, required_tags=required_tags,
tag_strings=tag_strings)
# test deleting a single image from the db file, and then add it back in:
del_img = db_imgs[0]
del_tags = db_img_tags[del_img]
imt.db.del_plots_from_dbfile(imt_db, del_img)
# now put it back in:
imt.db.write_img_to_dbfile(imt_db, del_img, del_tags)
print('Database integrity checks/memory optimsations completed')
# Now make the next type of web page.
# This test produces the same thing as before, but created in parallel.
# This isn't needed for a small set of plots like this example, but the
# code appears to scale well. For example, within the Met Office, this
# code is used to produce internal web pages with approx 2 million images.
n_proc = 4
skip_key_relist = False # default setting
# set up a generator to break up the images into smaller chunks:
extra_opts = (tagorder, skip_key_relist,
selector_animated, animation_direction)
subdict_gen = imt.dict_split(db_img_tags,
n_split=n_proc,
extra_opts=extra_opts)
if n_proc == 1:
# this calls the processing, in series. It is much easier to debug
# when not using the parallel calls:
pool_out = [define_img_dict_in_tuple(x) for x in subdict_gen]
else:
# now in parallel:
proc_pool = Pool(n_proc)
pool_out = proc_pool.map(define_img_dict_in_tuple, subdict_gen)
proc_pool.close()
proc_pool.join()
# now stitch the parallel image dict back together:
img_dict_para = pool_out[0]
for i_dict in range(1, len(pool_out)):
img_dict_para.append(pool_out[i_dict])
if skip_key_relist:
# if we skipped relisting the keys (as it's faster to do that)
# then make sure we list them at the end:
img_dict_para.list_keys_by_depth()
# sort the keys:
img_dict.sort_keys(sort_methods)
img_dict_para.sort_keys(sort_methods)
# now these should be the same, on a print:
print(img_dict)
print(img_dict_para)
# quick test of dict_index_array:
# first to a particular depth:
test_depth = 4
array_inds = img_dict.dict_index_array(maxdepth=test_depth)
# now work out how many keys there should be. This can apply in
# this test because we have made an image for every combination of
# keys. If that wasn't the case then the test would break. It's in
# those cases when the img_dict.dict_index_array is useful!
n_test_depth = 1
for depth in range(test_depth):
n_test_depth *= len(img_dict.keys[depth])
if len(array_inds[1]) != n_test_depth:
raise ValueError('Mismatch between indices to depth and keys')
# and in full:
array_indsf = img_dict.dict_index_array()
if len(array_indsf[1]) != len(db_img_tags):
raise ValueError('Mismatched indices and image array lengths')
# now reorganise the img_dict to merge some of the images together
# (to display multiple images side-by-side)
#
# This is best done after an ImageDict has been created that describes
# all of the images, so we can use it to quickly group images together,
# rather than having to search the long list, or the database, each time.
# This is much quicker because the ImageDict has the same tree structure
# that we are going to need to do the search!
#
# this defines the tag in the tagorder that we are grouping over.
# It is a single integer, NOT a list as only a single tag is grouped in
# the code below. In this case it is focusing on the plot color:
multi_depth = tagorder.index('plot color')
# the key_filter can be used to filter out images to go on the web page,
# as well as report they are part of a special list for multiple images:
# (a filter value of None means a filter is not applied,
# not that nothing passes the filter)
key_filter = {'data source': None,
'number of rolls': img_dict.keys[tagorder.index('number of rolls')][1:],
'plot type': None,
'plot color': [img_dict.keys[multi_depth][0],
('Primary colors', img_dict.keys[multi_depth][1:]),
('All colors', img_dict.keys[multi_depth])],
'image trim': None,
'border': None,
'image compression': None}
# does the multi image require that ALL of the images is
# specifies are available, in order to present anything?
multi_req_all = True
# now do the work to reorganise (with a time around it):
date_start_reorg_multi = datetime.now()
if not args.minimal:
# This returns a copy of the previous image dict:
img_dict_multi = img_dict.copy_except_dict_and_keys()
img_dict_multi.dict = {}
# now assemble the ImageDict:
# This is the simple way, but it is possible to parallelise this
# step as done below:
for img_file, img_info in images_and_tags.items():
# test the image to see if its needed, and if it's needed
# for the complex/multiple image case:
img_tests = imt.simple_dict_filter(img_info, key_filter)
# unpack the variables in img_tests:
use_plain, use_multi, first_multi = img_tests
if use_plain:
# just add the image, as is:
tmp_dict = imt.dict_heirachy_from_list(img_info,
img_file,
tagorder)
img_dict_multi.append(imt.ImageDict(tmp_dict))
# now we are filterig one of the levels of the dict (multi_depth)
# by the multi_keys list:
if use_multi and first_multi:
for tuple_test in key_filter[tagorder[multi_depth]]:
if isinstance(tuple_test, tuple):
# split the tuple test up into meaningful variables:
# the name, as it will be in img_dict_multi
group_name = tuple_test[0]
# the contents that will be grouped together
group_values = tuple_test[1]
# now check that the image is the first element
# of this tuple_test:
if img_info[tagorder[multi_depth]] == group_values[0]:
# lookup to see if this combination has already
# been added to img_dict_multi, by creating a list
# of keys to lookup within the ImageDict:
key_lookup = [img_info[x] for x in tagorder]
key_lookup[multi_depth] = group_name
if len(img_dict_multi.dict) == 0:
already_in_img_dict_multi = False
else:
if img_dict_multi.return_from_list(key_lookup) is None:
already_in_img_dict_multi = False
else:
already_in_img_dict_multi = True
if already_in_img_dict_multi:
print('multi image already added!')
print(key_lookup)
#msg = 'Adding a multi-image that has already been added.'
#msg += ' Checks on first_image_multi should prevent that.'
#raise ValueError(msg)
else:
# this group hasn't already been added to the img_dict_multi:
if group_name in img_dict.keys[multi_depth]:
# the group_name shouldn't be the same as
# a img that identifies a single image.
# That will cause problems, and will
# change what is presented depening on
# the order that the img_file, img_info
# comes up in images_and_tags.iteritems()
msg = ('A multi image group has the same img '
'name as a single image')
raise ValueError(msg)
all_img_relpaths = []
for this_value in group_values:
# now use the key_lookup again,