idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
24,100
def real ( self ) : result = self . __raw [ '1' ] . copy ( ) result [ 'c' ] = self . __raw [ '1' ] [ 'value' ] result [ 'value' ] = self . __raw [ '200' ] [ 'v2' ] result [ 'date' ] = self . __raw [ '0' ] [ 'time' ] return result
Get realtime data
24,101
def importcsv ( self ) : csv_path = os . path . join ( os . path . dirname ( __file__ ) , self . stock_no_files ) with open ( csv_path ) as csv_file : csv_data = csv . reader ( csv_file ) result = { } for i in csv_data : try : result [ i [ 0 ] ] = str ( i [ 1 ] ) . decode ( 'utf-8' ) except ValueError : if i [ 0 ] == '...
import data from csv
24,102
def __loadindcomps ( self ) : csv_path = os . path . join ( os . path . dirname ( __file__ ) , self . stock_no_files ) with open ( csv_path ) as csv_file : csv_data = csv . reader ( csv_file ) result = { } check_words = re . compile ( r'^[\d]{2,}[\w]?' ) for i in csv_data : if check_words . match ( i [ 2 ] ) : try : re...
import industry comps
24,103
def caldata ( self , time ) : if time . date ( ) in self . __ocdate [ 'close' ] : return False elif time . date ( ) in self . __ocdate [ 'open' ] : return True else : if time . weekday ( ) <= 4 : return True else : return False
Market open or not .
24,104
def flaky ( max_runs = None , min_passes = None , rerun_filter = None ) : wrapped = None if hasattr ( max_runs , '__call__' ) : wrapped , max_runs = max_runs , None attrib = default_flaky_attributes ( max_runs , min_passes , rerun_filter ) def wrapper ( wrapped_object ) : for name , value in attrib . items ( ) : setatt...
Decorator used to mark a test as flaky . When used in conjuction with the flaky nosetests plugin will cause the decorated test to be retried until min_passes successes are achieved out of up to max_runs test runs .
24,105
def options ( self , parser , env = os . environ ) : super ( FlakyPlugin , self ) . options ( parser , env = env ) self . add_report_option ( parser . add_option ) group = OptionGroup ( parser , "Force flaky" , "Force all tests to be flaky." ) self . add_force_flaky_options ( group . add_option ) parser . add_option_gr...
Base class override . Add options to the nose argument parser .
24,106
def _get_stream ( self , multiprocess = False ) : if multiprocess : from flaky . multiprocess_string_io import MultiprocessingStringIO return MultiprocessingStringIO ( ) return self . _stream
Get the stream used to store the flaky report . If this nose run is going to use the multiprocess plugin then use a multiprocess - list backed StringIO proxy ; otherwise use the default stream .
24,107
def configure ( self , options , conf ) : super ( FlakyPlugin , self ) . configure ( options , conf ) if not self . enabled : return is_multiprocess = int ( getattr ( options , 'multiprocess_workers' , 0 ) ) > 0 self . _stream = self . _get_stream ( is_multiprocess ) self . _flaky_result = TextTestResult ( self . _stre...
Base class override .
24,108
def handleError ( self , test , err ) : want_error = self . _handle_test_error_or_failure ( test , err ) if not want_error and id ( test ) in self . _tests_that_reran : self . _nose_result . addError ( test , err ) return want_error or None
Baseclass override . Called when a test raises an exception .
24,109
def handleFailure ( self , test , err ) : want_failure = self . _handle_test_error_or_failure ( test , err ) if not want_failure and id ( test ) in self . _tests_that_reran : self . _nose_result . addFailure ( test , err ) return want_failure or None
Baseclass override . Called when a test fails .
24,110
def addSuccess ( self , test ) : will_handle = self . _handle_test_success ( test ) test_id = id ( test ) if will_handle and test_id not in self . _tests_that_reran : self . _tests_that_have_been_reported . add ( test_id ) if not will_handle and test_id in self . _tests_that_reran and test_id not in self . _tests_that_...
Baseclass override . Called when a test succeeds .
24,111
def default_flaky_attributes ( max_runs = None , min_passes = None , rerun_filter = None ) : if max_runs is None : max_runs = 2 if min_passes is None : min_passes = 1 if min_passes <= 0 : raise ValueError ( 'min_passes must be positive' ) if max_runs < min_passes : raise ValueError ( 'min_passes cannot be greater than ...
Returns the default flaky attributes to set on a flaky test .
24,112
def ensure_unicode_string ( obj ) : try : return unicode_type ( obj ) except UnicodeDecodeError : if hasattr ( obj , 'decode' ) : return obj . decode ( 'utf-8' , 'replace' ) return str ( obj ) . decode ( 'utf-8' , 'replace' )
Return a unicode string representation of the given obj .
24,113
def _report_final_failure ( self , err , flaky , name ) : min_passes = flaky [ FlakyNames . MIN_PASSES ] current_passes = flaky [ FlakyNames . CURRENT_PASSES ] message = self . _failure_message . format ( current_passes , min_passes , ) self . _log_test_failure ( name , err , message )
Report that the test has failed too many times to pass at least min_passes times .
24,114
def _log_intermediate_failure ( self , err , flaky , name ) : max_runs = flaky [ FlakyNames . MAX_RUNS ] runs_left = max_runs - flaky [ FlakyNames . CURRENT_RUNS ] message = self . _retry_failure_message . format ( runs_left , max_runs , ) self . _log_test_failure ( name , err , message )
Report that the test has failed but still has reruns left . Then rerun the test .
24,115
def add_report_option ( add_option ) : add_option ( '--no-flaky-report' , action = 'store_false' , dest = 'flaky_report' , default = True , help = "Suppress the report at the end of the " "run detailing flaky test results." , ) add_option ( '--no-success-flaky-report' , action = 'store_false' , dest = 'flaky_success_re...
Add an option to the test runner to suppress the flaky report .
24,116
def add_force_flaky_options ( add_option ) : add_option ( '--force-flaky' , action = "store_true" , dest = "force_flaky" , default = False , help = "If this option is specified, we will treat all tests as " "flaky." ) add_option ( '--max-runs' , action = "store" , dest = "max_runs" , type = int , default = 2 , help = "...
Add options to the test runner that force all tests to be flaky .
24,117
def _add_flaky_report ( self , stream ) : value = self . _stream . getvalue ( ) if not self . _flaky_success_report and not value : return stream . write ( '===Flaky Test Report===\n\n' ) try : stream . write ( value ) except UnicodeEncodeError : stream . write ( value . encode ( 'utf-8' , 'replace' ) ) stream . write ...
Baseclass override . Write details about flaky tests to the test report .
24,118
def _copy_flaky_attributes ( cls , test , test_class ) : test_callable = cls . _get_test_callable ( test ) if test_callable is None : return for attr , value in cls . _get_flaky_attributes ( test_class ) . items ( ) : already_set = hasattr ( test , attr ) if already_set : continue attr_on_callable = getattr ( test_call...
Copy flaky attributes from the test callable or class to the test .
24,119
def _increment_flaky_attribute ( cls , test_item , flaky_attribute ) : cls . _set_flaky_attribute ( test_item , flaky_attribute , cls . _get_flaky_attribute ( test_item , flaky_attribute ) + 1 )
Increments the value of an attribute on a flaky test .
24,120
def _has_flaky_attributes ( cls , test ) : current_runs = cls . _get_flaky_attribute ( test , FlakyNames . CURRENT_RUNS ) return current_runs is not None
Returns True if the test callable in question is marked as flaky .
24,121
def _get_flaky_attributes ( cls , test_item ) : return { attr : cls . _get_flaky_attribute ( test_item , attr , ) for attr in FlakyNames ( ) }
Get all the flaky related attributes from the test .
24,122
def connection_ok ( ) : try : urlopen ( Dataset . base_url , timeout = 1 ) return True except HTTPError : return True except URLError : return False
Check web connection . Returns True if web connection is OK False otherwise .
24,123
def _download_datasets ( ) : def filepath ( * args ) : return abspath ( join ( dirname ( __file__ ) , '..' , 'vega_datasets' , * args ) ) dataset_listing = { } for name in DATASETS_TO_DOWNLOAD : data = Dataset ( name ) url = data . url filename = filepath ( '_data' , data . filename ) print ( "retrieving data {0} -> {1...
Utility to download datasets into package source
24,124
def init ( cls , name ) : clsdict = { subcls . name : subcls for subcls in cls . __subclasses__ ( ) if hasattr ( subcls , 'name' ) } return clsdict . get ( name , cls ) ( name )
Return an instance of this class or an appropriate subclass
24,125
def _infodict ( cls , name ) : info = cls . _dataset_info . get ( name , None ) if info is None : raise ValueError ( 'No such dataset {0} exists, ' 'use list_datasets() to get a list ' 'of available datasets.' . format ( name ) ) return info
load the info dictionary for the given name
24,126
def raw ( self , use_local = True ) : if use_local and self . is_local : return pkgutil . get_data ( 'vega_datasets' , self . pkg_filename ) else : return urlopen ( self . url ) . read ( )
Load the raw dataset from remote URL or local file
24,127
def run ( self ) : chromedriver_dir = os . path . join ( os . path . abspath ( os . path . dirname ( __file__ ) ) , 'chromedriver_binary' ) chromedriver_filename = find_binary_in_path ( get_chromedriver_filename ( ) ) if chromedriver_filename : print ( "\nChromedriver already installed at {}...\n" . format ( chromedriv...
Downloads unzips and installs chromedriver . If a chromedriver binary is found in PATH it will be copied otherwise downloaded .
24,128
def add_chromedriver_to_path ( ) : chromedriver_dir = os . path . abspath ( os . path . dirname ( __file__ ) ) if 'PATH' not in os . environ : os . environ [ 'PATH' ] = chromedriver_dir elif chromedriver_dir not in os . environ [ 'PATH' ] : os . environ [ 'PATH' ] += utils . get_variable_separator ( ) + chromedriver_di...
Appends the directory of the chromedriver binary file to PATH .
24,129
async def _remote_close ( self , exc = None ) : if self . state in ( STATE_CLOSING , STATE_CLOSED ) : return log . info ( "close session: %s" , self . id ) self . state = STATE_CLOSING if exc is not None : self . exception = exc self . interrupted = True try : await self . handler ( SockjsMessage ( MSG_CLOSE , exc ) , ...
close session from remote .
24,130
def send ( self , msg ) : assert isinstance ( msg , str ) , "String is required" if self . _debug : log . info ( "outgoing message: %s, %s" , self . id , str ( msg ) [ : 200 ] ) if self . state != STATE_OPEN : return self . _feed ( FRAME_MESSAGE , msg )
send message to client .
24,131
def send_frame ( self , frm ) : if self . _debug : log . info ( "outgoing message: %s, %s" , self . id , frm [ : 200 ] ) if self . state != STATE_OPEN : return self . _feed ( FRAME_MESSAGE_BLOB , frm )
send message frame to client .
24,132
async def clear ( self ) : for session in list ( self . values ( ) ) : if session . state != STATE_CLOSED : await session . _remote_closed ( ) self . sessions . clear ( ) super ( SessionManager , self ) . clear ( )
Manually expire all sessions in the pool .
24,133
def new ( cls , alias , cert ) : timestamp = int ( time . time ( ) ) * 1000 tke = cls ( timestamp = timestamp , alias = alias . lower ( ) , cert = cert ) return tke
Helper function to create a new TrustedCertEntry .
24,134
def new ( cls , alias , certs , key , key_format = 'pkcs8' ) : timestamp = int ( time . time ( ) ) * 1000 cert_chain = [ ] for cert in certs : cert_chain . append ( ( 'X.509' , cert ) ) pke = cls ( timestamp = timestamp , alias = alias . lower ( ) , cert_chain = cert_chain ) if key_format == 'pkcs8' : private_key_info ...
Helper function to create a new PrivateKeyEntry .
24,135
def encrypt ( self , key_password ) : if not self . is_decrypted ( ) : return encrypted_private_key = sun_crypto . jks_pkey_encrypt ( self . pkey_pkcs8 , key_password ) a = AlgorithmIdentifier ( ) a . setComponentByName ( 'algorithm' , sun_crypto . SUN_JKS_ALGO_ID ) a . setComponentByName ( 'parameters' , '\x05\x00' ) ...
Encrypts the private key so that it can be saved to a keystore .
24,136
def new ( cls , alias , sealed_obj , algorithm , key , key_size ) : timestamp = int ( time . time ( ) ) * 1000 raise NotImplementedError ( "Creating Secret Keys not implemented" )
Helper function to create a new SecretKeyEntry .
24,137
def new ( cls , store_type , store_entries ) : if store_type not in [ 'jks' , 'jceks' ] : raise UnsupportedKeystoreTypeException ( "The Keystore Type '%s' is not supported" % store_type ) entries = { } for entry in store_entries : if not isinstance ( entry , AbstractKeystoreEntry ) : raise UnsupportedKeystoreEntryTypeE...
Helper function to create a new KeyStore .
24,138
def saves ( self , store_password ) : if self . store_type == 'jks' : keystore = MAGIC_NUMBER_JKS elif self . store_type == 'jceks' : raise NotImplementedError ( "Saving of JCEKS keystores is not implemented" ) else : raise UnsupportedKeystoreTypeException ( "Only JKS and JCEKS keystores are supported" ) keystore += b4...
Saves the keystore so that it can be read by other applications .
24,139
def _java_is_subclass ( cls , obj , class_name ) : clazz = obj . get_class ( ) while clazz : if clazz . name == class_name : return True clazz = clazz . superclass return False
Given a deserialized JavaObject as returned by the javaobj library determine whether it s a subclass of the given class name .
24,140
def _adjust ( a , a_offset , b ) : x = ( b [ - 1 ] & 0xFF ) + ( a [ a_offset + len ( b ) - 1 ] & 0xFF ) + 1 a [ a_offset + len ( b ) - 1 ] = ctypes . c_ubyte ( x ) . value x >>= 8 for i in range ( len ( b ) - 2 , - 1 , - 1 ) : x += ( b [ i ] & 0xFF ) + ( a [ a_offset + i ] & 0xFF ) a [ a_offset + i ] = ctypes . c_ubyte...
a = bytearray a_offset = int b = bytearray
24,141
def jks_pkey_encrypt ( key , password_str ) : password_bytes = password_str . encode ( 'utf-16be' ) iv = os . urandom ( 20 ) key = bytearray ( key ) xoring = zip ( key , _jks_keystream ( iv , password_bytes ) ) data = bytearray ( [ d ^ k for d , k in xoring ] ) check = hashlib . sha1 ( bytes ( password_bytes + key ) ) ...
Encrypts the private key with password protection algorithm used by JKS keystores .
24,142
def _jks_keystream ( iv , password ) : cur = iv while 1 : xhash = hashlib . sha1 ( bytes ( password + cur ) ) cur = bytearray ( xhash . digest ( ) ) for byte in cur : yield byte
Helper keystream generator for _jks_pkey_decrypt
24,143
def bitstring_to_bytes ( bitstr ) : bitlist = list ( bitstr ) bits_missing = ( 8 - len ( bitlist ) % 8 ) % 8 bitlist = [ 0 ] * bits_missing + bitlist result = bytearray ( ) for i in range ( 0 , len ( bitlist ) , 8 ) : byte = 0 for j in range ( 8 ) : byte = ( byte << 1 ) | bitlist [ i + j ] result . append ( byte ) retu...
Converts a pyasn1 univ . BitString instance to byte sequence of type bytes . The bit string is interpreted big - endian and is left - padded with 0 bits to form a multiple of 8 .
24,144
def _read_bks_key ( cls , data , pos , store_type ) : key_type = b1 . unpack_from ( data , pos ) [ 0 ] pos += 1 key_format , pos = BksKeyStore . _read_utf ( data , pos , kind = "key format" ) key_algorithm , pos = BksKeyStore . _read_utf ( data , pos , kind = "key algorithm" ) key_enc , pos = BksKeyStore . _read_data (...
Given a data stream attempt to parse a stored BKS key entry at the given position and return it as a BksKeyEntry .
24,145
def calc_columns_rows ( n ) : num_columns = int ( ceil ( sqrt ( n ) ) ) num_rows = int ( ceil ( n / float ( num_columns ) ) ) return ( num_columns , num_rows )
Calculate the number of columns and rows required to divide an image into n parts .
24,146
def get_combined_size ( tiles ) : columns , rows = calc_columns_rows ( len ( tiles ) ) tile_size = tiles [ 0 ] . image . size return ( tile_size [ 0 ] * columns , tile_size [ 1 ] * rows )
Calculate combined size of tiles .
24,147
def validate_image ( image , number_tiles ) : TILE_LIMIT = 99 * 99 try : number_tiles = int ( number_tiles ) except : raise ValueError ( 'number_tiles could not be cast to integer.' ) if number_tiles > TILE_LIMIT or number_tiles < 2 : raise ValueError ( 'Number of tiles must be between 2 and {} (you \ ...
Basic sanity checks prior to performing a split .
24,148
def validate_image_col_row ( image , col , row ) : SPLIT_LIMIT = 99 try : col = int ( col ) row = int ( row ) except : raise ValueError ( 'columns and rows values could not be cast to integer.' ) if col < 2 : raise ValueError ( 'Number of columns must be between 2 and {} (you \ asked for {}).' ...
Basic checks for columns and rows values
24,149
def slice ( filename , number_tiles = None , col = None , row = None , save = True ) : im = Image . open ( filename ) im_w , im_h = im . size columns = 0 rows = 0 if not number_tiles is None : validate_image ( im , number_tiles ) columns , rows = calc_columns_rows ( number_tiles ) extras = ( columns * rows ) - number_t...
Split an image into a specified number of tiles .
24,150
def generate_filename ( self , directory = os . getcwd ( ) , prefix = 'tile' , format = 'png' , path = True ) : filename = prefix + '_{col:02d}_{row:02d}.{ext}' . format ( col = self . column , row = self . row , ext = format . lower ( ) . replace ( 'jpeg' , 'jpg' ) ) if not path : return filename return os . path . jo...
Construct and return a filename for this tile .
24,151
def get_columns_rows ( filenames ) : tiles = [ ] for filename in filenames : row , column = os . path . splitext ( filename ) [ 0 ] [ - 5 : ] . split ( '_' ) tiles . append ( ( int ( row ) , int ( column ) ) ) rows = [ pos [ 0 ] for pos in tiles ] columns = [ pos [ 1 ] for pos in tiles ] num_rows = max ( rows ) num_col...
Derive number of columns and rows from filenames .
24,152
def get_signed_range ( se , expr ) : size = expr . size ( ) umin = umax = smin = smax = None if not sat_zero ( se , expr ) : try : umin = se . min ( expr , extra_constraints = [ claripy . Extract ( size - 1 , size - 1 , expr ) == 0 ] ) umax = se . max ( expr , extra_constraints = [ claripy . Extract ( size - 1 , size -...
Calculate the range of the expression with signed boundaries
24,153
def fn_check_full ( fn ) : status = True if not os . path . isfile ( fn ) : status = False else : try : open ( fn ) except IOError : status = False return status
Check for file existence
24,154
def fn_getma ( fn , bnum = 1 , return_ds = False ) : ds = fn_getds ( fn ) out = ds_getma ( ds , bnum = bnum ) if return_ds : out = ( out , ds ) return out
Get masked array from input filename
24,155
def b_getma ( b ) : b_ndv = get_ndv_b ( b ) bma = np . ma . masked_values ( b . ReadAsArray ( ) , b_ndv ) return bma
Get masked array from input GDAL Band
24,156
def get_sub_dim ( src_ds , scale = None , maxdim = 1024 ) : ns = src_ds . RasterXSize nl = src_ds . RasterYSize maxdim = float ( maxdim ) if scale is None : scale_ns = ns / maxdim scale_nl = nl / maxdim scale = max ( scale_ns , scale_nl ) if scale > 1 : ns = int ( round ( ns / scale ) ) nl = int ( round ( nl / scale ) ...
Compute dimensions of subsampled dataset
24,157
def ds_getma_sub ( src_ds , bnum = 1 , scale = None , maxdim = 1024. , return_ds = False ) : b = src_ds . GetRasterBand ( bnum ) b_ndv = get_ndv_b ( b ) ns , nl , scale = get_sub_dim ( src_ds , scale , maxdim ) b_array = b . ReadAsArray ( buf_xsize = ns , buf_ysize = nl ) bma = np . ma . masked_values ( b_array , b_ndv...
Load a subsampled array rather than full resolution
24,158
def writeGTiff ( a , dst_fn , src_ds = None , bnum = 1 , ndv = None , gt = None , proj = None , create = False , sparse = False ) : from pygeotools . lib . malib import checkma a = checkma ( a , fix = False ) if ndv is not None : a . set_fill_value ( ndv ) driver = gtif_drv nbands = 1 np_dt = a . dtype . name if src_ds...
Write input array to disk as GeoTiff
24,159
def writevrt ( out_csv , srs = 'EPSG:4326' , x = 'field_1' , y = 'field_2' ) : out_vrt = os . path . splitext ( out_csv ) [ 0 ] + '.vrt' out_csv = os . path . split ( out_csv ) [ - 1 ] f = open ( out_vrt , 'w' ) f . write ( '<OGRVRTDataSource>\n' ) f . write ( ' <OGRVRTLayer name="%s">\n' % os . path . splitext ( out...
Write out a vrt to accompany a csv of points
24,160
def np2gdal_dtype ( d ) : dt_dict = gdal_array . codes if isinstance ( d , ( np . ndarray , np . generic ) ) : d = d . dtype if isinstance ( d , np . dtype ) : if d . name == 'int8' : gdal_dt = 1 elif d . name == 'bool' : gdal_dt = 1 else : gdal_dt = list ( dt_dict . keys ( ) ) [ list ( dt_dict . values ( ) ) . index (...
Get GDAL RasterBand datatype that corresponds with NumPy datatype Input should be numpy array or numpy dtype
24,161
def gdal2np_dtype ( b ) : dt_dict = gdal_array . codes if isinstance ( b , str ) : b = gdal . Open ( b ) if isinstance ( b , gdal . Dataset ) : b = b . GetRasterBand ( 1 ) if isinstance ( b , gdal . Band ) : b = b . DataType if isinstance ( b , int ) : np_dtype = dt_dict [ b ] else : np_dtype = None print ( "Input must...
Get NumPy datatype that corresponds with GDAL RasterBand datatype Input can be filename GDAL Dataset GDAL RasterBand or GDAL integer dtype
24,162
def get_ndv_b ( b ) : b_ndv = b . GetNoDataValue ( ) if b_ndv is None : ns = b . XSize nl = b . YSize ul = float ( b . ReadAsArray ( 0 , 0 , 1 , 1 ) ) lr = float ( b . ReadAsArray ( ns - 1 , nl - 1 , 1 , 1 ) ) if np . isnan ( ul ) or ul == lr : b_ndv = ul else : b_ndv = 0 elif np . isnan ( b_ndv ) : b_dt = gdal . GetDa...
Get NoData value for GDAL band .
24,163
def cpu_count ( logical = True ) : if logical : from multiprocessing import cpu_count ncpu = cpu_count ( ) else : import psutil ncpu = psutil . cpu_count ( logical = False ) return ncpu
Return system CPU count
24,164
def getfile ( url , outdir = None ) : fn = os . path . split ( url ) [ - 1 ] if outdir is not None : fn = os . path . join ( outdir , fn ) if not os . path . exists ( fn ) : try : from urllib . request import urlretrieve except ImportError : from urllib import urlretrieve print ( "Retrieving: %s" % url ) urlretrieve ( ...
Function to fetch files using urllib
24,165
def getfile2 ( url , auth = None , outdir = None ) : import requests print ( "Retrieving: %s" % url ) fn = os . path . split ( url ) [ - 1 ] if outdir is not None : fn = os . path . join ( outdir , fn ) if auth is not None : r = requests . get ( url , stream = True , auth = auth ) else : r = requests . get ( url , stre...
Function to fetch files using requests
24,166
def get_auth ( ) : import getpass from requests . auth import HTTPDigestAuth input_func = input try : input_func = raw_input except NameError : pass uname = input_func ( "MODSCAG Username:" ) pw = getpass . getpass ( "MODSCAG Password:" ) auth = HTTPDigestAuth ( uname , pw ) return auth
Get authorization token for https
24,167
def readcsv ( fn ) : import csv with open ( fn , 'r' ) as f : reader = csv . DictReader ( f ) hdr = reader . fieldnames skiprows = 1 if np . all ( f . isdigit ( ) for f in hdr ) : hdr = None skiprows = 0 pts = np . loadtxt ( fn , delimiter = ',' , skiprows = skiprows , dtype = None ) return pts
Wrapper to read arbitrary csv check for header
24,168
def absrange_fltr ( dem , rangelim ) : out = range_fltr ( np . ma . abs ( dem ) , * rangelim ) out = np . ma . array ( dem , mask = np . ma . getmaskarray ( out ) ) out . set_fill_value ( dem . fill_value ) return out
Absolute range filter
24,169
def gauss_fltr_astropy ( dem , size = None , sigma = None , origmask = False , fill_interior = False ) : import astropy . convolution dem = malib . checkma ( dem ) if size is not None : size = int ( np . floor ( size / 2 ) * 2 + 1 ) size = max ( size , 3 ) truncate = 3.0 if size is not None and sigma is None : sigma = ...
Astropy gaussian filter properly handles convolution with NaN
24,170
def gauss_fltr_pyramid ( dem , size = None , full = False , origmask = False ) : dem = malib . checkma ( dem ) levels = int ( np . floor ( np . log2 ( size ) ) ) dim = np . floor ( np . array ( dem . shape ) / float ( 2 ** levels ) + 1 ) * ( 2 ** levels ) dem2 = np . full ( dim , dem . fill_value ) offset = ( dim - np ...
Pyaramidal downsampling approach for gaussian smoothing Avoids the need for large kernels very fast Needs testing
24,171
def gauss_fltr_opencv ( dem , size = 3 , sigma = 1 ) : import cv2 dem = malib . checkma ( dem ) dem_cv = cv2 . GaussianBlur ( dem . filled ( np . nan ) , ( size , size ) , sigma ) out = np . ma . fix_invalid ( dem_cv ) out . set_fill_value ( dem . fill_value ) return out
OpenCV Gaussian filter Still propagates NaN values
24,172
def gaussfill ( dem , size = 3 , newmask = None ) : smooth = gauss_fltr_astropy ( dem , size = size ) smooth [ ~ dem . mask ] = dem [ ~ dem . mask ] if newmask is not None : smooth = np . ma . array ( smooth , mask = newmask ) return smooth
Gaussian filter with filling
24,173
def median_fltr ( dem , fsize = 7 , origmask = False ) : print ( "Applying median filter with size %s" % fsize ) from scipy . ndimage . filters import median_filter dem_filt_med = median_filter ( dem . filled ( np . nan ) , fsize ) out = np . ma . fix_invalid ( dem_filt_med , copy = False , fill_value = dem . fill_valu...
Scipy . ndimage median filter
24,174
def median_fltr_opencv ( dem , size = 3 , iterations = 1 ) : import cv2 dem = malib . checkma ( dem ) if size > 5 : print ( "Need to implement iteration" ) n = 0 out = dem while n <= iterations : dem_cv = cv2 . medianBlur ( out . astype ( np . float32 ) . filled ( np . nan ) , size ) out = np . ma . fix_invalid ( dem_c...
OpenCV median filter
24,175
def circular_mask ( size ) : r = size / 2 c = ( r , r ) y , x = np . ogrid [ - c [ 0 ] : size - c [ 0 ] , - c [ 1 ] : size - c [ 1 ] ] mask = ~ ( x * x + y * y <= r * r ) return mask
Create a circular mask for an array Useful when sampling rasters for a laser shot
24,176
def median_fltr_skimage ( dem , radius = 3 , erode = 1 , origmask = False ) : dem = malib . checkma ( dem ) dem = dem . astype ( np . float64 ) if erode > 0 : print ( "Eroding islands smaller than %s pixels" % ( erode * 2 ) ) dem = malib . mask_islands ( dem , iterations = erode ) print ( "Applying median filter with r...
Older skimage . filter . median_filter
24,177
def dz_fltr ( dem_fn , refdem_fn , perc = None , rangelim = ( 0 , 30 ) , smooth = False ) : try : open ( refdem_fn ) except IOError : sys . exit ( 'Unable to open reference DEM: %s' % refdem_fn ) from pygeotools . lib import warplib dem_ds , refdem_ds = warplib . memwarp_multi_fn ( [ dem_fn , refdem_fn ] , res = 'first...
Absolute elevation difference range filter using values from a source raster file and a reference raster file
24,178
def dz_fltr_ma ( dem , refdem , perc = None , rangelim = ( 0 , 30 ) , smooth = False ) : if smooth : refdem = gauss_fltr_astropy ( refdem ) dem = gauss_fltr_astropy ( dem ) dz = refdem - dem demmask = np . ma . getmaskarray ( dem ) if perc : dz_perc = malib . calcperc ( dz , perc ) print ( "Applying dz percentile filte...
Absolute elevation difference range filter using values from a source array and a reference array
24,179
def erode_edge ( dem , iterations = 1 ) : import scipy . ndimage as ndimage print ( 'Eroding pixels near nodata: %i iterations' % iterations ) mask = np . ma . getmaskarray ( dem ) mask_dilate = ndimage . morphology . binary_dilation ( mask , iterations = iterations ) out = np . ma . array ( dem , mask = mask_dilate ) ...
Erode pixels near nodata
24,180
def butter ( dt_list , val , lowpass = 1.0 ) : import scipy . signal import matplotlib . pyplot as plt dt_diff = np . diff ( dt_list ) dt_diff = np . array ( [ dt . total_seconds ( ) for dt in dt_diff ] ) dt = malib . fast_median ( dt_diff ) fs = 1. / dt nyq = fs / 2. if False : sp_f , sp_psd = scipy . signal . periodo...
This is framework for a butterworth bandpass for 1D data Needs to be cleaned up and generalized
24,181
def freq_filt ( bma ) : bf = malib . randomfill ( bma ) import scipy . fftpack f = scipy . fftpack . fft2 ( bf ) ff = scipy . fftpack . fftshift ( f ) ff_dim = np . array ( ff . shape ) a , b = ff_dim / 2 n = ff_dim . max ( ) y , x = np . ogrid [ - a : n - a , - b : n - b ] r1 = 40 r2 = 60 ff_mask = np . ma . make_mask...
This is a framework for 2D FFT filtering . It has not be tested or finished - might be a dead end
24,182
def stack_smooth ( s_orig , size = 7 , save = False ) : from copy import deepcopy from pygeotools . lib import filtlib print ( "Copying original DEMStack" ) s = deepcopy ( s_orig ) s . stack_fn = os . path . splitext ( s_orig . stack_fn ) [ 0 ] + '_smooth%ipx.npz' % size print ( "Smoothing all arrays in stack with %i p...
Run Gaussian smoothing filter on exising stack object
24,183
def stack_clip ( s_orig , extent , out_stack_fn = None , copy = True , save = False ) : if copy or save : from copy import deepcopy print ( "Copying original DEMStack" ) s = deepcopy ( s_orig ) else : s = s_orig from pygeotools . lib import geolib gt = s . gt s_shape = s . ma_stack . shape [ 1 : 3 ] min_x_px , max_y_px...
Create a new stack object with limited extent from an exising stack object
24,184
def get_stack_subset ( s_orig , idx , out_stack_fn = None , copy = True , save = False ) : idx = np . array ( idx ) if np . any ( idx ) : if copy or save : from copy import deepcopy print ( "Copying original DEMStack" ) s = deepcopy ( s_orig ) else : s = s_orig print ( "Original stack: %i" % len ( s_orig . fn_list ) ) ...
Create a new stack object as a subset of an exising stack object
24,185
def stack_merge ( s1 , s2 , out_stack_fn = None , sort = True , save = False ) : from pygeotools . lib import geolib from copy import deepcopy if s1 . ma_stack . shape [ 1 : 3 ] != s2 . ma_stack . shape [ 1 : 3 ] : print ( s1 . ma_stack . shape ) print ( s2 . ma_stack . shape ) sys . exit ( 'Input stacks must have iden...
Merge two stack objects
24,186
def randomfill ( a ) : a = checkma ( a ) noise = a . mask * np . random . normal ( a . mean ( ) , a . std ( ) , a . shape ) b = a . filled ( 0 ) + noise return b
Fill masked areas with random noise This is needed for any fft - based operations
24,187
def nanfill ( a , f_a , * args , ** kwargs ) : a = checkma ( a ) ndv = a . fill_value b = f_a ( a . filled ( np . nan ) , * args , ** kwargs ) out = np . ma . fix_invalid ( b , copy = False ) out . set_fill_value ( ndv ) return out
Fill masked areas with np . nan
24,188
def fast_median ( a ) : a = checkma ( a ) if a . count ( ) > 0 : out = np . percentile ( a . compressed ( ) , 50 ) else : out = np . ma . masked return out
Fast median operation for masked array using 50th - percentile
24,189
def mad ( a , axis = None , c = 1.4826 , return_med = False ) : a = checkma ( a ) if a . count ( ) > 0 : if axis is None : med = fast_median ( a ) out = fast_median ( np . fabs ( a - med ) ) * c else : med = np . ma . median ( a , axis = axis ) med = np . expand_dims ( med , axis = axis ) out = np . ma . median ( np . ...
Compute normalized median absolute difference Can also return median array as this can be expensive and often we want both med and nmad
24,190
def calcperc ( b , perc = ( 0.1 , 99.9 ) ) : b = checkma ( b ) if b . count ( ) > 0 : low = np . percentile ( b . compressed ( ) , perc [ 0 ] ) high = np . percentile ( b . compressed ( ) , perc [ 1 ] ) else : low = 0 high = 0 return low , high
Calculate values at specified percentiles
24,191
def calcperc_sym ( b , perc = ( 0.1 , 99.9 ) ) : clim = np . max ( np . abs ( calcperc ( b , perc ) ) ) return - clim , clim
Get symmetrical percentile values Useful for determining clim centered on 0 for difference maps
24,192
def iqr ( b , perc = ( 25 , 75 ) ) : b = checkma ( b ) low , high = calcperc ( b , perc ) return low , high , high - low
Inter - quartile range
24,193
def iv ( b , ** kwargs ) : import matplotlib . pyplot as plt import imview . imviewer as imview b = checkma ( b ) fig = plt . figure ( ) imview . bma_fig ( fig , b , ** kwargs ) plt . show ( ) return fig
Quick access to imview for interactive sessions
24,194
def norm_shape ( shape ) : try : i = int ( shape ) return ( i , ) except TypeError : pass try : t = tuple ( shape ) return t except TypeError : pass raise TypeError ( 'shape must be an int, or a tuple of ints' )
Normalize numpy array shapes so they re always expressed as a tuple even for one - dimensional shapes . Parameters shape - an int or a tuple of ints Returns a shape tuple
24,195
def localortho ( lon , lat ) : local_srs = osr . SpatialReference ( ) local_proj = '+proj=ortho +lat_0=%0.7f +lon_0=%0.7f +datum=WGS84 +units=m +no_defs ' % ( lat , lon ) local_srs . ImportFromProj4 ( local_proj ) return local_srs
Create srs for local orthographic projection centered at lat lon
24,196
def geom2localortho ( geom ) : cx , cy = geom . Centroid ( ) . GetPoint_2D ( ) lon , lat , z = cT_helper ( cx , cy , 0 , geom . GetSpatialReference ( ) , wgs_srs ) local_srs = localortho ( lon , lat ) local_geom = geom_dup ( geom ) geom_transform ( local_geom , local_srs ) return local_geom
Convert existing geom to local orthographic projection
24,197
def dd2dms ( dd ) : n = dd < 0 dd = abs ( dd ) m , s = divmod ( dd * 3600 , 60 ) d , m = divmod ( m , 60 ) if n : d = - d return d , m , s
Convert decimal degrees to degrees minutes seconds
24,198
def dms2dd ( d , m , s ) : if d < 0 : sign = - 1 else : sign = 1 dd = sign * ( int ( abs ( d ) ) + float ( m ) / 60 + float ( s ) / 3600 ) return dd
Convert degrees minutes seconds to decimal degrees
24,199
def dd2dm ( dd ) : d , m , s = dd2dms ( dd ) m = m + float ( s ) / 3600 return d , m , s
Convert decimal to degrees decimal minutes