Best Python code snippet using Kiwi_python
affect.py
Source:affect.py  
1function [clu,W_bar,alpha_est,eigenvectors,eigenvalues] ...2	= batch_affect_spectral(ids,W,num_clust,varargin)3% batch_affect_spectral(ids,W,num_clust) performs AFFECT evolutionary4% spectral clustering with adaptively estimated forgetting factor.5% 6% ids is a length T cell array corresponding to the object IDs of the rows7% and columns of the similarity matrices specified by the length T cell8% array W. The object IDs at each time step must be stored in a cell array9% of strings. The similarity matrices can be either full or sparse matrices.10% 11% num_clust specifies the number of clusters and can either be a scalar, 12% vector of length T, or a string specifying the name of a cluster selection13% heuristic to apply. The choices for heuristic are14%	- 'modularity': calculate the first m eigenvectors, perform15%	  spectral clustering with 2 to m clusters, and select the number16%	  of clusters with maximum modularity.17%	- 'silhouette': similar to modularity except that the number of18%	  clusters with the maximum average silhouette index is chosen.19%	- 'eigengap': plot the first m eigenvalues of the Laplacian matrix20%	  obtained from the mean similarity matrix over all time steps and ask21%	  the user to select the number of clusters. This method results in a22%	  fixed number of clusters over all time steps.23%	- 'eigengap_var': plot the first m eigenvalues of the Laplacian matrix24%	  obtained from each similarity matrix. This method allows the number25%	  of clusters to vary over time, but requires the user to select the26%	  number of clusters at each iteration during each time step.27% m is specified through the optional parameter max_clust, described below.28% 29% Additional parameters are specified in ('name',value) pairs, e.g.30% batch_affect_spectral(ids,W,num_clust,'name1',value1,'name2',value2) and31% are as follows:32%	- max_clust (default: 10): Maximum number of clusters for cluster33%	  selection heuristics. Has no effect when number of clusters is34%	  specified by the user.35%	- alpha (default: 'estimate'): The choice of forgetting factor alpha36%	  can be a scalar between 0 and 1 (for constant forgetting factor) or 37%	  the string 'estimate' to adaptively estimate the forgetting factor at38%	  each time step.39%	- num_iter (default: 3): Number of iterations to use when estimating40%	  forgetting factor. Has no effect for constant forgetting factor.41%	- initialize (default: 'previous'): How to initialize iterative42%	  estimation of alpha. Choices are to initialize with the previous43%	  clusters ('previous') or to first perform clustering with alpha = 044%	  ('ordinary'). Has no effect for constant forgetting factor.45%	- objective (default: 'NC'): The spectral clustering objective function46%	  to optimize. Choices are average association ('AA'), ratio cut47%	  ('RC'), and normalized cut ('NC').48%	- eig_type (default: 'lanczos'): The method of eigendecomposition to49%	  use. Choices are full eigendecomposition ('full') or Lanczos50%	  iteration ('lanczos'), which is recommended for sparse matrices.51%	- disc_type (default: 'kmeans'): The method of discretization to use.52%	  Choices are k-means ('kmeans') and Yu and Shi's (2003) method of53%	  orthogonal transformations ('ortho'). 'kmeans' requires the54%	  Statistics Toolbox, and 'ortho' requires Yu and Shi's (2003) NCut55%	  clustering toolbox.56%	- num_reps (default: 1): The number of times the k-means algorithm57%	  should be run to determine the final clustering. Has no effect when58%	  discretizing by orthogonal transformations.59%	- remove_cc (default: false): Set to true to remove all connected60%	  components aside from the giant connected component before performing61%	  eigendecomposition. The clustering result would then contain k + c62%	  clusters, where c is the number of connected components. It is63%	  recommended to set remove_cc to true if there are a lot of such64%	  components, because they will be identified much more quickly than by65%	  eigendecomposition. Requires the Bioinformatics Toolbox.66%	- output (default: 0): Set to 0 to suppress most output messages and to67%	  higher numbers to display progressively more output.68% 69% Additional outputs can be obtained by specifying them as follows: 70% [clu,W_bar,alpha_est,eigenvectors,eigenvalues] = batch_affect_spectral(...)71%	- W_bar: Length T cell array of smoothed similarity matrices.72%	- alpha_est: M-by-T matrix of estimated forgetting factor at each73%	  iteration. M is specified in the second cell of input parameter alpha.74%	- eigenvectors: Length T cell array of leading eigenvectors.75%	- eigenvalues: Length T cell array of leading eigenvalues.76% 77% Author: Kevin Xu7879ip = inputParser;80ip.addRequired('ids',@iscell);81ip.addRequired('W',@iscell);82ip.addRequired('num_clust');83ip.addParamValue('max_clust',10,@(x)floor(x)==x);84ip.addParamValue('alpha','estimate');85ip.addParamValue('num_iter',3,@(x)floor(x)==x);86ip.addParamValue('initialize','previous');87ip.addParamValue('objective','NC');88ip.addParamValue('eig_type','lanczos');89ip.addParamValue('disc_type','kmeans');90ip.addParamValue('num_reps',1);91ip.addParamValue('remove_cc',false,@(x)islogical(x));92ip.addParamValue('output',0,@(x)(floor(x)==x) && (x>=0));93ip.parse(ids,W,num_clust,varargin{:});94max_clust = ip.Results.max_clust;95alpha = ip.Results.alpha;96num_iter = ip.Results.num_iter;97initialize = ip.Results.initialize;98objective = ip.Results.objective;99eig_type = ip.Results.eig_type;100disc_type = ip.Results.disc_type;101num_reps = ip.Results.num_reps;102remove_cc = ip.Results.remove_cc;103output = ip.Results.output;104105t_max = length(W);106% Validate number of clusters107num_ev_plot = 10;	% Number of eigenvalues to plot, if necessary108if isnumeric(num_clust)109	% Scalar or vector specifying number of clusters at each time step110	if isscalar(num_clust)111		num_clust = num_clust*ones(1,t_max);112	end113	assert(length(num_clust) == t_max, ...114		'Length of num_clust must equal the number of time steps');115else116	% Use specified heuristic for choosing number of clusters117	if strcmp(num_clust,'eigengap_var')118		m = 0;119		num_ev_plot = max_clust;120	elseif strcmp(num_clust,'eigengap')121		num_ev_plot = max_clust;122	elseif strcmp(num_clust,'modularity') || strcmp(num_clust, ...123			'silhouette')124		m = 2:max_clust;125	else126		error(['num_clust must be one of ' ...127			'''eigengap'', ''modularity'', or ''silhouette'''])128	end129end130131% Validate alpha and other parameters related to forgetting factor132if isnumeric(alpha)133	num_iter = 0;134	assert((alpha>=0) && (alpha<=1),'alpha must be between 0 and 1');135else136	if ~strcmp(alpha,'estimate')137		error('alpha must either be a number or ''estimate''');138	end139	if ~(strcmp(initialize,'previous') || strcmp(initialize,'ordinary'))140		error('initialize must either be ''previous'' or ''ordinary''')141	end142end143144% Normalize eigenvectors before running k-means for normalized cut spectral145% clustering146if strcmp(objective,'NC')147	norm_ev = true;148else149	norm_ev = false;150end151152% Initialize variable sizes153alpha_est = zeros(num_iter,t_max);154clu = cell(1,t_max);155W_bar = cell(1,t_max);156eigenvectors = cell(1,t_max);157eigenvalues = cell(1,t_max);158159% For the eigengap heuristic compute the mean similarity matrix over all160% time steps and plot the leading eigenvalues161if strcmp(num_clust,'eigengap')162	num_clust = num_clusters_eigengap(ids,W,objective,num_ev_plot) ...163		* ones(1,t_max);164end165166for t = 1:t_max167	if output > 0168		disp(['Processing time step ' int2str(t)])169	end170	171	if isnumeric(num_clust)172		m = num_clust(t);173	end174	175	n = length(ids{t});	176	% Only do temporal smoothing if not the first time step177	if t > 1178		% Identify rows and columns of new objects in current similarity179		% matrix180		[both_tf,both_loc] = ismember(ids{t},ids{t-1});181		W_prev = W_bar{t-1}(both_loc(both_tf),both_loc(both_tf));182		clu_prev = clu{t-1}(both_loc(both_tf));183		new_tf = ~both_tf;184		185		% Initialize smoothed similarity matrix186		if issparse(W{t})187			W_bar{t} = sparse(n,n);188		else189			W_bar{t} = zeros(n,n);190		end191		192		% No smoothing for new objects193		W_bar{t}(new_tf,:) = W{t}(new_tf,:);194		W_bar{t}(:,new_tf) = W{t}(:,new_tf);195		196		% Smoothing for objects present at both time steps197		if strcmp(alpha,'estimate')198			% Initialize current clustering result to be previous clustering199			% result or the result of one run of ordinary clustering200			clu{t} = zeros(n,1);201			if strcmp(initialize,'previous')202				clu{t}(both_tf) = clu_prev;203			else204				[clu{t},eigenvectors{t},eigenvalues{t}] = spectral_cluster ...205					(W{t},m,'objective',objective,'eig_type',eig_type, ...206					'disc_type',disc_type,'num_reps',num_reps,'norm_ev', ...207					norm_ev,'num_ev_plot',num_ev_plot,'remove_cc',remove_cc);208			end209			210			% Estimate alpha iteratively211			for iter = 1:num_iter212				alpha_est(iter,t) = estimate_alpha(W{t}(both_tf,both_tf), ...213					W_prev,clu{t}(both_tf));214				W_bar{t}(both_tf,both_tf) = alpha_est(iter,t)*W_prev ...215					+ (1-alpha_est(iter,t))*W{t}(both_tf,both_tf);216				[clu{t},eigenvectors{t},eigenvalues{t}] = spectral_cluster ...217					(W_bar{t},m,'objective',objective,'eig_type',eig_type, ...218					'disc_type',disc_type,'num_reps',num_reps,'norm_ev', ...219					norm_ev,'num_ev_plot',num_ev_plot,'remove_cc',remove_cc);220			end221		else222			W_bar{t}(both_tf,both_tf) = alpha*W_prev + (1-alpha) ...223				*W{t}(both_tf,both_tf);224			225			% Perform ordinary spectral clustering on W_bar226			[clu{t},eigenvectors{t},eigenvalues{t}] = spectral_cluster ...227				(W_bar{t},m,'objective',objective,'eig_type',eig_type, ...228				'disc_type',disc_type,'num_reps',num_reps,'norm_ev', ...229				norm_ev,'num_ev_plot',num_ev_plot,'remove_cc',remove_cc);			230		end231	else232		W_bar{t} = W{t};233		234		% Perform ordinary spectral clustering235		[clu{t},eigenvectors{t},eigenvalues{t}] = spectral_cluster ...236			(W_bar{t},m,'objective',objective,'eig_type',eig_type, ...237			'disc_type',disc_type,'num_reps',num_reps,'norm_ev', ...238			norm_ev,'num_ev_plot',num_ev_plot,'remove_cc',remove_cc);239	end240	241	% Select optimal number of clusters242	if ~isnumeric(num_clust)243		if strcmp(num_clust,'silhouette')244			[clu{t},avg_width] = select_clu_silhouette(W_bar{t}, ...245				clu{t},'similarity');246		elseif strcmp(num_clust,'modularity')247			[clu{t},Q] = select_clu_modularity(W_bar{t},clu{t},0);248		end249	end250	251	if t > 1	252		% Match clusters (using greedy method if more than 4 clusters)253		k = length(unique(clu{t}));254		if k > 4255			clu{t} = permute_clusters_greedy(ids{t},clu{t},ids{t-1}, ...256				clu{t-1},unmatched);257		else258			clu{t} = permute_clusters_opt(ids{t},clu{t},ids{t-1}, ...259				clu{t-1},unmatched);260		end261		unmatched = max(unmatched,max(clu{t})+1);262	else263		unmatched = max(clu{t})+1;264	end265end266267function k = num_clusters_eigengap(ids,W,objective,num_ev_plot)268269% Create mean similarity matrix over all time steps270W_seq = cell2matseq(ids,[],W,'union');271% W_seq will be a cell array if the similarity matrices are sparse and a272% 3-D matrix if the similarity matrices are full273if iscell(W_seq)274	t_max = length(W_seq);275	n = size(W_seq{1},1);276	W = sparse(n,n);277	for t = 1:t_max278		W = W + W_seq{t}/t_max;279	end280	281	if strcmp(objective,'AA')282		eigenvalues = eigs(W,num_ev_plot,'LA');283	else284		% Add eps to prevent zero entries, which cannot be inverted285		d = sum(abs(W),2) + eps;286		if strcmp(objective,'NC')287			D_inv_sqrt = spdiags(1./sqrt(d),0,n,n);288			L = speye(n) - D_inv_sqrt*W*D_inv_sqrt;289		else290			D = spdiags(d,0,n,n);291			L = D - W;292		end293		L = (L+L')/2;294		eigenvalues = eigs(L,num_ev_plot,'SA');295	end296else297	n = size(W_seq,1);298	W = mean(W_seq,3);299	300	if strcmp(objective,'AA')301		eigenvalues = sort(eig(W),'descend');302	else303		% Add eps to prevent zero entries, which cannot be inverted304		d = sum(abs(W),2) + eps;305		D = diag(d);306		if strcmp(objective,'NC')307			L = eye(n) - D^-0.5*W*D^-0.5;308		else309			L = D - W;310		end311		L = (L+L')/2;312		eigenvalues = sort(eig(L));313	end314end315plot(real(eigenvalues(1:min(n,num_ev_plot))),'x')
...formats.py
Source:formats.py  
...54                # Loop for each member of format catalog55                for j in i.dictvals:56                    # start57                    if i.fmt_type==Format_type.N:58                        f.write(f"\t\t{remove_cc(j[0])} = ")59                    else:60                        f.write(f"\t\t\"{remove_cc(j[0])}\" = ")61                    # label62                    if i.fmt_type == Format_val.I:63                        f.write(f"{remove_cc(j[1])}\n")64                    else:65                        f.write(f'"{remove_cc(j[1])}"\n')66                f.write("\t\t;\n")67            f.write("run;")68    @staticmethod69    def write_comment(fmt):70        # Break text into max segments71        fit_txt=[fmt.desc[i:i+65] for i in range(0, len(fmt.desc), 65)]72        com_txt=[]73        com_txt.append(f"\t*{'-' * 68}*")74        com_txt.append(f"\t|Format: {fmt.name}\n\t|Extensible:{fmt.is_extensible}")75        com_txt.append(f"\t|Description:")76        [com_txt.append(f"\t| {i}") for i in fit_txt]77        com_txt.append(f"\t*{'-' * 68}*;\n")78        return "\n".join(com_txt)79def remove_cc(s):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
