idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
22,000
def compute_frame ( self , ** kwargs ) : r if self . G . N > 2000 : _logger . warning ( 'Creating a big matrix. ' 'You should prefer the filter method.' ) s = np . identity ( self . G . N ) return self . filter ( s , ** kwargs ) . T . reshape ( - 1 , self . G . N )
r Compute the associated frame .
22,001
def complement ( self , frame_bound = None ) : r def kernel ( x , * args , ** kwargs ) : y = self . evaluate ( x ) np . power ( y , 2 , out = y ) y = np . sum ( y , axis = 0 ) if frame_bound is None : bound = y . max ( ) elif y . max ( ) > frame_bound : raise ValueError ( 'The chosen bound is not feasible. ' 'Choose at...
r Return the filter that makes the frame tight .
22,002
def inverse ( self ) : r A , _ = self . estimate_frame_bounds ( ) if A == 0 : _logger . warning ( 'The filter bank is not invertible as it is not ' 'a frame (lower frame bound A=0).' ) def kernel ( g , i , x ) : y = g . evaluate ( x ) . T z = np . linalg . pinv ( np . expand_dims ( y , axis = - 1 ) ) . squeeze ( axis =...
r Return the pseudo - inverse filter bank .
22,003
def grad ( self , x ) : r x = self . _check_signal ( x ) return self . D . T . dot ( x )
r Compute the gradient of a signal defined on the vertices .
22,004
def div ( self , y ) : r y = np . asanyarray ( y ) if y . shape [ 0 ] != self . Ne : raise ValueError ( 'First dimension must be the number of edges ' 'G.Ne = {}, got {}.' . format ( self . Ne , y . shape ) ) return self . D . dot ( y )
r Compute the divergence of a signal defined on the edges .
22,005
def gft ( self , s ) : r s = self . _check_signal ( s ) U = np . conjugate ( self . U ) return np . tensordot ( U , s , ( [ 0 ] , [ 0 ] ) )
r Compute the graph Fourier transform .
22,006
def igft ( self , s_hat ) : r s_hat = self . _check_signal ( s_hat ) return np . tensordot ( self . U , s_hat , ( [ 1 ] , [ 0 ] ) )
r Compute the inverse graph Fourier transform .
22,007
def compute_cheby_coeff ( f , m = 30 , N = None , * args , ** kwargs ) : r G = f . G i = kwargs . pop ( 'i' , 0 ) if not N : N = m + 1 a_arange = [ 0 , G . lmax ] a1 = ( a_arange [ 1 ] - a_arange [ 0 ] ) / 2 a2 = ( a_arange [ 1 ] + a_arange [ 0 ] ) / 2 c = np . zeros ( m + 1 ) tmpN = np . arange ( N ) num = np . cos ( ...
r Compute Chebyshev coefficients for a Filterbank .
22,008
def cheby_op ( G , c , signal , ** kwargs ) : r if not isinstance ( c , np . ndarray ) : c = np . array ( c ) c = np . atleast_2d ( c ) Nscales , M = c . shape if M < 2 : raise TypeError ( "The coefficients have an invalid shape" ) try : Nv = np . shape ( signal ) [ 1 ] r = np . zeros ( ( G . N * Nscales , Nv ) ) excep...
r Chebyshev polynomial of graph Laplacian applied to vector .
22,009
def cheby_rect ( G , bounds , signal , ** kwargs ) : r if not ( isinstance ( bounds , ( list , np . ndarray ) ) and len ( bounds ) == 2 ) : raise ValueError ( 'Bounds of wrong shape.' ) bounds = np . array ( bounds ) m = int ( kwargs . pop ( 'order' , 30 ) + 1 ) try : Nv = np . shape ( signal ) [ 1 ] r = np . zeros ( (...
r Fast filtering using Chebyshev polynomial for a perfect rectangle filter .
22,010
def compute_jackson_cheby_coeff ( filter_bounds , delta_lambda , m ) : r if delta_lambda [ 0 ] > filter_bounds [ 0 ] or delta_lambda [ 1 ] < filter_bounds [ 1 ] : _logger . error ( "Bounds of the filter are out of the lambda values" ) raise ( ) elif delta_lambda [ 0 ] > delta_lambda [ 1 ] : _logger . error ( "lambda_mi...
r To compute the m + 1 coefficients of the polynomial approximation of an ideal band - pass between a and b between a range of values defined by lambda_min and lambda_max .
22,011
def lanczos_op ( f , s , order = 30 ) : r G = f . G Nf = len ( f . g ) try : Nv = np . shape ( s ) [ 1 ] is2d = True c = np . zeros ( ( G . N * Nf , Nv ) ) except IndexError : Nv = 1 is2d = False c = np . zeros ( ( G . N * Nf ) ) tmpN = np . arange ( G . N , dtype = int ) for j in range ( Nv ) : if is2d : V , H , _ = l...
r Perform the lanczos approximation of the signal s .
22,012
def interpolate ( G , f_subsampled , keep_inds , order = 100 , reg_eps = 0.005 , ** kwargs ) : r L_reg = G . L + reg_eps * sparse . eye ( G . N ) K_reg = getattr ( G . mr , 'K_reg' , kron_reduction ( L_reg , keep_inds ) ) green_kernel = getattr ( G . mr , 'green_kernel' , filters . Filter ( G , lambda x : 1. / ( reg_ep...
r Interpolate a graph signal .
22,013
def kron_reduction ( G , ind ) : r if isinstance ( G , graphs . Graph ) : if G . lap_type != 'combinatorial' : msg = 'Unknown reduction for {} Laplacian.' . format ( G . lap_type ) raise NotImplementedError ( msg ) if G . is_directed ( ) : msg = 'This method only work for undirected graphs.' raise NotImplementedError (...
r Compute the Kron reduction .
22,014
def pyramid_analysis ( Gs , f , ** kwargs ) : r if np . shape ( f ) [ 0 ] != Gs [ 0 ] . N : raise ValueError ( "PYRAMID ANALYSIS: The signal to analyze should have the same dimension as the first graph." ) levels = len ( Gs ) - 1 h_filters = kwargs . pop ( 'h_filters' , lambda x : 1. / ( 2 * x + 1 ) ) if not isinstance...
r Compute the graph pyramid transform coefficients .
22,015
def pyramid_synthesis ( Gs , cap , pe , order = 30 , ** kwargs ) : r least_squares = bool ( kwargs . pop ( 'least_squares' , False ) ) def_ul = Gs [ 0 ] . N > 3000 or Gs [ 0 ] . _e is None or Gs [ 0 ] . _U is None use_landweber = bool ( kwargs . pop ( 'use_landweber' , def_ul ) ) reg_eps = float ( kwargs . get ( 'reg_e...
r Synthesize a signal from its pyramid coefficients .
22,016
def tree_multiresolution ( G , Nlevel , reduction_method = 'resistance_distance' , compute_full_eigen = False , root = None ) : r if not root : if hasattr ( G , 'root' ) : root = G . root else : root = 1 Gs = [ G ] if compute_full_eigen : Gs [ 0 ] . compute_fourier_basis ( ) subsampled_vertex_indices = [ ] depths , par...
r Compute a multiresolution of trees
22,017
def close_all ( ) : r global _qtg_windows for window in _qtg_windows : window . close ( ) _qtg_windows = [ ] global _qtg_widgets for widget in _qtg_widgets : widget . close ( ) _qtg_widgets = [ ] global _plt_figures for fig in _plt_figures : _ , plt , _ = _import_plt ( ) plt . close ( fig ) _plt_figures = [ ]
r Close all opened windows .
22,018
def _plot_filter ( filters , n , eigenvalues , sum , title , ax , ** kwargs ) : r if eigenvalues is None : eigenvalues = ( filters . G . _e is not None ) if sum is None : sum = filters . n_filters > 1 if title is None : title = repr ( filters ) return _plt_plot_filter ( filters , n = n , eigenvalues = eigenvalues , sum...
r Plot the spectral response of a filter bank .
22,019
def _plot_spectrogram ( G , node_idx ) : r from pygsp import features qtg , _ , _ = _import_qtg ( ) if not hasattr ( G , 'spectr' ) : features . compute_spectrogram ( G ) M = G . spectr . shape [ 1 ] spectr = G . spectr [ node_idx , : ] if node_idx is not None else G . spectr spectr = np . ravel ( spectr ) min_spec , m...
r Plot the graph s spectrogram .
22,020
def classification_tikhonov ( G , y , M , tau = 0 ) : r y [ M == False ] = 0 Y = _to_logits ( y . astype ( np . int ) ) return regression_tikhonov ( G , Y , M , tau )
r Solve a classification problem on graph via Tikhonov minimization .
22,021
def regression_tikhonov ( G , y , M , tau = 0 ) : r if tau > 0 : y [ M == False ] = 0 if sparse . issparse ( G . L ) : def Op ( x ) : return ( M * x . T ) . T + tau * ( G . L . dot ( x ) ) LinearOp = sparse . linalg . LinearOperator ( [ G . N , G . N ] , Op ) if y . ndim > 1 : sol = np . empty ( shape = y . shape ) res...
r Solve a regression problem on graph via Tikhonov minimization .
22,022
def set_signal ( self , signal , name ) : r signal = self . _check_signal ( signal ) self . signals [ name ] = signal
r Attach a signal to the graph .
22,023
def subgraph ( self , vertices ) : r adjacency = self . W [ vertices , : ] [ : , vertices ] try : coords = self . coords [ vertices ] except AttributeError : coords = None graph = Graph ( adjacency , self . lap_type , coords , self . plotting ) for name , signal in self . signals . items ( ) : graph . set_signal ( sign...
r Create a subgraph from a list of vertices .
22,024
def extract_components ( self ) : r if self . A . shape [ 0 ] != self . A . shape [ 1 ] : self . logger . error ( 'Inconsistent shape to extract components. ' 'Square matrix required.' ) return None if self . is_directed ( ) : raise NotImplementedError ( 'Directed graphs not supported yet.' ) graphs = [ ] visited = np ...
r Split the graph into connected components .
22,025
def compute_laplacian ( self , lap_type = 'combinatorial' ) : r if lap_type != self . lap_type : self . _lmax = None self . _U = None self . _e = None self . _coherence = None self . _D = None self . lap_type = lap_type if not self . is_directed ( ) : W = self . W else : W = utils . symmetrize ( self . W , method = 'av...
r Compute a graph Laplacian .
22,026
def _check_signal ( self , s ) : r s = np . asanyarray ( s ) if s . shape [ 0 ] != self . n_vertices : raise ValueError ( 'First dimension must be the number of vertices ' 'G.N = {}, got {}.' . format ( self . N , s . shape ) ) return s
r Check if signal is valid .
22,027
def dirichlet_energy ( self , x ) : r x = self . _check_signal ( x ) return x . T . dot ( self . L . dot ( x ) )
r Compute the Dirichlet energy of a signal defined on the vertices .
22,028
def dw ( self ) : r if self . _dw is None : if not self . is_directed ( ) : self . _dw = np . ravel ( self . W . sum ( axis = 0 ) ) else : degree_in = np . ravel ( self . W . sum ( axis = 0 ) ) degree_out = np . ravel ( self . W . sum ( axis = 1 ) ) self . _dw = ( degree_in + degree_out ) / 2 return self . _dw
r The weighted degree of vertices .
22,029
def lmax ( self ) : r if self . _lmax is None : self . logger . warning ( 'The largest eigenvalue G.lmax is not ' 'available, we need to estimate it. ' 'Explicitly call G.estimate_lmax() or ' 'G.compute_fourier_basis() ' 'once beforehand to suppress the warning.' ) self . estimate_lmax ( ) return self . _lmax
r Largest eigenvalue of the graph Laplacian .
22,030
def _get_upper_bound ( self ) : r if self . lap_type == 'normalized' : return 2 elif self . lap_type == 'combinatorial' : bounds = [ ] bounds += [ self . n_vertices * np . max ( self . W ) ] bounds += [ 2 * np . max ( self . dw ) ] if self . n_edges > 0 : sources , targets , _ = self . get_edge_list ( ) bounds += [ np ...
r Return an upper bound on the eigenvalues of the Laplacian .
22,031
def get_edge_list ( self ) : r if self . is_directed ( ) : W = self . W . tocoo ( ) else : W = sparse . triu ( self . W , format = 'coo' ) sources = W . row targets = W . col weights = W . data assert self . n_edges == sources . size == targets . size == weights . size return sources , targets , weights
r Return an edge list an alternative representation of the graph .
22,032
def prox_tv ( x , gamma , G , A = None , At = None , nu = 1 , tol = 10e-4 , maxit = 200 , use_matrix = True ) : r if A is None : def A ( x ) : return x if At is None : def At ( x ) : return x tight = 0 l1_nu = 2 * G . lmax * nu if use_matrix : def l1_a ( x ) : return G . Diff * A ( x ) def l1_at ( x ) : return G . Diff...
r Total Variation proximal operator for graphs .
22,033
def is_regular ( self ) : r warn = False msg = 'The given matrix' if np . abs ( self . A - self . A . T ) . sum ( ) > 0 : warn = True msg = '{} is not symmetric,' . format ( msg ) if self . A . max ( axis = None ) > 1 : warn = True msg = '{} has parallel edges,' . format ( msg ) if np . min ( self . d ) != np . max ( s...
r Troubleshoot a given regular graph .
22,034
def _break_signals ( self ) : r for name in list ( self . signals . keys ( ) ) : if self . signals [ name ] . ndim == 2 : for i , signal_1d in enumerate ( self . signals [ name ] . T ) : self . signals [ name + '_' + str ( i ) ] = signal_1d del self . signals [ name ]
r Break N - dimensional signals into N 1D signals .
22,035
def _join_signals ( self ) : r joined = dict ( ) for name in self . signals : name_base = name . rsplit ( '_' , 1 ) [ 0 ] names = joined . get ( name_base , list ( ) ) names . append ( name ) joined [ name_base ] = names for name_base , names in joined . items ( ) : if len ( names ) > 1 : names = sorted ( names ) signa...
r Join N 1D signals into one N - dimensional signal .
22,036
def to_networkx ( self ) : r nx = _import_networkx ( ) def convert ( number ) : if issubclass ( number . dtype . type , ( np . integer , np . bool_ ) ) : return int ( number ) else : return float ( number ) def edges ( ) : for source , target , weight in zip ( * self . get_edge_list ( ) ) : yield int ( source ) , int (...
r Export the graph to NetworkX .
22,037
def to_graphtool ( self ) : r convert = { np . bool_ : 'bool' , np . int8 : 'int8_t' , np . int16 : 'int16_t' , np . int32 : 'int32_t' , np . int64 : 'int64_t' , np . short : 'short' , np . intc : 'int' , np . uintc : 'unsigned int' , np . long : 'long' , np . longlong : 'long long' , np . uint : 'unsigned long' , np ....
r Export the graph to graph - tool .
22,038
def from_networkx ( cls , graph , weight = 'weight' ) : r nx = _import_networkx ( ) from . graph import Graph adjacency = nx . to_scipy_sparse_matrix ( graph , weight = weight ) graph_pg = Graph ( adjacency ) for i , node in enumerate ( graph . nodes ( ) ) : for name in graph . nodes [ node ] . keys ( ) : try : signal ...
r Import a graph from NetworkX .
22,039
def from_graphtool ( cls , graph , weight = 'weight' ) : r gt = _import_graphtool ( ) import graph_tool . spectral from . graph import Graph weight = graph . edge_properties . get ( weight , None ) adjacency = gt . spectral . adjacency ( graph , weight = weight ) graph_pg = Graph ( adjacency . T ) for name , signal in ...
r Import a graph from graph - tool .
22,040
def load ( cls , path , fmt = None , backend = None ) : r if fmt is None : fmt = os . path . splitext ( path ) [ 1 ] [ 1 : ] if fmt not in [ 'graphml' , 'gml' , 'gexf' ] : raise ValueError ( 'Unsupported format {}.' . format ( fmt ) ) def load_networkx ( path , fmt ) : nx = _import_networkx ( ) load = getattr ( nx , 'r...
r Load a graph from a file .
22,041
def save ( self , path , fmt = None , backend = None ) : r if fmt is None : fmt = os . path . splitext ( path ) [ 1 ] [ 1 : ] if fmt not in [ 'graphml' , 'gml' , 'gexf' ] : raise ValueError ( 'Unsupported format {}.' . format ( fmt ) ) def save_networkx ( graph , path , fmt ) : nx = _import_networkx ( ) graph = graph ....
r Save the graph to a file .
22,042
def loadmat ( path ) : r data = pkgutil . get_data ( 'pygsp' , 'data/' + path + '.mat' ) data = io . BytesIO ( data ) return scipy . io . loadmat ( data )
r Load a matlab data file .
22,043
def distanz ( x , y = None ) : r try : x . shape [ 1 ] except IndexError : x = x . reshape ( 1 , x . shape [ 0 ] ) if y is None : y = x else : try : y . shape [ 1 ] except IndexError : y = y . reshape ( 1 , y . shape [ 0 ] ) rx , cx = x . shape ry , cy = y . shape if rx != ry : raise ValueError ( "The sizes of x and y ...
r Calculate the distance between two colon vectors .
22,044
def resistance_distance ( G ) : r if sparse . issparse ( G ) : L = G . tocsc ( ) else : if G . lap_type != 'combinatorial' : raise ValueError ( 'Need a combinatorial Laplacian.' ) L = G . L . tocsc ( ) try : pseudo = sparse . linalg . inv ( L ) except RuntimeError : pseudo = sparse . lil_matrix ( np . linalg . pinv ( L...
r Compute the resistance distances of a graph .
22,045
def symmetrize ( W , method = 'average' ) : r if W . shape [ 0 ] != W . shape [ 1 ] : raise ValueError ( 'Matrix must be square.' ) if method == 'average' : return ( W + W . T ) / 2 elif method == 'maximum' : if sparse . issparse ( W ) : bigger = ( W . T > W ) return W - W . multiply ( bigger ) + W . T . multiply ( big...
r Symmetrize a square matrix .
22,046
def compute_log_scales ( lmin , lmax , Nscales , t1 = 1 , t2 = 2 ) : r scale_min = t1 / lmax scale_max = t2 / lmin return np . exp ( np . linspace ( np . log ( scale_max ) , np . log ( scale_min ) , Nscales ) )
r Compute logarithm scales for wavelets .
22,047
def import_modules ( names , src , dst ) : for name in names : module = importlib . import_module ( src + '.' + name ) setattr ( sys . modules [ dst ] , name , module )
Import modules in package .
22,048
def import_classes ( names , src , dst ) : for name in names : module = importlib . import_module ( 'pygsp.' + src + '.' + name . lower ( ) ) setattr ( sys . modules [ 'pygsp.' + dst ] , name , getattr ( module , name ) )
Import classes in package from their implementation modules .
22,049
def import_functions ( names , src , dst ) : for name in names : module = importlib . import_module ( 'pygsp.' + src ) setattr ( sys . modules [ 'pygsp.' + dst ] , name , getattr ( module , name ) )
Import functions in package from their implementation modules .
22,050
def _handle_api_result ( result : Optional [ Dict [ str , Any ] ] ) -> Any : if isinstance ( result , dict ) : if result . get ( 'status' ) == 'failed' : raise ActionFailed ( retcode = result . get ( 'retcode' ) ) return result . get ( 'data' )
Retrieve data field from the API result object .
22,051
def reduce ( self ) -> None : idx = 0 while idx < len ( self ) : if idx > 0 and self [ idx - 1 ] . type == 'text' and self [ idx ] . type == 'text' : self [ idx - 1 ] . data [ 'text' ] += self [ idx ] . data [ 'text' ] del self [ idx ] else : idx += 1
Remove redundant segments .
22,052
def extract_plain_text ( self , reduce : bool = False ) -> str : if reduce : self . reduce ( ) result = '' for seg in self : if seg . type == 'text' : result += ' ' + seg . data [ 'text' ] if result : result = result [ 1 : ] return result
Extract text segments from the message joined by single space .
22,053
def send_html_mail ( subject , message , message_html , from_email , recipient_list , priority = None , fail_silently = False , auth_user = None , auth_password = None , headers = { } ) : from django . utils . encoding import force_text from django . core . mail import EmailMultiAlternatives from mailer . models import...
Function to queue HTML e - mails
22,054
def make_message ( subject = "" , body = "" , from_email = None , to = None , bcc = None , attachments = None , headers = None , priority = None ) : to = filter_recipient_list ( to ) bcc = filter_recipient_list ( bcc ) core_msg = EmailMessage ( subject = subject , body = body , from_email = from_email , to = to , bcc =...
Creates a simple message for the email parameters supplied . The to and bcc lists are filtered using DontSendEntry .
22,055
def has_address ( self , address ) : queryset = self . filter ( to_address__iexact = address ) return queryset . exists ( )
is the given address on the don t send list?
22,056
def prioritize ( ) : while True : hp_qs = Message . objects . high_priority ( ) . using ( 'default' ) mp_qs = Message . objects . medium_priority ( ) . using ( 'default' ) lp_qs = Message . objects . low_priority ( ) . using ( 'default' ) while hp_qs . count ( ) or mp_qs . count ( ) : while hp_qs . count ( ) : for mess...
Yield the messages in the queue in the order they should be sent .
22,057
def send_all ( ) : EMAIL_BACKEND = getattr ( settings , "MAILER_EMAIL_BACKEND" , "django.core.mail.backends.smtp.EmailBackend" ) acquired , lock = acquire_lock ( ) if not acquired : return start_time = time . time ( ) deferred = 0 sent = 0 try : connection = None for message in prioritize ( ) : try : if connection is N...
Send all eligible messages in the queue .
22,058
def send_loop ( ) : while True : while not Message . objects . all ( ) : logging . debug ( "sleeping for %s seconds before checking queue again" % EMPTY_QUEUE_SLEEP ) time . sleep ( EMPTY_QUEUE_SLEEP ) send_all ( )
Loop indefinitely checking queue at intervals of EMPTY_QUEUE_SLEEP and sending messages if any are on queue .
22,059
def import_name ( name ) : if isinstance ( name , str ) : components = name . split ( '.' ) mod = __import__ ( '.' . join ( components [ 0 : - 1 ] ) , globals ( ) , locals ( ) , [ components [ - 1 ] ] ) return getattr ( mod , components [ - 1 ] ) else : return name
import module given by str or pass the module if it is not str
22,060
def copy_plan ( modeladmin , request , queryset ) : for plan in queryset : plan_copy = deepcopy ( plan ) plan_copy . id = None plan_copy . available = False plan_copy . default = False plan_copy . created = None plan_copy . save ( force_insert = True ) for pricing in plan . planpricing_set . all ( ) : pricing . id = No...
Admin command for duplicating plans preserving quotas and pricings .
22,061
def recalculate ( self , amount , billing_info ) : order = Order ( pk = - 1 ) order . amount = amount order . currency = self . get_currency ( ) country = getattr ( billing_info , 'country' , None ) if not country is None : country = country . code tax_number = getattr ( billing_info , 'tax_number' , None ) tax_session...
Calculates and return pre - filled Order
22,062
def get_all_context ( self ) : self . plan_pricing = get_object_or_404 ( PlanPricing . objects . all ( ) . select_related ( 'plan' , 'pricing' ) , Q ( pk = self . kwargs [ 'pk' ] ) & Q ( plan__available = True ) & ( Q ( plan__customized = self . request . user ) | Q ( plan__customized__isnull = True ) ) ) if not self ....
Retrieves Plan and Pricing for current order creation
22,063
def create_proforma_invoice ( sender , instance , created , ** kwargs ) : if created : Invoice . create ( instance , Invoice . INVOICE_TYPES [ 'PROFORMA' ] )
For every Order if there are defined billing_data creates invoice proforma which is an order confirmation document
22,064
def get_quota_value ( self , user , quota_dict = None ) : if quota_dict is None : quota_dict = get_user_quota ( user ) return quota_dict . get ( self . code , self . default_quota_value )
Returns quota value for a given user
22,065
def send_template_email ( recipients , title_template , body_template , context , language ) : send_emails = getattr ( settings , 'SEND_PLANS_EMAILS' , True ) if not send_emails : return site_name = getattr ( settings , 'SITE_NAME' , 'Please define settings.SITE_NAME' ) domain = getattr ( settings , 'SITE_URL' , None )...
Sends e - mail using templating system
22,066
def _calculate_day_cost ( self , plan , period ) : plan_pricings = plan . planpricing_set . order_by ( '-pricing__period' ) . select_related ( 'pricing' ) selected_pricing = None for plan_pricing in plan_pricings : selected_pricing = plan_pricing if plan_pricing . pricing . period <= period : break if selected_pricing ...
Finds most fitted plan pricing for a given period and calculate day cost
22,067
def get_change_price ( self , plan_old , plan_new , period ) : if period is None or period < 1 : return None plan_old_day_cost = self . _calculate_day_cost ( plan_old , period ) plan_new_day_cost = self . _calculate_day_cost ( plan_new , period ) if plan_new_day_cost <= plan_old_day_cost : return self . _calculate_fina...
Calculates total price of plan change . Returns None if no payment is required .
22,068
def comparator ( operator ) : @ wraps ( operator ) def wrapper ( self , other ) : if not isinstance ( other , ( VersionInfo , dict ) ) : return NotImplemented return operator ( self , other ) return wrapper
Wrap a VersionInfo binary op method in a type - check
22,069
def set_sampled_topics ( self , sampled_topics ) : assert sampled_topics . dtype == np . int and len ( sampled_topics . shape ) <= 2 if len ( sampled_topics . shape ) == 1 : self . sampled_topics = sampled_topics . reshape ( 1 , sampled_topics . shape [ 0 ] ) else : self . sampled_topics = sampled_topics self . samples...
Allocate sampled topics to the documents rather than estimate them . Automatically generate term - topic and document - topic matrices .
22,070
def dt_comp ( self , sampled_topics ) : samples = sampled_topics . shape [ 0 ] dt = np . zeros ( ( self . D , self . K , samples ) ) for s in range ( samples ) : dt [ : , : , s ] = samplers_lda . dt_comp ( self . docid , sampled_topics [ s , : ] , self . N , self . K , self . D , self . alpha ) return dt
Compute document - topic matrix from sampled_topics .
22,071
def tt_comp ( self , sampled_topics ) : samples = sampled_topics . shape [ 0 ] tt = np . zeros ( ( self . V , self . K , samples ) ) for s in range ( samples ) : tt [ : , : , s ] = samplers_lda . tt_comp ( self . tokens , sampled_topics [ s , : ] , self . N , self . V , self . K , self . beta ) return tt
Compute term - topic matrix from sampled_topics .
22,072
def topic_content ( self , W , output_file = "topic_description.csv" ) : topic_top_probs = [ ] topic_top_words = [ ] tt = self . tt_avg ( False ) for t in range ( self . K ) : top_word_indices = list ( tt [ : , t ] . argsort ( ) [ - W : ] [ : : - 1 ] ) topic_top_probs . append ( np . round ( np . sort ( tt [ : , t ] ) ...
Print top W words in each topic to file .
22,073
def perplexity ( self ) : return samplers_lda . perplexity_comp ( self . docid , self . tokens , self . tt , self . dt , self . N , self . K , self . samples )
Compute perplexity for each sample .
22,074
def samples_keep ( self , index ) : try : if isinstance ( index , ( int , long ) ) : index = range ( self . samples ) [ - index : ] except ( NameError ) : if isinstance ( index , int ) : index = range ( self . samples ) [ - index : ] self . sampled_topics = np . take ( self . sampled_topics , index , axis = 0 ) self . ...
Keep subset of samples . If index is an integer keep last N = index samples . If index is a list keep the samples corresponding to the index values in the list .
22,075
def tt_avg ( self , print_output = True , output_file = "tt.csv" ) : avg = self . tt . mean ( axis = 2 ) if print_output : np . savetxt ( output_file , avg , delimiter = "," ) return avg
Compute average term - topic matrix and print to file if print_output = True .
22,076
def dict_print ( self , output_file = "dict.csv" ) : with codecs . open ( output_file , "w" , encoding = 'utf-8' ) as f : for ( v , k ) in self . token_key . items ( ) : f . write ( "%s,%d\n" % ( v , k ) )
Print mapping from tokens to numeric indices .
22,077
def query ( self , query_samples ) : self . sampled_topics = np . zeros ( ( self . samples , self . N ) , dtype = np . int ) for s in range ( self . samples ) : self . sampled_topics [ s , : ] = samplers_lda . sampler_query ( self . docid , self . tokens , self . topic_seed , np . ascontiguousarray ( self . tt [ : , : ...
Query docs with query_samples number of Gibbs sampling iterations .
22,078
def dt_avg ( self , print_output = True , output_file = "dt_query.csv" ) : avg = self . dt . mean ( axis = 2 ) if print_output : np . savetxt ( output_file , avg , delimiter = "," ) return avg
Compute average document - topic matrix and print to file if print_output = True .
22,079
def phrase_replace ( self , replace_dict ) : def r ( tokens ) : text = ' ' + ' ' . join ( tokens ) for k , v in replace_dict . items ( ) : text = text . replace ( " " + k + " " , " " + v + " " ) return text . split ( ) self . stems = list ( map ( r , self . stems ) )
Replace phrases with single token mapping defined in replace_dict
22,080
def stem ( self ) : def s ( tokens ) : return [ PorterStemmer ( ) . stem ( t ) for t in tokens ] self . stems = list ( map ( s , self . tokens ) )
Stem tokens with Porter Stemmer .
22,081
def bigram ( self , items ) : def bigram_join ( tok_list ) : text = nltk . bigrams ( tok_list ) return list ( map ( lambda x : x [ 0 ] + '.' + x [ 1 ] , text ) ) if items == "tokens" : self . bigrams = list ( map ( bigram_join , self . tokens ) ) elif items == "stems" : self . bigrams = list ( map ( bigram_join , self ...
generate bigrams of either items = tokens or stems
22,082
def E_step ( self ) : if not hasattr ( self , 'type_prob' ) : self . type_prob = np . empty ( ( self . N , self . K ) ) temp_probs = np . zeros ( ( self . N , self . K ) ) for i in range ( self . N ) : for k in range ( self . K ) : temp_probs [ i , k ] = np . log ( self . rho [ k ] ) + np . dot ( self . feature_counts ...
compute type probabilities given current parameter estimates .
22,083
def M_step ( self ) : for k in range ( self . K ) : self . rho [ k ] = self . type_prob [ : , k ] . sum ( ) / self . N for k in range ( self . K ) : for m in range ( self . M ) : temp_prob = np . dot ( self . type_prob [ : , k ] , self . feature_counts [ : , m ] ) if temp_prob < 1e-99 : temp_prob = 1e-99 self . mu [ k ...
generate new parameter estimates given updated type distribution
22,084
def estimate ( self , maxiter = 250 , convergence = 1e-7 ) : self . loglik = np . zeros ( maxiter ) iter = 0 while iter < maxiter : self . loglik [ iter ] = self . E_step ( ) if np . isnan ( self . loglik [ iter ] ) : print ( "undefined log-likelihood" ) break self . M_step ( ) if self . loglik [ iter ] - self . loglik...
run EM algorithm until convergence or until maxiter reached
22,085
def get_content ( path ) : with codecs . open ( abs_path ( path ) , encoding = 'utf-8' ) as f : return f . read ( )
Get content of file .
22,086
def sanitize_dataframe ( df ) : import pandas as pd import numpy as np df = df . copy ( ) if isinstance ( df . index , pd . core . index . MultiIndex ) : raise ValueError ( 'Hierarchical indices not supported' ) if isinstance ( df . columns , pd . core . index . MultiIndex ) : raise ValueError ( 'Hierarchical indices n...
Sanitize a DataFrame to prepare it for serialization .
22,087
def prepare_spec ( spec , data = None ) : import pandas as pd if isinstance ( data , pd . DataFrame ) : data = sanitize_dataframe ( data ) spec [ 'data' ] = { 'values' : data . to_dict ( orient = 'records' ) } elif data is None : pass else : data = pd . DataFrame ( data ) data = sanitize_dataframe ( data ) spec [ 'data...
Prepare a Vega - Lite spec for sending to the frontend .
22,088
def _repr_mimebundle_ ( self , include = None , exclude = None ) : id = uuid . uuid4 ( ) return ( { 'application/javascript' : self . _generate_js ( id ) } , { 'jupyter-vega' : '#{0}' . format ( id ) } , )
Display the visualization in the Jupyter notebook .
22,089
def pass_creds_to_nylas ( ) : if not google . authorized : return "Error: not yet connected with Google!" , 400 if "refresh_token" not in google . token : return ( ( "Error: missing Google refresh token. " "Uncomment the `reprompt_consent` line in the code to fix this." ) , 500 , ) google_resp = google . get ( "/oauth2...
This view loads the credentials from Google and passes them to Nylas to set up native authentication .
22,090
def pass_creds_to_nylas ( name , email , password , server_host = None ) : nylas_authorize_data = { "client_id" : app . config [ "NYLAS_OAUTH_CLIENT_ID" ] , "name" : name , "email_address" : email , "provider" : "exchange" , "settings" : { "username" : email , "password" : password } , } if server_host : nylas_authoriz...
Passes Exchange credentials to Nylas to set up native authentication .
22,091
def _get_resource_raw ( self , cls , id , extra = None , headers = None , stream = False , ** filters ) : headers = headers or { } headers . update ( self . session . headers ) postfix = "/{}" . format ( extra ) if extra else "" if cls . api_root != "a" : url = "{}/{}/{}{}" . format ( self . api_server , cls . collecti...
Get an individual REST resource
22,092
def convert_datetimes_to_timestamps ( data , datetime_attrs ) : if not data : return data new_data = { } for key , value in data . items ( ) : if key in datetime_attrs and isinstance ( value , datetime ) : new_key = datetime_attrs [ key ] new_data [ new_key ] = timestamp_from_dt ( value ) else : new_data [ key ] = valu...
Given a dictionary of data and a dictionary of datetime attributes return a new dictionary that converts any datetime attributes that may be present to their timestamped equivalent .
22,093
def verify_signature ( message , key , signature ) : digest = hmac . new ( key , msg = message , digestmod = hashlib . sha256 ) . hexdigest ( ) return digest == signature
This function will verify the authenticity of a digital signature . For security purposes Nylas includes a digital signature in the headers of every webhook notification so that clients can verify that the webhook request came from Nylas and no one else . The signing key is your OAuth client secret which only you and N...
22,094
def process_delta ( delta ) : kwargs = { "type" : delta [ "type" ] , "date" : datetime . datetime . utcfromtimestamp ( delta [ "date" ] ) , "object_id" : delta [ "object_data" ] [ "id" ] , } print ( " * {type} at {date} with ID {object_id}" . format ( ** kwargs ) )
This is the part of the code where you would process the information from the webhook notification . Each delta is one change that happened and might require fetching message IDs updating your database and so on .
22,095
def error_respond ( self ) : response = JSONRPCErrorResponse ( ) response . error = self . message response . unique_id = None response . _jsonrpc_error_code = self . jsonrpc_error_code if hasattr ( self , 'data' ) : response . data = self . data return response
Converts the error to an error response object .
22,096
def error_respond ( self , error ) : if self . unique_id is None : return None response = JSONRPCErrorResponse ( ) response . unique_id = None if self . one_way else self . unique_id code , msg , data = _get_code_message_and_data ( error ) response . error = msg response . _jsonrpc_error_code = code if data : response ...
Create an error response to this request .
22,097
def respond ( self , result ) : if self . one_way or self . unique_id is None : return None response = JSONRPCSuccessResponse ( ) response . result = result response . unique_id = self . unique_id return response
Create a response to this request .
22,098
def parse_reply ( self , data ) : if isinstance ( data , bytes ) : data = data . decode ( ) try : rep = json . loads ( data ) except Exception as e : raise InvalidReplyError ( e ) for k in rep . keys ( ) : if not k in self . _ALLOWED_REPLY_KEYS : raise InvalidReplyError ( 'Key not allowed: %s' % k ) if 'jsonrpc' not in...
Deserializes and validates a response .
22,099
def parse_request ( self , data ) : if isinstance ( data , bytes ) : data = data . decode ( ) try : req = json . loads ( data ) except Exception as e : raise JSONRPCParseError ( ) if isinstance ( req , list ) : requests = JSONRPCBatchRequest ( ) for subreq in req : try : requests . append ( self . _parse_subrequest ( s...
Deserializes and validates a request .