def write_to_file(self, fn, filter_package=None): write_label_vector_as_testing = False if filter_package is not None: write_label_vector_as_testing = filter_package[1] sys.stderr.write( 'Info: writing image table %s; label vectors as testing? %d\n' % (fn, write_label_vector_as_testing)) (c_total, c_written, c_neg2_but_not_testing) = (0, 0, 0) with codecs.open(fn, 'w', encoding='UTF-8') as f: for mir_id in sorted(self.entries): e = self.entries[mir_id] c_total += 1 write_this_edge = (filter_package is None) or \ (e.mir_id in filter_package[0]) if not write_this_edge: continue c_written += 1 f.write('%d ' % e.mir_id) # 0 f.write('%d ' % e.flickr_id) # 1 f.write('%s ' % Util.qstr(e.flickr_owner)) # 2 f.write('%s ' % Util.qstr(e.flickr_title)) # 3 f.write('%s ' % Util.qstr(e.flickr_descr)) # 4 f.write('%s ' % e.exif_data.to_table_str()) # 5,6,7 if e.flickr_locality: # 8 f.write('%s ' % Util.qstr(e.flickr_locality)) else: f.write('none ') v = copy.copy(e.label_vector) if write_label_vector_as_testing: # set all non -1 values to -2 for i in range(0, len(v)): if v[i] != -1: v[i] = -2 else: # sanity check: if this is NOT testing, there should be no -2 values if v.count(-2): c_neg2_but_not_testing += 1 f.write('%s\n' % ','.join(map(str, v))) # 9 if c_neg2_but_not_testing != 0: sys.stderr.write('WARNING\nWARNING\nWARNING: %s not written as testing, but contained %d (of %d) images with -2 in the label vector\nWARNING\WARNING\n'\ % (fn, c_neg2_but_not_testing, c_written)) return (c_total, c_written)
def read_from_file( fn, id_dict_to_keep=None ): c = 0 t = ImageTable() with codecs.open( fn, 'r', encoding='utf-8' ) as f: while 1: raw_line = f.readline() if not raw_line: break c += 1 fields = Util.qstr_split( raw_line.strip() ) if len(fields) != 10: raise AssertionError( 'Image table %s:%d: found %d fields, expecting 10' % (fn, c, len(fields))) id = int( fields[0] ) if not ( (id_dict_to_keep is None) or (id in id_dict_to_keep) ): continue e = ImageTableEntry() e.mir_id = int( id ) e.flickr_id = int( fields[1] ) e.flickr_owner = fields[2] if (fields[2] != 'none') else None e.flickr_title = fields[3] if (fields[3] != 'none') else None e.flickr_descr = fields[4] if (fields[4] != 'none') else None if (fields[5] == 'none') and \ (fields[6] == 'none') and \ (fields[7] == 'U'): e.exif_data = EXIFData( e.mir_id, False, 'none', 'none', 'U' ) else: e.exif_data = EXIFData( e.mir_id, True, fields[5], fields[6], fields[7]) e.flickr_locality = fields[8] if fields[8] != 'none' else None e.label_vector = map(int, fields[9].split(',')) t.add_entry( e ) return t
def read_from_file(fn, id_dict_to_keep=None): c = 0 t = ImageTable() with codecs.open(fn, 'r', encoding='utf-8') as f: while 1: raw_line = f.readline() if not raw_line: break c += 1 fields = Util.qstr_split(raw_line.strip()) if len(fields) != 10: raise AssertionError( 'Image table %s:%d: found %d fields, expecting 10' % (fn, c, len(fields))) id = int(fields[0]) if not ((id_dict_to_keep is None) or (id in id_dict_to_keep)): continue e = ImageTableEntry() e.mir_id = int(id) e.flickr_id = int(fields[1]) e.flickr_owner = fields[2] if (fields[2] != 'none') else None e.flickr_title = fields[3] if (fields[3] != 'none') else None e.flickr_descr = fields[4] if (fields[4] != 'none') else None if (fields[5] == 'none') and \ (fields[6] == 'none') and \ (fields[7] == 'U'): e.exif_data = EXIFData(e.mir_id, False, 'none', 'none', 'U') else: e.exif_data = EXIFData(e.mir_id, True, fields[5], fields[6], fields[7]) e.flickr_locality = fields[8] if fields[8] != 'none' else None e.label_vector = map(int, fields[9].split(',')) t.add_entry(e) return t
def write_to_file( self, fn, filter_package = None ): write_label_vector_as_testing = False if filter_package is not None: write_label_vector_as_testing = filter_package[1] sys.stderr.write('Info: writing image table %s; label vectors as testing? %d\n' % (fn, write_label_vector_as_testing )) (c_total, c_written, c_neg2_but_not_testing) = (0,0,0) with codecs.open( fn, 'w', encoding='UTF-8' ) as f: for mir_id in sorted( self.entries ): e = self.entries[ mir_id ] c_total += 1 write_this_edge = (filter_package is None) or \ (e.mir_id in filter_package[0]) if not write_this_edge: continue c_written += 1 f.write( '%d ' % e.mir_id ) # 0 f.write( '%d ' % e.flickr_id ) # 1 f.write( '%s ' % Util.qstr( e.flickr_owner )) # 2 f.write( '%s ' % Util.qstr( e.flickr_title )) # 3 f.write( '%s ' % Util.qstr( e.flickr_descr )) # 4 f.write( '%s ' % e.exif_data.to_table_str() ) # 5,6,7 if e.flickr_locality: # 8 f.write( '%s ' % Util.qstr( e.flickr_locality )) else: f.write( 'none ' ) v = copy.copy( e.label_vector ) if write_label_vector_as_testing: # set all non -1 values to -2 for i in range(0, len(v)): if v[i] != -1: v[i] = -2 else: # sanity check: if this is NOT testing, there should be no -2 values if v.count(-2): c_neg2_but_not_testing += 1 f.write( '%s\n' % ','.join(map(str, v))) # 9 if c_neg2_but_not_testing != 0: sys.stderr.write('WARNING\nWARNING\nWARNING: %s not written as testing, but contained %d (of %d) images with -2 in the label vector\nWARNING\WARNING\n'\ % (fn, c_neg2_but_not_testing, c_written)) return (c_total, c_written)
def write_to_file( self, fn ): with open( fn, 'w' ) as f: f.write( '%d %d\n' % (len(self.group_text_lut), len(self.tag_word_text_lut))) for i in sorted( self.group_text_lut.iteritems(), key=lambda x:x[1] ): (entry_id, entry_text) = (i[1], i[0]) f.write( '%d G %s\n' % ( entry_id, Util.qstr( entry_text ))) for i in sorted( self.tag_word_text_lut.iteritems(), key=lambda x:x[1] ): (entry_id, entry_text) = (i[1], i[0]) f.write( '%d %s %s\n' % ( entry_id, self.tag_word_text_src[ entry_id], Util.qstr( entry_text )))
def read_from_file(fn): L2I = dict() I2L = dict() with open(fn, 'r') as f: while 1: raw_line = f.readline() if not raw_line: break fields = Util.qstr_split(raw_line.strip()) I2L[int(fields[0])] = fields[1] L2I[fields[1]] = int(fields[0]) return LabelTable(L2I, I2L)
def read_from_file( fn ): t = ImageIndicatorLookupTable() with open( fn, 'r' ) as f: header_fields = Util.qstr_split( f.readline().strip() ) if len(header_fields) != 2: raise AssertionError( 'ImageIndicatorLookupTable "%s": header had %d fields, expected 2' % \ ( fn, len(header_fields))) (n_groups, n_words_and_tags) = map(int, header_fields) for i in range(0, n_groups): fields = Util.qstr_split( f.readline().strip() ) if len(fields) != 3: raise AssertionError( 'ImageIndicatorLookupTable "%s": group %d had %d fields, expected 3' % \ (fn, i, len(fields))) if fields[1] != 'G': raise AssertionError( 'ImageIndicatorLookupTable "%s": entry %d flavor was %s; expected "G"' % \ (fn, i, fields[1])) t.group_text_lut[ fields[2] ] = int( fields[0] ) for i in range(0, n_words_and_tags): fields = Util.qstr_split( f.readline().strip() ) if len(fields) != 3: raise AssertionError( 'ImageIndicatorLookupTable "%s": word %d had %d fields, expected 3' % \ (fn, i, len(word_fields))) if (fields[1] != 'W') and (fields[1] != 'T') and (fields[1] != 'B'): raise AssertionError( 'ImageIndicatorLookupTable "%s": entry %d flavor was %s; expected "W", "T", or "B"' % \ (fn, i, fields[1])) t.tag_word_text_lut[ fields[2] ] = int( fields[0] ) t.tag_word_text_src[ int(fields[0] ) ] = fields[1] sys.stderr.write('Info: IILUT read %d / %d / %d / %d groups / tags / words / both\n' % \ (len(t.group_text_lut), t.tag_word_text_src.values().count('T'), \ t.tag_word_text_src.values().count('W'), \ t.tag_word_text_src.values().count('B'))) return t
g = Graph() g.add_vertices( len(nodes)) graph_edges = list( [node_id_map[e.image_A_id], node_id_map[e.image_B_id]] \ for e in edges if edges_predicate(e)) g.add_edges( graph_edges) layout = g.layout_circle() # adding text to plot courtesy http://stackoverflow.com/questions/18250684/add-title-and-legend-to-igraph-plots plot = Plot(output_fn, bbox=[2000,2000]) plot.add( g, layout=layout, bbox=[50,50,1950,1950]) ctx = cairo.Context( plot.surface ) ctx.set_font_size(36) drawer = TextDrawer( ctx, "Label: '%s'\n%s\n%d nodes, %d edges" % \ (LABEL, tag, len(nodes), len(graph_edges)), halign=TextDrawer.RIGHT) drawer.draw_at(10,1900,width=1900) plot.save() sys.stderr.write("Info: wrote %s\n" % output_fn) p = Paths() nodes = Util.nodes_with_label(p, PHASE, LABEL) edges = CP6ImageEdge.edges_in_nodeset( p, PHASE, nodes) # map node and edge IDs to graph vertex ordinals node_id_map = dict( [val, idx] for (idx, val) in enumerate(nodes)) make_graph( node_id_map, nodes, edges, shared_groups_only, "%s.group-only.png" % LABEL, "shared groups only") make_graph( node_id_map, nodes, edges, shared_flags_only, "%s.flags-only.png" % LABEL, "same user/location/contact only") make_graph( node_id_map, nodes, edges, all_edges, "%s.all-edges.png" % LABEL, "any relationship") for n in (1,2,5,10,15,20): make_graph( node_id_map, nodes, edges, functools.partial(only_N_shared_words, N=n), "%s.%02d-shared-word.png" % (LABEL, n) , "Exactly %d shared word(s)" % n)
def write_to_file(self, fn): with open(fn, 'w') as f: for index in self.idset: f.write('%d %s\n' % (index, Util.qstr(self.id2label[index])))