<s> """<STR_LIT>""" <EOL> import json <EOL> import os . path as op <EOL> import sys <EOL> import string <EOL> import logging <EOL> from math import sin , cos , pi <EOL> import numpy as np <EOL> from jcvi . graphics . base import plt , savefig , normalize_axes , Rectangle , latex <EOL> from Image import open as iopen <EOL> from wand . image import Image <EOL> from scipy . ndimage import binary_fill_holes , distance_transform_edt <EOL> from scipy . optimize import fmin_bfgs as fmin <EOL> from skimage . color import gray2rgb , rgb2gray <EOL> from skimage . filter import canny , roberts , sobel <EOL> from skimage . feature import peak_local_max <EOL> from skimage . measure import regionprops , label <EOL> from skimage . morphology import disk , closing , watershed <EOL> from skimage . segmentation import clear_border <EOL> from jcvi . utils . counter import Counter <EOL> from jcvi . utils . webcolors import rgb_to_hex , closest_color , normalize_integer_triplet <EOL> from jcvi . formats . base import must_open <EOL> from jcvi . algorithms . formula import reject_outliers , get_kmeans <EOL> from jcvi . apps . tesseract import image_to_string <EOL> from jcvi . apps . base import OptionParser , OptionGroup , ActionDispatcher , iglob , mkdir , datadir <EOL> np . seterr ( all = "<STR_LIT:ignore>" ) <EOL> class Seed ( object ) : <EOL> def __init__ ( self , imagename , accession , seedno , rgb , props , exif ) : <EOL> self . imagename = imagename <EOL> self . accession = accession <EOL> self . seedno = seedno <EOL> y , x = props . centroid <EOL> self . x , self . y = int ( round ( x ) ) , int ( round ( y ) ) <EOL> self . location = "<STR_LIT>" . format ( self . x , self . y ) <EOL> self . area = int ( round ( props . area ) ) <EOL> self . length = int ( round ( props . major_axis_length ) ) <EOL> self . width = int ( round ( props . minor_axis_length ) ) <EOL> self . props = props <EOL> self . circularity = <NUM_LIT:4> * pi * props . area / props . perimeter ** <NUM_LIT:2> <EOL> self . rgb = rgb <EOL> self . colorname = closest_color ( rgb ) <EOL> self . datetime = exif . get ( '<STR_LIT>' , "<STR_LIT:none>" ) <EOL> self . rgbtag = triplet_to_rgb ( rgb ) <EOL> self . pixeltag = "<STR_LIT>" . format ( self . length , self . width , self . area ) <EOL> self . hashtag = "<STR_LIT:U+0020>" . join ( ( self . rgbtag , self . colorname ) ) <EOL> self . calibrated = False <EOL> def __str__ ( self ) : <EOL> fields = [ self . imagename , self . datetime , <EOL> self . accession , self . seedno , self . location , <EOL> self . area , "<STR_LIT>" . format ( self . circularity ) , <EOL> self . length , self . width , <EOL> self . colorname , self . rgbtag ] <EOL> if self . calibrated : <EOL> fields += [ self . pixelcmratio , self . rgbtransform , <EOL> self . correctedlength , self . correctedwidth , <EOL> self . correctedcolorname , self . correctedrgb ] <EOL> return "<STR_LIT:\t>" . join ( str ( x ) for x in fields ) <EOL> @ classmethod <EOL> def header ( cls , calibrate = False ) : <EOL> fields = "<STR_LIT>" "<STR_LIT>" . split ( ) <EOL> if calibrate : <EOL> fields += "<STR_LIT>" "<STR_LIT>" . split ( ) <EOL> return "<STR_LIT:\t>" . join ( fields ) <EOL> def calibrate ( self , pixel_cm_ratio , tr ) : <EOL> self . pixelcmratio = "<STR_LIT>" . format ( pixel_cm_ratio ) <EOL> self . rgbtransform = "<STR_LIT:U+002C>" . join ( [ "<STR_LIT>" . format ( x ) for x in tr . flatten ( ) ] ) <EOL> self . correctedlength = "<STR_LIT>" . format ( self . length / pixel_cm_ratio ) <EOL> self . correctedwidth = "<STR_LIT>" . format ( self . width / pixel_cm_ratio ) <EOL> correctedrgb = np . dot ( tr , np . array ( self . rgb ) ) <EOL> self . correctedrgb = triplet_to_rgb ( correctedrgb ) <EOL> self . correctedcolorname = closest_color ( correctedrgb ) <EOL> self . calibrated = True <EOL> def rgb_to_triplet ( rgb ) : <EOL> return tuple ( [ int ( x ) for x in rgb . split ( '<STR_LIT:U+002C>' ) ] ) <EOL> def triplet_to_rgb ( triplet ) : <EOL> triplet = normalize_integer_triplet ( triplet ) <EOL> return "<STR_LIT:U+002C>" . join ( str ( int ( round ( x ) ) ) for x in triplet ) <EOL> def main ( ) : <EOL> actions = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> p = ActionDispatcher ( actions ) <EOL> p . dispatch ( globals ( ) ) <EOL> def total_error ( x , colormap ) : <EOL> xs = np . reshape ( x , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> error_squared = sum ( [ np . linalg . norm ( np . dot ( xs , o ) - e ) ** <NUM_LIT:2> for o , e in colormap ] ) <EOL> return error_squared ** <NUM_LIT> <EOL> def calibrate ( args ) : <EOL> """<STR_LIT>""" <EOL> xargs = args [ <NUM_LIT:2> : ] <EOL> p = OptionParser ( calibrate . __doc__ ) <EOL> opts , args , iopts = add_seeds_options ( p , args ) <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> imagefile , boxsize = args <EOL> boxsize = float ( boxsize ) <EOL> colorcheckerfile = op . join ( datadir , "<STR_LIT>" ) <EOL> colorchecker = [ ] <EOL> expected = <NUM_LIT:0> <EOL> for row in open ( colorcheckerfile ) : <EOL> boxes = row . split ( ) <EOL> colorchecker . append ( boxes ) <EOL> expected += len ( boxes ) <EOL> folder = op . split ( imagefile ) [ <NUM_LIT:0> ] <EOL> objects = seeds ( [ imagefile , "<STR_LIT>" . format ( folder ) ] + xargs ) <EOL> nseeds = len ( objects ) <EOL> logging . debug ( "<STR_LIT>" . format ( nseeds , expected ) ) <EOL> assert expected - <NUM_LIT:4> <= nseeds <= expected + <NUM_LIT:4> , "<STR_LIT>" . format ( expected ) <EOL> boxes = [ t . area for t in objects ] <EOL> reject = reject_outliers ( boxes ) <EOL> retained_boxes = [ b for r , b in zip ( reject , boxes ) if not r ] <EOL> mbox = np . median ( retained_boxes ) <EOL> pixel_cm_ratio = ( mbox / boxsize ) ** <NUM_LIT> <EOL> logging . debug ( "<STR_LIT>" . format ( mbox , boxsize ) ) <EOL> logging . debug ( "<STR_LIT>" . format ( pixel_cm_ratio ) ) <EOL> xs = [ t . x for t in objects ] <EOL> ys = [ t . y for t in objects ] <EOL> idx_xs = get_kmeans ( xs , <NUM_LIT:6> ) <EOL> idx_ys = get_kmeans ( ys , <NUM_LIT:4> ) <EOL> for xi , yi , s in zip ( idx_xs , idx_ys , objects ) : <EOL> s . rank = ( yi , xi ) <EOL> objects . sort ( key = lambda x : x . rank ) <EOL> colormap = [ ] <EOL> for s in objects : <EOL> x , y = s . rank <EOL> observed , expected = s . rgb , rgb_to_triplet ( colorchecker [ x ] [ y ] ) <EOL> colormap . append ( ( np . array ( observed ) , np . array ( expected ) ) ) <EOL> tr0 = np . eye ( <NUM_LIT:3> ) . flatten ( ) <EOL> print >> sys . stderr , "<STR_LIT>" , total_error ( tr0 , colormap ) <EOL> tr = fmin ( total_error , tr0 , args = ( colormap , ) ) <EOL> tr . resize ( ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> print >> sys . stderr , "<STR_LIT>" , tr <EOL> calib = { "<STR_LIT>" : pixel_cm_ratio , <EOL> "<STR_LIT>" : tr . tolist ( ) } <EOL> jsonfile = op . join ( folder , "<STR_LIT>" ) <EOL> fw = must_open ( jsonfile , "<STR_LIT:w>" ) <EOL> print >> fw , json . dumps ( calib , indent = <NUM_LIT:4> ) <EOL> fw . close ( ) <EOL> logging . debug ( "<STR_LIT>" . format ( jsonfile ) ) <EOL> return jsonfile <EOL> def add_seeds_options ( p , args ) : <EOL> g1 = OptionGroup ( p , "<STR_LIT>" ) <EOL> g1 . add_option ( "<STR_LIT>" , default = <NUM_LIT:0> , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> g1 . add_option ( "<STR_LIT>" , default = '<STR_LIT::>' , <EOL> help = "<STR_LIT>" ) <EOL> g1 . add_option ( "<STR_LIT>" , default = '<STR_LIT::>' , <EOL> help = "<STR_LIT>" ) <EOL> g1 . add_option ( "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> g1 . add_option ( "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option_group ( g1 ) <EOL> g2 = OptionGroup ( p , "<STR_LIT>" ) <EOL> g2 . add_option ( "<STR_LIT>" , default = <NUM_LIT> , type = "<STR_LIT:float>" , <EOL> help = "<STR_LIT>" ) <EOL> g2 . add_option ( "<STR_LIT>" , default = <NUM_LIT:50> , type = "<STR_LIT:float>" , <EOL> help = "<STR_LIT>" ) <EOL> g2 . add_option ( "<STR_LIT>" , default = <NUM_LIT:100> , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> g2 . add_option ( "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option_group ( g2 ) <EOL> g3 = OptionGroup ( p , "<STR_LIT>" ) <EOL> valid_filters = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> g3 . add_option ( "<STR_LIT>" , default = "<STR_LIT>" , choices = valid_filters , <EOL> help = "<STR_LIT>" ) <EOL> g3 . add_option ( "<STR_LIT>" , default = <NUM_LIT:1> , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> g3 . add_option ( "<STR_LIT>" , default = <NUM_LIT:2> , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> g3 . add_option ( "<STR_LIT>" , default = <NUM_LIT:5> , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option_group ( g3 ) <EOL> g4 = OptionGroup ( p , "<STR_LIT>" ) <EOL> g4 . add_option ( "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> g4 . add_option ( "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> g4 . add_option ( "<STR_LIT>" , default = "<STR_LIT:.>" , <EOL> help = "<STR_LIT>" ) <EOL> g4 . add_option ( "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> g4 . add_option ( "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option_group ( g4 ) <EOL> opts , args , iopts = p . set_image_options ( args , figsize = '<STR_LIT>' , style = "<STR_LIT>" ) <EOL> return opts , args , iopts <EOL> def batchseeds ( args ) : <EOL> """<STR_LIT>""" <EOL> from jcvi . formats . pdf import cat <EOL> xargs = args [ <NUM_LIT:1> : ] <EOL> p = OptionParser ( batchseeds . __doc__ ) <EOL> opts , args , iopts = add_seeds_options ( p , args ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> folder , = args <EOL> folder = folder . rstrip ( '<STR_LIT:/>' ) <EOL> outdir = folder + "<STR_LIT>" <EOL> outfile = folder + "<STR_LIT>" <EOL> assert op . isdir ( folder ) <EOL> images = [ ] <EOL> jsonfile = opts . calibrate or op . join ( folder , "<STR_LIT>" ) <EOL> if not op . exists ( jsonfile ) : <EOL> jsonfile = None <EOL> for im in iglob ( folder , "<STR_LIT>" ) : <EOL> if im . endswith ( "<STR_LIT>" ) or im . endswith ( "<STR_LIT>" ) or im . endswith ( "<STR_LIT>" ) : <EOL> continue <EOL> if op . basename ( im ) . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> images . append ( im ) <EOL> fw = must_open ( outfile , '<STR_LIT:w>' ) <EOL> print >> fw , Seed . header ( calibrate = jsonfile ) <EOL> nseeds = <NUM_LIT:0> <EOL> for im in images : <EOL> imargs = [ im , "<STR_LIT>" , "<STR_LIT>" . format ( outdir ) ] + xargs <EOL> if jsonfile : <EOL> imargs += [ "<STR_LIT>" . format ( jsonfile ) ] <EOL> objects = seeds ( imargs ) <EOL> for o in objects : <EOL> print >> fw , o <EOL> nseeds += len ( objects ) <EOL> fw . close ( ) <EOL> logging . debug ( "<STR_LIT>" . format ( len ( images ) ) ) <EOL> logging . debug ( "<STR_LIT>" . format ( nseeds , outfile ) ) <EOL> pdfs = iglob ( outdir , "<STR_LIT>" ) <EOL> outpdf = folder + "<STR_LIT>" <EOL> cat ( pdfs + [ "<STR_LIT>" . format ( outpdf ) ] ) <EOL> logging . debug ( "<STR_LIT>" . format ( outpdf ) ) <EOL> return outfile <EOL> def p_round ( n , precision = <NUM_LIT:5> ) : <EOL> precision = int ( precision ) <EOL> return int ( round ( n / float ( precision ) ) ) * precision <EOL> def pixel_stats ( img ) : <EOL> img = [ ( p_round ( r ) , p_round ( g ) , p_round ( b ) ) for r , g , b in img ] <EOL> c = Counter ( img ) <EOL> imgx , count = c . most_common ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> return imgx <EOL> def slice ( s , m ) : <EOL> assert '<STR_LIT::>' in s <EOL> ra , rb = s . split ( '<STR_LIT::>' ) <EOL> ra = <NUM_LIT:0> if ra == '<STR_LIT>' else int ( ra ) <EOL> rb = m if rb == '<STR_LIT>' else int ( rb ) <EOL> return ra , rb <EOL> def convert_image ( pngfile , pf , outdir = "<STR_LIT:.>" , <EOL> resize = <NUM_LIT:1000> , format = "<STR_LIT>" , rotate = <NUM_LIT:0> , <EOL> rows = '<STR_LIT::>' , cols = '<STR_LIT::>' , labelrows = None , labelcols = None ) : <EOL> resizefile = op . join ( outdir , pf + "<STR_LIT>" ) <EOL> mainfile = op . join ( outdir , pf + "<STR_LIT>" ) <EOL> labelfile = op . join ( outdir , pf + "<STR_LIT>" ) <EOL> img = Image ( filename = pngfile ) <EOL> exif = dict ( ( k , v ) for k , v in img . metadata . items ( ) if k . startswith ( '<STR_LIT>' ) ) <EOL> if rotate : <EOL> img . rotate ( rotate ) <EOL> if resize : <EOL> w , h = img . size <EOL> if min ( w , h ) > resize : <EOL> if w < h : <EOL> nw , nh = resize , resize * h / w <EOL> else : <EOL> nw , nh = resize * w / h , resize <EOL> img . resize ( nw , nh ) <EOL> logging . debug ( "<STR_LIT>" . format ( pngfile , w , h , nw , nh ) ) <EOL> img . format = format <EOL> img . save ( filename = resizefile ) <EOL> rimg = img . clone ( ) <EOL> if rows != '<STR_LIT::>' or cols != '<STR_LIT::>' : <EOL> w , h = img . size <EOL> ra , rb = slice ( rows , h ) <EOL> ca , cb = slice ( cols , w ) <EOL> logging . debug ( "<STR_LIT>" . format ( ra , rb , ca , cb ) ) <EOL> img . crop ( ca , ra , cb , rb ) <EOL> img . format = format <EOL> img . save ( filename = mainfile ) <EOL> else : <EOL> mainfile = resizefile <EOL> if labelrows or labelcols : <EOL> w , h = rimg . size <EOL> if labelrows and not labelcols : <EOL> labelcols = '<STR_LIT::>' <EOL> if labelcols and not labelrows : <EOL> labelrows = '<STR_LIT::>' <EOL> ra , rb = slice ( labelrows , h ) <EOL> ca , cb = slice ( labelcols , w ) <EOL> logging . debug ( "<STR_LIT>" . format ( ra , rb , ca , cb ) ) <EOL> rimg . crop ( ca , ra , cb , rb ) <EOL> rimg . format = format <EOL> rimg . save ( filename = labelfile ) <EOL> else : <EOL> labelfile = None <EOL> return resizefile , mainfile , labelfile , exif <EOL> def extract_label ( labelfile ) : <EOL> accession = image_to_string ( iopen ( labelfile ) ) <EOL> accession = "<STR_LIT:U+0020>" . join ( accession . split ( ) ) <EOL> accession = "<STR_LIT>" . join ( x for x in accession if x in string . printable ) <EOL> if not accession : <EOL> accession = "<STR_LIT:none>" <EOL> return accession <EOL> def load_image ( resizefile ) : <EOL> img = plt . imread ( resizefile ) <EOL> h , w , c = img . shape <EOL> logging . debug ( "<STR_LIT>" . format ( resizefile , w , h ) ) <EOL> return img <EOL> def seeds ( args ) : <EOL> """<STR_LIT>""" <EOL> p = OptionParser ( seeds . __doc__ ) <EOL> p . set_outfile ( ) <EOL> opts , args , iopts = add_seeds_options ( p , args ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> pngfile , = args <EOL> pf = opts . prefix or op . basename ( pngfile ) . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> sigma , kernel = opts . sigma , opts . kernel <EOL> rows , cols = opts . rows , opts . cols <EOL> labelrows , labelcols = opts . labelrows , opts . labelcols <EOL> ff = opts . filter <EOL> calib = opts . calibrate <EOL> outdir = opts . outdir <EOL> if outdir != '<STR_LIT:.>' : <EOL> mkdir ( outdir ) <EOL> if calib : <EOL> calib = json . load ( must_open ( calib ) ) <EOL> pixel_cm_ratio , tr = calib [ "<STR_LIT>" ] , calib [ "<STR_LIT>" ] <EOL> tr = np . array ( tr ) <EOL> resizefile , mainfile , labelfile , exif = convert_image ( pngfile , pf , outdir = outdir , <EOL> rotate = opts . rotate , <EOL> rows = rows , cols = cols , <EOL> labelrows = labelrows , labelcols = labelcols ) <EOL> oimg = load_image ( resizefile ) <EOL> img = load_image ( mainfile ) <EOL> fig , ( ax1 , ax2 , ax3 , ax4 ) = plt . subplots ( ncols = <NUM_LIT:4> , nrows = <NUM_LIT:1> , <EOL> figsize = ( iopts . w , iopts . h ) ) <EOL> img_gray = rgb2gray ( img ) <EOL> logging . debug ( "<STR_LIT>" . format ( ff ) ) <EOL> if ff == "<STR_LIT>" : <EOL> edges = canny ( img_gray , sigma = opts . sigma ) <EOL> elif ff == "<STR_LIT>" : <EOL> edges = roberts ( img_gray ) <EOL> elif ff == "<STR_LIT>" : <EOL> edges = sobel ( img_gray ) <EOL> edges = clear_border ( edges , buffer_size = opts . border ) <EOL> selem = disk ( kernel ) <EOL> closed = closing ( edges , selem ) if kernel else edges <EOL> filled = binary_fill_holes ( closed ) <EOL> if opts . watershed : <EOL> distance = distance_transform_edt ( filled ) <EOL> local_maxi = peak_local_max ( distance , threshold_rel = <NUM_LIT> , indices = False ) <EOL> coordinates = peak_local_max ( distance , threshold_rel = <NUM_LIT> ) <EOL> markers , nmarkers = label ( local_maxi , return_num = True ) <EOL> logging . debug ( "<STR_LIT>" . format ( nmarkers ) ) <EOL> labels = watershed ( closed , markers , mask = filled ) <EOL> else : <EOL> labels = label ( filled ) <EOL> w , h = img_gray . shape <EOL> canvas_size = w * h <EOL> min_size = int ( round ( canvas_size * opts . minsize / <NUM_LIT:100> ) ) <EOL> max_size = int ( round ( canvas_size * opts . maxsize / <NUM_LIT:100> ) ) <EOL> logging . debug ( "<STR_LIT>" . format ( min_size , opts . minsize , max_size , opts . maxsize ) ) <EOL> ax1 . set_title ( '<STR_LIT>' ) <EOL> ax1 . imshow ( oimg ) <EOL> params = "<STR_LIT>" . format ( ff , sigma , kernel ) <EOL> if opts . watershed : <EOL> params += "<STR_LIT>" <EOL> ax2 . set_title ( '<STR_LIT>' . format ( params ) ) <EOL> closed = gray2rgb ( closed ) <EOL> ax2_img = labels <EOL> if opts . edges : <EOL> ax2_img = closed <EOL> elif opts . watershed : <EOL> ax2 . plot ( coordinates [ : , <NUM_LIT:1> ] , coordinates [ : , <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> ax2 . imshow ( ax2_img , cmap = iopts . cmap ) <EOL> ax3 . set_title ( '<STR_LIT>' ) <EOL> ax3 . imshow ( img ) <EOL> filename = op . basename ( pngfile ) <EOL> if labelfile : <EOL> accession = extract_label ( labelfile ) <EOL> else : <EOL> accession = pf <EOL> rp = regionprops ( labels ) <EOL> rp = [ x for x in rp if min_size <= x . area <= max_size ] <EOL> nb_labels = len ( rp ) <EOL> logging . debug ( "<STR_LIT>" . format ( nb_labels ) ) <EOL> objects = [ ] <EOL> for i , props in enumerate ( rp ) : <EOL> i += <NUM_LIT:1> <EOL> if i > opts . count : <EOL> break <EOL> y0 , x0 = props . centroid <EOL> orientation = props . orientation <EOL> major , minor = props . major_axis_length , props . minor_axis_length <EOL> major_dx = cos ( orientation ) * major / <NUM_LIT:2> <EOL> major_dy = sin ( orientation ) * major / <NUM_LIT:2> <EOL> minor_dx = sin ( orientation ) * minor / <NUM_LIT:2> <EOL> minor_dy = cos ( orientation ) * minor / <NUM_LIT:2> <EOL> ax2 . plot ( ( x0 - major_dx , x0 + major_dx ) , <EOL> ( y0 + major_dy , y0 - major_dy ) , '<STR_LIT>' ) <EOL> ax2 . plot ( ( x0 - minor_dx , x0 + minor_dx ) , <EOL> ( y0 - minor_dy , y0 + minor_dy ) , '<STR_LIT>' ) <EOL> npixels = int ( props . area ) <EOL> d = min ( int ( round ( minor / <NUM_LIT:2> * <NUM_LIT> ) ) + <NUM_LIT:1> , <NUM_LIT:50> ) <EOL> square = img [ ( y0 - d ) : ( y0 + d ) , ( x0 - d ) : ( x0 + d ) ] <EOL> pixels = [ ] <EOL> for row in square : <EOL> pixels . extend ( row ) <EOL> logging . debug ( "<STR_LIT>" . format ( i , npixels , len ( pixels ) , <NUM_LIT> * npixels / canvas_size ) ) <EOL> rgb = pixel_stats ( pixels ) <EOL> objects . append ( Seed ( filename , accession , i , rgb , props , exif ) ) <EOL> minr , minc , maxr , maxc = props . bbox <EOL> rect = Rectangle ( ( minc , minr ) , maxc - minc , maxr - minr , <EOL> fill = False , ec = '<STR_LIT:w>' , lw = <NUM_LIT:1> ) <EOL> ax3 . add_patch ( rect ) <EOL> mc , mr = ( minc + maxc ) / <NUM_LIT:2> , ( minr + maxr ) / <NUM_LIT:2> <EOL> ax3 . text ( mc , mr , "<STR_LIT>" . format ( i ) , color = '<STR_LIT:w>' , <EOL> ha = "<STR_LIT>" , va = "<STR_LIT>" , size = <NUM_LIT:6> ) <EOL> for ax in ( ax2 , ax3 ) : <EOL> ax . set_xlim ( <NUM_LIT:0> , h ) <EOL> ax . set_ylim ( w , <NUM_LIT:0> ) <EOL> ax4 . text ( <NUM_LIT> , <NUM_LIT> , "<STR_LIT>" . format ( latex ( filename ) ) , color = '<STR_LIT:g>' ) <EOL> ax4 . text ( <NUM_LIT> , <NUM_LIT> , "<STR_LIT>" . format ( latex ( accession ) ) , color = '<STR_LIT:m>' ) <EOL> yy = <NUM_LIT> <EOL> fw = must_open ( opts . outfile , "<STR_LIT:w>" ) <EOL> if not opts . noheader : <EOL> print >> fw , Seed . header ( calibrate = calib ) <EOL> for o in objects : <EOL> if calib : <EOL> o . calibrate ( pixel_cm_ratio , tr ) <EOL> print >> fw , o <EOL> i = o . seedno <EOL> if i > <NUM_LIT:7> : <EOL> continue <EOL> ax4 . text ( <NUM_LIT> , yy , str ( i ) , va = "<STR_LIT>" , bbox = dict ( fc = '<STR_LIT:none>' , ec = '<STR_LIT:k>' ) ) <EOL> ax4 . text ( <NUM_LIT> , yy , o . pixeltag , va = "<STR_LIT>" ) <EOL> yy -= <NUM_LIT> <EOL> ax4 . add_patch ( Rectangle ( ( <NUM_LIT> , yy - <NUM_LIT> ) , <NUM_LIT> , <NUM_LIT> , lw = <NUM_LIT:0> , <EOL> fc = rgb_to_hex ( o . rgb ) ) ) <EOL> ax4 . text ( <NUM_LIT> , yy , o . hashtag , va = "<STR_LIT>" ) <EOL> yy -= <NUM_LIT> <EOL> ax4 . text ( <NUM_LIT> , yy , "<STR_LIT>" . format ( nb_labels ) , <EOL> color = "<STR_LIT>" ) <EOL> normalize_axes ( ax4 ) <EOL> for ax in ( ax1 , ax2 , ax3 ) : <EOL> xticklabels = [ int ( x ) for x in ax . get_xticks ( ) ] <EOL> yticklabels = [ int ( x ) for x in ax . get_yticks ( ) ] <EOL> ax . set_xticklabels ( xticklabels , family = '<STR_LIT>' , size = <NUM_LIT:8> ) <EOL> ax . set_yticklabels ( yticklabels , family = '<STR_LIT>' , size = <NUM_LIT:8> ) <EOL> image_name = op . join ( outdir , pf + "<STR_LIT:.>" + iopts . format ) <EOL> savefig ( image_name , dpi = iopts . dpi , iopts = iopts ) <EOL> return objects <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
