<s> from __future__ import absolute_import <EOL> import os . path <EOL> import re <EOL> import sys <EOL> import digits <EOL> from digits . task import Task <EOL> from digits . utils import subclass , override <EOL> PICKLE_VERSION = <NUM_LIT:1> <EOL> @ subclass <EOL> class AnalyzeDbTask ( Task ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , database , purpose , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . force_same_shape = kwargs . pop ( '<STR_LIT>' , False ) <EOL> super ( AnalyzeDbTask , self ) . __init__ ( ** kwargs ) <EOL> self . pickver_task_analyzedb = PICKLE_VERSION <EOL> self . database = database <EOL> self . purpose = purpose <EOL> self . backend = '<STR_LIT>' <EOL> self . image_count = None <EOL> self . image_width = None <EOL> self . image_height = None <EOL> self . image_channels = None <EOL> self . analyze_db_log_file = '<STR_LIT>' % '<STR_LIT:->' . join ( p . lower ( ) for p in self . purpose . split ( ) ) <EOL> def __getstate__ ( self ) : <EOL> state = super ( AnalyzeDbTask , self ) . __getstate__ ( ) <EOL> if '<STR_LIT>' in state : <EOL> del state [ '<STR_LIT>' ] <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> super ( AnalyzeDbTask , self ) . __setstate__ ( state ) <EOL> if not hasattr ( self , '<STR_LIT>' ) or self . backend is None : <EOL> self . backend = '<STR_LIT>' <EOL> @ override <EOL> def name ( self ) : <EOL> return '<STR_LIT>' % ( self . purpose ) <EOL> @ override <EOL> def html_id ( self ) : <EOL> return '<STR_LIT>' % '<STR_LIT:->' . join ( p . lower ( ) for p in self . purpose . split ( ) ) <EOL> @ override <EOL> def offer_resources ( self , resources ) : <EOL> key = '<STR_LIT>' <EOL> if key not in resources : <EOL> return None <EOL> for resource in resources [ key ] : <EOL> if resource . remaining ( ) >= <NUM_LIT:1> : <EOL> return { key : [ ( resource . identifier , <NUM_LIT:1> ) ] } <EOL> return None <EOL> @ override <EOL> def task_arguments ( self , resources , env ) : <EOL> args = [ sys . executable , os . path . join ( <EOL> os . path . dirname ( os . path . dirname ( os . path . abspath ( digits . __file__ ) ) ) , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> self . database , <EOL> ] <EOL> if self . force_same_shape : <EOL> args . append ( '<STR_LIT>' ) <EOL> else : <EOL> args . append ( '<STR_LIT>' ) <EOL> return args <EOL> @ override <EOL> def before_run ( self ) : <EOL> super ( AnalyzeDbTask , self ) . before_run ( ) <EOL> self . analyze_db_log = open ( self . path ( self . analyze_db_log_file ) , '<STR_LIT:a>' ) <EOL> @ override <EOL> def process_output ( self , line ) : <EOL> self . analyze_db_log . write ( '<STR_LIT>' % line ) <EOL> self . analyze_db_log . flush ( ) <EOL> timestamp , level , message = self . preprocess_output_digits ( line ) <EOL> if not message : <EOL> return False <EOL> match = re . match ( r'<STR_LIT>' , message ) <EOL> if match : <EOL> self . progress = float ( match . group ( <NUM_LIT:1> ) ) / float ( match . group ( <NUM_LIT:2> ) ) <EOL> self . emit_progress_update ( ) <EOL> return True <EOL> match = re . match ( r'<STR_LIT>' , message ) <EOL> if match : <EOL> self . image_count = int ( match . group ( <NUM_LIT:1> ) ) <EOL> return True <EOL> match = re . match ( r'<STR_LIT>' , message ) <EOL> if match : <EOL> count = int ( match . group ( <NUM_LIT:1> ) ) <EOL> dims = match . group ( <NUM_LIT:2> ) <EOL> self . image_width = int ( match . group ( <NUM_LIT:3> ) ) <EOL> self . image_height = int ( match . group ( <NUM_LIT:4> ) ) <EOL> self . image_channels = int ( match . group ( <NUM_LIT:5> ) ) <EOL> self . logger . debug ( '<STR_LIT>' % dims ) <EOL> return True <EOL> if level == '<STR_LIT>' : <EOL> self . logger . warning ( '<STR_LIT>' % ( self . name ( ) , message ) ) <EOL> return True <EOL> if level in [ '<STR_LIT:error>' , '<STR_LIT>' ] : <EOL> self . logger . error ( '<STR_LIT>' % ( self . name ( ) , message ) ) <EOL> self . exception = message <EOL> return True <EOL> return True <EOL> @ override <EOL> def after_run ( self ) : <EOL> super ( AnalyzeDbTask , self ) . after_run ( ) <EOL> self . analyze_db_log . close ( ) <EOL> def image_type ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . image_channels is None : <EOL> return None <EOL> elif self . image_channels == <NUM_LIT:1> : <EOL> return '<STR_LIT>' <EOL> elif self . image_channels == <NUM_LIT:3> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' % self . image_channels </s>
