text:
import wandb
import numpy as np
import yaml
import glob
import subprocess
import argparse

# argparse
parser = argparse.ArgumentParser(
        description='hyperparamter sweep from YAML'
        )
parser.add_argument('template_yml', help='path to template YAML',
        default='./configs/smaller.yml')
parser.add_argument('local_yml', help='path to local YAML',
        default='./configs/local_setup_neo.yml')
parser.add_argument('dummy_yml', help='path to dummy YAML',
        default='./configs/dummy.yml')
parser.add_argument('sweep_yml', help='path to sweep YAML',
        default='./configs/sweep_yml')
parser.add_argument('--conf_dir', '-d',
        help='directory containing configs',
        default='')
parser.add_argument('--project', '-p', help='WandB project name',
        default='neox_sweep')
parser.add_argument('--group', '-g', help='WandB group name',
        default='bernaise')
args = parser.parse_args()
template_yml = args.conf_dir+'/'+args.template_yml
local_yml = args.conf_dir+'/'+args.local_yml
dummy_yml = args.conf_dir+'/'+args.dummy_yml
sweep_yml = args.conf_dir+'/'+args.sweep_yml

# read template to dictionary
with open(template_yml, 'r') as f:
    base_conf = yaml.safe_load(f)

# convert nested dictionaries to strings
new_conf = {}
old_keys = []
for k in base_conf:
    if type(base_conf[k]) == dict:
        old_keys.append(k)
        for k2 in base_conf[k]:
            new_conf[k+'.'+k2] = base_conf[k][k2]

base_conf.update(new_conf)

# remove redundant settings
for k in set(old_keys):
    base_conf.pop(k, None)

sweep_config = {
    'name': args.project,
    'method': 'grid',
    'metric': {
        'name': 'lm_loss',
        'goal': 'minimize'
    }
}

# initialize sweep_config with template
sweep_config['parameters'] = {
    k: {'values': [v]} for k, v in zip(
        base_conf.keys(), base_conf.values()
    )
}

# update sweep_config with user-defined sweep
if sweep_yml:
    with open(sweep_yml, 'r') as f:
        user_conf = yaml.safe_load(f)
        sweep_config['parameters'].update(user_conf)

def train():
    run = wandb.init(group=args.group)
    # conf stores the modified configuration
    conf = run.config.as_dict()
    print('GETTING CURRENT RUN CONFIG')
    # find optimizer keys, convert back to dictionaries
    new_conf = {}
    for k in conf.keys():
        parts = k.split('.')
        if len(parts) == 2:
            if parts[0] not in new_conf:
                new_conf[parts[0]] = {}
            new_conf[parts[0]][parts[1]] = conf[k]
        if len(parts) == 3:
            if parts[0] not in new_conf:
                new_conf[parts[0]] = {}
            if parts[1] not in new_conf[parts[0]]:
                new_conf[parts[0]][parts[1]] = {}
            new_conf[parts[0]][parts[1]][parts[2]] = conf[k]
    # remove redundant string keys
    for k in [x for x in conf.keys() if '.' in x]:
        conf.pop(k, None)
    conf.pop('valid_set', None)
    # insert proper key-value pairs
    conf.update(new_conf)
    # write dummy yml
    print('WRITING DUMMY YAML')
    with open(dummy_yml, 'w') as f:
        yaml.safe_dump(conf, f, default_flow_style=False)
    # train
    print('TRAINING')
    print(conf)
    # function for running deepy
    def cmd(args):
        with subprocess.Popen(args, stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT) as process:
            for _line in process.stdout:
                line = _line.decode('utf8')
                print(line)
                if 'validation result' in line:
                    # find loss
                    lm_loss = float(
                            [x for x in line.split('|') if \
                                    'lm_loss value' in x][0].split(':')[1].strip()
                            )
                    # track loss
                    wandb.log({'lm_loss': lm_loss})
    # run deepy
    cmd('python deepy.py train.py {} {}'.format(dummy_yml,local_yml).split(' '))
    run.finish()

sweep_id = wandb.sweep(
    sweep_config, project=args.project,
)
agent = wandb.agent(
    sweep_id=sweep_id,
    function=train,
    project=args.project
)
agent.run()

max_stars_repo_name: mgobrain/gpt-neox

max_issues_repo_name: mgobrain/gpt-neox

max_forks_repo_name: mgobrain/gpt-neox


text:
from .utils import Atom, Residue, ActiveSite
import numpy as np
from scipy import linalg
import matplotlib as mpl
import matplotlib.pyplot as plt
import math
from scipy.signal import argrelextrema

def get_tail_coords(site):
    """
    For a given active site, go through the residues
    and return a list of coordinates for the last atoms
    of each residue. The last atom is simply the last
    element in the list of atoms for each residue in the
    active site

    Input: ActiveSite
    Output: List of Coordinates
    """

    # Grab the residues and upack the coordinates
    residues = site.residues
    coords = []

    for residue in residues:
        # Tail atoms are defined as the
        # last element in the list of atoms
        # for the given residue
        tail_atom = residue.atoms[-1]
        coords.append(tail_atom.coords)

    return coords

def find_dist(site):
    """
    For a given active site, find the average distance
    from the best fit plane through the coordinates of the
    tail atoms as defined above (in function get_tail_coords).

    Input: ActiveSite
    Output: average distance (float)
    """

    # Get the coordinates of the tail
    # atoms as defined above (get_tail_coords)
    coords = get_tail_coords(site)
    coords = np.asarray(coords)

    # If only three tail atoms, distance will
    # be defined as 0.0
    if len(coords) <= 3:
        return 0.0
    else:
        # Define the 3D least-squares problem. We are solving the least
        # squares coefficients C where AC = y where y are the coordinates
        # of the tail atoms
        A = np.c_[coords[:,0], coords[:,1], np.ones(coords.shape[0])]
        C,sum_residuals,_,_ = linalg.lstsq(A, coords[:,2])

        # Define the average distance as the sum of the squared
        # residuals normalized by the number of tail atoms
        avg_dist = sum_residuals / len(coords)

        return avg_dist

def compute_similarity(site_a, site_b):
    """
    Compute the similarity between two given ActiveSite instances.
    Note that here, a larger similarity means the two sites are
    ostensibly more different. This is accounted for when
    the similarity matrix is computed.

    Input: two ActiveSite instances
    Output: the similarity between them (a floating point number)
    """

    # Get the average distance for both active sites
    dist_a = find_dist(site_a)
    dist_b = find_dist(site_b)
    # Define Similarity as the absolute value
    # of the difference between the average
    # distances of the two active sites
    similarity = abs(dist_a - dist_b)

    # Set to logarithmic scale since the
    # range of distances might be very large
    if similarity <= 1.0:
        similarity = 0.0
    else:
        similarity = np.log(similarity)

    return similarity

def get_sim_matrix(active_sites):
    """
    For a given active site, return the similarity
    matrix, sim_matrix, where sim_matrix[i,j] is
    the similarity score between active site i and
    active site j

    Input: List of ActiveSites
    Output: Similarity Matrix (dictionary of dictionaries)
    """

    # Initiate matrix of zeros
    # sim_matrix = np.zeros((len(active_sites), len(active_sites)))
    sim_matrix = {}

    # Loop through the list of active sites and calculate the similarity
    # scores, filling out the similarity matrix. Since the matrix is
    # symmetric, we have that sim_matrix[i,j] = sim_matrix[j,i]

    # max_score keeps track of the biggest score found
    # for normalizing
    max_score = 0.0
    for i in range(len(active_sites)):
        for j in range(i, len(active_sites)):
            site_a = active_sites[i]
            site_b = active_sites[j]
            similarity = compute_similarity(site_a, site_b)
            if similarity > max_score:
                max_score = similarity

            if site_a in sim_matrix:
                sim_matrix[site_a][site_b] = similarity
            else:
                sim_matrix[site_a] = {site_b : similarity}

            if site_b in sim_matrix:
                sim_matrix[site_b][site_a] = similarity
            else:
                sim_matrix[site_b] = {site_a : similarity}

    # loop through the sim_matrix once to normalize all
    # the values and set to [0,1] scale where 1 is the best
    # possible similarity score
    for outer_site in sim_matrix:
        for inner_site in sim_matrix[outer_site]:
            current_sim = sim_matrix[outer_site][inner_site]
            sim_matrix[outer_site][inner_site] = 1.0 - (current_sim / max_score)

    return sim_matrix

def get_avg_sim(site, cluster, sim_matrix):
    """
    Compute the average similarity between the ActiveSite
    site and the cluster

    Input: site (ActiveSite)
    Output: cluster (list of ActiveSites)
    """

    sim = 0.0
    for cluster_site in cluster:
        sim += sim_matrix[site][cluster_site]

    sim = sim / float(len(cluster))
    return sim

def compute_cluster_centers(clusters, sim_matrix):
    """
    Given a clustering, return the cluster centers
    defined as the elements in each cluster that are most
    similar to all other elements in that same cluster.

    Input: Clustering (list of lists of ActiveSite instances)
           sim_matrix (a dictionary of dictionaries with similarity scores)

    Output: cluster_centers (A list of ActiveSite instances)
    """
    cluster_centers = []

    # For each cluster, find the cluster member that has the highest
    # average similarity to all other nodes in the same cluster.
    # Define this as the center of the respective cluster
    for cluster in clusters:
        best_center = cluster[0]
        best_sim = get_avg_sim(cluster[0], cluster, sim_matrix)
        for active_site in cluster:
            sim = get_avg_sim(active_site, cluster, sim_matrix)
            if sim > best_sim:
                best_sim = sim
                best_center = active_site
        cluster_centers.append(best_center)

    # Return list of ActiveSites that preserves the
    # ordering of the input clusters. cluster_centers[i]
    # is the ActiveSite which is found to be the center of
    # the cluster clusters[i]
    return cluster_centers

def k_means(k, active_sites, sim_matrix):
    """
    Perform k-means clustering given a list of ActiveSites,
    a similarity matrix, and a given value of k

    Input: k (int)
           active_sites (list of ActiveSites)
           sim_matrix (dictionary of dictionaries)

    Output: clusters (a list of lists of ActiveSites)
    """

    # Make a copy of the active_site list
    active_sites_working = list(active_sites)

    # Make sure k is at most the number of ActiveSites
    if k > len(active_sites):
        raise ValueError('K-means Clustering - Number of Cluster Centers'+
                         'cannot be larger than number of active sites')

    # Create initial Clusters
    clusters = []
    clusters_one_pass = []
    cluster_centers = []
    for i in range(k):
        cluster_centers.append(active_sites_working.pop())
        clusters.append([cluster_centers[i]])

    # Iteratively assign active sites to clusters until we reach the
    # point at which the resulting cluster configuration
    # has been seen either one or two passes previously
    while(True):

        # The working copy of the next clustering
        new_clusters = []

        # Iniitalize new_clusters with empty clusters
        for i in range(k):
            new_clusters.append([])
        active_sites_working = list(active_sites)

        # Add cluster centers to their respective clusters
        for i, center in enumerate(cluster_centers):
            active_sites_working.remove(center)
            new_clusters[i].append(center)

        # Assign every site that has not already been designated
        # as a cluster center to cluster with the most similar
        # center
        for active_site in active_sites_working:
            # best_sim_score = get_avg_sim(active_site, clusters[0], sim_matrix)
            best_sim_score = sim_matrix[active_site][cluster_centers[0]]
            best_cluster = 0
            for i in range(1, k):
                cluster = clusters[i]
                center_similarity = sim_matrix[active_site][cluster_centers[i]]
                if center_similarity > best_sim_score:
                    best_sim_score = center_similarity
                    best_cluster = i
            new_clusters[best_cluster].append(active_site)

        # If this clustering has been seen in the last two iterations,
        # exit the loop
        if new_clusters == clusters or new_clusters == clusters_one_pass:
            break
        else:
            cluster_centers = compute_cluster_centers(new_clusters, sim_matrix)
            clusters_one_pass = clusters
            clusters = new_clusters

    return clusters

def get_optimal_k(active_sites, sim_matrix, plot = False):
    """
    For use in determining a good k value to
    use for cluster_by_partitioning. Does k-means
    clustering for a range of k values and chooses
    one

    Input: active_sites (list of ActiveSites)
           sim_matrix (dictionary of dictionaries)
    Output: k (int)
    """

    clustering_sims = []
    top_limit =  int(math.ceil(len(active_sites) * (3.0/5)))
    k_vals = range(1, top_limit)

    # Iterate over a range of k values and find the average cluster similarity,
    # defined as the average similarity between nodes within a cluster averaged
    # across all clusters for every resulting clustering
    for k in k_vals:
        avg_cluster_sim = 0.0
        # clusterings.append(cluster_by_partitioning(active_sites, k))
        clustering = k_means(k, active_sites, sim_matrix)
        for cluster in clustering:
            cluster_sim = 0.0
            for active_site in cluster:
                cluster_sim += get_avg_sim(active_site, cluster, sim_matrix)
            cluster_sim = cluster_sim / float(len(cluster))
            avg_cluster_sim += cluster_sim

        avg_cluster_sim = avg_cluster_sim / float(k)
        clustering_sims.append(avg_cluster_sim)

    # If we allow k to get high enoug, the
    # intra-cluster similarity will converge to
    # 1 since every ActiveSite will be assigned to
    # its own cluster. So, find the lowest k value
    # at which the resulting intra-cluster similarity
    # is at 90% of the max intra-cluster similarity found
    max_sim = np.amax(clustering_sims)
    best_k = 1
    for k in k_vals:
        if clustering_sims[k - 1] >= max_sim * 0.90:
            best_k = k
            break

    # Plotting k values and resulting intra-cluster similarities
    if plot == True:
        fig = plt.figure()
        plt.plot(k_vals, clustering_sims)
        plt.plot([best_k], [clustering_sims[best_k - 1]], 'r*')
        plt.xlabel('k')
        plt.ylabel('Average Inter-Cluster Similarity')
        plt.title('Tested k values for Clustering by Partitioning')
        plt.show()

    return best_k

def compute_worst_similarity(cluster_1, cluster_2, sim_matrix):
    """
    Given two clusters, computes the worst similarity score between
    any possible pair of nodes between the two clusters

    Input: cluster1, cluster2 (lists of of ActiveSites)
           sim_matrix (dictionary of dictionaries)
    Output: worst_sim (a float between 0.0 and 1.0)
    """
    worst_sim = 1.0
    for site_1 in cluster_1:
        for site_2 in cluster_2:
            current_sim = sim_matrix[site_1][site_2]
            if current_sim < worst_sim:
                worst_sim = current_sim

    return worst_sim

def furthest_neighbor(active_sites, sim_matrix, similarity_cutoff):
    """
    Perform furthest_neighbor hierarchical agglomerative clustering
    given a similarity cutoff value. Begins by placing every ActiveSite
    into its own cluster then iteratively joining the two clusters between
    which the least similar elements have the highest similarity score among
    all respective clusters in the current clustering. Iteratively does this
    until the best similarity score between furthest cluster neighbors is
    less than the provided similarity cutoff value

    Input: active_sites (list of ActiveSites)
           sim_matrix (dictionary of dictionaries)
           similarity_cutoff (float)

    Output: clusters (a list of lists of ActiveSites)
    """
    # clusterings = []

    # Initialize current_clustering such that
    # every ActiveSite is its own cluster
    current_clustering = []
    for site in active_sites:
        current_clustering.append([site])

    # clusterings.append(current_clustering)

    while True:
        best_sim = 0.0
        join_index_1 = 0
        join_index_2 = 1

        # Go through every cluster and find the furthest-neighbor
        # similarity scores for every other cluster for all pairs
        # that haven't been checked yet. If a furhtest-neighbor
        # similarity score is found that is better than the
        # current best score, update the current best score
        # and keep track of which two clusters produce this
        # new best score
        for i in range(len(current_clustering)):
            current_cluster = current_clustering[i]
            for j in range(i + 1, len(current_clustering)):
                comparison_cluster = current_clustering[j]
                worst_sim = compute_worst_similarity(current_cluster, comparison_cluster, sim_matrix)
                if worst_sim > best_sim:
                    best_sim = worst_sim
                    join_index_1 = i
                    join_index_2 = j

        # If, this current iteration's best furthest-neighbor similarity score
        # is worse than the provided similarity cutoff, exit the loop
        if best_sim <= similarity_cutoff:
            break

        # Initialize a new clustering
        new_clustering = []
        # Create new cluster by joining the two clusters in the previous clustering that
        # produced the best furthest-neighbor similarity score
        joined_cluster = current_clustering[join_index_1] + current_clustering[join_index_2]
        # Add all old clusters from the previous clustering that were not joined
        # to the new clustering
        for cluster_index, current_cluster in enumerate(current_clustering):
            if (cluster_index != join_index_1 and cluster_index != join_index_2):
                new_clustering.append(current_cluster)
        # Add the new joined cluster
        new_clustering.append(joined_cluster)
        # clusterings.append(current_clustering)
        current_clustering = new_clustering

    return current_clustering

def get_sim_cutoff(active_sites, sim_matrix, plot = False):
    """
    For use in determining a good similarity cutoff value
    to use for cluster_hierarchically. Does hierarchical
    clustering over a range of possible similarity cutoff
    values and chooses one

    Input: active_sites (list of ActiveSites)
           sim_matrix (dictionary of dictionaries)
    Output: best_sim_cutoff (float)
    """
    sim_cutoffs = np.linspace(0.0,1, 30)
    clustering_sims = []

    # Iterate through range of possible sim_cutoff values and
    # perform hierarchical clustering for each one. For each
    # resulting clustering, calculate the average intra-cluster
    # similarity score averaged across all clusters
    for current_sim_cutoff in sim_cutoffs:
        avg_cluster_sim = 0.0
        # clusterings.append(cluster_by_partitioning(active_sites, k))
        # clustering = k_means(k, active_sites, sim_matrix)
        clustering = furthest_neighbor(active_sites, sim_matrix, current_sim_cutoff)
        for cluster in clustering:
            cluster_sim = 0.0
            for active_site in cluster:
                cluster_sim += get_avg_sim(active_site, cluster, sim_matrix)
            cluster_sim = cluster_sim / float(len(cluster))
            avg_cluster_sim += cluster_sim

        avg_cluster_sim = avg_cluster_sim / len(clustering)
        clustering_sims.append(avg_cluster_sim)

    best_sim_cutoff = 0.0
    best_cluster_sim = 0.0
    best_avg_diff = 0.0
    clustering_sims_array = np.asarray(clustering_sims)

    # For every clustering (and respective similarity cutoff value),
    # look for local maxima in intra-cluster similarities (for which
    # the intra-cluster similarity is higher than both the next highest
    # and next lowest similarity cutoff scores). Find similarity cutoff
    # value that produces the local intra-cluster similarity maximum whose
    # average distance from the neighboring intra-cluster similarities is
    # higher than for all other local maxima
    for index, clustering_sim in enumerate(clustering_sims):
        if index == 0 or index == (len(clustering_sims) - 1):
            continue

        prev_point = clustering_sims[index - 1]
        next_point = clustering_sims[index + 1]
        if (clustering_sim > prev_point) and (clustering_sim > next_point):
            avg_diff = ((clustering_sim - prev_point) + (clustering_sim - next_point)) / 2.0
            if avg_diff > best_avg_diff:
                best_avg_diff = avg_diff
                best_sim_cutoff = sim_cutoffs[index]
                best_cluster_sim = clustering_sim

    # Plotting for visualizing similarity cutoff inputs and
    # resulting intra-cluster similarities
    if plot == True:
        fig = plt.figure()
        plt.plot(sim_cutoffs, clustering_sims)
        plt.plot(best_sim_cutoff, best_cluster_sim, 'r*')
        plt.xlabel('similarity cutoff')
        plt.ylabel('Average Inter-Cluster Similarity')
        plt.title('Tested Similarity Cutoffs for Hierarchical clustering')
        plt.show()

    return best_sim_cutoff

def cluster_by_partitioning(active_sites, sim_matrix):
    """
    Cluster a given set of ActiveSite instances using a partitioning method.

    Input: a list of ActiveSite instances, k (int)
    Output: a clustering of ActiveSite instances
            (this is really a list of clusters, each of which is list of
            ActiveSite instances)
    """

    if len(active_sites) <= 1:
        clusters = []
        for site in active_sites:
            clusters.append([site])
        return clusters
    # For determining good k value to use
    best_k = get_optimal_k(active_sites, sim_matrix)
    # Use k-means implementation above to get clusters from partitioning
    clusters = k_means(best_k, active_sites, sim_matrix)
    return clusters

def cluster_hierarchically(active_sites, sim_matrix):
    """
    Cluster the given set of ActiveSite instances using a hierarchical algorithm.                                                                  #

    Input: a list of ActiveSite instances
    Output: a list of clusterings
            (each clustering is a list of lists of Sequence objects)
    """

    if len(active_sites) <= 1:
        clusters = []
        for site in active_sites:
            clusters.append([site])
        return clusters

    # Determine a good similarity cutoff score to use as defined above
    similarity_cutoff = get_sim_cutoff(active_sites, sim_matrix)
    # Use furthest neighbor agglomerative clustering to get clusters from
    # hierarchical clustering
    clusterings = furthest_neighbor(active_sites, sim_matrix, similarity_cutoff)
    return clusterings

def quality_score(clustering, active_sites, sim_matrix):
    """
    Measure the quality of the clustering by taking the average silhouette
    score of all ActiveSites in the clustering. For a given ActiveSite, the
    silhouette score s is defined as
            s = (a - b) / (min(a, b))
    where a is the average similarity of the ActiveSite with all other members
    of its assinged cluster and b is the average similarity between the ActiveSite
    and all others members of the closest cluster (defined as having the highest
    average similarity)

    Input: clustering (list of lists of ActiveSites)
    Output: Silhouette Score (float bewteen -1 and 1)
    """

    s = 0.0

    for site in active_sites:
        # Find the cluster to which the current site belongs
        for cluster in clustering:
            if site in cluster:
                break

        # Compute the a value
        a = get_avg_sim(site, cluster, sim_matrix)

        # Compute the b value
        b = 0.0
        for test_cluster in clustering:
            if test_cluster != cluster:
                test_sim = get_avg_sim(site, test_cluster, sim_matrix)
                if test_sim > b:
                    b = test_sim

        s += (a - b) / min(a, b)

    s = s / len(active_sites)
    return s

def comparison_score(hierarchical_clustering, partioning_clustering, active_sites, sim_matrix):
    """
    Given two clusterings, one hiearchical and one partioning, find their similarity defined as determined
    by the difference between their two respective silouette scores and normalized to the range [0,1] where
    1 means the two clusterings have the same silouette scores and 0 means that the two clusterings have
    the largest possible difference in silouette scores

    Input: hierarchical_clustering, partioning_clustering (list of lists of ActiveSites)
           active_sites (list of ActiveSites)
           sim_matrix (dictionary of dictionaries)

    Output: comparison_score (float)
    """

    # Get the two respective silhouette scores
    h_quality = quality_score(hierarchical_clustering, active_sites, sim_matrix)
    p_quality = quality_score(partioning_clustering, active_sites, sim_matrix)

    # Find their difference and normalize to [0,1]
    comparison_score = 1.0 - abs(h_quality - p_quality) / 2.0
    return comparison_score

def compare(hierarchical_clustering, partioning_clustering, active_sites, sim_matrix):
    """
    Given a hierarchical and partioning clustering, produce a plot that measures their
    respesctive qualities

    Intput: hierarchical_clustering, partioning_clustering (list of lists of ActiveSites)
            active_sites (list of ActiveSites)
            sim_matrix (dictionary of dictionaries)

    Output: None
    """

    partitioning_qualities = []
    partioning_clusterings = []
    top_limit =  int(math.ceil(len(active_sites) * (3.0/5)))
    k_vals = range(1, top_limit)

    hierarchical_qualities = []
    hierarchical_clusterings = []
    sim_cutoffs = np.linspace(0.0,1, len(k_vals))

    comparison_scores = []

    # Calculate partioning and hierarchical clusterings across range of k values and
    # similarity cutoffs. For each resulting clustering, calculate the quality scores
    for k in k_vals:
        partioning_clustering = k_means(k, active_sites, sim_matrix)
        partioning_clusterings.append(partioning_clustering)
        quality = quality_score(partioning_clustering, active_sites, sim_matrix)
        partitioning_qualities.append(quality)
    for current_sim_cutoff in sim_cutoffs:
        hierarchical_clustering = furthest_neighbor(active_sites, sim_matrix, current_sim_cutoff)
        hierarchical_clusterings.append(hierarchical_clustering)
        quality = quality_score(hierarchical_clustering, active_sites, sim_matrix)
        hierarchical_qualities.append(quality)

    # Calculate the comparison scores for clusterings produced at the same position in the
    # range [0,1] of input values (k value for partioning or similary cutoff score for hierarchical)
    for i in range(len(k_vals)):
        score = comparison_score(hierarchical_clusterings[i], partioning_clusterings[i], active_sites, sim_matrix)
        comparison_scores.append(score)

    # Plot the resulting scores
    k_vals_normalized = np.asarray(k_vals) / np.max(k_vals)
    fig = plt.figure()
    plt.plot(k_vals_normalized, partitioning_qualities, label = 'Partitioning Qualities')
    plt.plot(sim_cutoffs, hierarchical_qualities, label = 'Hierarchical Qualities')
    plt.plot(k_vals_normalized, comparison_scores, label = 'Comparison Scores')
    plt.xlabel('normalized k (Partitioning) and similarity cutoffs (Hierarchical)')
    plt.ylabel('Quality and Comparison Scores')
    plt.title('Quality and Comparison Scores for Partioning and Hierarchical Clustering')
    plt.legend()
    plt.show()

def biological_score(cluster):
    """
    Produces a measure of how biologically significant a given cluster in the
    following manner. For every pair of active sites in the cluster, compute
    a sequence similarity score between the two by dividing the number of residues
    the two sequences have in common by the length of the longer sequence. This
    produces a number in the range [0, 1] for every pair of active sites. Average
    this score across the entire cluster and return as the biological score of the
    cluster.

    Input: cluster (list of ActiveSites)

    Output: biological_score (float)
    """

    biological_score = 0.0
    # For every pair of active sites in the
    # cluster, find the residues (types of residues) in their
    # sequences and count the number of residues that they
    # have in common.
    for i in range(len(cluster)):
        for j in range(i + 1, len(cluster)):
            pair_score = 0.0
            residues_1 = []
            residues_2 = []
            for residue in cluster[i].residues:
                residues_1.append(residue.type)
            for residue in cluster[j].residues:
                residues_2.append(residue.type)

            for residue_type in set(residues_1):
                pair_score += residues_2.count(residue_type)

            # Divide the number of residues they have in common
            # by the length of the longer sequence
            pair_score = pair_score / max(len(residues_1), len(residues_2))

            biological_score += pair_score

    if len(cluster) > 1:
        biological_score = biological_score / (0.5 * (len(cluster) - 1) * len(cluster))

    return biological_score

def clustering_biology(clustering, active_sites, sim_matrix):
    """
    Given a clustering, calculate the average cluster biological
    significance score across all clusters

    Intput: clustering (list of lists of ActiveSites)
            active_sites (list of ActiveSites)
            sim_matrix (dictionary of dictionaries)

    Output: avg_bio_score (float)
    """

    # Calculate biological score for every cluster in
    # clustering then average
    avg_bio_score = 0.0
    for cluster in clustering:
        avg_bio_score += biological_score(cluster)

    avg_bio_score = avg_bio_score / float(len(clustering))
    return avg_bio_score

def compare_biology_significance(hierarchical_clustering, partioning_clustering, active_sites, sim_matrix):
    """
    Given a hierarchical and partioning clustering, produce a plot that measures their
    respesctive qualities

    Intput: hierarchical_clustering, partioning_clustering (list of lists of ActiveSites)
            active_sites (list of ActiveSites)
            sim_matrix (dictionary of dictionaries)

    Output: None
    """

    partitioning_biology_scores = []
    partioning_clusterings = []
    top_limit =  int(math.ceil(len(active_sites) * (3.0/5)))
    k_vals = range(1, top_limit)

    hierarchical_biology_scores = []
    hierarchical_clusterings = []
    sim_cutoffs = np.linspace(0.0,1, len(k_vals))

    # Calculate partioning and hierarchical clusterings across range of k values and
    # similarity cutoffs. For each resulting clustering, calculate the biological
    # significance scores
    for k in k_vals:
        partioning_clustering = k_means(k, active_sites, sim_matrix)
        partioning_clusterings.append(partioning_clustering)
        biology = clustering_biology(partioning_clustering, active_sites, sim_matrix)
        partitioning_biology_scores.append(biology)
    for current_sim_cutoff in sim_cutoffs:
        hierarchical_clustering = furthest_neighbor(active_sites, sim_matrix, current_sim_cutoff)
        hierarchical_clusterings.append(hierarchical_clustering)
        biology = clustering_biology(hierarchical_clustering, active_sites, sim_matrix)
        hierarchical_biology_scores.append(biology)

    # Plot the resulting biology scores for all resulting clusterings
    k_vals_normalized = np.asarray(k_vals) / np.max(k_vals)
    fig = plt.figure()
    plt.plot(k_vals_normalized, partitioning_biology_scores, label = 'Partitioning')
    plt.plot(sim_cutoffs, hierarchical_biology_scores, label = 'Hierarchical')
    plt.xlabel('normalized k (Partitioning) and similarity cutoffs (Hierarchical)')
    plt.ylabel('Biological Significance Scores')
    plt.title('Biological Significance for Partioning and Hierarchical Clustering')
    plt.legend()
    plt.show()

ext: py

reason: import numpy,from scipy


text:
# ******************************************************************************
# Copyright 2018-2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# *****************************************************************************

import tensorflow as tf
import numpy as np
import os
import sys

# Add parent directory to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from mnist_util import load_mnist_data, \
    get_variable, \
    conv2d_stride_2_valid, \
    avg_pool_3x3_same_size

def cryptonets_model(x, mode):
    """Builds the graph for classifying digits based on Cryptonets

    Args:
        x: an input tensor with the dimensions (N_examples, 28, 28)

    Returns:
        A tuple (y, a scalar placeholder). y is a tensor of shape
        (N_examples, 10), with values equal to the logits of classifying the
        digit into one of 10 classes (the digits 0-9).
    """
    if mode not in set(['train', 'test']):
        print('mode should be train or test')
        raise Exception()

    # Reshape to use within a conv neural net.
    # Last dimension is for "features" - there is only one here, since images
    # are grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc.
    # CryptoNets's output of the first conv layer has feature map size 13 x 13,
    # therefore, we manually add paddings.
    with tf.name_scope('reshape'):
        print('padding')
        paddings = [[0, 0], [0, 1], [0, 1], [0, 0]]
        x = tf.pad(x, paddings)
        print('padded')

    # First conv layer
    # Input: N x 28 x 28 x 1
    # Filter: 5 x 5 x 1 x 5
    # Output: N x 13 x 13 x 5
    with tf.name_scope('conv1'):
        W_conv1 = get_variable("W_conv1", [5, 5, 1, 5], mode)
        h_conv1 = tf.square(conv2d_stride_2_valid(x, W_conv1))

    # Pooling layer
    # Input: N x 13 x 13 x 5
    # Output: N x 13 x 13 x 5
    with tf.name_scope('pool1'):
        h_pool1 = avg_pool_3x3_same_size(h_conv1)

    # Second convolution
    # Input: N x 13 x 13 x 5
    # Filter: 5 x 5 x 5 x 50
    # Output: N x 5 x 5 x 50
    with tf.name_scope('conv2'):
        W_conv2 = get_variable("W_conv2", [5, 5, 5, 50], mode)
        h_conv2 = conv2d_stride_2_valid(h_pool1, W_conv2)

    # Second pooling layer
    # Input: N x 5 x 5 x 50
    # Output: N x 5 x 5 x 50
    with tf.name_scope('pool2'):
        h_pool2 = avg_pool_3x3_same_size(h_conv2)

    # Fully connected layer 1
    # Input: N x 5 x 5 x 50
    # Input flattened: N x 1250
    # Weight: 1250 x 100
    # Output: N x 100
    with tf.name_scope('fc1'):
        h_pool2_flat = tf.reshape(h_pool2, [-1, 5 * 5 * 50])
        W_fc1 = get_variable("W_fc1", [5 * 5 * 50, 100], mode)
        h_fc1 = tf.square(tf.matmul(h_pool2_flat, W_fc1))

    # Map the 100 features to 10 classes, one for each digit
    # Input: N x 100
    # Weight: 100 x 10
    # Output: N x 10
    with tf.name_scope('fc2'):
        W_fc2 = get_variable("W_fc2", [100, 10], mode)
        y_conv = tf.matmul(h_fc1, W_fc2)
    return y_conv

max_stars_repo_name: kthur/he-transformer

max_issues_repo_name: kthur/he-transformer

max_forks_repo_name: kthur/he-transformer

reason: import numpy


text:
"""
    Perform two way ANOVA.

"""
__author__ = 'Raquel G. Alhama'

from scipy import stats
import statsmodels
import statsmodels.api as sm
from statsmodels.formula.api import ols

def anova_mcconkey_test(data):
    """
        2 WAY ANOVA.
        Formula for mcconkey test: correct ~ C(entropy) + C(fixpos) + C(entropy):C(fixpos)
        :param data: pandas dataframe
        :return: p value for entropy, p value for fixation, p value for the interaction, full table
    """

    formula = 'correct ~ C(entropy) + C(fixpos) + C(entropy):C(fixpos)'
    model = ols(formula, data).fit()
    try:
        aov_table = statsmodels.stats.anova.anova_lm(model, typ=2)
    except Exception: #Avoid LinAlg exception for identical performance; return non-significant
        return 1,1,1, None

    #Extract relevant data
    p_entropy = aov_table.loc["C(entropy)","PR(>F)"]
    p_fixation = aov_table.loc["C(fixpos)","PR(>F)"]
    p_inter = aov_table.loc["C(entropy):C(fixpos)","PR(>F)"]

    return p_entropy, p_fixation, p_inter, aov_table

def anova_stepwise(data):
    """
    Performs 2 way ANOVA on the results of the model in the McConkey test.
    It returns the p values only.
    This was only used for debugging purposes.
    Most of the code is taken from the freely shared code in: https://www.marsja.se/three-ways-to-carry-out-2-way-anova-with-python/
    :param data: pandas dataframe
    :return:
    """

    #Degrees of freedom
    N = len(data.correct)
    df_a = len(data.entropy.unique()) - 1
    df_b = len(data.fixpos.unique()) - 1
    df_axb = df_a*df_b
    df_w = N - (len(data.fixpos.unique())*len(data.entropy.unique()))

    #Grand mean
    grand_mean = data['correct'].mean()

    #Sum of squares (SS)
    ssq_a = sum([(data[data.entropy == l].correct.mean()-grand_mean)**2 for l in data.entropy])
    ssq_b = sum([(data[data.fixpos == l].correct.mean()-grand_mean)**2 for l in data.fixpos])
    ssq_t = sum((data.correct - grand_mean)**2)
    ##residual (sum of squares within):
    neg = data[data.entropy == 'extreme_neg']
    pos = data[data.entropy == 'extreme_pos']
    neg_fixpos_means = [neg[neg.fixpos == d].correct.mean() for d in neg.fixpos]
    pos_fixpos_means = [pos[pos.fixpos == d].correct.mean() for d in pos.fixpos]
    ssq_w = sum((pos.correct - pos_fixpos_means)**2) +sum((neg.correct - neg_fixpos_means)**2)

    ##SS interaction
    ssq_axb = ssq_t-ssq_a-ssq_b-ssq_w

    #Factor means
    ms_a = ssq_a/df_a
    ms_b = ssq_b/df_b
    ms_axb = ssq_axb/df_axb
    ms_w = ssq_w/df_w

    #F stat
    f_a = ms_a/ms_w
    f_b = ms_b/ms_w
    f_axb = ms_axb/ms_w

    #P-values
    p_a = stats.f.sf(f_a, df_a, df_w)
    p_b = stats.f.sf(f_b, df_b, df_w)
    p_axb = stats.f.sf(f_axb, df_axb, df_w)

    #for the moment, return p-values
    return p_a, p_b, p_axb

reason: from scipy,import statsmodels,from statsmodels


text:
import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import numpy as np
import torch.utils.data as data
import os
from PIL import Image

def get_dataset(name, data_dir, sourcefile='', size=64, lsun_categories=None):
    transform = transforms.Compose([
        transforms.Resize(size),
        transforms.CenterCrop(size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
        transforms.Lambda(lambda x: x + 1./128 * torch.rand(x.size())),
    ])

    if name == 'image':
        dataset = datasets.ImageFolder(data_dir, transform)
        nlabels = len(dataset.classes)
    elif name == 'imagelist':
        dataset = ImageList(data_dir, sourcefile, transform)
        nlabels = len(dataset.classes)
    elif name == 'npy':
        # Only support normalization for now
        dataset = datasets.DatasetFolder(data_dir, npy_loader, ['npy'])
        nlabels = len(dataset.classes)
    elif name == 'cifar10':
        dataset = datasets.CIFAR10(root=data_dir, train=True, download=True,
                                   transform=transform)
        nlabels = 10
    elif name == 'lsun':
        if lsun_categories is None:
            lsun_categories = 'train'
        dataset = datasets.LSUN(data_dir, lsun_categories, transform)
        nlabels = len(dataset.classes)
    elif name == 'lsun_class':
        dataset = datasets.LSUNClass(data_dir, transform,
                                     target_transform=(lambda t: 0))
        nlabels = 1
    else:
        raise NotImplemented

    return dataset, nlabels

def npy_loader(path):
    img = np.load(path)

    if img.dtype == np.uint8:
        img = img.astype(np.float32)
        img = img/127.5 - 1.
    elif img.dtype == np.float32:
        img = img * 2 - 1.
    else:
        raise NotImplementedError

    img = torch.Tensor(img)
    if len(img.size()) == 4:
        img.squeeze_(0)

    return img

class ImageList(data.Dataset):
    def __init__(self, root, sourcefile, transform=None, target_transform=None):
        self.root = os.path.expanduser(root)
        with open(sourcefile, 'r') as f:
            images = []
            labels = []
            for line in f.readlines():
                line = line.rstrip().split()
                images.append(os.path.join(root, line[0]))
                labels.append(int(line[1]))
        self.images = images
        self.labels = labels
        self.classes = list(set(labels))
        self.transform = transform
        self.target_transform = target_transform

    def __getitem__(self, index):
        img = Image.open(self.images[index]).convert('RGB')
        lbl = self.labels[index]

        if self.transform is not None:
            img = self.transform(img)
        if self.target_transform is not None:
            lbl = self.target_transform(lbl)
        return img, lbl

    def __len__(self):
        return len(self.labels)


text:
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import enum
import itertools
import operator

from absl.testing import absltest
from absl.testing import parameterized

import numpy as np

import jax
from jax import dtypes
from jax import numpy as jnp
from jax import test_util as jtu
from jax.interpreters import xla

from jax.config import config
config.parse_flags_with_absl()
FLAGS = config.FLAGS

bool_dtypes = [np.dtype('bool')]

signed_dtypes = [np.dtype('int8'), np.dtype('int16'), np.dtype('int32'),
                 np.dtype('int64')]

unsigned_dtypes = [np.dtype('uint8'), np.dtype('uint16'), np.dtype('uint32'),
                   np.dtype('uint64')]

np_float_dtypes = [np.dtype('float16'), np.dtype('float32'),
                   np.dtype('float64')]

float_dtypes = [np.dtype(dtypes.bfloat16)] + np_float_dtypes

complex_dtypes = [np.dtype('complex64'), np.dtype('complex128')]

all_dtypes = (bool_dtypes + signed_dtypes + unsigned_dtypes + float_dtypes +
              complex_dtypes)

scalar_types = [jnp.bool_, jnp.int8, jnp.int16, jnp.int32, jnp.int64,
                jnp.uint8, jnp.uint16, jnp.uint32, jnp.uint64,
                jnp.bfloat16, jnp.float16, jnp.float32, jnp.float64,
                jnp.complex64, jnp.complex128]

def identity(x):
  """A named identity function for use in tests"""
  return x

class DtypesTest(jtu.JaxTestCase):

  @parameterized.named_parameters(
    {"testcase_name": "_type={}".format(type.__name__), "type": type,
     "dtype": dtype}
    for type, dtype in [(bool, jnp.bool_), (int, jnp.int_), (float, jnp.float_),
                        (complex, jnp.complex_)])
  def testDefaultTypes(self, type, dtype):
    for f in [jnp.array, jax.jit(jnp.array), jax.jit(lambda x: x)]:
      y = f(type(0))
      self.assertTrue(isinstance(y, jnp.ndarray), msg=(f, y))
      self.assertEqual(y.dtype, dtypes.canonicalize_dtype(dtype), msg=(f, y))

  def testUnsupportedType(self):
    with self.assertRaisesRegex(TypeError, "nonsense.* not understood"):
      dtypes.canonicalize_dtype("nonsense")

  @parameterized.named_parameters(
    {"testcase_name": "_swap={}_jit={}".format(swap, jit),
     "swap": swap, "jit": jit}
    for swap in [False, True] for jit in [False, True])
  @jtu.ignore_warning(category=UserWarning,
                      message="Explicitly requested dtype.*")
  def testBinaryPromotion(self, swap, jit):
    testcases = [
      (jnp.array(1.), 0., jnp.float_),
      (jnp.array(1.), jnp.array(0.), jnp.float_),
      (jnp.array(1.), jnp.array(0., dtype=jnp.float16), jnp.float16),
      (jnp.array(1.), jnp.array(0., dtype=jnp.float32), jnp.float32),
      (jnp.array(1.), jnp.array(0., dtype=jnp.float64), jnp.float64),
      (jnp.array(1., dtype=jnp.float16), 0., jnp.float16),
      (jnp.array(1., dtype=jnp.float32), 0., jnp.float32),
      (jnp.array(1., dtype=jnp.float64), 0., jnp.float64),
      (jnp.array(1., dtype=jnp.float16), jnp.array(0., dtype=jnp.float16), jnp.float16),
      (jnp.array(1., dtype=jnp.float16), jnp.array(0., dtype=jnp.float32), jnp.float32),
      (jnp.array(1., dtype=jnp.float16), jnp.array(0., dtype=jnp.float64), jnp.float64),
      (jnp.array(1., dtype=jnp.float32), jnp.array(0., dtype=jnp.float32), jnp.float32),
      (jnp.array(1., dtype=jnp.float32), jnp.array(0., dtype=jnp.float64), jnp.float64),
      (jnp.array(1., dtype=jnp.float64), jnp.array(0., dtype=jnp.float64), jnp.float64),
      (jnp.array([1.]), 0., jnp.float_),
      (jnp.array([1.]), jnp.array(0.), jnp.float_),
      (jnp.array([1.]), jnp.array(0., dtype=jnp.float16), jnp.float_),
      (jnp.array([1.]), jnp.array(0., dtype=jnp.float32), jnp.float_),
      (jnp.array([1.]), jnp.array(0., dtype=jnp.float64), jnp.float64),
      (jnp.array([1.], dtype=jnp.float32), jnp.array(0., dtype=jnp.float16), jnp.float32),
      (jnp.array([1.], dtype=jnp.float16), jnp.array(0., dtype=jnp.float32), jnp.float32),
      (jnp.array([1.], dtype=jnp.float16), 0., jnp.float16),
    ]
    op = jax.jit(operator.add) if jit else operator.add
    for x, y, dtype in testcases:
      x, y = (y, x) if swap else (x, y)
      z = op(x, y)
      self.assertTrue(isinstance(z, jnp.ndarray), msg=(x, y, z))
      self.assertEqual(z.dtype, dtypes.canonicalize_dtype(dtype), msg=(x, y, z))

  def testPromoteDtypes(self):
    for t1 in all_dtypes:
      self.assertEqual(t1, dtypes.promote_types(t1, t1))

      self.assertEqual(t1, dtypes.promote_types(t1, np.bool_))
      self.assertEqual(np.dtype(np.complex128),
                       dtypes.promote_types(t1, np.complex128))

      for t2 in all_dtypes:
        # Symmetry
        self.assertEqual(dtypes.promote_types(t1, t2),
                         dtypes.promote_types(t2, t1))

    self.assertEqual(np.dtype(np.float32),
                     dtypes.promote_types(np.float16, dtypes.bfloat16))

    # Promotions of non-inexact types against inexact types always prefer
    # the inexact types.
    for t in float_dtypes + complex_dtypes:
      for i in bool_dtypes + signed_dtypes + unsigned_dtypes:
        self.assertEqual(t, dtypes.promote_types(t, i))

    # Promotions between exact types, or between inexact types, match NumPy.
    for groups in [bool_dtypes + signed_dtypes + unsigned_dtypes,
                   np_float_dtypes + complex_dtypes]:
      for t1, t2 in itertools.combinations(groups, 2):
          self.assertEqual(np.promote_types(t1, t2),
                           dtypes.promote_types(t1, t2))

  def testScalarInstantiation(self):
    for t in [jnp.bool_, jnp.int32, jnp.bfloat16, jnp.float32, jnp.complex64]:
      a = t(1)
      self.assertEqual(a.dtype, jnp.dtype(t))
      self.assertIsInstance(a, xla.DeviceArray)
      self.assertEqual(0, jnp.ndim(a))

  def testIsSubdtype(self):
    for t in scalar_types:
      self.assertTrue(dtypes.issubdtype(t, t))
      self.assertTrue(dtypes.issubdtype(np.dtype(t).type, t))
      self.assertTrue(dtypes.issubdtype(t, np.dtype(t).type))
      self.assertTrue(dtypes.issubdtype(t, np.dtype(t)))
      if t != jnp.bfloat16:
        for category in [np.generic, jnp.inexact, jnp.integer, jnp.signedinteger,
                         jnp.unsignedinteger, jnp.floating, jnp.complexfloating]:
          self.assertEqual(dtypes.issubdtype(t, category),
                           np.issubdtype(np.dtype(t).type, category))
          self.assertEqual(dtypes.issubdtype(t, category),
                           np.issubdtype(np.dtype(t).type, category))

  def testArrayCasts(self):
    for t in [jnp.bool_, jnp.int32, jnp.bfloat16, jnp.float32, jnp.complex64]:
      a = np.array([1, 2.5, -3.7])
      self.assertEqual(a.astype(t).dtype, jnp.dtype(t))
      self.assertEqual(jnp.array(a).astype(t).dtype, jnp.dtype(t))

  def testEnumPromotion(self):
    class AnEnum(enum.IntEnum):
      A = 42
      B = 101
    np.testing.assert_equal(np.array(42), np.array(AnEnum.A))
    np.testing.assert_equal(jnp.array(42), jnp.array(AnEnum.A))
    np.testing.assert_equal(np.int32(101), np.int32(AnEnum.B))
    np.testing.assert_equal(jnp.int32(101), jnp.int32(AnEnum.B))

  def testScalarCastInsideJitWorks(self):
    # jnp.int32(tracer) should work.
    self.assertEqual(jnp.int32(101),
                     jax.jit(lambda x: jnp.int32(x))(jnp.float32(101.4)))

class TestPromotionTables(jtu.JaxTestCase):

  @parameterized.named_parameters(
    {"testcase_name": "_jaxtype={}".format(jaxtype),
     "jaxtype": jaxtype}
     for jaxtype in dtypes._jax_types)
  def testJaxTypeFromType(self, jaxtype):
    self.assertIs(dtypes._jax_type(jaxtype), jaxtype)

  @parameterized.named_parameters(
    {"testcase_name": "_jaxtype={}".format(jaxtype),
     "jaxtype": jaxtype}
     for jaxtype in dtypes._jax_types)
  def testJaxTypeFromVal(self, jaxtype):
    try:
      val = jaxtype(0)
    except TypeError:
      val = jaxtype.type(0)
    self.assertIs(dtypes._jax_type(val), jaxtype)

  @jtu.ignore_warning(category=UserWarning,
                      message="Explicitly requested dtype.*")
  def testObservedPromotionTable(self):
    """Test that the weak & strong dtype promotion table does not change over time."""
    # Note: * here refers to weakly-typed values
    typecodes = \
        ['b1','u1','u2','u4','u8','i1','i2','i4','i8','bf','f2','f4','f8','c4','c8','i*','f*','c*']
    if FLAGS.jax_enable_x64:
      expected = [
        ['b1','u1','u2','u4','u8','i1','i2','i4','i8','bf','f2','f4','f8','c4','c8','i*','f*','c*'],
        ['u1','u1','u2','u4','u8','i2','i2','i4','i8','bf','f2','f4','f8','c4','c8','u1','f*','c*'],
        ['u2','u2','u2','u4','u8','i4','i4','i4','i8','bf','f2','f4','f8','c4','c8','u2','f*','c*'],
        ['u4','u4','u4','u4','u8','i8','i8','i8','i8','bf','f2','f4','f8','c4','c8','u4','f*','c*'],
        ['u8','u8','u8','u8','u8','f*','f*','f*','f*','bf','f2','f4','f8','c4','c8','u8','f*','c*'],
        ['i1','i2','i4','i8','f*','i1','i2','i4','i8','bf','f2','f4','f8','c4','c8','i1','f*','c*'],
        ['i2','i2','i4','i8','f*','i2','i2','i4','i8','bf','f2','f4','f8','c4','c8','i2','f*','c*'],
        ['i4','i4','i4','i8','f*','i4','i4','i4','i8','bf','f2','f4','f8','c4','c8','i4','f*','c*'],
        ['i8','i8','i8','i8','f*','i8','i8','i8','i8','bf','f2','f4','f8','c4','c8','i8','f*','c*'],
        ['bf','bf','bf','bf','bf','bf','bf','bf','bf','bf','f4','f4','f8','c4','c8','bf','bf','c4'],
        ['f2','f2','f2','f2','f2','f2','f2','f2','f2','f4','f2','f4','f8','c4','c8','f2','f2','c4'],
        ['f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','f8','c4','c8','f4','f4','c4'],
        ['f8','f8','f8','f8','f8','f8','f8','f8','f8','f8','f8','f8','f8','c8','c8','f8','f8','c8'],
        ['c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c8','c4','c8','c4','c4','c4'],
        ['c8','c8','c8','c8','c8','c8','c8','c8','c8','c8','c8','c8','c8','c8','c8','c8','c8','c8'],
        ['i*','u1','u2','u4','u8','i1','i2','i4','i8','bf','f2','f4','f8','c4','c8','i*','f*','c*'],
        ['f*','f*','f*','f*','f*','f*','f*','f*','f*','bf','f2','f4','f8','c4','c8','f*','f*','c*'],
        ['c*','c*','c*','c*','c*','c*','c*','c*','c*','c4','c4','c4','c8','c4','c8','c*','c*','c*'],
      ]
    else:
      expected = [
        ['b1','u1','u2','u4','u4','i1','i2','i4','i4','bf','f2','f4','f4','c4','c4','i*','f*','c*'],
        ['u1','u1','u2','u4','u4','i2','i2','i4','i4','bf','f2','f4','f4','c4','c4','u1','f*','c*'],
        ['u2','u2','u2','u4','u4','i4','i4','i4','i4','bf','f2','f4','f4','c4','c4','u2','f*','c*'],
        ['u4','u4','u4','u4','u4','i4','i4','i4','i4','bf','f2','f4','f4','c4','c4','u4','f*','c*'],
        ['u4','u4','u4','u4','u4','i4','i4','i4','i4','bf','f2','f4','f4','c4','c4','u4','f*','c*'],
        ['i1','i2','i4','i4','i4','i1','i2','i4','i4','bf','f2','f4','f4','c4','c4','i1','f*','c*'],
        ['i2','i2','i4','i4','i4','i2','i2','i4','i4','bf','f2','f4','f4','c4','c4','i2','f*','c*'],
        ['i4','i4','i4','i4','i4','i4','i4','i4','i4','bf','f2','f4','f4','c4','c4','i4','f*','c*'],
        ['i4','i4','i4','i4','i4','i4','i4','i4','i4','bf','f2','f4','f4','c4','c4','i4','f*','c*'],
        ['bf','bf','bf','bf','bf','bf','bf','bf','bf','bf','f4','f4','f4','c4','c4','bf','bf','c4'],
        ['f2','f2','f2','f2','f2','f2','f2','f2','f2','f4','f2','f4','f4','c4','c4','f2','f2','c4'],
        ['f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','c4','c4','f4','f4','c4'],
        ['f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','f4','c4','c4','f4','f4','c4'],
        ['c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4'],
        ['c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4','c4'],
        ['i*','u1','u2','u4','u4','i1','i2','i4','i4','bf','f2','f4','f4','c4','c4','i*','f*','c*'],
        ['f*','f*','f*','f*','f*','f*','f*','f*','f*','bf','f2','f4','f4','c4','c4','f*','f*','c*'],
        ['c*','c*','c*','c*','c*','c*','c*','c*','c*','c4','c4','c4','c4','c4','c4','c*','c*','c*'],
      ]
    typecode_to_dtype = {
      'b1': jnp.bool_,
      'u1': jnp.uint8, 'u2': jnp.uint16, 'u4': jnp.uint32, 'u8': jnp.uint64,
      'i1': jnp.int8, 'i2': jnp.int16, 'i4': jnp.int32, 'i8': jnp.int64,
      'bf': jnp.bfloat16, 'f2': jnp.float16, 'f4': jnp.float32, 'f8': jnp.float64,
      'c4': jnp.complex64, 'c8': jnp.complex128,
      'i*': jnp.int64, 'f*': jnp.float64, 'c*': jnp.complex128,
    }
    dtype_to_typecode = {jnp.dtype(v): k for k, v in typecode_to_dtype.items()
                        if not k.endswith('*')}

    def typecode_to_val(typecode):
      weak_type = typecode.endswith('*')
      dtype = typecode_to_dtype[typecode]
      val = dtype(0)
      if weak_type:
        val = val.item()
      return val

    def val_to_typecode(val):
      dtype = dtypes.result_type(val)
      weak_type = dtypes.is_weakly_typed(val)
      typecode = dtype_to_typecode[dtype]
      if weak_type:
        typecode = typecode[:-1] + '*'
      return typecode

    vals = [typecode_to_val(t) for t in typecodes]
    table = [[val_to_typecode(v1 + v2) for v1 in vals] for v2 in vals]

    def show_differences(epected, actual):
      diffs = ""
      for i, t1 in enumerate(typecodes):
        for j, t2 in enumerate(typecodes):
          if expected[i][j] != actual[i][j]:
            diffs += f"\n{t1}, {t2} -> want {expected[i][j]}, got {actual[i][j]}"
      return diffs

    self.assertEqual(table, expected, show_differences(expected, table))

  @parameterized.named_parameters(
    {"testcase_name": "_xtype={}_ytype={}_xfun={}_yfun={}".format(
      xtype.__name__, ytype.__name__, xfun.__name__, yfun.__name__),
     "xtype": xtype, "ytype": ytype, "xfun": xfun, "yfun": yfun}
    for xtype, ytype in itertools.product(
      [int, float, jnp.int16, jnp.int32, jnp.float16, jnp.float32], repeat=2)
    for xfun, yfun in itertools.product(
      [identity, abs, jnp.array], repeat=2)
    )
  def testBinaryPromotionJitInvariance(self, xtype, ytype, xfun, yfun):
    """Test jit invariance of simple binary promotion rules with and without weak types."""
    f = lambda x, y: xfun(x) + yfun(y)
    args_maker = lambda: [xtype(1), ytype(1)]
    self._CompileAndCheck(f, args_maker, check_dtypes=True)

if __name__ == "__main__":
  absltest.main(testLoader=jtu.JaxTestLoader())

max_stars_repo_name: bileschi/jax

max_issues_repo_name: bileschi/jax

max_forks_repo_name: bileschi/jax

reason: import numpy,import jax,from jax


text:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--model", "-m", type=str, default='model.h5', help="the model to use in evaluation")
args = parser.parse_args()

import os
from keras.models import load_model
from util import data_generator
from data import CharVocab, Corpus
from sklearn.metrics import classification_report, confusion_matrix, precision_score, recall_score
import numpy as np

model = load_model(os.path.join('weight', args.model))

# load corpus and vocab
vocab = CharVocab(100000) # 100k
# corpus = Corpus(vocab, debug=True)
output_punc = {0:vocab.decode(0), 1:vocab.decode(1), 2:vocab.decode(2)}

vocab2punc = {'<comma>': '、', '<period>': '。'}
punc_dict = set(['、', '。', '「', '」', '・', '）', '（', '，', '？', '！', '…', '〜', '．', '‐', '『', '』', '―', '：', '“', '”'])

original = '周囲の疑惑の目にさらされながらも、堂々と愛人と同棲し、高級外車を乗りまわしたりと、異様に神経の図太い男だったのだが…'
input = [x for x in original if x not in punc_dict]
encoded_input = [vocab.encode(x) for x in input]

pred = model.predict(np.array(encoded_input).reshape((1,-1)))
pred_y = np.argmax((pred), axis=2)

decoded = []
result = list(pred_y.reshape(-1))
for i in range(len(encoded_input)):
    decoded.append(vocab.decode(encoded_input[i]))
    if vocab.is_punctuation(result[i]):
        decoded.append(vocab2punc[vocab.decode(result[i])])
    elif pred[0][i][1] > 0.1:
        decoded.append('<c %.2f>' % pred[0][i][1])
    elif pred[0][i][2] > 0.1:
        decoded.append('<p %.2f>' % pred[0][i][2])

print(original)
print(''.join(decoded))

max_stars_repo_name: jiali-ms/punctuator

max_issues_repo_name: jiali-ms/punctuator

max_forks_repo_name: jiali-ms/punctuator