def run_spectral_cluster( weights_path, with_shuffle=True, n_clusters=4, shuffle_method='layer', n_samples=None, n_workers=None, with_shuffled_ncuts=False, random_state=RANDOM_STATE, ): if 'mlp' in str(weights_path): named_configs = ['mlp_config'] elif 'cnn' in str(weights_path): named_configs = ['cnn_config'] else: raise ValueError( 'Either mlp or cnn should be in path to determine the config.') config_updates = { 'weights_path': weights_path, 'with_labels': True, 'with_shuffle': with_shuffle, 'seed': random_state, 'num_clusters': n_clusters, 'shuffle_method': shuffle_method, 'with_shuffled_ncuts': with_shuffled_ncuts } if n_samples is not None: config_updates['num_samples'] = n_samples if n_workers is not None: config_updates['n_workers'] = n_workers with suppress(), all_logging_disabled(): experiment_run = clustering_experiment.run( config_updates=config_updates, named_configs=named_configs) metrics = experiment_run.result clustering_labels = metrics.pop('labels') node_mask = metrics.pop('node_mask') metrics.pop('shuffle_method', None) labels = np.full(len(node_mask), -1) labels[node_mask] = clustering_labels classification_metrics = extract_classification_metrics( Path(weights_path).parent) metrics.update(classification_metrics['unpruned'] if 'unpruned' in str(weights_path) else classification_metrics['pruned']) return labels, metrics
def clean_repo(): repo = pygit2.Repository(ROOT_PATH) if any(v != pygit2.GIT_STATUS_IGNORED for v in repo.status().values()): print(u'Your repository is dirty (you have uncommitted changes).') branch_name = repo.head.shorthand if not branch_name.startswith((u'rel-', u'release-', u'nightly')): print(u"You are building off branch '{}', which does not " u'appear to be a release branch'.format(branch_name)) with hold_files(*TO_PRESERVE): # stash everything away # - stash modified files # - then stash ignored and untracked # - unstash modified files # and we have a clean repo! sig = get_repo_sig(repo) # Not unused, PyCharm is not smart enough to understand utils.suppress mod_stashed = False unt_stashed = False with utils.suppress(KeyError): repo.stash(sig, message=u'Modified') mod_stashed = True with utils.suppress(KeyError): repo.stash( sig, message=u'Untracked + Ignored', include_untracked=True, include_ignored=True, ) unt_stashed = True if mod_stashed: repo.stash_pop(index=[mod_stashed, unt_stashed].count(True) - 1) try: yield finally: if unt_stashed: # if we commit during the yield above # we need to update the git index # otherwise git will complain about the pop repo.status() repo.stash_pop(index=0)
def clean_repo(): repo = pygit2.Repository(ROOT_PATH) if any(v != pygit2.GIT_STATUS_IGNORED for v in repo.status().values()): print("Your repository is dirty (you have uncommitted changes).") branch_name = repo.head.shorthand if not branch_name.startswith(("rel-", "release-", "nightly")): print("You are building off branch '{}', which does not " "appear to be a release branch".format(branch_name)) with hold_files(NSIS_PATH, REDIST_PATH): # stash everything away # - stash modified files # - then stash ignored and untracked # - unstash modified files # and we have a clean repo! sig = repo.default_signature mod_stashed = False unt_stashed = False with utils.suppress(KeyError): repo.stash(sig, message="Modified") mod_stashed = True with utils.suppress(KeyError): repo.stash( sig, message="Untracked + Ignored", include_untracked=True, include_ignored=True, ) unt_stashed = True if mod_stashed: repo.stash_pop(index=[mod_stashed, unt_stashed].count(True) - 1) try: yield finally: if unt_stashed: # if we commit during the yield above # we need to update the git index # otherwise git will complain about the pop repo.status() repo.stash_pop(index=0)
def hold_files(*files): tmpdir = tempfile.mkdtemp() file_map = {} # don't calculate paths twice for path in files: target = os.path.join(tmpdir, os.path.basename(path)) with utils.suppress(OSError): # skip file if missing mv(path, target) file_map[path] = target try: yield finally: for orig, target in file_map.items(): mv(target, orig) rm(tmpdir)