idx
int64
0
63k
question
stringlengths
53
5.28k
target
stringlengths
5
805
17,200
def validation_statuses ( self , area_uuid ) : path = "/area/{uuid}/validations" . format ( uuid = area_uuid ) result = self . _make_request ( 'get' , path ) return result . json ( )
Get count of validation statuses for all files in upload_area
17,201
def language_name ( self , text : str ) -> str : values = extract ( text ) input_fn = _to_func ( ( [ values ] , [ ] ) ) pos : int = next ( self . _classifier . predict_classes ( input_fn = input_fn ) ) LOGGER . debug ( "Predicted language position %s" , pos ) return sorted ( self . languages ) [ pos ]
Predict the programming language name of the given source code .
17,202
def scores ( self , text : str ) -> Dict [ str , float ] : values = extract ( text ) input_fn = _to_func ( ( [ values ] , [ ] ) ) prediction = self . _classifier . predict_proba ( input_fn = input_fn ) probabilities = next ( prediction ) . tolist ( ) sorted_languages = sorted ( self . languages ) return dict ( zip ( so...
A score for each language corresponding to the probability that the text is written in the given language . The score is a float value between 0 . 0 and 1 . 0
17,203
def probable_languages ( self , text : str , max_languages : int = 3 ) -> Tuple [ str , ... ] : scores = self . scores ( text ) sorted_scores = sorted ( scores . items ( ) , key = itemgetter ( 1 ) , reverse = True ) languages , probabilities = list ( zip ( * sorted_scores ) ) rescaled_probabilities = [ log ( proba ) fo...
List of most probable programming languages the list is ordered from the most probable to the least probable one .
17,204
def learn ( self , input_dir : str ) -> float : if self . is_default : LOGGER . error ( "Cannot learn using default model" ) raise GuesslangError ( 'Cannot learn using default "readonly" model' ) languages = self . languages LOGGER . info ( "Extract training data" ) extensions = [ ext for exts in languages . values ( )...
Learn languages features from source files .
17,205
def main ( ) : parser = argparse . ArgumentParser ( description = __doc__ ) parser . add_argument ( 'reportfile' , type = argparse . FileType ( 'r' ) , help = "test report file generated by `guesslang --test TESTDIR`" ) parser . add_argument ( '-d' , '--debug' , default = False , action = 'store_true' , help = "show de...
Report graph creator command line
17,206
def search_files ( source : str , extensions : List [ str ] ) -> List [ Path ] : files = [ path for path in Path ( source ) . glob ( '**/*' ) if path . is_file ( ) and path . suffix . lstrip ( '.' ) in extensions ] nb_files = len ( files ) LOGGER . debug ( "Total files found: %d" , nb_files ) if nb_files < NB_FILES_MIN...
Retrieve files located the source directory and its subdirectories whose extension match one of the listed extensions .
17,207
def extract_from_files ( files : List [ Path ] , languages : Dict [ str , List [ str ] ] ) -> DataSet : enumerator = enumerate ( sorted ( languages . items ( ) ) ) rank_map = { ext : rank for rank , ( _ , exts ) in enumerator for ext in exts } with multiprocessing . Pool ( initializer = _process_init ) as pool : file_i...
Extract arrays of features from the given files .
17,208
def safe_read_file ( file_path : Path ) -> str : for encoding in FILE_ENCODINGS : try : return file_path . read_text ( encoding = encoding ) except UnicodeError : pass raise GuesslangError ( 'Encoding not supported for {!s}' . format ( file_path ) )
Read a text file . Several text encodings are tried until the file content is correctly decoded .
17,209
def config_logging ( debug : bool = False ) -> None : if debug : level = 'DEBUG' tf_level = tf . logging . INFO else : level = 'INFO' tf_level = tf . logging . ERROR logging_config = config_dict ( 'logging.json' ) for logger in logging_config [ 'loggers' ] . values ( ) : logger [ 'level' ] = level logging . config . di...
Set - up application and tensorflow logging .
17,210
def config_dict ( name : str ) -> Dict [ str , Any ] : try : content = resource_string ( PACKAGE , DATADIR . format ( name ) ) . decode ( ) except DistributionNotFound as error : LOGGER . warning ( "Cannot load %s from packages: %s" , name , error ) content = DATA_FALLBACK . joinpath ( name ) . read_text ( ) return cas...
Load a JSON configuration dict from Guesslang config directory .
17,211
def model_info ( model_dir : Optional [ str ] = None ) -> Tuple [ str , bool ] : if model_dir is None : try : model_dir = resource_filename ( PACKAGE , DATADIR . format ( 'model' ) ) except DistributionNotFound as error : LOGGER . warning ( "Cannot load model from packages: %s" , error ) model_dir = str ( DATA_FALLBACK...
Retrieve Guesslang model directory name and tells if it is the default model .
17,212
def format ( self , record : logging . LogRecord ) -> str : if platform . system ( ) != 'Linux' : return super ( ) . format ( record ) record . msg = ( self . STYLE [ record . levelname ] + record . msg + self . STYLE [ 'END' ] ) record . levelname = ( self . STYLE [ 'LEVEL' ] + record . levelname + self . STYLE [ 'END...
Format log records to produce colored messages .
17,213
def main ( ) : parser = argparse . ArgumentParser ( description = __doc__ ) parser . add_argument ( 'githubtoken' , help = "Github OAuth token, see https://developer.github.com/v3/oauth/" ) parser . add_argument ( 'destination' , help = "location of the downloaded repos" ) parser . add_argument ( '-n' , '--nbrepo' , he...
Github repositories downloaded command line
17,214
def retry ( default = None ) : def decorator ( func ) : @ functools . wraps ( func ) def _wrapper ( * args , ** kw ) : for pos in range ( 1 , MAX_RETRIES ) : try : return func ( * args , ** kw ) except ( RuntimeError , requests . ConnectionError ) as error : LOGGER . warning ( "Failed: %s, %s" , type ( error ) , error ...
Retry functions after failures
17,215
def main ( ) : parser = argparse . ArgumentParser ( description = __doc__ ) parser . add_argument ( 'learn' , help = "learning source codes directory" ) parser . add_argument ( 'keywords' , help = "output keywords file, JSON" ) parser . add_argument ( '-n' , '--nbkeywords' , type = int , default = 10000 , help = "the n...
Keywords generator command line
17,216
def main ( ) -> None : try : _real_main ( ) except GuesslangError as error : LOGGER . critical ( "Failed: %s" , error ) sys . exit ( - 1 ) except KeyboardInterrupt : LOGGER . critical ( "Cancelled!" ) sys . exit ( - 2 )
Run command line
17,217
def split ( text : str ) -> List [ str ] : return [ word for word in SEPARATOR . split ( text ) if word . strip ( ' \t' ) ]
Split a text into a list of tokens .
17,218
def main ( ) : parser = argparse . ArgumentParser ( description = __doc__ , formatter_class = argparse . RawDescriptionHelpFormatter ) parser . add_argument ( 'source' , help = "location of the downloaded repos" ) parser . add_argument ( 'destination' , help = "location of the extracted files" ) parser . add_argument (...
Files extractor command line
17,219
def combine_slices ( slice_datasets , rescale = None ) : if len ( slice_datasets ) == 0 : raise DicomImportException ( "Must provide at least one DICOM dataset" ) _validate_slices_form_uniform_grid ( slice_datasets ) voxels = _merge_slice_pixel_arrays ( slice_datasets , rescale ) transform = _ijk_to_patient_xyz_transfo...
Given a list of pydicom datasets for an image series stitch them together into a three - dimensional numpy array . Also calculate a 4x4 affine transformation matrix that converts the ijk - pixel - indices into the xyz - coordinates in the DICOM patient s coordinate system .
17,220
def _validate_slices_form_uniform_grid ( slice_datasets ) : invariant_properties = [ 'Modality' , 'SOPClassUID' , 'SeriesInstanceUID' , 'Rows' , 'Columns' , 'PixelSpacing' , 'PixelRepresentation' , 'BitsAllocated' , 'BitsStored' , 'HighBit' , ] for property_name in invariant_properties : _slice_attribute_equal ( slice_...
Perform various data checks to ensure that the list of slices form a evenly - spaced grid of data . Some of these checks are probably not required if the data follows the DICOM specification however it seems pertinent to check anyway .
17,221
def parse_url ( cls , string ) : match = cls . URL_RE . match ( string ) if not match : raise InvalidKeyError ( cls , string ) return match . groupdict ( )
If it can be parsed as a version_guid with no preceding org + offering returns a dict with key version_guid and the value
17,222
def offering ( self ) : warnings . warn ( "Offering is no longer a supported property of Locator. Please use the course and run properties." , DeprecationWarning , stacklevel = 2 ) if not self . course and not self . run : return None elif not self . run and self . course : return self . course return "/" . join ( [ se...
Deprecated . Use course and run independently .
17,223
def make_usage_key_from_deprecated_string ( self , location_url ) : warnings . warn ( "make_usage_key_from_deprecated_string is deprecated! Please use make_usage_key" , DeprecationWarning , stacklevel = 2 ) return BlockUsageLocator . from_string ( location_url ) . replace ( run = self . run )
Deprecated mechanism for creating a UsageKey given a CourseKey and a serialized Location .
17,224
def _from_string ( cls , serialized ) : course_key = CourseLocator . _from_string ( serialized ) parsed_parts = cls . parse_url ( serialized ) block_id = parsed_parts . get ( 'block_id' , None ) if block_id is None : raise InvalidKeyError ( cls , serialized ) return cls ( course_key , parsed_parts . get ( 'block_type' ...
Requests CourseLocator to deserialize its part and then adds the local deserialization of block
17,225
def _parse_block_ref ( cls , block_ref , deprecated = False ) : if deprecated and block_ref is None : return None if isinstance ( block_ref , LocalId ) : return block_ref is_valid_deprecated = deprecated and cls . DEPRECATED_ALLOWED_ID_RE . match ( block_ref ) is_valid = cls . ALLOWED_ID_RE . match ( block_ref ) if is_...
Given block_ref tries to parse it into a valid block reference .
17,226
def html_id ( self ) : if self . deprecated : id_fields = [ self . DEPRECATED_TAG , self . org , self . course , self . block_type , self . block_id , self . version_guid ] id_string = u"-" . join ( [ v for v in id_fields if v is not None ] ) return self . clean_for_html ( id_string ) else : return self . block_id
Return an id which can be used on an html page as an id attr of an html element . It is currently also persisted by some clients to identify blocks .
17,227
def to_deprecated_son ( self , prefix = '' , tag = 'i4x' ) : son = SON ( { prefix + 'tag' : tag } ) for field_name in ( 'org' , 'course' ) : son [ prefix + field_name ] = getattr ( self . course_key , field_name ) for ( dep_field_name , field_name ) in [ ( 'category' , 'block_type' ) , ( 'name' , 'block_id' ) ] : son [...
Returns a SON object that represents this location
17,228
def _from_deprecated_son ( cls , id_dict , run ) : course_key = CourseLocator ( id_dict [ 'org' ] , id_dict [ 'course' ] , run , id_dict [ 'revision' ] , deprecated = True , ) return cls ( course_key , id_dict [ 'category' ] , id_dict [ 'name' ] , deprecated = True )
Return the Location decoding this id_dict and run
17,229
def _from_string ( cls , serialized ) : library_key = LibraryLocator . _from_string ( serialized ) parsed_parts = LibraryLocator . parse_url ( serialized ) block_id = parsed_parts . get ( 'block_id' , None ) if block_id is None : raise InvalidKeyError ( cls , serialized ) block_type = parsed_parts . get ( 'block_type' ...
Requests LibraryLocator to deserialize its part and then adds the local deserialization of block
17,230
def for_branch ( self , branch ) : return self . replace ( library_key = self . library_key . for_branch ( branch ) )
Return a UsageLocator for the same block in a different branch of the library .
17,231
def for_version ( self , version_guid ) : return self . replace ( library_key = self . library_key . for_version ( version_guid ) )
Return a UsageLocator for the same block in a different version of the library .
17,232
def _strip_object ( key ) : if hasattr ( key , 'version_agnostic' ) and hasattr ( key , 'for_branch' ) : return key . for_branch ( None ) . version_agnostic ( ) else : return key
Strips branch and version info if the given key supports those attributes .
17,233
def _strip_value ( value , lookup = 'exact' ) : if lookup == 'in' : stripped_value = [ _strip_object ( el ) for el in value ] else : stripped_value = _strip_object ( value ) return stripped_value
Helper function to remove the branch and version information from the given value which could be a single object or a list .
17,234
def _deprecation_warning ( cls ) : if issubclass ( cls , Location ) : warnings . warn ( "Location is deprecated! Please use locator.BlockUsageLocator" , DeprecationWarning , stacklevel = 3 ) elif issubclass ( cls , AssetLocation ) : warnings . warn ( "AssetLocation is deprecated! Please use locator.AssetLocator" , Depr...
Display a deprecation warning for the given cls
17,235
def _check_location_part ( cls , val , regexp ) : cls . _deprecation_warning ( ) return CourseLocator . _check_location_part ( val , regexp )
Deprecated . See CourseLocator . _check_location_part
17,236
def _clean ( cls , value , invalid ) : cls . _deprecation_warning ( ) return BlockUsageLocator . _clean ( value , invalid )
Deprecated . See BlockUsageLocator . _clean
17,237
def _join_keys_v1 ( left , right ) : if left . endswith ( ':' ) or '::' in left : raise ValueError ( "Can't join a left string ending in ':' or containing '::'" ) return u"{}::{}" . format ( _encode_v1 ( left ) , _encode_v1 ( right ) )
Join two keys into a format separable by using _split_keys_v1 .
17,238
def _split_keys_v1 ( joined ) : left , _ , right = joined . partition ( '::' ) return _decode_v1 ( left ) , _decode_v1 ( right )
Split two keys out a string created by _join_keys_v1 .
17,239
def _split_keys_v2 ( joined ) : left , _ , right = joined . rpartition ( '::' ) return _decode_v2 ( left ) , _decode_v2 ( right )
Split two keys out a string created by _join_keys_v2 .
17,240
def refresher ( name , refreshers = CompletionRefresher . refreshers ) : def wrapper ( wrapped ) : refreshers [ name ] = wrapped return wrapped return wrapper
Decorator to add the decorated function to the dictionary of refreshers . Any function decorated with a
17,241
def refresh ( self , executor , callbacks , completer_options = None ) : if completer_options is None : completer_options = { } if self . is_refreshing ( ) : self . _restart_refresh . set ( ) return [ ( None , None , None , 'Auto-completion refresh restarted.' ) ] else : self . _completer_thread = threading . Thread ( ...
Creates a SQLCompleter object and populates it with the relevant completion suggestions in a background thread .
17,242
def handle_cd_command ( arg ) : CD_CMD = 'cd' tokens = arg . split ( CD_CMD + ' ' ) directory = tokens [ - 1 ] if len ( tokens ) > 1 else None if not directory : return False , "No folder name was provided." try : os . chdir ( directory ) subprocess . call ( [ 'pwd' ] ) return True , None except OSError as e : return F...
Handles a cd shell command by calling python s os . chdir .
17,243
def get_editor_query ( sql ) : sql = sql . strip ( ) pattern = re . compile ( '(^\\\e|\\\e$)' ) while pattern . search ( sql ) : sql = pattern . sub ( '' , sql ) return sql
Get the query part of an editor command .
17,244
def delete_favorite_query ( arg , ** _ ) : usage = 'Syntax: \\fd name.\n\n' + favoritequeries . usage if not arg : return [ ( None , None , None , usage ) ] status = favoritequeries . delete ( arg ) return [ ( None , None , None , status ) ]
Delete an existing favorite query .
17,245
def execute_system_command ( arg , ** _ ) : usage = "Syntax: system [command].\n" if not arg : return [ ( None , None , None , usage ) ] try : command = arg . strip ( ) if command . startswith ( 'cd' ) : ok , error_message = handle_cd_command ( arg ) if not ok : return [ ( None , None , None , error_message ) ] return ...
Execute a system shell command .
17,246
def need_completion_refresh ( queries ) : tokens = { 'use' , '\\u' , 'create' , 'drop' } for query in sqlparse . split ( queries ) : try : first_token = query . split ( ) [ 0 ] if first_token . lower ( ) in tokens : return True except Exception : return False
Determines if the completion needs a refresh by checking if the sql statement is an alter create drop or change db .
17,247
def is_mutating ( status ) : if not status : return False mutating = set ( [ 'insert' , 'update' , 'delete' , 'alter' , 'create' , 'drop' , 'replace' , 'truncate' , 'load' ] ) return status . split ( None , 1 ) [ 0 ] . lower ( ) in mutating
Determines if the statement is mutating based on the status .
17,248
def change_prompt_format ( self , arg , ** _ ) : if not arg : message = 'Missing required argument, format.' return [ ( None , None , None , message ) ] self . prompt = self . get_prompt ( arg ) return [ ( None , None , None , "Changed prompt format to %s" % arg ) ]
Change the prompt format .
17,249
def get_output_margin ( self , status = None ) : margin = self . get_reserved_space ( ) + self . get_prompt ( self . prompt ) . count ( '\n' ) + 1 if special . is_timing_enabled ( ) : margin += 1 if status : margin += 1 + status . count ( '\n' ) return margin
Get the output margin ( number of rows for the prompt footer and timing message .
17,250
def output ( self , output , status = None ) : if output : size = self . cli . output . get_size ( ) margin = self . get_output_margin ( status ) fits = True buf = [ ] output_via_pager = self . explicit_pager and special . is_pager_enabled ( ) for i , line in enumerate ( output , 1 ) : special . write_tee ( line ) spec...
Output text to stdout or a pager command . The status text is not outputted to pager or files . The message will be logged in the audit log if enabled . The message will be written to the tee file if enabled . The message will be written to the output file if enabled .
17,251
def _on_completions_refreshed ( self , new_completer ) : with self . _completer_lock : self . completer = new_completer if self . cli : self . cli . current_buffer . completer = new_completer if self . cli : self . cli . request_redraw ( )
Swap the completer object in cli with the newly created completer .
17,252
def get_reserved_space ( self ) : reserved_space_ratio = .45 max_reserved_space = 8 _ , height = click . get_terminal_size ( ) return min ( int ( round ( height * reserved_space_ratio ) ) , max_reserved_space )
Get the number of lines to reserve for the completion menu .
17,253
def find_matches ( text , collection , start_only = False , fuzzy = True , casing = None ) : last = last_word ( text , include = 'most_punctuations' ) text = last . lower ( ) completions = [ ] if fuzzy : regex = '.*?' . join ( map ( escape , text ) ) pat = compile ( '(%s)' % regex ) for item in sorted ( collection ) : ...
Find completion matches for the given text . Given the user s input text and a collection of available completions find completions matching the last word of the text . If start_only is True the text will match an available completion only at the beginning . Otherwise a completion is considered a match if the text appe...
17,254
def log ( logger , level , message ) : if logger . parent . name != 'root' : logger . log ( level , message ) else : print ( message , file = sys . stderr )
Logs message to stderr if logging isn t initialized .
17,255
def read_config_file ( f ) : if isinstance ( f , basestring ) : f = os . path . expanduser ( f ) try : config = ConfigObj ( f , interpolation = False , encoding = 'utf8' ) except ConfigObjError as e : log ( LOGGER , logging . ERROR , "Unable to parse line {0} of config file " "'{1}'." . format ( e . line_number , f ) )...
Read a config file .
17,256
def read_config_files ( files ) : config = ConfigObj ( ) for _file in files : _config = read_config_file ( _file ) if bool ( _config ) is True : config . merge ( _config ) config . filename = _config . filename return config
Read and merge a list of config files .
17,257
def cli_bindings ( ) : key_binding_manager = KeyBindingManager ( enable_open_in_editor = True , enable_system_bindings = True , enable_auto_suggest_bindings = True , enable_search = True , enable_abort_and_exit_bindings = True ) @ key_binding_manager . registry . add_binding ( Keys . F2 ) def _ ( event ) : _logger . de...
Custom key bindings for cli .
17,258
def prompt ( * args , ** kwargs ) : try : return click . prompt ( * args , ** kwargs ) except click . Abort : return False
Prompt the user for input and handle any abort exceptions .
17,259
def run ( self , statement ) : statement = statement . strip ( ) if not statement : yield ( None , None , None , None ) components = sqlparse . split ( statement ) for sql in components : sql = sql . rstrip ( ';' ) if sql . endswith ( '\\G' ) : special . set_expanded_output ( True ) sql = sql [ : - 2 ] . strip ( ) cur ...
Execute the sql in the database and return the results .
17,260
def get_result ( self , cursor ) : title = headers = None if cursor . description is not None : headers = [ x [ 0 ] for x in cursor . description ] rows = cursor . fetchall ( ) status = '%d row%s in set' % ( len ( rows ) , '' if len ( rows ) == 1 else 's' ) else : logger . debug ( 'No rows in result.' ) rows = None sta...
Get the current result s data from the cursor .
17,261
def tables ( self ) : with self . conn . cursor ( ) as cur : cur . execute ( self . TABLES_QUERY ) for row in cur : yield row
Yields table names .
17,262
def table_columns ( self ) : with self . conn . cursor ( ) as cur : cur . execute ( self . TABLE_COLUMNS_QUERY % self . database ) for row in cur : yield row
Yields column names .
17,263
def create_toolbar_tokens_func ( get_is_refreshing , show_fish_help ) : token = Token . Toolbar def get_toolbar_tokens ( cli ) : result = [ ] result . append ( ( token , ' ' ) ) if cli . buffers [ DEFAULT_BUFFER ] . always_multiline : result . append ( ( token . On , '[F3] Multiline: ON ' ) ) else : result . append ( ...
Return a function that generates the toolbar tokens .
17,264
def _get_vi_mode ( cli ) : return { InputMode . INSERT : 'I' , InputMode . NAVIGATION : 'N' , InputMode . REPLACE : 'R' , InputMode . INSERT_MULTIPLE : 'M' } [ cli . vi_state . input_mode ]
Get the current vi mode for display .
17,265
def export ( defn ) : globals ( ) [ defn . __name__ ] = defn __all__ . append ( defn . __name__ ) return defn
Decorator to explicitly mark functions that are exposed in a lib .
17,266
def execute ( cur , sql ) : command , verbose , arg = parse_special_command ( sql ) if ( command not in COMMANDS ) and ( command . lower ( ) not in COMMANDS ) : raise CommandNotFound try : special_cmd = COMMANDS [ command ] except KeyError : special_cmd = COMMANDS [ command . lower ( ) ] if special_cmd . case_sensitive...
Execute a special command and return the results . If the special command is not supported a KeyError will be raised .
17,267
def find_prev_keyword ( sql ) : if not sql . strip ( ) : return None , '' parsed = sqlparse . parse ( sql ) [ 0 ] flattened = list ( parsed . flatten ( ) ) logical_operators = ( 'AND' , 'OR' , 'NOT' , 'BETWEEN' ) for t in reversed ( flattened ) : if t . value == '(' or ( t . is_keyword and ( t . value . upper ( ) not i...
Find the last sql keyword in an SQL statement Returns the value of the last keyword and the text of the query with everything after the last keyword stripped
17,268
def _get_thumbnail_options ( self , context , instance ) : width , height = None , None subject_location = False placeholder_width = context . get ( 'width' , None ) placeholder_height = context . get ( 'height' , None ) if instance . use_autoscale and placeholder_width : width = int ( placeholder_width ) if instance ....
Return the size and options of the thumbnail that should be inserted
17,269
def create_image_plugin ( filename , image , parent_plugin , ** kwargs ) : from cmsplugin_filer_image . models import FilerImage from filer . models import Image image_plugin = FilerImage ( ) image_plugin . placeholder = parent_plugin . placeholder image_plugin . parent = CMSPlugin . objects . get ( pk = parent_plugin ...
Used for drag - n - drop image insertion with djangocms - text - ckeditor . Set TEXT_SAVE_IMAGE_FUNCTION = cmsplugin_filer_image . integrations . ckeditor . create_image_plugin to enable .
17,270
def rename_tables ( db , table_mapping , reverse = False ) : from django . db import connection if reverse : table_mapping = [ ( dst , src ) for src , dst in table_mapping ] table_names = connection . introspection . table_names ( ) for source , destination in table_mapping : if source in table_names and destination in...
renames tables from source to destination name if the source exists and the destination does not exist yet .
17,271
def group_and_sort_statements ( stmt_list , ev_totals = None ) : def _count ( stmt ) : if ev_totals is None : return len ( stmt . evidence ) else : return ev_totals [ stmt . get_hash ( ) ] stmt_rows = defaultdict ( list ) stmt_counts = defaultdict ( lambda : 0 ) arg_counts = defaultdict ( lambda : 0 ) for key , s in _g...
Group statements by type and arguments and sort by prevalence .
17,272
def make_stmt_from_sort_key ( key , verb ) : def make_agent ( name ) : if name == 'None' or name is None : return None return Agent ( name ) StmtClass = get_statement_by_name ( verb ) inps = list ( key [ 1 ] ) if verb == 'Complex' : stmt = StmtClass ( [ make_agent ( name ) for name in inps ] ) elif verb == 'Conversion'...
Make a Statement from the sort key .
17,273
def get_ecs_cluster_for_queue ( queue_name , batch_client = None ) : if batch_client is None : batch_client = boto3 . client ( 'batch' ) queue_resp = batch_client . describe_job_queues ( jobQueues = [ queue_name ] ) if len ( queue_resp [ 'jobQueues' ] ) == 1 : queue = queue_resp [ 'jobQueues' ] [ 0 ] else : raise Batch...
Get the name of the ecs cluster using the batch client .
17,274
def tag_instances_on_cluster ( cluster_name , project = 'cwc' ) : ecs = boto3 . client ( 'ecs' ) task_arns = ecs . list_tasks ( cluster = cluster_name ) [ 'taskArns' ] if not task_arns : return tasks = ecs . describe_tasks ( cluster = cluster_name , tasks = task_arns ) [ 'tasks' ] container_instances = ecs . describe_c...
Adds project tag to untagged instances in a given cluster .
17,275
def submit_reading ( basename , pmid_list_filename , readers , start_ix = None , end_ix = None , pmids_per_job = 3000 , num_tries = 2 , force_read = False , force_fulltext = False , project_name = None ) : sub = PmidSubmitter ( basename , readers , project_name ) sub . set_options ( force_read , force_fulltext ) sub . ...
Submit an old - style pmid - centered no - database s3 only reading job .
17,276
def submit_combine ( basename , readers , job_ids = None , project_name = None ) : sub = PmidSubmitter ( basename , readers , project_name ) sub . job_list = job_ids sub . submit_combine ( ) return sub
Submit a batch job to combine the outputs of a reading job .
17,277
def submit_reading ( self , input_fname , start_ix , end_ix , ids_per_job , num_tries = 1 , stagger = 0 ) : self . ids_per_job = ids_per_job id_list_key = 'reading_results/%s/%s' % ( self . basename , self . _s3_input_name ) s3_client = boto3 . client ( 's3' ) s3_client . upload_file ( input_fname , bucket_name , id_li...
Submit a batch of reading jobs
17,278
def watch_and_wait ( self , poll_interval = 10 , idle_log_timeout = None , kill_on_timeout = False , stash_log_method = None , tag_instances = False , ** kwargs ) : return wait_for_complete ( self . _job_queue , job_list = self . job_list , job_name_prefix = self . basename , poll_interval = poll_interval , idle_log_ti...
This provides shortcut access to the wait_for_complete_function .
17,279
def run ( self , input_fname , ids_per_job , stagger = 0 , ** wait_params ) : submit_thread = Thread ( target = self . submit_reading , args = ( input_fname , 0 , None , ids_per_job ) , kwargs = { 'stagger' : stagger } , daemon = True ) submit_thread . start ( ) self . watch_and_wait ( ** wait_params ) submit_thread . ...
Run this submission all the way .
17,280
def set_options ( self , force_read = False , force_fulltext = False ) : self . options [ 'force_read' ] = force_read self . options [ 'force_fulltext' ] = force_fulltext return
Set the options for this run .
17,281
def get_chebi_name_from_id ( chebi_id , offline = False ) : chebi_name = chebi_id_to_name . get ( chebi_id ) if chebi_name is None and not offline : chebi_name = get_chebi_name_from_id_web ( chebi_id ) return chebi_name
Return a ChEBI name corresponding to the given ChEBI ID .
17,282
def get_chebi_name_from_id_web ( chebi_id ) : url_base = 'http://www.ebi.ac.uk/webservices/chebi/2.0/test/' url_fmt = url_base + 'getCompleteEntity?chebiId=%s' resp = requests . get ( url_fmt % chebi_id ) if resp . status_code != 200 : logger . warning ( "Got bad code form CHEBI client: %s" % resp . status_code ) retur...
Return a ChEBI mame corresponding to a given ChEBI ID using a REST API .
17,283
def get_subnetwork ( statements , nodes , relevance_network = None , relevance_node_lim = 10 ) : if relevance_network is not None : relevant_nodes = _find_relevant_nodes ( nodes , relevance_network , relevance_node_lim ) all_nodes = nodes + relevant_nodes else : all_nodes = nodes filtered_statements = _filter_statement...
Return a PySB model based on a subset of given INDRA Statements .
17,284
def _filter_statements ( statements , agents ) : filtered_statements = [ ] for s in stmts : if all ( [ a is not None for a in s . agent_list ( ) ] ) and all ( [ a . name in agents for a in s . agent_list ( ) ] ) : filtered_statements . append ( s ) return filtered_statements
Return INDRA Statements which have Agents in the given list .
17,285
def _find_relevant_nodes ( query_nodes , relevance_network , relevance_node_lim ) : all_nodes = relevance_client . get_relevant_nodes ( relevance_network , query_nodes ) nodes = [ n [ 0 ] for n in all_nodes [ : relevance_node_lim ] ] return nodes
Return a list of nodes that are relevant for the query .
17,286
def process_jsonld_file ( fname ) : with open ( fname , 'r' ) as fh : json_dict = json . load ( fh ) return process_jsonld ( json_dict )
Process a JSON - LD file in the new format to extract Statements .
17,287
def tag_instance ( instance_id , ** tags ) : logger . debug ( "Got request to add tags %s to instance %s." % ( str ( tags ) , instance_id ) ) ec2 = boto3 . resource ( 'ec2' ) instance = ec2 . Instance ( instance_id ) filtered_tags = { k : v for k , v in tags . items ( ) if v and k } if instance . tags is not None : exi...
Tag a single ec2 instance .
17,288
def tag_myself ( project = 'cwc' , ** other_tags ) : base_url = "http://169.254.169.254" try : resp = requests . get ( base_url + "/latest/meta-data/instance-id" ) except requests . exceptions . ConnectionError : logger . warning ( "Could not connect to service. Note this should only " "be run from within a batch job."...
Function run when indra is used in an EC2 instance to apply tags .
17,289
def get_batch_command ( command_list , project = None , purpose = None ) : command_str = ' ' . join ( command_list ) ret = [ 'python' , '-m' , 'indra.util.aws' , 'run_in_batch' , command_str ] if not project and has_config ( 'DEFAULT_AWS_PROJECT' ) : project = get_config ( 'DEFAULT_AWS_PROJECT' ) if project : ret += [ ...
Get the command appropriate for running something on batch .
17,290
def get_jobs ( job_queue = 'run_reach_queue' , job_status = 'RUNNING' ) : batch = boto3 . client ( 'batch' ) jobs = batch . list_jobs ( jobQueue = job_queue , jobStatus = job_status ) return jobs . get ( 'jobSummaryList' )
Returns a list of dicts with jobName and jobId for each job with the given status .
17,291
def get_job_log ( job_info , log_group_name = '/aws/batch/job' , write_file = True , verbose = False ) : job_name = job_info [ 'jobName' ] job_id = job_info [ 'jobId' ] logs = boto3 . client ( 'logs' ) batch = boto3 . client ( 'batch' ) resp = batch . describe_jobs ( jobs = [ job_id ] ) job_desc = resp [ 'jobs' ] [ 0 ]...
Gets the Cloudwatch log associated with the given job .
17,292
def get_log_by_name ( log_group_name , log_stream_name , out_file = None , verbose = True ) : logs = boto3 . client ( 'logs' ) kwargs = { 'logGroupName' : log_group_name , 'logStreamName' : log_stream_name , 'startFromHead' : True } lines = [ ] while True : response = logs . get_log_events ( ** kwargs ) if response . g...
Download a log given the log s group and stream name .
17,293
def dump_logs ( job_queue = 'run_reach_queue' , job_status = 'RUNNING' ) : jobs = get_jobs ( job_queue , job_status ) for job in jobs : get_job_log ( job , write_file = True )
Write logs for all jobs with given the status to files .
17,294
def get_s3_file_tree ( s3 , bucket , prefix ) : def get_some_keys ( keys , marker = None ) : if marker : relevant_files = s3 . list_objects ( Bucket = bucket , Prefix = prefix , Marker = marker ) else : relevant_files = s3 . list_objects ( Bucket = bucket , Prefix = prefix ) keys . extend ( [ entry [ 'Key' ] for entry ...
Overcome s3 response limit and return NestedDict tree of paths .
17,295
def print_model ( self , include_unsigned_edges = False ) : sif_str = '' for edge in self . graph . edges ( data = True ) : n1 = edge [ 0 ] n2 = edge [ 1 ] data = edge [ 2 ] polarity = data . get ( 'polarity' ) if polarity == 'negative' : rel = '-1' elif polarity == 'positive' : rel = '1' elif include_unsigned_edges : ...
Return a SIF string of the assembled model .
17,296
def save_model ( self , fname , include_unsigned_edges = False ) : sif_str = self . print_model ( include_unsigned_edges ) with open ( fname , 'wb' ) as fh : fh . write ( sif_str . encode ( 'utf-8' ) )
Save the assembled model s SIF string into a file .
17,297
def print_boolean_net ( self , out_file = None ) : init_str = '' for node_key in self . graph . nodes ( ) : node_name = self . graph . node [ node_key ] [ 'name' ] init_str += '%s = False\n' % node_name rule_str = '' for node_key in self . graph . nodes ( ) : node_name = self . graph . node [ node_key ] [ 'name' ] in_e...
Return a Boolean network from the assembled graph .
17,298
def _ensure_api_keys ( task_desc , failure_ret = None ) : def check_func_wrapper ( func ) : @ wraps ( func ) def check_api_keys ( * args , ** kwargs ) : global ELSEVIER_KEYS if ELSEVIER_KEYS is None : ELSEVIER_KEYS = { } if not has_config ( INST_KEY_ENV_NAME ) : logger . warning ( 'Institution API key %s not found in c...
Wrap Elsevier methods which directly use the API keys .
17,299
def check_entitlement ( doi ) : if doi . lower ( ) . startswith ( 'doi:' ) : doi = doi [ 4 : ] url = '%s/%s' % ( elsevier_entitlement_url , doi ) params = { 'httpAccept' : 'text/xml' } res = requests . get ( url , params , headers = ELSEVIER_KEYS ) if not res . status_code == 200 : logger . error ( 'Could not check ent...
Check whether IP and credentials enable access to content for a doi .