OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """ | 3 """ |
4 Copyright 2014 Google Inc. | 4 Copyright 2014 Google Inc. |
5 | 5 |
6 Use of this source code is governed by a BSD-style license that can be | 6 Use of this source code is governed by a BSD-style license that can be |
7 found in the LICENSE file. | 7 found in the LICENSE file. |
8 | 8 |
9 ImagePairSet class; see its docstring below. | 9 ImagePairSet class; see its docstring below. |
10 """ | 10 """ |
11 | 11 |
12 import column | 12 import column |
13 | 13 |
14 # Keys used within dictionary representation of ImagePairSet. | 14 # Keys used within dictionary representation of ImagePairSet. |
15 KEY__COLUMNHEADERS = 'columnHeaders' | 15 # NOTE: Keep these in sync with static/constants.js |
| 16 KEY__EXTRACOLUMNHEADERS = 'extraColumnHeaders' |
16 KEY__IMAGEPAIRS = 'imagePairs' | 17 KEY__IMAGEPAIRS = 'imagePairs' |
17 KEY__IMAGESETS = 'imageSets' | 18 KEY__IMAGESETS = 'imageSets' |
18 KEY__IMAGESETS__BASE_URL = 'baseUrl' | 19 KEY__IMAGESETS__BASE_URL = 'baseUrl' |
19 KEY__IMAGESETS__DESCRIPTION = 'description' | 20 KEY__IMAGESETS__DESCRIPTION = 'description' |
20 | 21 |
21 DEFAULT_DESCRIPTIONS = ('setA', 'setB') | 22 DEFAULT_DESCRIPTIONS = ('setA', 'setB') |
22 | 23 |
23 | 24 |
24 class ImagePairSet(object): | 25 class ImagePairSet(object): |
25 """A collection of ImagePairs, representing two arbitrary sets of images. | 26 """A collection of ImagePairs, representing two arbitrary sets of images. |
(...skipping 22 matching lines...) Expand all Loading... |
48 if not self._image_pair_dicts: | 49 if not self._image_pair_dicts: |
49 self._base_url = image_pair.base_url | 50 self._base_url = image_pair.base_url |
50 | 51 |
51 if image_pair.base_url != self._base_url: | 52 if image_pair.base_url != self._base_url: |
52 raise Exception('added ImagePair with base_url "%s" instead of "%s"' % ( | 53 raise Exception('added ImagePair with base_url "%s" instead of "%s"' % ( |
53 image_pair.base_url, self._base_url)) | 54 image_pair.base_url, self._base_url)) |
54 self._image_pair_dicts.append(image_pair.as_dict()) | 55 self._image_pair_dicts.append(image_pair.as_dict()) |
55 extra_columns_dict = image_pair.extra_columns_dict | 56 extra_columns_dict = image_pair.extra_columns_dict |
56 if extra_columns_dict: | 57 if extra_columns_dict: |
57 for column_id, value in extra_columns_dict.iteritems(): | 58 for column_id, value in extra_columns_dict.iteritems(): |
58 self._add_extra_column_entry(column_id, value) | 59 self._add_extra_column_value_to_summary(column_id, value) |
59 | 60 |
60 def set_column_header_factory(self, column_id, column_header_factory): | 61 def set_column_header_factory(self, column_id, column_header_factory): |
61 """Overrides the default settings for one of the extraColumn headers. | 62 """Overrides the default settings for one of the extraColumn headers. |
62 | 63 |
63 Args: | 64 Args: |
64 column_id: string; unique ID of this column (must match a key within | 65 column_id: string; unique ID of this column (must match a key within |
65 an ImagePair's extra_columns dictionary) | 66 an ImagePair's extra_columns dictionary) |
66 column_header_factory: a ColumnHeaderFactory object | 67 column_header_factory: a ColumnHeaderFactory object |
67 """ | 68 """ |
68 self._column_header_factories[column_id] = column_header_factory | 69 self._column_header_factories[column_id] = column_header_factory |
69 | 70 |
70 def get_column_header_factory(self, column_id): | 71 def get_column_header_factory(self, column_id): |
71 """Returns the ColumnHeaderFactory object for a particular extraColumn. | 72 """Returns the ColumnHeaderFactory object for a particular extraColumn. |
72 | 73 |
73 Args: | 74 Args: |
74 column_id: string; unique ID of this column (must match a key within | 75 column_id: string; unique ID of this column (must match a key within |
75 an ImagePair's extra_columns dictionary) | 76 an ImagePair's extra_columns dictionary) |
76 """ | 77 """ |
77 column_header_factory = self._column_header_factories.get(column_id, None) | 78 column_header_factory = self._column_header_factories.get(column_id, None) |
78 if not column_header_factory: | 79 if not column_header_factory: |
79 column_header_factory = column.ColumnHeaderFactory(header_text=column_id) | 80 column_header_factory = column.ColumnHeaderFactory(header_text=column_id) |
80 self._column_header_factories[column_id] = column_header_factory | 81 self._column_header_factories[column_id] = column_header_factory |
81 return column_header_factory | 82 return column_header_factory |
82 | 83 |
83 def _add_extra_column_entry(self, column_id, value): | 84 def ensure_extra_column_values_in_summary(self, column_id, values): |
| 85 """Ensure this column_id/value pair is part of the extraColumns summary. |
| 86 |
| 87 Args: |
| 88 column_id: string; unique ID of this column |
| 89 value: string; a possible value for this column |
| 90 """ |
| 91 for value in values: |
| 92 self._add_extra_column_value_to_summary( |
| 93 column_id=column_id, value=value, addend=0) |
| 94 |
| 95 def _add_extra_column_value_to_summary(self, column_id, value, addend=1): |
84 """Records one column_id/value extraColumns pair found within an ImagePair. | 96 """Records one column_id/value extraColumns pair found within an ImagePair. |
85 | 97 |
86 We use this information to generate tallies within the column header | 98 We use this information to generate tallies within the column header |
87 (how many instances we saw of a particular value, within a particular | 99 (how many instances we saw of a particular value, within a particular |
88 extraColumn). | 100 extraColumn). |
| 101 |
| 102 Args: |
| 103 column_id: string; unique ID of this column (must match a key within |
| 104 an ImagePair's extra_columns dictionary) |
| 105 value: string; a possible value for this column |
| 106 addend: integer; how many instances to add to the tally |
89 """ | 107 """ |
90 known_values_for_column = self._extra_column_tallies.get(column_id, None) | 108 known_values_for_column = self._extra_column_tallies.get(column_id, None) |
91 if not known_values_for_column: | 109 if not known_values_for_column: |
92 known_values_for_column = {} | 110 known_values_for_column = {} |
93 self._extra_column_tallies[column_id] = known_values_for_column | 111 self._extra_column_tallies[column_id] = known_values_for_column |
94 instances_of_this_value = known_values_for_column.get(value, 0) | 112 instances_of_this_value = known_values_for_column.get(value, 0) |
95 instances_of_this_value += 1 | 113 instances_of_this_value += addend |
96 known_values_for_column[value] = instances_of_this_value | 114 known_values_for_column[value] = instances_of_this_value |
97 | 115 |
98 def _column_headers_as_dict(self): | 116 def _column_headers_as_dict(self): |
99 """Returns all column headers as a dictionary.""" | 117 """Returns all column headers as a dictionary.""" |
100 asdict = {} | 118 asdict = {} |
101 for column_id, values_for_column in self._extra_column_tallies.iteritems(): | 119 for column_id, values_for_column in self._extra_column_tallies.iteritems(): |
102 column_header_factory = self.get_column_header_factory(column_id) | 120 column_header_factory = self.get_column_header_factory(column_id) |
103 asdict[column_id] = column_header_factory.create_as_dict( | 121 asdict[column_id] = column_header_factory.create_as_dict( |
104 values_for_column) | 122 values_for_column) |
105 return asdict | 123 return asdict |
106 | 124 |
107 def as_dict(self): | 125 def as_dict(self): |
108 """Returns a dictionary describing this package of ImagePairs. | 126 """Returns a dictionary describing this package of ImagePairs. |
109 | 127 |
110 Uses the KEY__* constants as keys. | 128 Uses the KEY__* constants as keys. |
111 """ | 129 """ |
112 return { | 130 return { |
113 KEY__COLUMNHEADERS: self._column_headers_as_dict(), | 131 KEY__EXTRACOLUMNHEADERS: self._column_headers_as_dict(), |
114 KEY__IMAGEPAIRS: self._image_pair_dicts, | 132 KEY__IMAGEPAIRS: self._image_pair_dicts, |
115 KEY__IMAGESETS: [{ | 133 KEY__IMAGESETS: [{ |
116 KEY__IMAGESETS__BASE_URL: self._base_url, | 134 KEY__IMAGESETS__BASE_URL: self._base_url, |
117 KEY__IMAGESETS__DESCRIPTION: self._descriptions[0], | 135 KEY__IMAGESETS__DESCRIPTION: self._descriptions[0], |
118 }, { | 136 }, { |
119 KEY__IMAGESETS__BASE_URL: self._base_url, | 137 KEY__IMAGESETS__BASE_URL: self._base_url, |
120 KEY__IMAGESETS__DESCRIPTION: self._descriptions[1], | 138 KEY__IMAGESETS__DESCRIPTION: self._descriptions[1], |
121 }], | 139 }], |
122 } | 140 } |
OLD | NEW |