idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
60,800
def _get_service_names ( self ) : master_info = None connection_errors = [ ] for sentinel in self . _sentinel . sentinels : try : master_info = sentinel . sentinel_masters ( ) break except ( redis . ConnectionError , redis . TimeoutError ) as e : connection_errors . append ( 'Failed to connect to {} due to error: "{}"....
Get a list of service names from Sentinel . Tries Sentinel hosts until one succeeds ; if none succeed raises a ConnectionError .
60,801
def timid_relpath ( arg ) : from os . path import isabs , relpath , sep if isabs ( arg ) : result = relpath ( arg ) if result . count ( sep ) + 1 < arg . count ( sep ) : return result return arg
convert an argument to a relative path carefully
60,802
def ensure_virtualenv ( args , return_values ) : def adjust_options ( options , args ) : venv_path = return_values . venv_path = args [ 0 ] if venv_path == DEFAULT_VIRTUALENV_PATH or options . prompt == '<dirname>' : from os . path import abspath , basename , dirname options . prompt = '(%s)' % basename ( dirname ( abs...
Ensure we have a valid virtualenv .
60,803
def touch ( filename , timestamp ) : if timestamp is not None : timestamp = ( timestamp , timestamp ) from os import utime utime ( filename , timestamp )
set the mtime of a file
60,804
def pip_faster ( venv_path , pip_command , install , bootstrap_deps ) : execfile_ ( venv_executable ( venv_path , 'activate_this.py' ) ) from os import environ environ [ 'PIP_DISABLE_PIP_VERSION_CHECK' ] = '1' run ( ( 'pip' , 'install' ) + bootstrap_deps ) run ( pip_command + install )
install and run pip - faster
60,805
def raise_on_failure ( mainfunc ) : try : errors = mainfunc ( ) if errors : exit ( errors ) except CalledProcessError as error : exit ( error . returncode ) except SystemExit as error : if error . code : raise except KeyboardInterrupt : exit ( 1 )
raise if and only if mainfunc fails
60,806
def cache_installed_wheels ( index_url , installed_packages ) : for installed_package in installed_packages : if not _can_be_cached ( installed_package ) : continue _store_wheel_in_cache ( installed_package . link . path , index_url )
After installation pip tells us what it installed and from where .
60,807
def pip ( args ) : from sys import stdout stdout . write ( colorize ( ( 'pip' , ) + args ) ) stdout . write ( '\n' ) stdout . flush ( ) return pipmodule . _internal . main ( list ( args ) )
Run pip in - process .
60,808
def dist_to_req ( dist ) : try : from pip . _internal . operations . freeze import FrozenRequirement except ImportError : from pip import FrozenRequirement orig_name , dist . project_name = dist . project_name , dist . key result = FrozenRequirement . from_dist ( dist , [ ] ) dist . project_name = orig_name return resu...
Make a pip . FrozenRequirement from a pkg_resources distribution object
60,809
def req_cycle ( req ) : cls = req . __class__ seen = { req . name } while isinstance ( req . comes_from , cls ) : req = req . comes_from if req . name in seen : return True else : seen . add ( req . name ) return False
is this requirement cyclic?
60,810
def pretty_req ( req ) : from copy import copy req = copy ( req ) req . link = None req . satisfied_by = None return req
return a copy of a pip requirement that is a bit more readable at the expense of removing some of its data
60,811
def trace_requirements ( requirements ) : requirements = tuple ( pretty_req ( r ) for r in requirements ) working_set = fresh_working_set ( ) from collections import deque queue = deque ( requirements ) queued = { _package_req_to_pkg_resources_req ( req . req ) for req in queue } errors = [ ] result = [ ] while queue :...
given an iterable of pip InstallRequirements return the set of required packages given their transitive requirements .
60,812
def patch ( attrs , updates ) : orig = { } for attr , value in updates : orig [ attr ] = attrs [ attr ] attrs [ attr ] = value return orig
Perform a set of updates to a attribute dictionary return the original values .
60,813
def patched ( attrs , updates ) : orig = patch ( attrs , updates . items ( ) ) try : yield orig finally : patch ( attrs , orig . items ( ) )
A context in which some attributes temporarily have a modified value .
60,814
def pipfaster_packagefinder ( ) : try : from pip . _internal . cli import base_command except ImportError : from pip . _internal import basecommand as base_command return patched ( vars ( base_command ) , { 'PackageFinder' : FasterPackageFinder } )
Provide a short - circuited search when the requirement is pinned and appears on disk .
60,815
def pipfaster_download_cacher ( index_urls ) : from pip . _internal import download orig = download . _download_http_url patched_fn = get_patched_download_http_url ( orig , index_urls ) return patched ( vars ( download ) , { '_download_http_url' : patched_fn } )
vanilla pip stores a cache of the http session in its cache and not the wheel files . We intercept the download and save those files into our cache
60,816
def run ( self , options , args ) : if options . prune : previously_installed = pip_get_installed ( ) index_urls = [ options . index_url ] + options . extra_index_urls with pipfaster_download_cacher ( index_urls ) : requirement_set = super ( FasterInstallCommand , self ) . run ( options , args , ) required = requiremen...
update install options with caching values
60,817
def setEncoder ( self , encoder ) : if not encoder : self . _encoder = json . JSONEncoder ( ) else : self . _encoder = encoder self . _encode = self . _encoder . encode
Sets the client s encoder encoder should be an instance of a json . JSONEncoder class
60,818
def setDecoder ( self , decoder ) : if not decoder : self . _decoder = json . JSONDecoder ( ) else : self . _decoder = decoder self . _decode = self . _decoder . decode
Sets the client s decoder decoder should be an instance of a json . JSONDecoder class
60,819
def jsondel ( self , name , path = Path . rootPath ( ) ) : return self . execute_command ( 'JSON.DEL' , name , str_path ( path ) )
Deletes the JSON value stored at key name under path
60,820
def jsonget ( self , name , * args ) : pieces = [ name ] if len ( args ) == 0 : pieces . append ( Path . rootPath ( ) ) else : for p in args : pieces . append ( str_path ( p ) ) try : return self . execute_command ( 'JSON.GET' , * pieces ) except TypeError : return None
Get the object stored as a JSON value at key name args is zero or more paths and defaults to root path
60,821
def jsonmget ( self , path , * args ) : pieces = [ ] pieces . extend ( args ) pieces . append ( str_path ( path ) ) return self . execute_command ( 'JSON.MGET' , * pieces )
Gets the objects stored as a JSON values under path from keys args
60,822
def jsonset ( self , name , path , obj , nx = False , xx = False ) : pieces = [ name , str_path ( path ) , self . _encode ( obj ) ] if nx and xx : raise Exception ( 'nx and xx are mutually exclusive: use one, the ' 'other or neither - but not both' ) elif nx : pieces . append ( 'NX' ) elif xx : pieces . append ( 'XX' )...
Set the JSON value at key name under the path to obj nx if set to True set value only if it does not exist xx if set to True set value only if it exists
60,823
def jsontype ( self , name , path = Path . rootPath ( ) ) : return self . execute_command ( 'JSON.TYPE' , name , str_path ( path ) )
Gets the type of the JSON value under path from key name
60,824
def jsonstrappend ( self , name , string , path = Path . rootPath ( ) ) : return self . execute_command ( 'JSON.STRAPPEND' , name , str_path ( path ) , self . _encode ( string ) )
Appends to the string JSON value under path at key name the provided string
60,825
def jsonstrlen ( self , name , path = Path . rootPath ( ) ) : return self . execute_command ( 'JSON.STRLEN' , name , str_path ( path ) )
Returns the length of the string JSON value under path at key name
60,826
def jsonarrappend ( self , name , path = Path . rootPath ( ) , * args ) : pieces = [ name , str_path ( path ) ] for o in args : pieces . append ( self . _encode ( o ) ) return self . execute_command ( 'JSON.ARRAPPEND' , * pieces )
Appends the objects args to the array under the path in key name
60,827
def jsonarrindex ( self , name , path , scalar , start = 0 , stop = - 1 ) : return self . execute_command ( 'JSON.ARRINDEX' , name , str_path ( path ) , self . _encode ( scalar ) , start , stop )
Returns the index of scalar in the JSON array under path at key name . The search can be limited using the optional inclusive start and exclusive stop indices .
60,828
def jsonarrinsert ( self , name , path , index , * args ) : pieces = [ name , str_path ( path ) , index ] for o in args : pieces . append ( self . _encode ( o ) ) return self . execute_command ( 'JSON.ARRINSERT' , * pieces )
Inserts the objects args to the array at index index under the path in key name
60,829
def jsonarrlen ( self , name , path = Path . rootPath ( ) ) : return self . execute_command ( 'JSON.ARRLEN' , name , str_path ( path ) )
Returns the length of the array JSON value under path at key name
60,830
def jsonarrpop ( self , name , path = Path . rootPath ( ) , index = - 1 ) : return self . execute_command ( 'JSON.ARRPOP' , name , str_path ( path ) , index )
Pops the element at index in the array JSON value under path at key name
60,831
def jsonarrtrim ( self , name , path , start , stop ) : return self . execute_command ( 'JSON.ARRTRIM' , name , str_path ( path ) , start , stop )
Trim the array JSON value under path at key name to the inclusive range given by start and stop
60,832
def jsonobjkeys ( self , name , path = Path . rootPath ( ) ) : return self . execute_command ( 'JSON.OBJKEYS' , name , str_path ( path ) )
Returns the key names in the dictionary JSON value under path at key name
60,833
def jsonobjlen ( self , name , path = Path . rootPath ( ) ) : return self . execute_command ( 'JSON.OBJLEN' , name , str_path ( path ) )
Returns the length of the dictionary JSON value under path at key name
60,834
def get_pg_info ( ) : from psycopg2 import connect , OperationalError log . debug ( "entered get_pg_info" ) try : conf = settings . DATABASES [ 'default' ] database = conf [ "NAME" ] user = conf [ "USER" ] host = conf [ "HOST" ] port = conf [ "PORT" ] password = conf [ "PASSWORD" ] except ( AttributeError , KeyError ) ...
Check PostgreSQL connection .
60,835
def get_redis_info ( ) : from kombu . utils . url import _parse_url as parse_redis_url from redis import ( StrictRedis , ConnectionError as RedisConnectionError , ResponseError as RedisResponseError , ) for conf_name in ( 'REDIS_URL' , 'BROKER_URL' , 'CELERY_BROKER_URL' ) : if hasattr ( settings , conf_name ) : url = g...
Check Redis connection .
60,836
def get_elasticsearch_info ( ) : from elasticsearch import ( Elasticsearch , ConnectionError as ESConnectionError ) if hasattr ( settings , 'ELASTICSEARCH_URL' ) : url = settings . ELASTICSEARCH_URL else : return { "status" : NO_CONFIG } start = datetime . now ( ) try : search = Elasticsearch ( url , request_timeout = ...
Check Elasticsearch connection .
60,837
def get_celery_info ( ) : import celery if not getattr ( settings , 'USE_CELERY' , False ) : log . error ( "No celery config found. Set USE_CELERY in settings to enable." ) return { "status" : NO_CONFIG } start = datetime . now ( ) try : app = celery . Celery ( 'tasks' ) app . config_from_object ( 'django.conf:settings...
Check celery availability
60,838
def get_certificate_info ( ) : if hasattr ( settings , 'MIT_WS_CERTIFICATE' ) and settings . MIT_WS_CERTIFICATE : mit_ws_certificate = settings . MIT_WS_CERTIFICATE else : return { "status" : NO_CONFIG } app_cert = OpenSSL . crypto . load_certificate ( OpenSSL . crypto . FILETYPE_PEM , ( mit_ws_certificate if not isins...
checks app certificate expiry status
60,839
def _start ( self ) : if self . whoami is None : me = self . get_me ( ) if me . get ( 'ok' , False ) : self . whoami = me [ 'result' ] else : raise ValueError ( 'Bot Cannot request information, check ' 'api_key' )
Requests bot information based on current api_key and sets self . whoami to dictionary with username first_name and id of the configured bot .
60,840
def poll ( self , offset = None , poll_timeout = 600 , cooldown = 60 , debug = False ) : if self . config [ 'api_key' ] is None : raise ValueError ( 'config api_key is undefined' ) if offset or self . config . get ( 'offset' , None ) : self . offset = offset or self . config . get ( 'offset' , None ) self . _start ( ) ...
These should also be in the config section but some here for overrides
60,841
def get_attr ( obj , attr , default = None ) : if '.' not in attr : return getattr ( obj , attr , default ) else : L = attr . split ( '.' ) return get_attr ( getattr ( obj , L [ 0 ] , default ) , '.' . join ( L [ 1 : ] ) , default )
Recursive get object s attribute . May use dot notation .
60,842
def asset ( path ) : commit = bitcaster . get_full_version ( ) return mark_safe ( '{0}?{1}' . format ( _static ( path ) , commit ) )
Join the given path with the STATIC_URL setting .
60,843
def get_client_ip ( request ) : try : return request . META [ 'HTTP_X_FORWARDED_FOR' ] . split ( ',' ) [ 0 ] . strip ( ) except ( KeyError , IndexError ) : return request . META . get ( 'REMOTE_ADDR' )
Naively yank the first IP address in an X - Forwarded - For header and assume this is correct .
60,844
def _pack_image ( filename , max_size , form_field = 'image' , f = None ) : if f is None : try : if os . path . getsize ( filename ) > ( max_size * 1024 ) : raise TweepError ( 'File is too big, must be less than %skb.' % max_size ) except os . error as e : raise TweepError ( 'Unable to access file: %s' % e . strerror )...
Pack image from file into multipart - formdata post body
60,845
def channel_submit_row ( context ) : change = context [ 'change' ] is_popup = context [ 'is_popup' ] save_as = context [ 'save_as' ] show_save = context . get ( 'show_save' , True ) show_save_and_continue = context . get ( 'show_save_and_continue' , True ) can_delete = context [ 'has_delete_permission' ] can_add = cont...
Display the row of buttons for delete and save .
60,846
def get_setting ( self , name ) : notfound = object ( ) "get configuration from 'constance.config' first " value = getattr ( config , name , notfound ) if name . endswith ( '_WHITELISTED_DOMAINS' ) : if value : return value . split ( ',' ) else : return [ ] if value is notfound : value = getattr ( settings , name ) if ...
get configuration from constance . config first
60,847
def debug ( self , request , message , extra_tags = '' , fail_silently = False ) : add ( self . target_name , request , constants . DEBUG , message , extra_tags = extra_tags , fail_silently = fail_silently )
Add a message with the DEBUG level .
60,848
def info ( self , request , message , extra_tags = '' , fail_silently = False ) : add ( self . target_name , request , constants . INFO , message , extra_tags = extra_tags , fail_silently = fail_silently )
Add a message with the INFO level .
60,849
def success ( self , request , message , extra_tags = '' , fail_silently = False ) : add ( self . target_name , request , constants . SUCCESS , message , extra_tags = extra_tags , fail_silently = fail_silently )
Add a message with the SUCCESS level .
60,850
def warning ( self , request , message , extra_tags = '' , fail_silently = False ) : add ( self . target_name , request , constants . WARNING , message , extra_tags = extra_tags , fail_silently = fail_silently )
Add a message with the WARNING level .
60,851
def error ( self , request , message , extra_tags = '' , fail_silently = False ) : add ( self . target_name , request , constants . ERROR , message , extra_tags = extra_tags , fail_silently = fail_silently )
Add a message with the ERROR level .
60,852
def signup ( request , signup_form = SignupForm , template_name = 'userena/signup_form.html' , success_url = None , extra_context = None ) : if userena_settings . USERENA_DISABLE_SIGNUP : raise PermissionDenied if userena_settings . USERENA_WITHOUT_USERNAMES and ( signup_form == SignupForm ) : signup_form = SignupFormO...
Signup of an account .
60,853
def extend ( self , other ) : overlap = [ key for key in other . defaults if key in self . defaults ] if overlap : raise ValueError ( "Duplicate hyperparameter(s): %s" % " " . join ( overlap ) ) new = dict ( self . defaults ) new . update ( other . defaults ) return HyperparameterDefaults ( ** new )
Return a new HyperparameterDefaults instance containing the hyperparameters from the current instance combined with those from other .
60,854
def with_defaults ( self , obj ) : self . check_valid_keys ( obj ) obj = dict ( obj ) for ( key , value ) in self . defaults . items ( ) : if key not in obj : obj [ key ] = value return obj
Given a dict of hyperparameter settings return a dict containing those settings augmented by the defaults for any keys missing from the dict .
60,855
def subselect ( self , obj ) : return dict ( ( key , value ) for ( key , value ) in obj . items ( ) if key in self . defaults )
Filter a dict of hyperparameter settings to only those keys defined in this HyperparameterDefaults .
60,856
def check_valid_keys ( self , obj ) : invalid_keys = [ x for x in obj if x not in self . defaults ] if invalid_keys : raise ValueError ( "No such model parameters: %s. Valid parameters are: %s" % ( " " . join ( invalid_keys ) , " " . join ( self . defaults ) ) )
Given a dict of hyperparameter settings throw an exception if any keys are not defined in this HyperparameterDefaults instance .
60,857
def models_grid ( self , ** kwargs ) : self . check_valid_keys ( kwargs ) for ( key , value ) in kwargs . items ( ) : if not isinstance ( value , list ) : raise ValueError ( "All parameters must be lists, but %s is %s" % ( key , str ( type ( value ) ) ) ) parameters = dict ( ( key , [ value ] ) for ( key , value ) in s...
Make a grid of models by taking the cartesian product of all specified model parameter lists .
60,858
def fixed_length_vector_encoded_sequences ( self , vector_encoding_name ) : cache_key = ( "fixed_length_vector_encoding" , vector_encoding_name ) if cache_key not in self . encoding_cache : index_encoded_matrix = amino_acid . index_encoding ( self . fixed_length_sequences . values , amino_acid . AMINO_ACID_INDEX ) vect...
Encode alleles .
60,859
def index_encoding ( sequences , letter_to_index_dict ) : df = pandas . DataFrame ( iter ( s ) for s in sequences ) result = df . replace ( letter_to_index_dict ) return result . values
Encode a sequence of same - length strings to a matrix of integers of the same shape . The map from characters to integers is given by letter_to_index_dict .
60,860
def apply_hyperparameter_renames ( cls , hyperparameters ) : for ( from_name , to_name ) in cls . hyperparameter_renames . items ( ) : if from_name in hyperparameters : value = hyperparameters . pop ( from_name ) if to_name : hyperparameters [ to_name ] = value return hyperparameters
Handle hyperparameter renames .
60,861
def borrow_cached_network ( klass , network_json , network_weights ) : assert network_weights is not None key = klass . keras_network_cache_key ( network_json ) if key not in klass . KERAS_MODELS_CACHE : import keras . models network = keras . models . model_from_json ( network_json ) existing_weights = None else : ( n...
Return a keras Model with the specified architecture and weights . As an optimization when possible this will reuse architectures from a process - wide cache .
60,862
def network ( self , borrow = False ) : if self . _network is None and self . network_json is not None : self . load_weights ( ) if borrow : return self . borrow_cached_network ( self . network_json , self . network_weights ) else : import keras . models self . _network = keras . models . model_from_json ( self . netwo...
Return the keras model associated with this predictor .
60,863
def load_weights ( self ) : if self . network_weights_loader : self . network_weights = self . network_weights_loader ( ) self . network_weights_loader = None
Load weights by evaluating self . network_weights_loader if needed .
60,864
def predict ( self , peptides , allele_encoding = None , batch_size = 4096 ) : assert self . prediction_cache is not None use_cache = ( allele_encoding is None and isinstance ( peptides , EncodableSequences ) ) if use_cache and peptides in self . prediction_cache : return self . prediction_cache [ peptides ] . copy ( )...
Predict affinities .
60,865
def make_scores ( ic50_y , ic50_y_pred , sample_weight = None , threshold_nm = 500 , max_ic50 = 50000 ) : y_pred = from_ic50 ( ic50_y_pred , max_ic50 ) try : auc = sklearn . metrics . roc_auc_score ( ic50_y <= threshold_nm , y_pred , sample_weight = sample_weight ) except ValueError as e : logging . warning ( e ) auc =...
Calculate AUC F1 and Kendall Tau scores .
60,866
def variable_length_to_fixed_length_vector_encoding ( self , vector_encoding_name , left_edge = 4 , right_edge = 4 , max_length = 15 ) : cache_key = ( "fixed_length_vector_encoding" , vector_encoding_name , left_edge , right_edge , max_length ) if cache_key not in self . encoding_cache : fixed_length_sequences = ( self...
Encode variable - length sequences using a fixed - length encoding designed for preserving the anchor positions of class I peptides .
60,867
def sequences_to_fixed_length_index_encoded_array ( klass , sequences , left_edge = 4 , right_edge = 4 , max_length = 15 ) : result = numpy . full ( fill_value = amino_acid . AMINO_ACID_INDEX [ 'X' ] , shape = ( len ( sequences ) , max_length ) , dtype = "int32" ) df = pandas . DataFrame ( { "peptide" : sequences } ) d...
Transform a sequence of strings where each string is of length at least left_edge + right_edge and at most max_length into strings of length max_length using a scheme designed to preserve the anchor positions of class I peptides .
60,868
def robust_mean ( log_values ) : if log_values . shape [ 1 ] <= 3 : return numpy . nanmean ( log_values , axis = 1 ) without_nans = numpy . nan_to_num ( log_values ) mask = ( ( ~ numpy . isnan ( log_values ) ) & ( without_nans <= numpy . nanpercentile ( log_values , 75 , axis = 1 ) . reshape ( ( - 1 , 1 ) ) ) & ( witho...
Mean of values falling within the 25 - 75 percentiles .
60,869
def neural_networks ( self ) : result = [ ] for models in self . allele_to_allele_specific_models . values ( ) : result . extend ( models ) result . extend ( self . class1_pan_allele_models ) return result
List of the neural networks in the ensemble .
60,870
def merge ( cls , predictors ) : assert len ( predictors ) > 0 if len ( predictors ) == 1 : return predictors [ 0 ] allele_to_allele_specific_models = collections . defaultdict ( list ) class1_pan_allele_models = [ ] allele_to_fixed_length_sequence = predictors [ 0 ] . allele_to_fixed_length_sequence for predictor in p...
Merge the ensembles of two or more Class1AffinityPredictor instances .
60,871
def merge_in_place ( self , others ) : new_model_names = [ ] for predictor in others : for model in predictor . class1_pan_allele_models : model_name = self . model_name ( "pan-class1" , len ( self . class1_pan_allele_models ) ) self . class1_pan_allele_models . append ( model ) row = pandas . Series ( collections . Or...
Add the models present other predictors into the current predictor .
60,872
def percentile_ranks ( self , affinities , allele = None , alleles = None , throw = True ) : if allele is not None : try : transform = self . allele_to_percent_rank_transform [ allele ] return transform . transform ( affinities ) except KeyError : msg = "Allele %s has no percentile rank information" % allele if throw :...
Return percentile ranks for the given ic50 affinities and alleles .
60,873
def calibrate_percentile_ranks ( self , peptides = None , num_peptides_per_length = int ( 1e5 ) , alleles = None , bins = None ) : if bins is None : bins = to_ic50 ( numpy . linspace ( 1 , 0 , 1000 ) ) if alleles is None : alleles = self . supported_alleles if peptides is None : peptides = [ ] lengths = range ( self . ...
Compute the cumulative distribution of ic50 values for a set of alleles over a large universe of random peptides to enable computing quantiles in this distribution later .
60,874
def filter_networks ( self , predicate ) : allele_to_allele_specific_models = { } for ( allele , models ) in self . allele_to_allele_specific_models . items ( ) : allele_to_allele_specific_models [ allele ] = [ m for m in models if predicate ( m ) ] class1_pan_allele_models = [ m for m in self . class1_pan_allele_model...
Return a new Class1AffinityPredictor containing a subset of this predictor s neural networks .
60,875
def model_select ( self , score_function , alleles = None , min_models = 1 , max_models = 10000 ) : if alleles is None : alleles = self . supported_alleles dfs = [ ] allele_to_allele_specific_models = { } for allele in alleles : df = pandas . DataFrame ( { 'model' : self . allele_to_allele_specific_models [ allele ] } ...
Perform model selection using a user - specified scoring function .
60,876
def to_series ( self ) : return pandas . Series ( self . cdf , index = [ numpy . nan ] + list ( self . bin_edges ) + [ numpy . nan ] )
Serialize the fit to a pandas . Series .
60,877
def get_default_class1_models_dir ( test_exists = True ) : if _MHCFLURRY_DEFAULT_CLASS1_MODELS_DIR : result = join ( get_downloads_dir ( ) , _MHCFLURRY_DEFAULT_CLASS1_MODELS_DIR ) if test_exists and not exists ( result ) : raise IOError ( "No such directory: %s" % result ) return result else : return get_path ( "models...
Return the absolute path to the default class1 models dir .
60,878
def get_current_release_downloads ( ) : downloads = ( get_downloads_metadata ( ) [ 'releases' ] [ get_current_release ( ) ] [ 'downloads' ] ) return OrderedDict ( ( download [ "name" ] , { 'downloaded' : exists ( join ( get_downloads_dir ( ) , download [ "name" ] ) ) , 'metadata' : download , } ) for download in downlo...
Return a dict of all available downloads in the current release .
60,879
def get_path ( download_name , filename = '' , test_exists = True ) : assert '/' not in download_name , "Invalid download: %s" % download_name path = join ( get_downloads_dir ( ) , download_name , filename ) if test_exists and not exists ( path ) : raise RuntimeError ( "Missing MHCflurry downloadable file: %s. " "To do...
Get the local path to a file in a MHCflurry download
60,880
def configure ( ) : global _DOWNLOADS_DIR global _CURRENT_RELEASE _CURRENT_RELEASE = None _DOWNLOADS_DIR = environ . get ( "MHCFLURRY_DOWNLOADS_DIR" ) if not _DOWNLOADS_DIR : metadata = get_downloads_metadata ( ) _CURRENT_RELEASE = environ . get ( "MHCFLURRY_DOWNLOADS_CURRENT_RELEASE" ) if not _CURRENT_RELEASE : _CURRE...
Setup various global variables based on environment variables .
60,881
def make_worker_pool ( processes = None , initializer = None , initializer_kwargs_per_process = None , max_tasks_per_worker = None ) : if not processes : processes = cpu_count ( ) pool_kwargs = { 'processes' : processes , } if max_tasks_per_worker : pool_kwargs [ "maxtasksperchild" ] = max_tasks_per_worker if initializ...
Convenience wrapper to create a multiprocessing . Pool .
60,882
def calibrate_percentile_ranks ( allele , predictor , peptides = None ) : global GLOBAL_DATA if peptides is None : peptides = GLOBAL_DATA [ "calibration_peptides" ] predictor . calibrate_percentile_ranks ( peptides = peptides , alleles = [ allele ] ) return { allele : predictor . allele_to_percent_rank_transform [ alle...
Private helper function .
60,883
def set_keras_backend ( backend = None , gpu_device_nums = None , num_threads = None ) : os . environ [ "KERAS_BACKEND" ] = "tensorflow" original_backend = backend if not backend : backend = "tensorflow-default" if gpu_device_nums is not None : os . environ [ "CUDA_VISIBLE_DEVICES" ] = "," . join ( [ str ( i ) for i in...
Configure Keras backend to use GPU or CPU . Only tensorflow is supported .
60,884
def uproot ( tree ) : uprooted = tree . copy ( ) uprooted . parent = None for child in tree . all_children ( ) : uprooted . add_general_child ( child ) return uprooted
Take a subranch of a tree and deep - copy the children of this subbranch into a new LabeledTree
60,885
def copy ( self ) : return LabeledTree ( udepth = self . udepth , depth = self . depth , text = self . text , label = self . label , children = self . children . copy ( ) if self . children != None else [ ] , parent = self . parent )
Deep Copy of a LabeledTree
60,886
def add_child ( self , child ) : self . children . append ( child ) child . parent = self self . udepth = max ( [ child . udepth for child in self . children ] ) + 1
Adds a branch to the current tree .
60,887
def lowercase ( self ) : if len ( self . children ) > 0 : for child in self . children : child . lowercase ( ) else : self . text = self . text . lower ( )
Lowercase all strings in this tree . Works recursively and in - place .
60,888
def inject_visualization_javascript ( tree_width = 1200 , tree_height = 400 , tree_node_radius = 10 ) : from . javascript import insert_sentiment_markup insert_sentiment_markup ( tree_width = tree_width , tree_height = tree_height , tree_node_radius = tree_node_radius )
In an Ipython notebook show SST trees using the same Javascript code as used by Jason Chuang s visualisations .
60,889
def create_tree_from_string ( line ) : depth = 0 current_word = "" root = None current_node = root for char in line : if char == '(' : if current_node is not None and len ( current_word ) > 0 : attribute_text_label ( current_node , current_word ) current_word = "" depth += 1 if depth > 1 : child = LabeledTree ( depth =...
Parse and convert a string representation of an example into a LabeledTree datastructure .
60,890
def import_tree_corpus ( path ) : tree_list = LabeledTreeCorpus ( ) with codecs . open ( path , "r" , "UTF-8" ) as f : for line in f : tree_list . append ( create_tree_from_string ( line ) ) return tree_list
Import a text file of treebank trees .
60,891
def load_sst ( path = None , url = 'http://nlp.stanford.edu/sentiment/trainDevTestTrees_PTB.zip' ) : if path is None : path = os . path . expanduser ( "~/stanford_sentiment_treebank/" ) makedirs ( path , exist_ok = True ) fnames = download_sst ( path , url ) return { key : import_tree_corpus ( value ) for key , value i...
Download and read in the Stanford Sentiment Treebank dataset into a dictionary with a train dev and test keys . The dictionary keys point to lists of LabeledTrees .
60,892
def labels ( self ) : labelings = OrderedDict ( ) for tree in self : for label , line in tree . to_labeled_lines ( ) : labelings [ line ] = label return labelings
Construct a dictionary of string - > labels
60,893
def to_file ( self , path , mode = "w" ) : with open ( path , mode = mode ) as f : for tree in self : for label , line in tree . to_labeled_lines ( ) : f . write ( line + "\n" )
Save the corpus to a text file in the original format .
60,894
def import_tree_corpus ( labels_path , parents_path , texts_path ) : with codecs . open ( labels_path , "r" , "UTF-8" ) as f : label_lines = f . readlines ( ) with codecs . open ( parents_path , "r" , "UTF-8" ) as f : parent_lines = f . readlines ( ) with codecs . open ( texts_path , "r" , "UTF-8" ) as f : word_lines =...
Import dataset from the TreeLSTM data generation scrips .
60,895
def assign_texts ( node , words , next_idx = 0 ) : if len ( node . children ) == 0 : node . text = words [ next_idx ] return next_idx + 1 else : for child in node . children : next_idx = assign_texts ( child , words , next_idx ) return next_idx
Recursively assign the words to nodes by finding and assigning strings to the leaves of a tree in left to right order .
60,896
def read_tree ( parents , labels , words ) : trees = { } root = None for i in range ( 1 , len ( parents ) + 1 ) : if not i in trees and parents [ i - 1 ] != - 1 : idx = i prev = None while True : parent = parents [ idx - 1 ] if parent == - 1 : break tree = LabeledTree ( ) if prev is not None : tree . add_child ( prev )...
Take as input a list of integers for parents and labels along with a list of words and reconstruct a LabeledTree .
60,897
def set_initial_status ( self , configuration = None ) : super ( CognitiveOpDynModel , self ) . set_initial_status ( configuration ) for node in self . status : self . status [ node ] = np . random . random_sample ( ) self . initial_status = self . status . copy ( ) self . params [ 'nodes' ] [ 'cognitive' ] = { } T_ran...
Override behaviour of methods in class DiffusionModel . Overwrites initial status using random real values . Generates random node profiles .
60,898
def add_node_configuration ( self , param_name , node_id , param_value ) : if param_name not in self . config [ 'nodes' ] : self . config [ 'nodes' ] [ param_name ] = { node_id : param_value } else : self . config [ 'nodes' ] [ param_name ] [ node_id ] = param_value
Set a parameter for a given node
60,899
def add_node_set_configuration ( self , param_name , node_to_value ) : for nid , val in future . utils . iteritems ( node_to_value ) : self . add_node_configuration ( param_name , nid , val )
Set Nodes parameter