Class: Artbase::Base
- Inherits:
-
Object
- Object
- Artbase::Base
- Defined in:
- lib/artbase/collection/base.rb
Overview
“abstract” Base collection - check -use a different name - why? why not?
Direct Known Subclasses
Instance Method Summary collapse
-
#_normalize_trait_type(trait_type) ⇒ Object
“private” helpers.
- #_normalize_trait_value(trait_value) ⇒ Object
-
#calc_attribute_counters ⇒ Object
todo/check: use a different name _counts/_stats etc - why? why not?.
- #convert_images(overwrite:) ⇒ Object
- #dump_attributes ⇒ Object
-
#export_attributes(order: [], renames: {}) ⇒ Object
order - allow “custom” attribute order export renames - allow renames of attributes.
- #make_composite(limit: nil, mirror: false) ⇒ Object
- #make_strip ⇒ Object
Instance Method Details
#_normalize_trait_type(trait_type) ⇒ Object
“private” helpers
309 310 311 312 313 314 315 |
# File 'lib/artbase/collection/base.rb', line 309 def _normalize_trait_type( trait_type ) if @patch && @patch[:trait_types] @patch[:trait_types][ trait_type ] || trait_type else trait_type end end |
#_normalize_trait_value(trait_value) ⇒ Object
317 318 319 320 321 322 323 |
# File 'lib/artbase/collection/base.rb', line 317 def _normalize_trait_value( trait_value ) if @patch && @patch[:trait_values] @patch[:trait_values][ trait_value ] || trait_value else trait_value end end |
#calc_attribute_counters ⇒ Object
todo/check: use a different name _counts/_stats etc - why? why not?
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 |
# File 'lib/artbase/collection/base.rb', line 113 def calc_attribute_counters ## todo/check: use a different name _counts/_stats etc - why? why not? attributes_by_count = { count: 0, by_count: Hash.new(0) } counter = {} do |, id| ## todo/fix: change id to index traits = .traits # print "#{traits.size} - " # pp traits print "#{id}.." if id % 100 == 0 ## print progress report attributes_by_count[ :count ] +=1 attributes_by_count[ :by_count ][ traits.size ] += 1 traits.each do |trait_type, trait_value| trait_type = _normalize_trait_type( trait_type ) trait_value = _normalize_trait_value( trait_value ) rec = counter[ trait_type ] ||= { count: 0, by_type: Hash.new(0) } rec[ :count ] +=1 rec[ :by_type ][ trait_value ] += 1 end end print "\n" puts ## return all-in-one hash { total: attributes_by_count, traits: counter, } end |
#convert_images(overwrite:) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/artbase/collection/base.rb', line 6 def convert_images( overwrite: ) image_dir = "./#{slug}/token-i" Image.convert( image_dir, from: 'jpg', to: 'png', overwrite: overwrite ) Image.convert( image_dir, from: 'gif', to: 'png', overwrite: overwrite ) Image.convert( image_dir, from: 'svg', to: 'png', overwrite: overwrite ) end |
#dump_attributes ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/artbase/collection/base.rb', line 155 def dump_attributes stats = calc_attribute_counters total = stats[:total] counter = stats[:traits] puts puts "attribute usage / counts:" pp total puts puts "#{counter.size} attribute(s):" counter.each do |trait_name, trait_rec| puts " #{trait_name} #{trait_rec[:count]} (#{trait_rec[:by_type].size} uniques)" end puts pp counter end |
#export_attributes(order: [], renames: {}) ⇒ Object
order - allow “custom” attribute order export
renames - allow renames of attributes
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 |
# File 'lib/artbase/collection/base.rb', line 180 def export_attributes( order: [], renames: {} ) ## step 1: get counters stats = calc_attribute_counters total = stats[:total] counter = stats[:traits] puts puts "attribute usage / counts:" pp total puts puts "#{counter.size} attribute(s):" counter.each do |trait_name, trait_rec| puts " #{trait_name} #{trait_rec[:count]} (#{trait_rec[:by_type].size} uniques)" end trait_names = [] trait_names += order ## get attributes if any in pre-defined order counter.each do |trait_name, _| if trait_names.include?( trait_name ) next ## skip already included else trait_names << trait_name end end recs = [] ## step 2: get tabular data do |, id| ## todo/fix: change id to index traits = .traits # print "#{traits.size} - " # pp traits print "#{id}.." if id % 100 == 0 ## print progress report ## setup empty hash table (with all attributes) rec = {} ## note: use __Slug__& __Name__ ## to avoid conflict with attribute names ## e.g. attribute with "Name" will overwrite built-in and so on rec['__Slug__'] = if respond_to?( :_meta_slugify ) ( , id ) else ## default to id (six digits) as string with leading zeros ## for easy sorting using strings ## e.g. 1 => '000001' ## 2 => '000002' '%06d' % id end rec['__Name__'] = .name ## add all attributes/traits names/keys trait_names.reduce( rec ) { |h,value| h[value] = []; h } ## pp rec ## note: use an array (to allow multiple values for attributes) traits.each do |trait_type, trait_value| trait_type = _normalize_trait_type( trait_type ) trait_value = _normalize_trait_value( trait_value ) values = rec[ trait_type ] values << trait_value end recs << rec end print "\n" ## pp recs ## flatten recs data = [] recs.each do |rec| row = rec.values.map do |value| if value.is_a?( Array ) value.join( ' / ' ) else value end end data << row end ## sort by slug data = data.sort {|l,r| l[0] <=> r[0] } pp data ### save dataset ## note: change first colum Slug to ID - only used for "internal" sort etc. headers = ['ID', 'Name'] headers += trait_names.map do |trait_name| ## check for renames renames[trait_name] || trait_name end path = "./#{@slug}/tmp/#{@slug}.csv" dirname = File.dirname( path ) FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname ) File.open( path, 'w:utf-8' ) do |f| f.write( headers.join( ', ' )) f.write( "\n" ) ## note: replace ID with our own internal running (zero-based) counter data.each_with_index do |row,i| f.write( ([i]+row[1..-1]).join( ', ')) f.write( "\n" ) end end end |
#make_composite(limit: nil, mirror: false) ⇒ Object
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 |
# File 'lib/artbase/collection/base.rb', line 37 def make_composite( limit: nil, mirror: false ) ### use well-known / pre-defined (default) grids ## (cols x rows) for now - why? why not? composite_count = if limit limit else @count - @excludes.size end cols, rows = case composite_count when 99 then [10, 10] when 100 then [10, 10] when 150 then [15, 10] when 314 then [15, 21] when 500 then [25, 20] when 1000 then [25, 40] when 2200 then [50, 44] when 2222 then [50, 45] when 2469 then [50, 50] when 3000 then [100, 30] ## or use 50*60 - why? why not? when 3500 then [100, 35] ## or use 50*x ?? when 3979 then [100, 40] when 4000 then [100, 40] ## or use 50x80 - why? why not? when 4444 then [100, 45] ## or use 50x?? when 5000 then [100, 50] ## or use 50x100 - why? why not? when 5555 then [100, 56] # 5600 (45 left empty) when 6666 then [100, 67] # 6700 (34 left empty) when 6688 then [100, 67] # 6700 (12 left empty) when 6969 then [100, 70] # 7000 (31 left empty) when 7500 then [100, 75] when 8888 then [100, 89] when 9969 then [100,100] when 10000 then [100,100] else raise ArgumentError, "sorry - unknown composite count #{composite_count}/#{@count} for now" end composite = ImageComposite.new( cols, rows, width: @width, height: @height ) count = 0 each_image do |img, id| puts "==> #{id}" composite << if mirror img.mirror else img end count += 1 break if limit && count >= limit end slug = "#{@slug}" slug += "#{limit}" if limit slug += "_left" if mirror path = "./#{@slug}/tmp/#{slug}-#{@width}x#{@height}.png" puts " saving #{path}..." composite.save( path ) if composite_count < 1000 path = "./#{@slug}/tmp/#{slug}-#{@width}x#{@height}@2x.png" puts " saving 2x #{path}..." composite.zoom(2).save( path ) end end |
#make_strip ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/artbase/collection/base.rb', line 15 def make_strip composite_count = @count - @excludes.size composite = ImageComposite.new( 9, 1, width: @width, height: @height ) i = 0 each_image do |img, id| puts "==> [#{i+1}/9] #{id}" composite << img i += 1 break if i >= 9 end composite.save( "./#{@slug}/tmp/#{@slug}-strip.png" ) end |