<s> '''<STR_LIT>''' <EOL> import time <EOL> import os , sys <EOL> import csv <EOL> import itertools <EOL> import traceback <EOL> import subprocess <EOL> from optparse import OptionParser <EOL> import utilities <EOL> from osgeo import gdalconst <EOL> from osgeo import gdal <EOL> from osgeo import osr <EOL> class MAXENTRunner ( object ) : <EOL> def __init__ ( self ) : <EOL> self . verbose = False <EOL> self . maxentpath = '<STR_LIT>' <EOL> self . inputMDS = '<STR_LIT>' <EOL> self . projectionlayers = '<STR_LIT>' <EOL> self . testCSV = '<STR_LIT>' <EOL> self . trainingCSV = '<STR_LIT>' <EOL> self . backgroundCSV = '<STR_LIT>' <EOL> self . outputDir = '<STR_LIT>' <EOL> self . categoricals = [ ] <EOL> self . argsCSV = '<STR_LIT>' <EOL> self . logger = None <EOL> def run ( self ) : <EOL> self . loadArgs ( ) <EOL> self . args [ '<STR_LIT>' ] = self . outputDir <EOL> self . validateInputs ( ) <EOL> if self . inputMDS < > '<STR_LIT>' : <EOL> self . prepInputs ( ) <EOL> else : <EOL> raise Exception , "<STR_LIT>" <EOL> if not self . args . has_key ( '<STR_LIT>' ) : <EOL> self . args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if self . trainingCSV < > '<STR_LIT>' : <EOL> self . args [ '<STR_LIT>' ] = self . trainingCSV <EOL> else : <EOL> raise Exception , "<STR_LIT>" <EOL> if self . testCSV < > '<STR_LIT>' : <EOL> self . args [ '<STR_LIT>' ] = self . testCSV <EOL> if self . backgroundCSV < > '<STR_LIT>' : <EOL> self . args [ '<STR_LIT>' ] = self . backgroundCSV <EOL> self . args [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> if '<STR_LIT:U+0020>' in self . args [ '<STR_LIT>' ] : <EOL> self . args [ '<STR_LIT>' ] = self . args [ '<STR_LIT>' ] . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> strargs = [ '<STR_LIT:=>' . join ( ( str ( k ) , str ( v ) ) ) for k , v in self . args . iteritems ( ) if k < > "<STR_LIT>" ] <EOL> for categorical in self . categoricals : <EOL> strargs += [ '<STR_LIT>' + categorical . replace ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> if not self . maxentpath . endswith ( '<STR_LIT>' ) : <EOL> jar = os . path . join ( self . maxentpath , '<STR_LIT>' ) <EOL> else : <EOL> jar = self . maxentpath <EOL> self . run_cmd_line_jar ( jar , strargs ) <EOL> def run_cmd_line_jar ( self , jar_name , args ) : <EOL> cmd = '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , jar_name ] + args ) <EOL> self . writetolog ( '<STR_LIT>' + cmd , True , False ) <EOL> p = subprocess . Popen ( cmd , shell = True , stdout = subprocess . PIPE , stderr = subprocess . STDOUT ) <EOL> self . writetolog ( '<STR_LIT>' , True , False ) <EOL> ret = p . communicate ( ) <EOL> self . writetolog ( '<STR_LIT>' + str ( ret [ <NUM_LIT:0> ] ) ) <EOL> if ret [ <NUM_LIT:1> ] is not None : <EOL> msg = "<STR_LIT>" <EOL> msg += ret [ <NUM_LIT:1> ] <EOL> writetolog ( msg ) <EOL> raise RuntimeError , msg <EOL> del ret <EOL> def loadArgs ( self ) : <EOL> argsReader = csv . reader ( open ( self . argsCSV , '<STR_LIT:r>' ) ) <EOL> header = argsReader . next ( ) <EOL> self . args = { } <EOL> for row in argsReader : <EOL> self . args [ row [ <NUM_LIT:0> ] ] = row [ <NUM_LIT:1> ] <EOL> def validateInputs ( self ) : <EOL> if not os . path . exists ( self . argsCSV ) : <EOL> raise RuntimeError ( self , '<STR_LIT>' + self . argsCSV + '<STR_LIT>' ) <EOL> if not os . path . exists ( self . inputMDS ) : <EOL> raise RuntimeError ( self , '<STR_LIT>' + self . inputMDS + '<STR_LIT>' ) <EOL> if not self . args . has_key ( '<STR_LIT>' ) : <EOL> self . args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if self . args [ '<STR_LIT>' ] < > '<STR_LIT>' : <EOL> dirs = self . args [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> for dir in dirs : <EOL> if not os . path . isdir ( dir ) : <EOL> raise RuntimeError ( self , "<STR_LIT>" ) <EOL> if not utilities . isMDSFile ( self . inputMDS ) : <EOL> raise RuntimeError ( self , '<STR_LIT>' + self . inputMDS + '<STR_LIT>' ) <EOL> if not os . path . exists ( self . outputDir ) : <EOL> raise RuntimeError ( self , '<STR_LIT>' + self . outputDir + '<STR_LIT>' ) <EOL> if self . logger is None : <EOL> self . logger = utilities . logger ( outDir , self . verbose ) <EOL> self . writetolog = self . logger . writetolog <EOL> def prepInputs ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . testCSV = os . path . join ( self . outputDir , '<STR_LIT>' ) <EOL> self . trainingCSV = os . path . join ( self . outputDir , '<STR_LIT>' ) <EOL> self . backgroundCSV = os . path . join ( self . outputDir , '<STR_LIT>' ) <EOL> testWriter = csv . writer ( open ( self . testCSV , '<STR_LIT:wb>' ) ) <EOL> trainingWriter = csv . writer ( open ( self . trainingCSV , '<STR_LIT:wb>' ) ) <EOL> backgroundWriter = csv . writer ( open ( self . backgroundCSV , '<STR_LIT:wb>' ) ) <EOL> MDSreader = csv . reader ( open ( self . inputMDS , '<STR_LIT:r>' ) ) <EOL> header1 = MDSreader . next ( ) <EOL> header2 = MDSreader . next ( ) <EOL> header3 = MDSreader . next ( ) <EOL> self . pullCategoricals ( header1 ) <EOL> splitcol = None <EOL> try : <EOL> splitcol = header1 . index ( '<STR_LIT>' ) <EOL> deleteTest = False <EOL> except ValueError : <EOL> self . writetolog ( "<STR_LIT>" ) <EOL> deleteTest = True <EOL> covariateIndexes = self . usedIndexes ( header1 , header2 ) <EOL> covariateNames = self . usedValues ( header1 , covariateIndexes ) <EOL> covariateNamesClean = [ name . replace ( '<STR_LIT>' , '<STR_LIT>' ) for name in covariateNames ] <EOL> usedCovariateFiles = self . usedValues ( header3 , covariateIndexes ) <EOL> self . writetolog ( '<STR_LIT>' + "<STR_LIT:U+002CU+0020>" . join ( covariateNames ) , False , False ) <EOL> testWriter . writerow ( [ '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT:y>' ] + covariateNamesClean ) <EOL> trainingWriter . writerow ( [ '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT:y>' ] + covariateNamesClean ) <EOL> backgroundWriter . writerow ( [ '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT:y>' ] + covariateNamesClean ) <EOL> hasBackground = False <EOL> for row in MDSreader : <EOL> if row [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> hasBackground = True <EOL> vals = self . usedValues ( row , covariateIndexes ) <EOL> backgroundWriter . writerow ( [ '<STR_LIT>' ] + row [ : <NUM_LIT:2> ] + vals ) <EOL> elif splitcol is None and row [ <NUM_LIT:2> ] < > <NUM_LIT:0> : <EOL> vals = self . usedValues ( row , covariateIndexes ) <EOL> trainingWriter . writerow ( [ self . args [ '<STR_LIT>' ] ] + row [ : <NUM_LIT:2> ] + vals ) <EOL> elif ( row [ splitcol ] == '<STR_LIT:test>' and row [ <NUM_LIT:2> ] < > <NUM_LIT:0> ) or self . testCSV == '<STR_LIT>' : <EOL> vals = self . usedValues ( row , covariateIndexes ) <EOL> testWriter . writerow ( [ self . args [ '<STR_LIT>' ] ] + row [ : <NUM_LIT:2> ] + vals ) <EOL> elif row [ splitcol ] == '<STR_LIT:train>' and row [ <NUM_LIT:2> ] < > <NUM_LIT:0> : <EOL> vals = self . usedValues ( row , covariateIndexes ) <EOL> trainingWriter . writerow ( [ self . args [ '<STR_LIT>' ] ] + row [ : <NUM_LIT:2> ] + vals ) <EOL> if not hasBackground : <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" <EOL> msg += "<STR_LIT>" <EOL> self . writetolog ( msg ) <EOL> raise RuntimeError ( msg ) <EOL> try : <EOL> del testWriter <EOL> if deleteTest : <EOL> os . remove ( self . testCSV ) <EOL> self . testCSV = '<STR_LIT>' <EOL> del backgroundWriter <EOL> if not hasBackground : <EOL> os . remove ( self . backgroundCSV ) <EOL> self . backgroundCSV = '<STR_LIT>' <EOL> del trainingWriter <EOL> except : <EOL> print '<STR_LIT:U+0020>' . join ( [ str ( i ) for i in sys . exc_info ( ) [ : <NUM_LIT:2> ] ] ) <EOL> pass <EOL> if self . args [ '<STR_LIT>' ] < > '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> self . args [ '<STR_LIT>' ] = '<STR_LIT:false>' <EOL> def usedIndexes ( self , header1 , header2 ) : <EOL> covariateIndexes = [ ] <EOL> for i in range ( len ( header1 ) ) : <EOL> if header2 [ i ] == '<STR_LIT:1>' and header1 [ i ] < > '<STR_LIT>' : <EOL> covariateIndexes . append ( i ) <EOL> return covariateIndexes <EOL> def usedValues ( self , values , indexes ) : <EOL> usedvals = [ ] <EOL> for i in indexes : <EOL> usedvals . append ( values [ i ] ) <EOL> return usedvals <EOL> def pullCategoricals ( self , headerline ) : <EOL> for item in headerline : <EOL> if item . endswith ( '<STR_LIT>' ) : <EOL> self . categoricals . append ( item ) <EOL> def isSWD ( self , file ) : <EOL> '''<STR_LIT>''' <EOL> if os . path . exists ( file ) : <EOL> reader = csv . reader ( open ( file , '<STR_LIT:r>' ) ) <EOL> header = reader . next ( ) <EOL> if header [ <NUM_LIT:0> ] . lower ( ) in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return True <EOL> return False <EOL> def main ( argv ) : <EOL> '''<STR_LIT>''' <EOL> usageStmt = "<STR_LIT>" <EOL> desc = "<STR_LIT>" <EOL> parser = OptionParser ( usage = usageStmt , description = desc ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , <EOL> default = False , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( argv ) <EOL> ourMaxent = MAXENTRunner ( ) <EOL> ourMaxent . verbose = options . verbose <EOL> ourMaxent . maxentpath = options . maxentExecutable <EOL> ourMaxent . inputMDS = options . MDSFile <EOL> ourMaxent . outputDir = options . outputDir <EOL> ourMaxent . argsCSV = options . argsCSV <EOL> ourMaxent . projectionDataFile = options . projectionData <EOL> utilities . createsessionlog ( options . outputDir , options . verbose ) <EOL> ourMaxent . run ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv [ <NUM_LIT:1> : ] ) ) </s>
