idx
int64
0
63k
question
stringlengths
53
5.28k
target
stringlengths
5
805
20,000
def _fast_hit_windows ( ref , est , window ) : ref = np . asarray ( ref ) est = np . asarray ( est ) ref_idx = np . argsort ( ref ) ref_sorted = ref [ ref_idx ] left_idx = np . searchsorted ( ref_sorted , est - window , side = 'left' ) right_idx = np . searchsorted ( ref_sorted , est + window , side = 'right' ) hit_ref...
Fast calculation of windowed hits for time events .
20,001
def validate_events ( events , max_time = 30000. ) : if ( events > max_time ) . any ( ) : raise ValueError ( 'An event at time {} was found which is greater than ' 'the maximum allowable time of max_time = {} (did you' ' supply event times in ' 'seconds?)' . format ( events . max ( ) , max_time ) ) if events . ndim != ...
Checks that a 1 - d event location ndarray is well - formed and raises errors if not .
20,002
def validate_frequencies ( frequencies , max_freq , min_freq , allow_negatives = False ) : if allow_negatives : frequencies = np . abs ( frequencies ) if ( np . abs ( frequencies ) > max_freq ) . any ( ) : raise ValueError ( 'A frequency of {} was found which is greater than ' 'the maximum allowable value of max_freq =...
Checks that a 1 - d frequency ndarray is well - formed and raises errors if not .
20,003
def intervals_to_durations ( intervals ) : validate_intervals ( intervals ) return np . abs ( np . diff ( intervals , axis = - 1 ) ) . flatten ( )
Converts an array of n intervals to their n durations .
20,004
def validate ( reference_sources , estimated_sources ) : if reference_sources . shape != estimated_sources . shape : raise ValueError ( 'The shape of estimated sources and the true ' 'sources should match. reference_sources.shape ' '= {}, estimated_sources.shape ' '= {}' . format ( reference_sources . shape , estimate...
Checks that the input data to a metric are valid and throws helpful errors if not .
20,005
def _any_source_silent ( sources ) : return np . any ( np . all ( np . sum ( sources , axis = tuple ( range ( 2 , sources . ndim ) ) ) == 0 , axis = 1 ) )
Returns true if the parameter sources has any silent first dimensions
20,006
def bss_eval_sources ( reference_sources , estimated_sources , compute_permutation = True ) : if estimated_sources . ndim == 1 : estimated_sources = estimated_sources [ np . newaxis , : ] if reference_sources . ndim == 1 : reference_sources = reference_sources [ np . newaxis , : ] validate ( reference_sources , estimat...
Ordering and measurement of the separation quality for estimated source signals in terms of filtered true source interference and artifacts .
20,007
def bss_eval_sources_framewise ( reference_sources , estimated_sources , window = 30 * 44100 , hop = 15 * 44100 , compute_permutation = False ) : if estimated_sources . ndim == 1 : estimated_sources = estimated_sources [ np . newaxis , : ] if reference_sources . ndim == 1 : reference_sources = reference_sources [ np . ...
Framewise computation of bss_eval_sources
20,008
def bss_eval_images_framewise ( reference_sources , estimated_sources , window = 30 * 44100 , hop = 15 * 44100 , compute_permutation = False ) : estimated_sources = np . atleast_3d ( estimated_sources ) reference_sources = np . atleast_3d ( reference_sources ) validate ( reference_sources , estimated_sources ) if refer...
Framewise computation of bss_eval_images
20,009
def _project ( reference_sources , estimated_source , flen ) : nsrc = reference_sources . shape [ 0 ] nsampl = reference_sources . shape [ 1 ] reference_sources = np . hstack ( ( reference_sources , np . zeros ( ( nsrc , flen - 1 ) ) ) ) estimated_source = np . hstack ( ( estimated_source , np . zeros ( flen - 1 ) ) ) ...
Least - squares projection of estimated source on the subspace spanned by delayed versions of reference sources with delays between 0 and flen - 1
20,010
def _bss_image_crit ( s_true , e_spat , e_interf , e_artif ) : sdr = _safe_db ( np . sum ( s_true ** 2 ) , np . sum ( ( e_spat + e_interf + e_artif ) ** 2 ) ) isr = _safe_db ( np . sum ( s_true ** 2 ) , np . sum ( e_spat ** 2 ) ) sir = _safe_db ( np . sum ( ( s_true + e_spat ) ** 2 ) , np . sum ( e_interf ** 2 ) ) sar ...
Measurement of the separation quality for a given image in terms of filtered true source spatial error interference and artifacts .
20,011
def _safe_db ( num , den ) : if den == 0 : return np . Inf return 10 * np . log10 ( num / den )
Properly handle the potential + Inf db SIR instead of raising a RuntimeWarning . Only denominator is checked because the numerator can never be 0 .
20,012
def evaluate ( reference_sources , estimated_sources , ** kwargs ) : scores = collections . OrderedDict ( ) sdr , isr , sir , sar , perm = util . filter_kwargs ( bss_eval_images , reference_sources , estimated_sources , ** kwargs ) scores [ 'Images - Source to Distortion' ] = sdr . tolist ( ) scores [ 'Images - Image t...
Compute all metrics for the given reference and estimated signals .
20,013
def clicks ( times , fs , click = None , length = None ) : if click is None : click = np . sin ( 2 * np . pi * np . arange ( fs * .1 ) * 1000 / ( 1. * fs ) ) click *= np . exp ( - np . arange ( fs * .1 ) / ( fs * .01 ) ) if length is None : length = int ( times . max ( ) * fs + click . shape [ 0 ] + 1 ) click_signal = ...
Returns a signal with the signal click placed at each specified time
20,014
def time_frequency ( gram , frequencies , times , fs , function = np . sin , length = None , n_dec = 1 ) : if times . ndim == 1 : times = util . boundaries_to_intervals ( times ) if length is None : length = int ( times [ - 1 , 1 ] * fs ) times , _ = util . adjust_intervals ( times , t_max = length ) n_times = gram . s...
Reverse synthesis of a time - frequency representation of a signal
20,015
def pitch_contour ( times , frequencies , fs , amplitudes = None , function = np . sin , length = None , kind = 'linear' ) : fs = float ( fs ) if length is None : length = int ( times . max ( ) * fs ) frequencies = np . maximum ( frequencies , 0.0 ) f_interp = interp1d ( times * fs , 2 * np . pi * frequencies / fs , ki...
Sonify a pitch contour .
20,016
def chords ( chord_labels , intervals , fs , ** kwargs ) : util . validate_intervals ( intervals ) roots , interval_bitmaps , _ = chord . encode_many ( chord_labels ) chromagram = np . array ( [ np . roll ( interval_bitmap , root ) for ( interval_bitmap , root ) in zip ( interval_bitmaps , roots ) ] ) . T return chroma...
Synthesizes chord labels
20,017
def validate ( reference_onsets , estimated_onsets ) : if reference_onsets . size == 0 : warnings . warn ( "Reference onsets are empty." ) if estimated_onsets . size == 0 : warnings . warn ( "Estimated onsets are empty." ) for onsets in [ reference_onsets , estimated_onsets ] : util . validate_events ( onsets , MAX_TIM...
Checks that the input annotations to a metric look like valid onset time arrays and throws helpful errors if not .
20,018
def f_measure ( reference_onsets , estimated_onsets , window = .05 ) : validate ( reference_onsets , estimated_onsets ) if reference_onsets . size == 0 or estimated_onsets . size == 0 : return 0. , 0. , 0. matching = util . match_events ( reference_onsets , estimated_onsets , window ) precision = float ( len ( matching...
Compute the F - measure of correct vs incorrectly predicted onsets . Corectness is determined over a small window .
20,019
def validate ( ref_intervals , ref_pitches , est_intervals , est_pitches ) : validate_intervals ( ref_intervals , est_intervals ) if not ref_intervals . shape [ 0 ] == ref_pitches . shape [ 0 ] : raise ValueError ( 'Reference intervals and pitches have different ' 'lengths.' ) if not est_intervals . shape [ 0 ] == est_...
Checks that the input annotations to a metric look like time intervals and a pitch list and throws helpful errors if not .
20,020
def validate_intervals ( ref_intervals , est_intervals ) : if ref_intervals . size == 0 : warnings . warn ( "Reference notes are empty." ) if est_intervals . size == 0 : warnings . warn ( "Estimated notes are empty." ) util . validate_intervals ( ref_intervals ) util . validate_intervals ( est_intervals )
Checks that the input annotations to a metric look like time intervals and throws helpful errors if not .
20,021
def match_note_offsets ( ref_intervals , est_intervals , offset_ratio = 0.2 , offset_min_tolerance = 0.05 , strict = False ) : if strict : cmp_func = np . less else : cmp_func = np . less_equal offset_distances = np . abs ( np . subtract . outer ( ref_intervals [ : , 1 ] , est_intervals [ : , 1 ] ) ) offset_distances =...
Compute a maximum matching between reference and estimated notes only taking note offsets into account .
20,022
def match_note_onsets ( ref_intervals , est_intervals , onset_tolerance = 0.05 , strict = False ) : if strict : cmp_func = np . less else : cmp_func = np . less_equal onset_distances = np . abs ( np . subtract . outer ( ref_intervals [ : , 0 ] , est_intervals [ : , 0 ] ) ) onset_distances = np . around ( onset_distance...
Compute a maximum matching between reference and estimated notes only taking note onsets into account .
20,023
def validate_voicing ( ref_voicing , est_voicing ) : if ref_voicing . size == 0 : warnings . warn ( "Reference voicing array is empty." ) if est_voicing . size == 0 : warnings . warn ( "Estimated voicing array is empty." ) if ref_voicing . sum ( ) == 0 : warnings . warn ( "Reference melody has no voiced frames." ) if e...
Checks that voicing inputs to a metric are in the correct format .
20,024
def hz2cents ( freq_hz , base_frequency = 10.0 ) : freq_cent = np . zeros ( freq_hz . shape [ 0 ] ) freq_nonz_ind = np . flatnonzero ( freq_hz ) normalized_frequency = np . abs ( freq_hz [ freq_nonz_ind ] ) / base_frequency freq_cent [ freq_nonz_ind ] = 1200 * np . log2 ( normalized_frequency ) return freq_cent
Convert an array of frequency values in Hz to cents . 0 values are left in place .
20,025
def constant_hop_timebase ( hop , end_time ) : end_time = np . round ( end_time , 10 ) times = np . linspace ( 0 , hop * int ( np . floor ( end_time / hop ) ) , int ( np . floor ( end_time / hop ) ) + 1 ) times = np . round ( times , 10 ) return times
Generates a time series from 0 to end_time with times spaced hop apart
20,026
def detection ( reference_intervals , estimated_intervals , window = 0.5 , beta = 1.0 , trim = False ) : validate_boundary ( reference_intervals , estimated_intervals , trim ) reference_boundaries = util . intervals_to_boundaries ( reference_intervals ) estimated_boundaries = util . intervals_to_boundaries ( estimated_...
Boundary detection hit - rate .
20,027
def deviation ( reference_intervals , estimated_intervals , trim = False ) : validate_boundary ( reference_intervals , estimated_intervals , trim ) reference_boundaries = util . intervals_to_boundaries ( reference_intervals ) estimated_boundaries = util . intervals_to_boundaries ( estimated_intervals ) if trim : refere...
Compute the median deviations between reference and estimated boundary times .
20,028
def pairwise ( reference_intervals , reference_labels , estimated_intervals , estimated_labels , frame_size = 0.1 , beta = 1.0 ) : validate_structure ( reference_intervals , reference_labels , estimated_intervals , estimated_labels ) if reference_intervals . size == 0 or estimated_intervals . size == 0 : return 0. , 0....
Frame - clustering segmentation evaluation by pair - wise agreement .
20,029
def _contingency_matrix ( reference_indices , estimated_indices ) : ref_classes , ref_class_idx = np . unique ( reference_indices , return_inverse = True ) est_classes , est_class_idx = np . unique ( estimated_indices , return_inverse = True ) n_ref_classes = ref_classes . shape [ 0 ] n_est_classes = est_classes . shap...
Computes the contingency matrix of a true labeling vs an estimated one .
20,030
def _adjusted_rand_index ( reference_indices , estimated_indices ) : n_samples = len ( reference_indices ) ref_classes = np . unique ( reference_indices ) est_classes = np . unique ( estimated_indices ) if ( ref_classes . shape [ 0 ] == est_classes . shape [ 0 ] == 1 or ref_classes . shape [ 0 ] == est_classes . shape ...
Compute the Rand index adjusted for change .
20,031
def _mutual_info_score ( reference_indices , estimated_indices , contingency = None ) : if contingency is None : contingency = _contingency_matrix ( reference_indices , estimated_indices ) . astype ( float ) contingency_sum = np . sum ( contingency ) pi = np . sum ( contingency , axis = 1 ) pj = np . sum ( contingency ...
Compute the mutual information between two sequence labelings .
20,032
def _entropy ( labels ) : if len ( labels ) == 0 : return 1.0 label_idx = np . unique ( labels , return_inverse = True ) [ 1 ] pi = np . bincount ( label_idx ) . astype ( np . float ) pi = pi [ pi > 0 ] pi_sum = np . sum ( pi ) return - np . sum ( ( pi / pi_sum ) * ( np . log ( pi ) - np . log ( pi_sum ) ) )
Calculates the entropy for a labeling .
20,033
def validate_tempi ( tempi , reference = True ) : if tempi . size != 2 : raise ValueError ( 'tempi must have exactly two values' ) if not np . all ( np . isfinite ( tempi ) ) or np . any ( tempi < 0 ) : raise ValueError ( 'tempi={} must be non-negative numbers' . format ( tempi ) ) if reference and np . all ( tempi == ...
Checks that there are two non - negative tempi . For a reference value at least one tempo has to be greater than zero .
20,034
def validate ( reference_tempi , reference_weight , estimated_tempi ) : validate_tempi ( reference_tempi , reference = True ) validate_tempi ( estimated_tempi , reference = False ) if reference_weight < 0 or reference_weight > 1 : raise ValueError ( 'Reference weight must lie in range [0, 1]' )
Checks that the input annotations to a metric look like valid tempo annotations .
20,035
def detection ( reference_tempi , reference_weight , estimated_tempi , tol = 0.08 ) : validate ( reference_tempi , reference_weight , estimated_tempi ) if tol < 0 or tol > 1 : raise ValueError ( 'invalid tolerance {}: must lie in the range ' '[0, 1]' . format ( tol ) ) if tol == 0. : warnings . warn ( 'A tolerance of 0...
Compute the tempo detection accuracy metric .
20,036
def validate ( ref_time , ref_freqs , est_time , est_freqs ) : util . validate_events ( ref_time , max_time = MAX_TIME ) util . validate_events ( est_time , max_time = MAX_TIME ) if ref_time . size == 0 : warnings . warn ( "Reference times are empty." ) if ref_time . ndim != 1 : raise ValueError ( "Reference times have...
Checks that the time and frequency inputs are well - formed .
20,037
def resample_multipitch ( times , frequencies , target_times ) : if target_times . size == 0 : return [ ] if times . size == 0 : return [ np . array ( [ ] ) ] * len ( target_times ) n_times = len ( frequencies ) frequency_index = np . arange ( 0 , n_times ) new_frequency_index = scipy . interpolate . interp1d ( times ,...
Resamples multipitch time series to a new timescale . Values in target_times outside the range of times return no pitch estimate .
20,038
def compute_num_true_positives ( ref_freqs , est_freqs , window = 0.5 , chroma = False ) : n_frames = len ( ref_freqs ) true_positives = np . zeros ( ( n_frames , ) ) for i , ( ref_frame , est_frame ) in enumerate ( zip ( ref_freqs , est_freqs ) ) : if chroma : matching = util . match_events ( ref_frame , est_frame , w...
Compute the number of true positives in an estimate given a reference . A frequency is correct if it is within a quartertone of the correct frequency .
20,039
def compute_accuracy ( true_positives , n_ref , n_est ) : true_positive_sum = float ( true_positives . sum ( ) ) n_est_sum = n_est . sum ( ) if n_est_sum > 0 : precision = true_positive_sum / n_est . sum ( ) else : warnings . warn ( "Estimate frequencies are all empty." ) precision = 0.0 n_ref_sum = n_ref . sum ( ) if ...
Compute accuracy metrics .
20,040
def compute_err_score ( true_positives , n_ref , n_est ) : n_ref_sum = float ( n_ref . sum ( ) ) if n_ref_sum == 0 : warnings . warn ( "Reference frequencies are all empty." ) return 0. , 0. , 0. , 0. e_sub = ( np . min ( [ n_ref , n_est ] , axis = 0 ) - true_positives ) . sum ( ) / n_ref_sum e_miss_numerator = n_ref -...
Compute error score metrics .
20,041
def _hierarchy_bounds ( intervals_hier ) : boundaries = list ( itertools . chain ( * list ( itertools . chain ( * intervals_hier ) ) ) ) return min ( boundaries ) , max ( boundaries )
Compute the covered time range of a hierarchical segmentation .
20,042
def _align_intervals ( int_hier , lab_hier , t_min = 0.0 , t_max = None ) : return [ list ( _ ) for _ in zip ( * [ util . adjust_intervals ( np . asarray ( ival ) , labels = lab , t_min = t_min , t_max = t_max ) for ival , lab in zip ( int_hier , lab_hier ) ] ) ]
Align a hierarchical annotation to span a fixed start and end time .
20,043
def _compare_frame_rankings ( ref , est , transitive = False ) : idx = np . argsort ( ref ) ref_sorted = ref [ idx ] est_sorted = est [ idx ] levels , positions , counts = np . unique ( ref_sorted , return_index = True , return_counts = True ) positions = list ( positions ) positions . append ( len ( ref_sorted ) ) ind...
Compute the number of ranking disagreements in two lists .
20,044
def validate_hier_intervals ( intervals_hier ) : label_top = util . generate_labels ( intervals_hier [ 0 ] ) boundaries = set ( util . intervals_to_boundaries ( intervals_hier [ 0 ] ) ) for level , intervals in enumerate ( intervals_hier [ 1 : ] , 1 ) : label_current = util . generate_labels ( intervals ) validate_stru...
Validate a hierarchical segment annotation .
20,045
def evaluate ( ref_intervals_hier , ref_labels_hier , est_intervals_hier , est_labels_hier , ** kwargs ) : _ , t_end = _hierarchy_bounds ( ref_intervals_hier ) ref_intervals_hier , ref_labels_hier = _align_intervals ( ref_intervals_hier , ref_labels_hier , t_min = 0.0 , t_max = None ) est_intervals_hier , est_labels_hi...
Compute all hierarchical structure metrics for the given reference and estimated annotations .
20,046
def __expand_limits ( ax , limits , which = 'x' ) : if which == 'x' : getter , setter = ax . get_xlim , ax . set_xlim elif which == 'y' : getter , setter = ax . get_ylim , ax . set_ylim else : raise ValueError ( 'invalid axis: {}' . format ( which ) ) old_lims = getter ( ) new_lims = list ( limits ) if np . isfinite ( ...
Helper function to expand axis limits
20,047
def __get_axes ( ax = None , fig = None ) : new_axes = False if ax is not None : return ax , new_axes if fig is None : import matplotlib . pyplot as plt fig = plt . gcf ( ) if not fig . get_axes ( ) : new_axes = True return fig . gca ( ) , new_axes
Get or construct the target axes object for a new plot .
20,048
def segments ( intervals , labels , base = None , height = None , text = False , text_kw = None , ax = None , ** kwargs ) : if text_kw is None : text_kw = dict ( ) text_kw . setdefault ( 'va' , 'top' ) text_kw . setdefault ( 'clip_on' , True ) text_kw . setdefault ( 'bbox' , dict ( boxstyle = 'round' , facecolor = 'whi...
Plot a segmentation as a set of disjoint rectangles .
20,049
def labeled_intervals ( intervals , labels , label_set = None , base = None , height = None , extend_labels = True , ax = None , tick = True , ** kwargs ) : ax , _ = __get_axes ( ax = ax ) intervals = np . atleast_2d ( intervals ) if label_set is None : label_set = [ _ . get_text ( ) for _ in ax . get_yticklabels ( ) ]...
Plot labeled intervals with each label on its own row .
20,050
def hierarchy ( intervals_hier , labels_hier , levels = None , ax = None , ** kwargs ) : if levels is None : levels = list ( range ( len ( intervals_hier ) ) ) ax , _ = __get_axes ( ax = ax ) n_patches = len ( ax . patches ) for ints , labs , key in zip ( intervals_hier [ : : - 1 ] , labels_hier [ : : - 1 ] , levels [ ...
Plot a hierarchical segmentation
20,051
def events ( times , labels = None , base = None , height = None , ax = None , text_kw = None , ** kwargs ) : if text_kw is None : text_kw = dict ( ) text_kw . setdefault ( 'va' , 'top' ) text_kw . setdefault ( 'clip_on' , True ) text_kw . setdefault ( 'bbox' , dict ( boxstyle = 'round' , facecolor = 'white' ) ) times ...
Plot event times as a set of vertical lines
20,052
def pitch ( times , frequencies , midi = False , unvoiced = False , ax = None , ** kwargs ) : ax , _ = __get_axes ( ax = ax ) times = np . asarray ( times ) frequencies , voicings = freq_to_voicing ( np . asarray ( frequencies , dtype = np . float ) ) v_changes = 1 + np . flatnonzero ( voicings [ 1 : ] != voicings [ : ...
Visualize pitch contours
20,053
def multipitch ( times , frequencies , midi = False , unvoiced = False , ax = None , ** kwargs ) : ax , _ = __get_axes ( ax = ax ) style_voiced = dict ( ) style_voiced . update ( next ( ax . _get_lines . prop_cycler ) ) style_voiced . update ( kwargs ) style_unvoiced = style_voiced . copy ( ) style_unvoiced . pop ( 'la...
Visualize multiple f0 measurements
20,054
def piano_roll ( intervals , pitches = None , midi = None , ax = None , ** kwargs ) : if midi is None : if pitches is None : raise ValueError ( 'At least one of `midi` or `pitches` ' 'must be provided.' ) midi = hz_to_midi ( pitches ) scale = np . arange ( 128 ) ax = labeled_intervals ( intervals , np . round ( midi ) ...
Plot a quantized piano roll as intervals
20,055
def separation ( sources , fs = 22050 , labels = None , alpha = 0.75 , ax = None , ** kwargs ) : ax , new_axes = __get_axes ( ax = ax ) sources = np . atleast_2d ( sources ) if labels is None : labels = [ 'Source {:d}' . format ( _ ) for _ in range ( len ( sources ) ) ] kwargs . setdefault ( 'scaling' , 'spectrum' ) cu...
Source - separation visualization
20,056
def __ticker_midi_note ( x , pos ) : NOTES = [ 'C' , 'C#' , 'D' , 'D#' , 'E' , 'F' , 'F#' , 'G' , 'G#' , 'A' , 'A#' , 'B' ] cents = float ( np . mod ( x , 1.0 ) ) if cents >= 0.5 : cents = cents - 1.0 x = x + 0.5 idx = int ( x % 12 ) octave = int ( x / 12 ) - 1 if cents == 0 : return '{:s}{:2d}' . format ( NOTES [ idx ...
A ticker function for midi notes .
20,057
def ticker_notes ( ax = None ) : ax , _ = __get_axes ( ax = ax ) ax . yaxis . set_major_formatter ( FMT_MIDI_NOTE ) for tick in ax . yaxis . get_ticklabels ( ) : tick . set_verticalalignment ( 'baseline' )
Set the y - axis of the given axes to MIDI notes
20,058
def ticker_pitch ( ax = None ) : ax , _ = __get_axes ( ax = ax ) ax . yaxis . set_major_formatter ( FMT_MIDI_HZ )
Set the y - axis of the given axes to MIDI frequencies
20,059
def run ( self , ** import_params ) : if self . file : import_params [ "url" ] = self . file self . id_field = "id" if "connection" in import_params : self . fields . append ( "connector" ) self . update_from_dict ( import_params [ "connection" ] ) self . save ( force_create = True ) else : super ( FileImportJob , self...
Actually creates the import job on the CARTO server
20,060
def filter ( self ) : try : response = self . send ( self . get_collection_endpoint ( ) , "get" ) if self . json_collection_attribute is not None : resource_ids = self . client . get_response_data ( response , self . Meta . parse_json ) [ self . json_collection_attribute ] else : resource_ids = self . client . get_resp...
Get a filtered list of file imports
20,061
def send ( self , relative_path , http_method , ** requests_args ) : try : http_method , requests_args = self . prepare_send ( http_method , ** requests_args ) response = super ( APIKeyAuthClient , self ) . send ( relative_path , http_method , ** requests_args ) except Exception as e : raise CartoException ( e ) if Car...
Makes an API - key - authorized request
20,062
def is_valid_api_key ( self ) : res = self . send ( 'api/v3/api_keys' , 'get' ) return res . ok and self . api_key in ( ak [ 'token' ] for ak in res . json ( ) [ 'result' ] )
Checks validity . Right now an API key is considered valid if it can list user API keys and the result contains that API key . This might change in the future .
20,063
def send ( self , url , http_method , ** client_args ) : try : client_args = client_args or { } if "params" not in client_args : client_args [ "params" ] = { } client_args [ "params" ] . update ( { "type" : "table" , "exclude_shared" : "true" } ) return super ( DatasetManager , self ) . send ( url , http_method , ** cl...
Sends an API request taking into account that datasets are part of the visualization endpoint .
20,064
def is_sync_table ( self , archive , interval , ** import_args ) : return ( hasattr ( archive , "startswith" ) and archive . startswith ( "http" ) or "connection" in import_args ) and interval is not None
Checks if this is a request for a sync dataset .
20,065
def create ( self , archive , interval = None , ** import_args ) : archive = archive . lower ( ) if hasattr ( archive , "lower" ) else archive if self . is_sync_table ( archive , interval , ** import_args ) : manager = SyncTableJobManager ( self . client ) else : manager = FileImportJobManager ( self . client ) import_...
Creating a table means uploading a file or setting up a sync table
20,066
def send ( self , url , http_method , ** client_args ) : try : client_args . setdefault ( 'params' , { } ) client_args [ "params" ] . update ( { "type" : "derived" , "exclude_shared" : "true" } ) return super ( VisualizationManager , self ) . send ( url , http_method , ** client_args ) except Exception as e : raise Car...
Sends API request taking into account that visualizations are only a subset of the resources available at the visualization endpoint
20,067
def is_rate_limited ( response ) : if ( response . status_code == codes . too_many_requests and 'Retry-After' in response . headers and int ( response . headers [ 'Retry-After' ] ) >= 0 ) : return True return False
Checks if the response has been rate limited by CARTO APIs
20,068
def update_from_dict ( self , attribute_dict ) : if 'template' in attribute_dict : self . update_from_dict ( attribute_dict [ 'template' ] ) setattr ( self , self . Meta . id_field , attribute_dict [ 'template' ] [ 'name' ] ) return try : for k , v in attribute_dict . items ( ) : setattr ( self , k , v ) except Excepti...
Method overriden from the base class
20,069
def run ( self , ** client_params ) : try : self . send ( self . get_collection_endpoint ( ) , http_method = "POST" , ** client_params ) except Exception as e : raise CartoException ( e )
Actually creates the async job on the CARTO server
20,070
def send ( self , sql , parse_json = True , do_post = True , format = None , ** request_args ) : try : params = { 'q' : sql } if format : params [ 'format' ] = format if format not in [ 'json' , 'geojson' ] : parse_json = False if request_args is not None : for attr in request_args : params [ attr ] = request_args [ at...
Executes SQL query in a CARTO server
20,071
def send ( self , url , http_method , json_body = None , http_header = None ) : try : data = self . client . send ( url , http_method = http_method , headers = http_header , json = json_body ) data_json = self . client . get_response_data ( data ) except CartoRateLimitException as e : raise e except Exception as e : ra...
Executes Batch SQL query in a CARTO server
20,072
def create ( self , sql_query ) : header = { 'content-type' : 'application/json' } data = self . send ( self . api_url , http_method = "POST" , json_body = { "query" : sql_query } , http_header = header ) return data
Creates a new batch SQL query .
20,073
def create_and_wait_for_completion ( self , sql_query ) : header = { 'content-type' : 'application/json' } data = self . send ( self . api_url , http_method = "POST" , json_body = { "query" : sql_query } , http_header = header ) warnings . warn ( 'Batch SQL job created with job_id: {job_id}' . format ( job_id = data [ ...
Creates a new batch SQL query and waits for its completion or failure
20,074
def read ( self , job_id ) : data = self . send ( self . api_url + job_id , http_method = "GET" ) return data
Reads the information for a specific Batch API request
20,075
def update ( self , job_id , sql_query ) : header = { 'content-type' : 'application/json' } data = self . send ( self . api_url + job_id , http_method = "PUT" , json_body = { "query" : sql_query } , http_header = header ) return data
Updates the sql query of a specific job
20,076
def cancel ( self , job_id ) : try : confirmation = self . send ( self . api_url + job_id , http_method = "DELETE" ) except CartoException as e : if 'Cannot set status from done to cancelled' in e . args [ 0 ] . args [ 0 ] : return 'done' else : raise e return confirmation [ 'status' ]
Cancels a job
20,077
def copyfrom ( self , query , iterable_data , compress = True , compression_level = DEFAULT_COMPRESSION_LEVEL ) : url = self . api_url + '/copyfrom' headers = { 'Content-Type' : 'application/octet-stream' , 'Transfer-Encoding' : 'chunked' } params = { 'api_key' : self . api_key , 'q' : query } if compress : headers [ '...
Gets data from an iterable object into a table
20,078
def copyfrom_file_object ( self , query , file_object , compress = True , compression_level = DEFAULT_COMPRESSION_LEVEL ) : chunk_generator = self . _read_in_chunks ( file_object ) return self . copyfrom ( query , chunk_generator , compress , compression_level )
Gets data from a readable file object into a table
20,079
def copyfrom_file_path ( self , query , path , compress = True , compression_level = DEFAULT_COMPRESSION_LEVEL ) : with open ( path , 'rb' ) as f : result = self . copyfrom_file_object ( query , f , compress , compression_level ) return result
Gets data from a readable file into a table
20,080
def copyto ( self , query ) : url = self . api_url + '/copyto' params = { 'api_key' : self . api_key , 'q' : query } try : response = self . client . send ( url , http_method = 'GET' , params = params , stream = True ) response . raise_for_status ( ) except CartoRateLimitException as e : raise e except HTTPError as e :...
Gets data from a table into a Response object that can be iterated
20,081
def copyto_file_object ( self , query , file_object ) : response = self . copyto ( query ) for block in response . iter_content ( DEFAULT_CHUNK_SIZE ) : file_object . write ( block )
Gets data from a table into a writable file object
20,082
def copyto_file_path ( self , query , path , append = False ) : file_mode = 'wb' if not append else 'ab' with open ( path , file_mode ) as f : self . copyto_file_object ( query , f )
Gets data from a table into a writable file
20,083
def run ( self , ** import_params ) : import_params [ "url" ] = self . url import_params [ "interval" ] = self . interval if "connection" in import_params : self . fields . append ( "connector" ) import_params [ "connection" ] [ "interval" ] = self . interval self . update_from_dict ( import_params [ "connection" ] ) s...
Actually creates the job import on the CARTO server
20,084
def force_sync ( self ) : try : self . send ( self . get_resource_endpoint ( ) , "put" ) except Exception as e : raise CartoException ( e )
Forces to sync the SyncTableJob
20,085
def set_prob_type ( cls , problem_type , classification_type , eval_type ) : assert problem_type in problem_type_list , 'Need to set Problem Type' if problem_type == 'classification' : assert classification_type in classification_type_list , 'Need to set Classification Type' assert eval_type in eval_type_list , 'Need t...
Set problem type
20,086
def make_multi_cols ( self , num_class , name ) : cols = [ 'c' + str ( i ) + '_' for i in xrange ( num_class ) ] cols = map ( lambda x : x + name , cols ) return cols
make cols for multi - class predictions
20,087
def parse ( self , data ) : ( self . len_di , self . xattr_length , self . extent_location , self . parent_directory_num ) = struct . unpack_from ( self . FMT , data [ : 8 ] , 0 ) if self . len_di % 2 != 0 : self . directory_identifier = data [ 8 : - 1 ] else : self . directory_identifier = data [ 8 : ] self . dirrecor...
A method to parse an ISO9660 Path Table Record out of a string .
20,088
def _record ( self , ext_loc , parent_dir_num ) : return struct . pack ( self . FMT , self . len_di , self . xattr_length , ext_loc , parent_dir_num ) + self . directory_identifier + b'\x00' * ( self . len_di % 2 )
An internal method to generate a string representing this Path Table Record .
20,089
def record_little_endian ( self ) : if not self . _initialized : raise pycdlibexception . PyCdlibInternalError ( 'Path Table Record not yet initialized' ) return self . _record ( self . extent_location , self . parent_directory_num )
A method to generate a string representing the little endian version of this Path Table Record .
20,090
def record_big_endian ( self ) : if not self . _initialized : raise pycdlibexception . PyCdlibInternalError ( 'Path Table Record not yet initialized' ) return self . _record ( utils . swab_32bit ( self . extent_location ) , utils . swab_16bit ( self . parent_directory_num ) )
A method to generate a string representing the big endian version of this Path Table Record .
20,091
def _new ( self , name , parent_dir_num ) : self . len_di = len ( name ) self . xattr_length = 0 self . parent_directory_num = parent_dir_num self . directory_identifier = name self . _initialized = True
An internal method to create a new Path Table Record .
20,092
def new_dir ( self , name ) : if self . _initialized : raise pycdlibexception . PyCdlibInternalError ( 'Path Table Record already initialized' ) self . _new ( name , 0 )
A method to create a new Path Table Record .
20,093
def update_extent_location ( self , extent_loc ) : if not self . _initialized : raise pycdlibexception . PyCdlibInternalError ( 'Path Table Record not yet initialized' ) self . extent_location = extent_loc
A method to update the extent location for this Path Table Record .
20,094
def update_parent_directory_number ( self , parent_dir_num ) : if not self . _initialized : raise pycdlibexception . PyCdlibInternalError ( 'Path Table Record not yet initialized' ) self . parent_directory_num = parent_dir_num
A method to update the parent directory number for this Path Table Record from the directory record .
20,095
def equal_to_be ( self , be_record ) : if not self . _initialized : raise pycdlibexception . PyCdlibInternalError ( 'This Path Table Record is not yet initialized' ) if be_record . len_di != self . len_di or be_record . xattr_length != self . xattr_length or utils . swab_32bit ( be_record . extent_location ) != self . ...
A method to compare a little - endian path table record to its big - endian counterpart . This is used to ensure that the ISO is sane .
20,096
def copy_data ( data_length , blocksize , infp , outfp ) : use_sendfile = False if have_sendfile : try : x_unused = infp . fileno ( ) y_unused = outfp . fileno ( ) use_sendfile = True except ( AttributeError , io . UnsupportedOperation ) : pass if use_sendfile : in_offset = infp . tell ( ) out_offset = outfp . tell ( )...
A utility function to copy data from the input file object to the output file object . This function will use the most efficient copy method available which is often sendfile .
20,097
def encode_space_pad ( instr , length , encoding ) : output = instr . decode ( 'utf-8' ) . encode ( encoding ) if len ( output ) > length : raise pycdlibexception . PyCdlibInvalidInput ( 'Input string too long!' ) encoded_space = ' ' . encode ( encoding ) left = length - len ( output ) while left > 0 : output += encode...
A function to pad out an input string with spaces to the length specified . The space is first encoded into the specified encoding then appended to the input string until the length is reached .
20,098
def gmtoffset_from_tm ( tm , local ) : gmtime = time . gmtime ( tm ) tmpyear = gmtime . tm_year - local . tm_year tmpyday = gmtime . tm_yday - local . tm_yday tmphour = gmtime . tm_hour - local . tm_hour tmpmin = gmtime . tm_min - local . tm_min if tmpyday < 0 : tmpyday = - 1 else : if tmpyear > 0 : tmpyday = 1 return ...
A function to compute the GMT offset from the time in seconds since the epoch and the local time object .
20,099
def zero_pad ( fp , data_size , pad_size ) : padbytes = pad_size - ( data_size % pad_size ) if padbytes == pad_size : return fp . seek ( padbytes - 1 , os . SEEK_CUR ) fp . write ( b'\x00' )
A function to write padding out from data_size up to pad_size efficiently .