text:
import numpy as np
from matplotlib import cm
import networkx as nx
import copy
from Influence.sdnet.utils import make_dist_matrix, euclidean_dist
from sdnet import SDA

def make_graph(A, C, belief_name, media_locs=[]):
    G = nx.from_numpy_array(A, create_using=nx.DiGraph)
    for i in range(len(A)):
        G.nodes[i][belief_name] = C[i]
        G.add_weighted_edges_from([(i, i, 1)])
    if media_locs:
        # print("media_locs", media_locs)
        for idx in media_locs:
            media_name = list(G.nodes.keys())[idx]
            G.nodes[media_name]['color'] = 'magenta'
            neighbors = copy.deepcopy(G.adj[media_name])
            for i in neighbors:
                G.remove_edge(media_name, i)
    return G

def viz_space(ax, X, labels=None, edgecolors='#000000', linewidths=.6, **kwds):
    x, y = X[:, 0], X[:, 1]
    if labels is not None:
        labels = [cm.Set1(x) for x in labels]
    ax.scatter(x, y, c=labels, edgecolors=edgecolors, linewidths=linewidths,
               **kwds)
    ax.set_axisbelow(True)
    ax.grid(zorder=0, linestyle='--')
    ax.get_xaxis().set_ticks([])
    ax.get_yaxis().set_ticks([])
    return ax

def viz_degseq(ax, degseq):
    ax.hist(degseq, color=cm.Set1(1), edgecolor='#000000', linewidth=1)
    ax.get_xaxis().set_ticks([])
    ax.get_yaxis().set_ticks([])
    return ax

def viz_sda_graph(ax, G, pos=None, with_labels=False, node_color=None,
                  node_border_color='#000000', node_border_width=.6,
                  edge_color='#000000', nodes_kws=None, edges_kws=None,
                  size_scaler=lambda x: np.sqrt(x)*2):
    if nodes_kws is None:
        nodes_kws = {}
    if edges_kws is None:
        edges_kws = {}
    if node_color is None:
        node_color = np.array([n['color'] for n in G.nodes.values()])
    if pos is None:
        pos = nx.drawing.kamada_kawai_layout(G)
    nodes = \
        nx.draw_networkx_nodes(G, pos, node_color=node_color,
                               with_labels=with_labels, ax=ax, node_size=size_scaler, **nodes_kws)
    nodes.set_edgecolor(node_border_color)
    nodes.set_linewidth(node_border_width)
    # nx.draw_networkx_labels(G, pos, ax=ax)
    nx.draw_networkx_edges(G, pos, edge_color=edge_color, alpha=.2,
                           ax=ax, **edges_kws)
    return ax

def plot_results(ax, results, node_names, show_legend=False,
                 remove_dupes=False, colors=None, idx=0):
    """
    """
    for i in range(len(results)):
        if remove_dupes:
            if not results[i, -1] == results[i, 0]:
                ax.plot(results[i], label=node_names[i],
                        color=colors[node_names[i]])
        else:
            ax.plot(results[i], label=node_names[i])
    if show_legend:
        ax.legend(bbox_to_anchor=(1.01, 1), loc='upper left')

# def insert_node(beliefs, b):
#     """
#         Appends a b vector to a belief matrix.
#     """
#     beliefs = np.append(beliefs, [b], axis=0)
#     return beliefs, len(beliefs) - 1
#
#
# def generate_network(N, NDIM, clusters, degseq, alpha, K,  belief_name,
#                      media_beliefs=None, media_changes=None):
#     """
#         Returns a network generated using the Social Distance Attachment model
#         and inserts influencer node(s) if media beliefs are given.
#     """
#     media_locs = None
#     if isinstance(media_beliefs, np.ndarray):
#         clusters = np.append(clusters, media_beliefs, axis=0)
#         media_locs = list(range(len(clusters)-len(media_beliefs),
#                                 len(clusters)))
#         N += len(media_beliefs)
#     D = make_dist_matrix(clusters, euclidean_dist,
#                          symmetric=True).astype(np.float32)
#     sda = SDA.from_dist_matrix(D, alpha=alpha, k=K, directed=False)
#     A = sda.adjacency_matrix(sparse=False)
#     G = make_graph(A, clusters, belief_name, media_locs=media_locs)
#     return G, media_locs

reason: import numpy,import networkx


text:
import numpy as np

from .get_string_similarity import get_string_similarity

def get_sentence_distance(words1, words2, first_char_weight=0, case_sensitivity=1, method='jaro_winkler'):
	"""
	:type words1: list[str] or str
	:type words2: list[str] or str
	:type first_char_weight: float
	:type case_sensitivity: float
	:type method: str
	:rtype: float
	"""
	if isinstance(words1, str):
		words1 = words1.split()
	elif not isinstance(words1, list):
		raise TypeError('words1 should either be a string or a list!')

	if isinstance(words2, str):
		words2 = words2.split()
	elif not isinstance(words2, list):
		raise TypeError('words2 should either be a string or a list')

	size_x = len(words1) + 1
	size_y = len(words2) + 1
	m = np.zeros((size_x, size_y))
	# create_matrix(h=size_x, w=size_y, obj=0)

	for i in range(size_x): m[i, 0] = i
	for j in range(size_y): m[0, j] = j
	for i in range(1, size_x):
		for j in range(1, size_y):
			w1 = words1[i - 1]
			w2 = words2[j - 1]
			normalized_distance = 1 - get_string_similarity(
				s1=w1, s2=w2, method=method,
				first_char_weight=first_char_weight,
				case_sensitivity=case_sensitivity
			)
			m[i][j] = min(
				m[i - 1, j - 1] + normalized_distance,
				m[i - 1, j] + 1,
				m[i, j - 1] + 1
			)

	return m[size_x - 1, size_y - 1]

def get_sentence_similarity(
		words1, words2, first_char_weight=0, first_word_weight=0, case_sensitivity=1, method='jaro_winkler'
):
	"""
	:type words1: list[str] or str
	:type words2: list[str] or str
	:type first_char_weight: float
	:type first_word_weight: float
	:type case_sensitivity: float
	:type method: str
	:rtype: float
	"""
	if isinstance(words1, str):
		words1 = words1.split()
	elif not isinstance(words1, list):
		raise TypeError('words1 should either be a string or a list!')

	if isinstance(words2, str):
		words2 = words2.split()
	elif not isinstance(words2, list):
		raise TypeError('words2 should either be a string or a list')

	if len(words1)==0 and len(words2)==0:
		return 1.0
	elif len(words1)==0 or len(words2)==0:
		return 0.0

	distance = get_sentence_distance(
		words1=words1, words2=words2, method=method,
		case_sensitivity=case_sensitivity, first_char_weight=first_char_weight
	)

	similarity = 1 - distance/max(len(words1), len(words2))

	if first_word_weight==0:
		return similarity
	elif first_word_weight<0:
		raise ValueError('first_word_weight cannot be negative!')
	else:
		first_word_similarity = get_string_similarity(
			s1=words1[0], s2=words2[0], method=method,
			case_sensitivity=case_sensitivity, first_char_weight=first_char_weight
		)
		return (similarity+first_word_similarity*first_word_weight)/(1+first_word_weight)

ext: py

max_stars_repo_name: idin/mercurius

max_issues_repo_name: idin/mercurius

max_forks_repo_name: idin/mercurius

reason: import numpy


text:
# -*- coding: utf-8 -*-

"""
Created on Tue Jun 12 2018
Last edit: Tue Jun 12 2018
@author: kfinc

"""

import numpy as np
import pandas as pd

def calculate_lsn_edges (A, labels):
    """Function calculates number of edges between and within predefined large-scale networks (LSNs).
    The function takes binary symetrical adjacency matrix, module assignment of each ROI and calculate number of edges between and within each
    large-scale network.

    Parameters
    ------------
    array: N x N binary ajdacency matrix
    array: N-length vector with module assignment for each node

    Returns
    ------------
    array: M x M matrix with number of edges between each module

    """
    columns = np.unique(labels)
    lsn_matrix = np.zeros((len(labels), len(columns)))
    lsn_edges = np.zeros((len(columns), len(columns)))

    for col in range(len(columns)):
        module = columns[col, ]
        for row in range(len(labels)):
            if (labels[row, ] == module):
                lsn_matrix[row, col] = 1
            else:
                lsn_matrix[row, col] = 0

    lsn_edges = lsn_matrix.T @ A @ lsn_matrix
    return lsn_edges

def allegiance_matrix(M):
    """Calculates n x n allegiance matrix P from n x t matrix M, where n represents nodes, and t represents time windows.
    Each value on allegiance matrix P represents the probability that node i and node j have been assigned to the same
    community (Bassett et al., 2014; Mattar et al., 2015).

    Parameters
    ------------
    array: n x t module assignment matrix t

    Returns
    ------------
    array: n x n allegiance matrix P """

    n_nodes = M.shape[0]
    n_slices = M.shape[1]
    T = np.zeros((n_nodes, n_nodes))

    for i in range(n_nodes):
        for j in range(i):
            if i == j:
                continue
            else:
                t = np.sum(M[i, :] == M[j, :])
                T[i, j] = t

    P = (T + T.T)/n_slices
    np.fill_diagonal(P, 1)

    return P

def allegiance_matrices_4d(M):
    """Calculates 4D array composed of allegiance matrices for each subject and each condition/session.

    Parameters
    ------------
    array: 4D array S(subjects) x C(condition/session) x N(node) x M(window)

    Returns
    ------------
    array: 4D array S(subjects) x C(condition/session) x N(node) x N(node), where N x N is allegiance matrix

    """
    n_roi = len(M[0,0,:,0])
    n_sub = len(M[:,0,0,0])
    n_ses = len(M[0,:,0,0])
    AM = np.zeros((n_sub, n_ses, n_roi, n_roi))

    for sub in range(n_sub):
        for ses in range(n_ses):
            AM[sub, ses, :, :] = allegiance_matrix(M[sub, ses, :, :])
    return AM

def allegiance_matrix_opti(M):
    """Calculates n x n allegiance matrix P from o x n x t matrix M, where o represents module community detection
    optimizations, n represents nodes, and t represents time windows. Each value on allegiance matrix PO represents the
    probability that node i and node j have been assigned to the same
    community (Bassett et al., 2014; Mattar et al., 2015).

    Parameters
    ------------
    array: o x n x t module assignment matrix t

    Returns
    ------------
    array: n x n allegiance matrix P (mean across all optimizations)"""

    n_optimizations = M.shape[0]
    n_nodes = M.shape[1]
    P = np.zeros((n_optimizations, n_nodes, n_nodes))
    PO = np.zeros((n_nodes, n_nodes))

    for i in range(n_optimizations):
        P[i, :, :] = allegiance_matrix(M[i, :, :])
        PO = P.mean(axis = 0)
    return PO

def allegiance_matrix_opti_5d(M):

    n_subs = M.shape[0]
    n_sess = M.shape[1]
    n_optimizations = M.shape[2]
    n_nodes = M.shape[3]

    P = np.zeros((n_subs, n_sess, n_optimizations, n_nodes, n_nodes))

    for i in range(n_subs):
        for j in range(n_sess):
            P[i, j, :, :, :] = allegiance_matrix_opti_3d(M[i, j, :, :, :])
    return P

def sort_matrices_4d(M, idx):
    """Sorts matrices according to predefinded index.

    Parameters
    ------------
    array: 4D array S(subjects) x C(condition/session) x N(node) x N(node) (unsorted)
    array: N-length vector with index to sort matrix

    Returns
    ------------
    array: 4D array S(subjects) x C(condition/session) x N(node) x N(node) (sorted)"""
    M1 = M[:,:,:,idx]
    M2 = M1[:,:,idx,:]
    return M2

def dumming(labels):
    """Gennerate vectors with dummy variables from single vector with categorical variable

    Parameters
    ------------
    array: np.array, N-length vector with categorical variable

    Returns
    ------------
    array: 3D array with M rows and N columns with binary variables"""

    columns = np.unique(labels)
    dummies = np.zeros((len(labels), len(columns)))

    for col in range(len(columns)):
        module = columns[col]
        for row in range(len(labels)):
            if (labels[row] == module):
                dummies[row, col] = 1
            else:
                dummies[row, col] = 0
    return dummies

def dumming_pd(labels):
    """Gennerate vectors with dummy variables from single vector with categorical variable

    Parameters
    ------------
    array: np.array, N-length vector with categorical variable

    Returns
    ------------
    array: pd.DataFrame with M rows and N columns with binary variables

    """
    columns = np.unique(labels)
    return pd.DataFrame(dumming(labels), columns=columns)

def upper_tri_masking(A):
    """Getting values of upper triangle of matrix without diagonal"""
    m = A.shape[0]
    r = np.arange(m)
    mask = r[:,None] < r
    return A[mask]

def fc_cartography(M, modules):
    """Function which calculates mean integration and recruitment values from sorted allegiance matrices"""
    dummy_networks = dumming(sorted(modules))
    roi_n = len(dummy_networks)
    net = np.size(dummy_networks,1)
    diagnostics = np.zeros((net, net))

    for i in range(net):
        for j in range(net):
            vec1 = dummy_networks[:,i].astype('bool')
            vec2 = dummy_networks[:,j].astype('bool')

            L = M[vec1,:]
            P = L[:,vec2]

            #if i == j:
             #   m = upper_tri_masking(P).mean()
            #else:
            m = P.mean()

            diagnostics[i, j] = m
    return diagnostics

'''def fc_cartography(M, modules):
    """Function which calculates mean integration and recruitment values from sorted allegiance matrices"""
    dummy_networks = dumming(sorted(modules))
    n_nod = len(dummy_networks)
    n_net = np.size(dummy_networks,1)
    diagnostics = np.zeros((n_net, n_net))

    for i in range(n_net):
        for j in range(n_net):
            vec1 = dummy_networks[:,i].astype('bool')
            vec2 = dummy_networks[:,j].astype('bool')

            L = M[vec1,:]
            P = L[:,vec2]

            #if i == j:
            #    m = upper_tri_masking(P).mean()
            #else:
            m = P.mean()

            diagnostics[i, j] = m
    return diagnostics
    '''

def fc_cartography_4d(M, modules):
    """Function which calculates mean integration and recruitment values from sorted allegiance matrices. 4D version"""
    sub_n = len(M[:,0,0,0])
    ses_n = len(M[0,:,0,0])
    mod_n = len(np.unique(modules))

    fc_cart = np.zeros((sub_n, ses_n, mod_n, mod_n))

    for i in range(sub_n):
        for j in range(ses_n):
            X = M[i,j,:,:]
            fc_cart[i, j, :, :] = fc_cartography(X, modules)
    return fc_cart

def net_filter(M, modules, net_list):
    modules = np.sort(np.asarray(modules))
    net_filt = [True if elem in net_list else False for elem in modules]
    M1 = M[net_filt, :]
    M2 = M1[:, net_filt]
    return M2

def net_filter_4d(M, modules, net_list):
    modules = np.sort(np.asarray(modules))
    net_filt = [True if elem in net_list else False for elem in modules]
    M1 = M[:, :, net_filt, :]
    M2 = M1[:, :, :, net_filt]
    return M2

def single_window_allegiance(t):
    n_nod = len(t)
    T = np.zeros((n_nod, n_nod))

    for i, row in enumerate(t):
        for j, col in enumerate(t):
            if row == col:
                T[i,j] = 1
            else:
                continue
    return T

def single_window_allegiance_mean(M):
    n_opt = M.shape[0]
    n_nod = M.shape[1]
    T = np.zeros((n_opt, n_nod, n_nod))

    for i in range(n_opt):
        T[i, :, :] = single_window_allegiance(M[i, :])

    return T.mean(axis=0)

def all_window_allegiance_mean(M):

    n_win = M.shape[2]
    n_nod = M.shape[1]

    T = np.zeros((n_win, n_nod, n_nod))

    for i in range(n_win):
        T[i, :, :] =  single_window_allegiance_mean(M[:,:,i])

    return T

def node_flexibility(node_vector):
    n_win = len(node_vector)
    count = 0
    for i in range(n_win-1):
        if node_vector[i] != node_vector[i+1]:
            count += 1
    return count/n_win

def flexibility(matrix):
    n_nod = matrix.shape[0]
    n_win = matrix.shape[1]

    node_flex = [node_flexibility(elem) for elem in matrix]
    return node_flex

def randmio_und(R, itr):
    '''
    Edited from aestrivex/bctpy with corrected matrix symetry check

    This function randomizes an undirected network, while preserving the
    degree distribution. The function does not preserve the strength
    distribution in weighted networks.
    Parameters
    ----------
    W : NxN np.ndarray
        undirected binary/weighted connection matrix
    itr : int
        rewiring parameter. Each edge is rewired approximately itr times.
    Returns
    -------
    R : NxN np.ndarray
        randomized network
    eff : int
        number of actual rewirings carried out
    '''
    if not np.allclose(R, R.T):
        raise BCTParamError("Input must be undirected")
    R = R.copy()
    n = len(R)
    i, j = np.where(np.tril(R))
    k = len(i)
    itr *= k

    # maximum number of rewiring attempts per iteration
    max_attempts = np.round(n * k / (n * (n - 1)))
    # actual number of successful rewirings
    eff = 0

    for it in range(int(itr)):
        att = 0
        while att <= max_attempts:  # while not rewired
            while True:
                e1, e2 = np.random.randint(k, size=(2,))
                while e1 == e2:
                    e2 = np.random.randint(k)
                a = i[e1]
                b = j[e1]
                c = i[e2]
                d = j[e2]

                if a != c and a != d and b != c and b != d:
                    break  # all 4 vertices must be different

            if np.random.random() > .5:
                i.setflags(write=True)
                j.setflags(write=True)
                i[e2] = d
                j[e2] = c  # flip edge c-d with 50% probability
                c = i[e2]
                d = j[e2]  # to explore all potential rewirings

            # rewiring condition
            if not (R[a, d] or R[c, b]):
                R[a, d] = R[a, b]
                R[a, b] = 0
                R[d, a] = R[b, a]
                R[b, a] = 0
                R[c, b] = R[c, d]
                R[c, d] = 0
                R[b, c] = R[d, c]
                R[d, c] = 0

                j.setflags(write=True)
                j[e1] = d
                j[e2] = b  # reassign edge indices
                eff += 1
                break
            att += 1

    return R, eff


text:
"""
XDFStreams
----------
"""
import pyxdf
from typing import List, Union, Any, Dict
from functools import lru_cache
from pathlib import Path
from collections import defaultdict
from numpy import ndarray

def convert_desc(source: defaultdict, target: dict = None):
    if source is None:
        return None
    target = target or dict()
    for key, item in source.items():
        if type(item) == list:
            if len(item) == 1:
                item = item[0]
            else:
                item = [convert_desc(i, dict()) for i in item]
        if type(item) == defaultdict:
            item = convert_desc(item, dict())

        target[key] = item

    return target

class XDFStream:
    """Interface to an XDFstream loaded from an :code:`xdf`-file
    """

    def __init__(self, unparsed_stream: dict):
        self._stream = unparsed_stream
        try:
            self.desc = convert_desc(unparsed_stream["info"]["desc"][0])
        except KeyError:
            self.desc = None

    @property
    @lru_cache(maxsize=1)
    def channel_labels(self) -> Union[List[str], None]:
        "get the channel labels as a list of strings"
        if self.desc is not None:
            try:
                return [i["label"] for i in self.desc["channels"]["channel"]]
            except TypeError:
                return [self.desc["channels"]["channel"]["label"]]
        else:
            return None

    @property
    @lru_cache(maxsize=1)
    def channel_types(self) -> Union[List[str], None]:
        "get the channel types as a list of strings"
        if self.desc is not None:
            return [i["type"] for i in self.desc["channels"]["channel"]]
        else:
            return None

    @property
    @lru_cache(maxsize=1)
    def channel_units(self) -> Union[List[str], None]:
        "get the channel units as a list of strings"
        if self.desc is not None:
            return [i["unit"] for i in self.desc["channels"]["channel"]]
        else:
            return None

    @property
    @lru_cache(maxsize=1)
    def nominal_srate(self) -> float:
        "get the nominal sampling rate as float"
        return float(self._stream["info"]["nominal_srate"][0])

    @property
    @lru_cache(maxsize=1)
    def channel_count(self) -> int:
        "get the channel count as int"
        return int(self._stream["info"]["channel_count"][0])

    @property
    @lru_cache(maxsize=1)
    def name(self) -> str:
        "get the streams name as str"
        return self._stream["info"]["name"][0]

    @property
    @lru_cache(maxsize=1)
    def type(self) -> str:
        "get the streams type as str"
        return self._stream["info"]["type"][0]

    @property
    @lru_cache(maxsize=1)
    def channel_format(self) -> str:
        "get the streams data format as str"
        return self._stream["info"]["channel_format"][0]

    @property
    @lru_cache(maxsize=1)
    def created_at(self):
        "get the time stamp from when the channel was created as float"
        return float(self._stream["info"]["created_at"][0])

    @property
    @lru_cache(maxsize=1)
    def hostname(self):
        "get the hostname of the machine where the streams was created as str"
        try:
            return self._stream["info"]["hostname"][0]
        except IndexError:
            return "NotImplemented"

    @property
    def time_series(self) -> ndarray:
        "get the time_series of the stream, i.e its data as ndarray"
        return self._stream["time_series"]

    @property
    def time_stamps(self) -> ndarray:
        "get the time_stamps for each sample as ndarray"
        return self._stream["time_stamps"]

    def __repr__(self) -> str:
        try:
            return f"XDFStream(name={self.name}, type={self.type}, hostname={self.hostname}, created_at={self.created_at}, duration={self.time_stamps[0]-self.time_stamps[1]}, chan_count={self.channel_count}, )"
        # unless there is only one sample
        except IndexError:  # pragma: no cover
            return f"XDFStream(name={self.name}, type={self.type}, hostname={self.hostname}, created_at={self.created_at}, chan_count={self.channel_count}, )"

# -----------------------------------------------------------------------------
def XDFFile(filename: Union[Path, str], verbose=False) -> Dict[str, XDFStream]:
    """load an :code:`xdf`-file and return a dictionary of its streams

    args
    ----
    filename:
        the name of the xdffile

    returns
    -------
    streams: Dict[str, XDFStream]
        a collection of all :class:`~.XDFStream` s in the file. These can be indexed by their respective name

    """
    filename = Path(filename).expanduser()
    streams, _ = pyxdf.load_xdf(filename=str(filename))
    collection: Dict[str, XDFStream] = dict()
    doublettes: Dict[str, int] = dict()
    for stream in streams:
        if not verbose:
            print("XDFFile: Parsing", stream["info"]["name"][0])
        x = XDFStream(stream)
        x.origin = str(filename)
        doublettes[x.name] = doublettes.get(x.name, 0) + 1
        if doublettes[x.name] == 1:
            collection[x.name] = x
        else:
            collection[x.name + str(doublettes[x.name])] = x

    return collection

max_stars_repo_name: jasmainak/pyliesl

max_issues_repo_name: jasmainak/pyliesl

max_forks_repo_name: jasmainak/pyliesl

reason: from numpy


text:
import numpy as np
import torch
import torchaudio

from utils.common import parse_config

def compute_sad(sig, fs, threshold=0.0001, sad_start_end_sil_length=100, sad_margin_length=50):
    """
        Compute threshold based sound activity
    """
    # Leading/Trailing margin
    sad_start_end_sil_length = int(sad_start_end_sil_length * 1e-3 * fs)
    # Margin around active samples
    sad_margin_length = int(sad_margin_length * 1e-3 * fs)

    sample_activity = np.zeros(sig.shape)
    sample_activity[np.power(sig, 2) > threshold] = 1
    sad = np.zeros(sig.shape)
    for i in range(sample_activity.shape[1]):
        if sample_activity[0, i] == 1:
            sad[0, i - sad_margin_length:i + sad_margin_length] = 1
    sad[0, 0:sad_start_end_sil_length] = 0
    sad[0, -sad_start_end_sil_length:] = 0
    return sad

class FeatureExtractor:
    def __init__(self, config_file):
        args = parse_config(config_file)["default"]
        self.feature_type = args["feature_type"]
        assert self.feature_type in ["MFCC", "MelSpec", "logMelSpec"], (
            "Expected the feature_type to be MFCC / MelSpec / logMelSpec"
        )

        self.sample_rate = int(args["resampling_rate"])
        self.mel_args = {
            "n_fft": int(float(args['window_size']) * 1e-3 * self.sample_rate),
            "n_mels": int(args['n_mels']),
            "f_max": int(args['f_max']),
            "hop_length": int(float(args['hop_length']) * 1e-3 * self.sample_rate)
        }
        self.n_mfcc = int(args["n_mfcc"]) if "n_mfcc" in args else 0
        self.compute_deltas = bool(args["compute_deltas"]) if "compute_deltas" in args else False
        self.compute_delta_deltas = bool(args["compute_delta_deltas"]) if "compute_delta_deltas" in args else False

    def extract(self, audio_loc):
        s = self._read_audio(audio_loc)
        features = self.get_extractor()(s)
        if self.feature_type == 'logMelSpec':
            features = torchaudio.functional.amplitude_to_DB(features, multiplier=10, amin=1e-10, db_multiplier=0)
        final_features = features
        if self.compute_deltas or self.compute_delta_deltas:
            delta_features = torchaudio.functional.compute_deltas(features)
            final_features = [features, delta_features]
            if self.compute_delta_deltas:
                double_delta_features = torchaudio.functional.compute_deltas(delta_features)
                final_features.append(double_delta_features)
            final_features = torch.cat(final_features, dim=0)
        return final_features.T

    def get_extractor(self):
        if self.feature_type == "MFCC":
            feature_args = {"sample_rate": self.sample_rate, "n_mfcc": self.n_mfcc, "melkwargs": self.mel_args}
            return torchaudio.transforms.MFCC(**feature_args)
        else:
            feature_args = {**{"sample_rate": self.sample_rate}, **self.mel_args}
            return torchaudio.transforms.MelSpectrogram(**feature_args)

    def _read_audio(self, filepath):
        """
        This code does the following:
            1. Read audio,
            2. Resample the audio if required,
            3. Perform waveform normalization,
            4. Compute sound activity using threshold based method
            5. Discard the silence regions
        """
        s, fs = torchaudio.load(filepath)
        if fs != self.sample_rate:
            s, fs = torchaudio.sox_effects.apply_effects_tensor(s, fs, [['rate', str(self.sample_rate)]])
        s = s / torch.max(torch.abs(s))
        sad = compute_sad(s.numpy(), self.sample_rate)
        s = s[np.where(sad == 1)]
        return s

max_stars_repo_name: project-coswara/cloud-functions

max_issues_repo_name: project-coswara/cloud-functions

max_forks_repo_name: project-coswara/cloud-functions


text:
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ============================================================================
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.

from npu_bridge.npu_init import *
import unittest
import numpy as np
import tensorflow as tf

from avod.core import box_8c_encoder
from avod.core import box_3d_encoder

class Box8cEncoderTest(unittest.TestCase):

    def test_box_3d_to_box_8co(self):
        # Tests the numpy version of the anchors_to_box_3d
        # function. This is the non-vectorized version.

        # Sample ground-truth in box3D format
        gt_box_3d = np.asarray(
            [-0.69, 1.69, 25.01, 3.2, 1.66, 1.61, -1.59],
            dtype=np.float32)
        # Sample box in anchor format
        anchors = np.asarray([[-0.59, 1.90, 25.01, 3.2, 1.61, 1.66]],
                             dtype=np.float32)
        # Convert the anchor to box3D format
        anchor_box_3d = box_3d_encoder.anchors_to_box_3d(anchors,
                                                         fix_lw=True)

        exp_gt_box_8co = np.asarray(
            [[-1.55, 0.10, 0.17, -1.49, -1.55, 0.11, 0.17, -1.49],
             [1.69, 1.69, 1.69, 1.69, 0.08, 0.08, 0.08, 0.08],
             [26.59, 26.62, 23.43, 23.39, 26.59, 26.62, 23.42, 23.39]])

        exp_anchor_box_8co = np.asarray(
            [[1.01, 1.01, -2.19, -2.19, 1.01, 1.01, -2.19, -2.19],
             [1.89, 1.89, 1.89, 1.89, 0.24, 0.24, 0.24, 0.24],
             [25.81, 24.21, 24.21, 25.82, 25.82, 24.21, 24.21, 25.82]])

        # convert to 8 corners
        gt_box_8co = box_8c_encoder.np_box_3d_to_box_8co(gt_box_3d)
        # the numpy version takes a single box
        anchor_box_8co = \
            box_8c_encoder.np_box_3d_to_box_8co(anchor_box_3d[0])

        np.testing.assert_almost_equal(exp_gt_box_8co,
                                       gt_box_8co,
                                       decimal=2,
                                       err_msg='GT corner encoding mismatch')

        np.testing.assert_almost_equal(
            exp_anchor_box_8co, anchor_box_8co, decimal=1,
            err_msg='Anchor corner encoding mismatch')

    def test_box_3d_tensor_to_box_8co(self):
        # Tests the tensor version of the anchors_to_box_3d
        # function. This is the vectorized version.

        anchors = np.asarray([
            [-0.59, 1.90, 25.01, 3.2, 1.66, 1.61],
            [-0.80, 1.50, 22.01, 1.2, 1.70, 1.50]
        ])
        anchor_boxes_3d = box_3d_encoder.anchors_to_box_3d(anchors,
                                                           fix_lw=True)

        # convert each box to corner using the numpy version
        boxes_8c_1 = \
            box_8c_encoder.np_box_3d_to_box_8co(anchor_boxes_3d[0])
        boxes_8c_2 = \
            box_8c_encoder.np_box_3d_to_box_8co(anchor_boxes_3d[1])

        exp_anchor_box_8co = np.stack((boxes_8c_1,
                                       boxes_8c_2),
                                      axis=0)

        anchors_box3d_tensor = tf.convert_to_tensor(anchor_boxes_3d,
                                                    dtype=tf.float32)
        # convert to 8 corners
        anchor_box_corner_tensor = \
            box_8c_encoder.tf_box_3d_to_box_8co(anchors_box3d_tensor)

        sess = tf.Session(config=npu_config_proto())
        with sess.as_default():
            anchor_box_corner_out = anchor_box_corner_tensor.eval()

        np.testing.assert_almost_equal(
            exp_anchor_box_8co[0], anchor_box_corner_out[0], decimal=2,
            err_msg='Anchor tensor corner encoding mismatch')

        np.testing.assert_almost_equal(
            exp_anchor_box_8co[1], anchor_box_corner_out[1], decimal=2,
            err_msg='Anchor tensor corner encoding mismatch')

    def test_gt_box_3d_tensor_to_8c(self):
        # This test is slightly different from above as it tests
        # the conversion of ground-truth boxes with *any* orientation
        # to 8-corner format.
        # Sample ground-truth in box3D format
        gt_boxes_3d = np.asarray([
            [-0.69, 1.69, 25.01, 3.2, 1.66, 1.61, -1.59],
            [-7.43, 1.88, 47.55, 3.7, 1.51, 1.4, 1.55]],
            dtype=np.float32)

        gt_boxes_3d_tensor = tf.convert_to_tensor(gt_boxes_3d,
                                                  dtype=tf.float32)

        boxes_8c_1 = box_8c_encoder.np_box_3d_to_box_8co(gt_boxes_3d[0])
        boxes_8c_2 = box_8c_encoder.np_box_3d_to_box_8co(gt_boxes_3d[1])

        exp_boxes_8c_gt = np.stack((boxes_8c_1,
                                    boxes_8c_2),
                                   axis=0)

        boxes_c8_gt = \
            box_8c_encoder.tf_box_3d_to_box_8co(gt_boxes_3d_tensor)

        sess = tf.Session(config=npu_config_proto())
        with sess.as_default():
            corner_box_8co_out = boxes_c8_gt.eval()

        np.testing.assert_almost_equal(
            exp_boxes_8c_gt[0], corner_box_8co_out[0], decimal=3,
            err_msg='Gt box 3D tensor to corner encoding mismatch')

        np.testing.assert_almost_equal(
            exp_boxes_8c_gt[1], corner_box_8co_out[1], decimal=3,
            err_msg='Gt box 3D tensor to corner encoding mismatch')

    def test_align_box_8co(self):
        # Given 8 corners of an irregular shape, tests whether
        # they get aligned correctly
        irregular_box_8co = \
            np.asarray([[[6.0, 6.0, 3.0, 4.0, 5.0, 5.0, 3.0, 2.0],
                         [0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 2.0],
                         [4.0, 1.0, 1.0, 6.0, 4.0, 1.0, 1.0, 4.0]]])

        exp_box_8co = np.asarray([[6.0, 6.0, 2.0, 2.0, 6.0, 6.0, 2.0, 2.0],
                                  [2.0, 2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0],
                                  [6.0, 1.0, 1.0, 6.0, 6.0, 1.0, 1.0, 6.0]])

        irregular_box_8co_tensor = tf.convert_to_tensor(irregular_box_8co,
                                                        dtype=tf.float32)

        aligned_box_8co = box_8c_encoder.align_boxes_8c(
            irregular_box_8co_tensor)

        sess = tf.Session(config=npu_config_proto())
        with sess.as_default():
            aligned_box_8co_out = aligned_box_8co.eval()

        np.testing.assert_array_equal(exp_box_8co,
                                      aligned_box_8co_out[0],
                                      err_msg='Wrong aligned corners.')

    def test_box3d_to_box8c_and_back(self):

        boxes_3d = np.asarray([
            [-0.59, 1.9, 25.01, 3.2, 1.61, 1.66, 0.],
            [-0.69, 1.69, 25.01, 3.2, 1.66, 1.61, -1.59]],
            dtype=np.float32)

        box_8co_1 = box_8c_encoder.np_box_3d_to_box_8co(boxes_3d[0])
        box_8co_2 = box_8c_encoder.np_box_3d_to_box_8co(boxes_3d[1])

        boxes_8c = np.stack((box_8co_1,
                             box_8co_2),
                            axis=0)

        boxes_c8_tensor = tf.convert_to_tensor(boxes_8c,
                                               dtype=tf.float32)

        boxes_3d_tensor = box_8c_encoder.box_8c_to_box_3d(boxes_c8_tensor)

        sess = tf.Session(config=npu_config_proto())
        with sess.as_default():
            boxes_3d_out = boxes_3d_tensor.eval()

        np.testing.assert_almost_equal(boxes_3d[0],
                                       boxes_3d_out[0],
                                       decimal=2)
        np.testing.assert_almost_equal(boxes_3d[1],
                                       boxes_3d_out[1],
                                       decimal=2)

    def test_box_8co_and_box3d(self):

        # These corners are slightly skewed to test the
        # corner alignment during transformation
        exp_box_3d = np.asarray(
            [-0.59, 1.9, 25.01, 3.2, 1.61, 1.66, 0.],
            dtype=np.float32)

        box_8co = box_8c_encoder.np_box_3d_to_box_8co(exp_box_3d)

        # skew the corners slightly
        box_8co[0, 2] += 0.4
        box_8co[0, 6] += 0.9

        box_c8_tensor = tf.convert_to_tensor(box_8co,
                                             dtype=tf.float32)
        box_c8_tensor = tf.expand_dims(box_c8_tensor, axis=0)

        box_3d = box_8c_encoder.box_8c_to_box_3d(box_c8_tensor)

        sess = tf.Session(config=npu_config_proto())
        with sess.as_default():
            box_3d_out = box_3d.eval()

        np.testing.assert_almost_equal(box_3d_out[0],
                                       exp_box_3d,
                                       decimal=2)

    def test_tf_box_8co_diagonal_length(self):
        # Tests the calculation of diagonal for both box_3d
        # and box_8co
        boxes_3d = np.asarray([
            [-0.59, 1.9, 25.01, 3.2, 1.61, 1.66, 0.],
            [-0.69, 1.69, 25.01, 2.2, 1.70, 1.61, -1.59]],
            dtype=np.float32)

        exp_diagonal_box1 = 3.9481
        exp_diagonal_box2 = 3.2128

        boxes_3d_tensor = tf.convert_to_tensor(boxes_3d,
                                               dtype=tf.float32)

        boxes_c8_tensor = \
            box_8c_encoder.tf_box_3d_to_box_8co(boxes_3d_tensor)

        boxes_8c_diagonals = \
            box_8c_encoder.tf_box_8c_diagonal_length(boxes_c8_tensor)
        boxes_3d_diagonals = \
            box_3d_encoder.tf_box_3d_diagonal_length(boxes_3d_tensor)

        sess = tf.Session(config=npu_config_proto())
        with sess.as_default():
            boxes_8c_diagonals_out = boxes_8c_diagonals.eval()
            boxes_3d_diagonals_out = boxes_3d_diagonals.eval()

        # Test box_8co diagonals
        self.assertAlmostEqual(boxes_8c_diagonals_out[0],
                               exp_diagonal_box1,
                               places=4,
                               msg='Diagonal mistmatch')

        self.assertAlmostEqual(boxes_8c_diagonals_out[1],
                               exp_diagonal_box2,
                               places=4,
                               msg='Diagonal mistmatch')
        # Test box_3d diagonals
        self.assertAlmostEqual(boxes_3d_diagonals_out[0],
                               exp_diagonal_box1,
                               places=4,
                               msg='Diagonal mistmatch')

        self.assertAlmostEqual(boxes_3d_diagonals_out[1],
                               exp_diagonal_box2,
                               places=4,
                               msg='Diagonal mistmatch')

    def test_box_8co_to_offset(self):
        # tests corners to normalized offsets
        boxes_3d = np.asarray([
            [-0.59, 1.9, 25.01, 3.2, 1.61, 1.66, 0.]],
            dtype=np.float32)
        boxes_3d_gt = np.asarray([
            [-0.69, 1.69, 25.01, 3.2, 1.62, 1.61, -1.59]],
            dtype=np.float32)

        exp_normalized_offsets = np.asarray([
            [[-0.6434, -0.233, 0.592, 0.182,
              -0.643, -0.233, 0.592, 0.182],
             [-0.053, -0.0531, -0.0531, -0.0531,
              -0.0405, -0.0405, -0.0405, -0.0405],
             [0.197, 0.613, -0.197, -0.613,
              0.197, 0.613, -0.197, -0.613]]
        ])

        boxes_3d_tensor = tf.convert_to_tensor(boxes_3d,
                                               dtype=tf.float32)

        boxes_3d_gt_tensor = tf.convert_to_tensor(boxes_3d_gt,
                                                  dtype=tf.float32)

        boxes_8c = \
            box_8c_encoder.tf_box_3d_to_box_8co(boxes_3d_tensor)

        boxes_8c_gt = \
            box_8c_encoder.tf_box_3d_to_box_8co(boxes_3d_gt_tensor)

        normalized_offsets = box_8c_encoder.tf_box_8c_to_offsets(
            boxes_8c, boxes_8c_gt)

        sess = tf.Session(config=npu_config_proto())
        with sess.as_default():
            normalized_offsets_out = normalized_offsets.eval()

        np.testing.assert_almost_equal(normalized_offsets_out,
                                       exp_normalized_offsets,
                                       decimal=3,
                                       err_msg='box_8co to offsets mistmatch')

    def test_box_offset_to_8c(self):
        # tests corners to normalized offsets
        boxes_3d = np.asarray([
            [-0.59, 1.9, 25.01, 3.2, 1.61, 1.66, 0.],
            [-0.49, 1.8, 25.01, 3.2, 1.61, 1.78, 0.]],
            dtype=np.float32)
        boxes_3d_gt = np.asarray([
            [-0.69, 1.69, 25.01, 3.2, 1.62, 1.61, -1.59],
            [-0.49, 1.78, 25.01, 3.2, 1.63, 1.61, -1.57]],
            dtype=np.float32)

        boxes_3d_tensor = tf.convert_to_tensor(boxes_3d,
                                               dtype=tf.float32)

        boxes_3d_gt_tensor = tf.convert_to_tensor(boxes_3d_gt,
                                                  dtype=tf.float32)

        boxes_8c = \
            box_8c_encoder.tf_box_3d_to_box_8co(boxes_3d_tensor)

        boxes_8c_gt = \
            box_8c_encoder.tf_box_3d_to_box_8co(boxes_3d_gt_tensor)

        # Convert to normalized offsets
        normalized_offsets = box_8c_encoder.tf_box_8c_to_offsets(
            boxes_8c, boxes_8c_gt)

        # Convert the offsets back to boxes_8c
        # This should gives us the gt boxes back
        boxes_8c_gt_back = box_8c_encoder.tf_offsets_to_box_8c(
            boxes_8c, normalized_offsets)

        sess = tf.Session(config=npu_config_proto())
        with sess.as_default():
            boxes_8c_out = boxes_8c_gt_back.eval()
            boxes_8c_gt_out = boxes_8c_gt.eval()

        np.testing.assert_almost_equal(boxes_8c_out,
                                       boxes_8c_gt_out,
                                       decimal=3,
                                       err_msg='Offset to box_8co mistmatch')

    def test_np_box_8c_conversion(self):
        # Box at 180 orientation
        boxes_3d = np.asarray([
            [-0.59, 1.9, 25.01, 3.2, 1.61, 1.66, 3.14]],
            dtype=np.float32)
        # Box at 0 orientation
        boxes_3d_gt = np.asarray([
            [-0.59, 1.9, 25.01, 3.2, 1.61, 1.66, 0.]],
            dtype=np.float32)

        # Convert to box_8c
        box_8c = box_8c_encoder.np_box_3d_to_box_8c(boxes_3d[0])
        box_8c_gt = box_8c_encoder.np_box_3d_to_box_8c(boxes_3d_gt[0])

        np.testing.assert_almost_equal(
            box_8c, box_8c_gt, decimal=2,
            err_msg='Wrong box_8c nonordered corners.')

    def test_tf_box_8c_conversion(self):
        # Box at 180 orientation
        boxes_3d = np.asarray([
            [-0.59, 1.9, 25.01, 3.2, 1.61, 1.66, 3.14]],
            dtype=np.float32)
        # Box at 0 orientation
        boxes_3d_gt = np.asarray([
            [-0.59, 1.9, 25.01, 3.2, 1.61, 1.66, 0.]],
            dtype=np.float32)

        boxes_3d_tensor = tf.convert_to_tensor(boxes_3d,
                                               dtype=tf.float32)

        boxes_3d_gt_tensor = tf.convert_to_tensor(boxes_3d_gt,
                                                  dtype=tf.float32)

        # Convert to box_8c
        boxes_8c = box_8c_encoder.tf_box_3d_to_box_8c(boxes_3d_tensor)
        boxes_8c_gt = box_8c_encoder.tf_box_3d_to_box_8c(boxes_3d_gt_tensor)

        sess = tf.Session(config=npu_config_proto())
        with sess.as_default():
            boxes_8c_out = boxes_8c.eval()
            boxes_8c_gt_out = boxes_8c_gt.eval()

        np.testing.assert_almost_equal(
            boxes_8c_out, boxes_8c_gt_out, decimal=2,
            err_msg='Wrong box_8c nonordered corners.')

if __name__ == '__main__':
    unittest.main()

max_stars_repo_name: Ascend-Huawei/AVOD

max_issues_repo_name: Ascend-Huawei/AVOD

max_forks_repo_name: Ascend-Huawei/AVOD


text:
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license.

import os
import numpy as np
import tensorflow as tf
from PIL import Image
from azureml.core import Model

def init():
    global g_tf_sess

    # AZUREML_MODEL_DIR is an environment variable created during deployment
    # It is the path to the model folder (./azureml-models)
    # Please provide your model's folder name if there's one
    model_path = os.path.join(os.environ["AZUREML_MODEL_DIR"], "model")

    # contruct graph to execute
    tf.reset_default_graph()
    saver = tf.train.import_meta_graph(os.path.join(model_path, "mnist-tf.model.meta"))
    g_tf_sess = tf.Session(config=tf.ConfigProto(device_count={"GPU": 0}))
    saver.restore(g_tf_sess, os.path.join(model_path, "mnist-tf.model"))

def run(mini_batch):
    print(f"run method start: {__file__}, run({mini_batch})")
    resultList = []
    in_tensor = g_tf_sess.graph.get_tensor_by_name("network/X:0")
    output = g_tf_sess.graph.get_tensor_by_name("network/output/MatMul:0")

    for image in mini_batch:
        # prepare each image
        data = Image.open(image)
        np_im = np.array(data).reshape((1, 784))
        # perform inference
        inference_result = output.eval(feed_dict={in_tensor: np_im}, session=g_tf_sess)
        # find best probability, and add to result list
        best_result = np.argmax(inference_result)
        resultList.append("{}: {}".format(os.path.basename(image), best_result))

    return resultList

max_stars_repo_name: Ali-ry/azureml-examples

max_issues_repo_name: Ali-ry/azureml-examples

max_forks_repo_name: Ali-ry/azureml-examples


text:
"""
The `GraphGenerator` class defines how to build molecular graphs during the general
training process using the following actions:
 * "add" a node to graph
 * "connect" existing nodes in graph
 * "terminate" graph
"""
# load general packages and functions
import time
from typing import Tuple
import numpy as np
from tqdm import tqdm
import torch
import rdkit

# load GraphINVENT-specific functions
from parameters.constants import constants
from MolecularGraph import GenerationGraph

class GraphGenerator:
    """
    Class for graph generation. Generates graphs in batches using the defined
    model. Optimized for quick generation on a GPU (sacrificed a bit of
    readability for speed here).
    """
    def __init__(self, model : torch.nn.Module, batch_size : int) -> None:
        """
        Args:
        ----
            model (torch.nn.Module) : Trained model.
            batch_size (int) : Generation batch size.
        """
        self.start_time = time.time()  # start the timer
        self.batch_size = batch_size
        self.model      = model

        # initializes `self.nodes`, `self.edges`, and `self.n_nodes`, which are
        # tensors for keeping track of the batch of graphs
        self.initialize_graph_batch()

        # allocate tensors for finished graphs; these will get filled in gradually
        # as graphs terminate: `self.generated_nodes`, `self.generated_edges`,
        # `self.generated_n_nodes`, `self.generated_likelihoods`, and
        # `self.properly_terminated`
        self.allocate_graph_tensors()

    def sample(self) -> Tuple[list, torch.Tensor, torch.Tensor, torch.Tensor]:
        """
        Samples the model for new molecular graphs, and cleans up after
        `build_graphs()`.

        Returns:
        -------
            graphs (list)                        : Generated molecular graphs
                                                   (`GenerationGraphs`s).
            generated_likelihoods (torch.Tensor) : Sampled likelihoodss per action
                                                   for generated graphs.
            final_loglikelihoods (torch.Tensor)  : Final total NLLs (sum) for
                                                   generated graphs.
            properly_terminated (torch.Tensor)   : Binary vector indicating if graphs
                                                   were properly terminated or not.
        """
        # build the graphs (these are stored as `self` attributes)
        n_generated_graphs = self.build_graphs()

        # get the time it took to generate graphs
        self.start_time = time.time() - self.start_time
        print(f"Generated {n_generated_graphs} molecules in "
              f"{self.start_time:.4} s")
        print(f"--{n_generated_graphs/self.start_time:4.5} molecules/s")

        # convert the molecular graphs (currently separate node and edge
        # features tensors) into `GenerationGraph` objects; sometimes
        # `n_generated_graphs` > `self.batch_size`, in which case the extra
        # generated graphs are simply discarded below
        graphs = [self.graph_to_graph(idx) for idx in range(self.batch_size)]

        # sum NLL per action to get the total NLL for each structure; remove
        # extra zero padding
        final_loglikelihoods = torch.log(
            torch.sum(self.generated_likelihoods, dim=1)[:self.batch_size]
        )

        # remove extra zero padding from NLLs
        generated_likelihoods = self.generated_likelihoods[
            self.generated_likelihoods != 0
        ]

        # remove extra padding from `properly_terminated` tensor
        properly_terminated = self.properly_terminated[:self.batch_size]

        return (graphs,
                generated_likelihoods,
                final_loglikelihoods,
                properly_terminated)

    def build_graphs(self) ->  int:
        """
        Builds molecular graphs in batches, starting from empty graphs.

        Returns:
        -------
            n_generated_so_far (int) : Number molecules built (may be >
                                       `self.batch_size` due to buffer).
        """
        softmax = torch.nn.Softmax(dim=1)

        # keep track of a few things...
        n_generated_so_far = 0
        t_bar = tqdm(total=self.batch_size)
        generation_round = 0

        # generate graphs in a batch, saving graphs when either the terminate
        # action or an invalid action is sampled, until `self.batch_size` number
        # of graphs have been generated
        while n_generated_so_far < self.batch_size:

            # predict the APDs for this batch of graphs
            apd = softmax(self.model(self.nodes, self.edges))

            # sample the actions from the predicted APDs
            add, conn, term, invalid, likelihoods_just_sampled = self.get_actions(apd)

            # indicate (with a 1) the structures which have been properly terminated
            self.properly_terminated[n_generated_so_far:(n_generated_so_far + len(term))] = 1

            # collect the indices for all structures to write (and reset) this round
            termination_idc = torch.cat((term, invalid))

            # never write out the dummy graph at index 0
            termination_idc = termination_idc[termination_idc != 0]

            # copy the graphs indicated by `terminated_idc` to the tensors for
            # finished graphs (i.e. `generated_{nodes/edges}`)
            n_generated_so_far = self.copy_terminated_graphs(
                termination_idc,
                n_generated_so_far,
                generation_round,
                likelihoods_just_sampled
            )

            # apply actions to all graphs (note: applies dummy actions to
            # terminated graphs, since output will be reset anyways)
            self.apply_actions(add,
                               conn,
                               generation_round,
                               likelihoods_just_sampled)

            # after actions are applied, reset graphs which were set to
            # terminate this round
            self.reset_graphs(termination_idc)

            # update variables for tracking the progress
            t_bar.update(len(termination_idc))
            generation_round += 1

        t_bar.close()  # done sampling

        return n_generated_so_far

    def allocate_graph_tensors(self) -> None:
        """
        Allocates tensors for the node features, edge features, NLLs, and
        termination status for all graphs to be generated. These then get filled
        in during the graph generation process.
        """
        # define tensor shapes
        node_shape = (self.batch_size, *constants.dim_nodes)
        edge_shape = (self.batch_size, *constants.dim_edges)
        likelihoods_shape = (self.batch_size, constants.max_n_nodes * 2)  # the 2 is arbitrary

        # allocate a buffer equal to the size of an extra batch
        n_allocate = self.batch_size * 2

        # create the placeholder tensors:

        # placeholder for node features tensor for all graphs
        self.generated_nodes = torch.zeros((n_allocate, *node_shape[1:]),
                                           dtype=torch.float32,
                                           device=constants.device)

        # placeholder for edge features tensor for all graphs
        self.generated_edges = torch.zeros((n_allocate, *edge_shape[1:]),
                                           dtype=torch.float32,
                                           device=constants.device)

        # placeholder for number of nodes per graph in all graphs
        self.generated_n_nodes = torch.zeros(n_allocate,
                                             dtype=torch.int8,
                                             device=constants.device)

        # placeholder for sampled NLL per action for all graphs
        self.likelihoods = torch.zeros(likelihoods_shape,
                                       device=constants.device)

        # placeholder for sampled NLLs per action for all finished graphs
        self.generated_likelihoods = torch.zeros(
            (n_allocate, *likelihoods_shape[1:]),
            device=constants.device
        )

        # placeholder for graph termination status (1 == properly terminated,
        # 0 == improper)
        self.properly_terminated = torch.zeros(n_allocate,
                                               dtype=torch.int8,
                                               device=constants.device)

    def apply_actions(self, add : Tuple[torch.Tensor, ...],
                      conn : Tuple[torch.Tensor, ...], generation_round : int,
                      likelihoods_sampled : torch.Tensor) -> None:
        """
        Applies the batch of sampled actions (specified by `add` and `conn`) to
        the batch of graphs under construction. Also adds the NLLs for the newly
        sampled actions (`likelihoods_sampled`) to the running list of NLLs.
        Updates the following tensors:
            self.nodes (torch.Tensor)       : Updated node features tensor (batch).
            self.edges (torch.Tensor)       : Updated edge features tensor (batch).
            self.n_nodes (torch.Tensor)     : Updated number of nodes per graph
                                              (batch).
            self.likelihoods (torch.Tensor) : Updated sampled NLL per action
                                              for graphs (batch).

        Args:
        ----
            add (tuple)            : Indices for "add" actions sampled for batch
                                     of graphs.
            conn (tuple)           : Indices for "connect" actions sampled for
                                     batch of graphs.
            generation_round (int) : Indicates current generation round.
            likelihoods_sampled (torch.Tensor) : NLL per action sampled for the
                                                 most recent set of actions.
        """
        def _add_nodes(add : Tuple[torch.Tensor, ...], generation_round : int,
                       likelihoods_sampled : torch.Tensor) -> None:
            """
            Adds new nodes to graphs which sampled the "add" action.

            Args:
            ----
                add (tuple)                        : Indices for "add" actions sampled
                                                     for batch of graphs.
                generation_round (int)             : Indicates current generation
                                                     round.
                likelihoods_sampled (torch.Tensor) : NLL per action sampled for
                                                     the most recent set of actions.
            """
            # get the action indices
            add = [idx.long() for idx in add]
            n_node_features = [constants.n_atom_types,
                               constants.n_formal_charge,
                               constants.n_imp_H,
                               constants.n_chirality]

            if not constants.use_explicit_H and not constants.ignore_H:
                if constants.use_chirality:
                    (batch, bond_to, atom_type, charge,
                     imp_h, chirality, bond_type, bond_from) = add

                    # add the new nodes to the node features tensors
                    self.nodes[batch, bond_from, atom_type] = 1
                    self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
                    self.nodes[batch, bond_from, imp_h + sum(n_node_features[0:2])] = 1
                    self.nodes[batch, bond_from, chirality + sum(n_node_features[0:3])] = 1
                else:
                    (batch, bond_to, atom_type, charge,
                     imp_h, bond_type, bond_from) = add

                    # add the new nodes to the node features tensors
                    self.nodes[batch, bond_from, atom_type] = 1
                    self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
                    self.nodes[batch, bond_from, imp_h + sum(n_node_features[0:2])] = 1
            elif constants.use_chirality:
                (batch, bond_to, atom_type, charge,
                 chirality, bond_type, bond_from) = add

                # add the new nodes to the node features tensors
                self.nodes[batch, bond_from, atom_type] = 1
                self.nodes[batch, bond_from, charge + n_node_features[0]] = 1
                self.nodes[batch, bond_from, chirality + sum(n_node_features[0:2])] = 1
            else:
                (batch, bond_to, atom_type, charge,
                 bond_type, bond_from) = add

                # add the new nodes to the node features tensors
                self.nodes[batch, bond_from, atom_type] = 1
                self.nodes[batch, bond_from, charge + n_node_features[0]] = 1

            # mask dummy edges (self-loops) introduced from adding node to empty graph
            batch_masked = batch[torch.nonzero(self.n_nodes[batch] != 0)]
            bond_to_masked = bond_to[torch.nonzero(self.n_nodes[batch] != 0)]
            bond_from_masked = bond_from[torch.nonzero(self.n_nodes[batch] != 0)]
            bond_type_masked = bond_type[torch.nonzero(self.n_nodes[batch] != 0)]

            # connect newly added nodes to the graphs
            self.edges[batch_masked, bond_to_masked, bond_from_masked, bond_type_masked] = 1
            self.edges[batch_masked, bond_from_masked, bond_to_masked, bond_type_masked] = 1

            # keep track of the newly added node
            self.n_nodes[batch] += 1

            # include the NLLs for the add actions for this generation round
            self.likelihoods[batch, generation_round] = likelihoods_sampled[batch]

        def _conn_nodes(conn : Tuple[torch.Tensor, ...], generation_round : int,
                        likelihoods_sampled : torch.Tensor) -> None:
            """
            Connects nodes in graphs which sampled the "connect" action.

            Args:
            ----
                conn (tuple)                       : Indices for "connect" actions
                                                     sampled for batch of graphs.
                generation_round (int)             : Indicates current generation
                                                     round.
                likelihoods_sampled (torch.Tensor) : NLL per action sampled for the
                                                     most recent set of actions.
            """
            # get the action indices
            conn = [idx.long() for idx in conn]
            batch, bond_to, bond_type, bond_from = conn

            # apply the connect actions
            self.edges[batch, bond_from, bond_to, bond_type] = 1
            self.edges[batch, bond_to, bond_from, bond_type] = 1

            # include the NLLs for the connect actions for this generation round
            self.likelihoods[batch, generation_round] = likelihoods_sampled[batch]

        # first applies the "add" action to all graphs in batch (note: does
        # nothing if a graph did not sample "add")
        _add_nodes(add, generation_round, likelihoods_sampled)

        # then applies the "connect" action to all graphs in batch (note: does
        # nothing if a graph did not sample "connect")
        _conn_nodes(conn, generation_round, likelihoods_sampled)

    def copy_terminated_graphs(self, terminate_idc : torch.Tensor,
                               n_graphs_generated : int, generation_round : int,
                               likelihoods_sampled : torch.Tensor) -> int:
        """
        Copies terminated graphs (either because "terminate" action sampled, or
        invalid action sampled) to `generated_nodes` and `generated_edges`
        before they are removed from the running batch of graphs being generated.

        Args:
        ----
            terminate_idc (torch.Tensor) : Indices for graphs that will terminate
                                           this round.
            n_graphs_generated (int)     : Number of graphs generated thus far (not
                                           including those about to be copied).
            generation_round (int)       : Indicates the current generation round
                                           (running count).
            likelihoods_sampled (torch.Tensor) : NLLs for the newest sampled action
                                                 for each graph in a batch of graphs
                                                 (not yet included in `likelihoods`).

        Returns:
        -------
            n_graphs_generated (int) : Number of graphs generated thus far.
        """
        # number of graphs to be terminated
        self.likelihoods[terminate_idc, generation_round] = likelihoods_sampled[terminate_idc]

        # number of graphs to be terminated
        n_done_graphs = len(terminate_idc)

        # copy the new graphs to the finished tensors
        nodes_local = self.nodes[terminate_idc]
        edges_local = self.edges[terminate_idc]
        n_nodes_local = self.n_nodes[terminate_idc]
        likelihoods_local = self.likelihoods[terminate_idc]

        begin_idx = n_graphs_generated
        end_idx = n_graphs_generated + n_done_graphs
        self.generated_nodes[begin_idx : end_idx] = nodes_local
        self.generated_edges[begin_idx : end_idx] = edges_local
        self.generated_n_nodes[begin_idx : end_idx] = n_nodes_local
        self.generated_likelihoods[begin_idx : end_idx] = likelihoods_local

        n_graphs_generated += n_done_graphs

        return n_graphs_generated

    def initialize_graph_batch(self) -> None:
        """
        Initializes a batch of empty graphs (zero `torch.Tensor`s) to begin the
        generation process. Creates the following:
            self.nodes (torch.Tensor)   : Empty node features tensor (batch).
            self.edges (torch.Tensor)   : Empty edge features tensor (batch).
            self.n_nodes (torch.Tensor) : Number of nodes per graph in (batch),
                                          currently all 0.

        Also, creates a dummy "non-empty" graph at index 0, so that the models
        do not freak out when they receive entirely zero torch tensors as input
        (haven't found a more elegant solution to this problem; without the
        dummy non-empty graph, there is a silent error in the message update
        function of the MPNNs).
        """
        # define tensor shapes
        node_shape = ([self.batch_size] + constants.dim_nodes)
        edge_shape = ([self.batch_size] + constants.dim_edges)
        n_nodes_shape = [self.batch_size]

        # initialize tensors
        self.nodes = torch.zeros(node_shape,
                                 dtype=torch.float32,
                                 device=constants.device)
        self.edges = torch.zeros(edge_shape,
                                 dtype=torch.float32,
                                 device=constants.device)
        self.n_nodes = torch.zeros(n_nodes_shape,
                                   dtype=torch.int8,
                                   device=constants.device)

        # add a dummy non-empty graph at idx 0, since models cannot receive
        # purely empty graphs
        self.nodes[0] = torch.ones(([1] + constants.dim_nodes),
                                   device=constants.device)
        self.edges[0, 0, 0, 0] = 1
        self.n_nodes[0] = 1

    def reset_graphs(self, idc : torch.Tensor) -> None:
        """
        Resets the `nodes` and `edges` tensors by reseting graphs which sampled
        invalid actions (indicated by `idc`). Updates the following:
            self.nodes_reset (torch.Tensor)   : Reset node features tensor (batch).
            self.edges_reset (torch.Tensor)   : Reset edge features tensor (batch).
            self.n_nodes_reset (torch.Tensor) : Reset number of nodes per graph
                                                (batch).
            self.likelihoods_reset (torch.Tensor) : Reset sampled NLL per action
                                                    for graphs (batch).

        Args:
        ----
            idc (torch.Tensor) : Indices corresponding to graphs to reset.
        """
        # define constants
        node_shape = ([self.batch_size] + constants.dim_nodes)
        edge_shape = ([self.batch_size] + constants.dim_edges)
        n_nodes_shape = ([self.batch_size])
        likelihoods_shape = ([self.batch_size] + [constants.max_n_nodes * 2])  # the 2 is arbitrary

        # reset the "bad" graphs with zero tensors
        if len(idc) > 0:
            self.nodes[idc] = torch.zeros((len(idc), *node_shape[1:]),
                                          dtype=torch.float32,
                                          device=constants.device)
            self.edges[idc] = torch.zeros((len(idc), *edge_shape[1:]),
                                          dtype=torch.float32,
                                          device=constants.device)
            self.n_nodes[idc] = torch.zeros((len(idc), *n_nodes_shape[1:]),
                                            dtype=torch.int8,
                                            device=constants.device)
            self.likelihoods[idc] = torch.zeros((len(idc), *likelihoods_shape[1:]),
                                                dtype=torch.float32,
                                                device=constants.device)

        # create a dummy non-empty graph
        self.nodes[0] = torch.ones(([1] + constants.dim_nodes),
                                   device=constants.device)
        self.edges[0, 0, 0, 0] = 1
        self.n_nodes[0] = 1

    def get_actions(self, apds : torch.Tensor) -> Tuple[torch.Tensor, ...]:
        """
        Samples the input batch of APDs for a batch of actions to apply to the
        graphs, and separates the action indices.

        Args:
        ----
            apds (torch.Tensor) : APDs for a batch of graphs.

        Returns:
        -------
            f_add_idc (torch.Tensor)   : Indices corresponding to "add" action.
            f_conn_idc (torch.Tensor)  : Indices corresponding to "connect" action.
            f_term_idc (torch.Tensor)  : Indices corresponding to "terminate" action.
            invalid_idc (torch.Tensor) : Indices corresponding graphs which sampled
                                         an invalid action.
            likelihoods (torch.Tensor) : NLLs per action corresponding to graphs
                                         in batch.
        """
        def _reshape_apd(apds : torch.Tensor, batch_size : int) -> \
            Tuple[torch.Tensor, ...]:
            """
            Reshapes the input batch of APDs (inverse to flattening).

            Args:
            ----
                apds (torch.Tensor) : APDs for a batch of graphs.
                batch_size (int)    : Batch size.

            Returns:
            -------
                f_add (torch.Tensor)  : Reshaped APD segment for "add" action.
                f_conn (torch.Tensor) : Reshaped APD segment for "connect"
                                        action.
                f_term (torch.Tensor) : Reshaped APD segment for "terminate"
                                        action.
            """
            # get shapes of "add" and "connect" actions
            f_add_shape = (batch_size, *constants.dim_f_add)
            f_conn_shape = (batch_size, *constants.dim_f_conn)

            # get len of flattened segment of APD corresponding to "add" action
            f_add_size = np.prod(constants.dim_f_add)

            # reshape the various APD components
            f_add = torch.reshape(apds[:, :f_add_size], f_add_shape)
            f_conn = torch.reshape(apds[:, f_add_size:-1], f_conn_shape)
            f_term = apds[:, -1]

            return f_add, f_conn, f_term

        def _sample_apd(apds : torch.Tensor, batch_size : int) -> \
            Tuple[torch.Tensor, ...]:
            """
            Samples the input APDs for all graphs in the batch.

            Args:
            ----
                apds (torch.Tensor) : APDs for a batch of graphs.
                batch_size (int)    : Batch size.

            Returns:
            -------
                add_idc (torch.Tensor)     : Nonzero elements in `f_add`.
                conn_idc (torch.Tensor)    : Nonzero elements in `f_conn`.
                term_idc (torch.Tensor)    : Nonzero elements in `f_term`.
                likelihoods (torch.Tensor) : Contains NLLs for samples actions.
            """
            action_probability_distribution = torch.distributions.Multinomial(
                1,
                probs=apds
            )
            apd_one_hot = action_probability_distribution.sample()
            f_add, f_conn, f_term = _reshape_apd(apd_one_hot, batch_size)

            likelihoods = apds[apd_one_hot == 1]

            add_idc = torch.nonzero(f_add, as_tuple=True)
            conn_idc = torch.nonzero(f_conn, as_tuple=True)
            term_idc = torch.nonzero(f_term).view(-1)

            return add_idc, conn_idc, term_idc, likelihoods

        # sample the APD for all graphs in the batch for action indices
        f_add_idc, f_conn_idc, f_term_idc, likelihoods = _sample_apd(
            apds,
            self.batch_size
        )

        # get indices for the "add" action
        f_add_from = self.n_nodes[f_add_idc[0]]
        f_add_idc = (*f_add_idc, f_add_from)

        # get indices for the "connect" action
        f_conn_from = self.n_nodes[f_conn_idc[0]] - 1
        f_conn_idc = (*f_conn_idc, f_conn_from)

        # get indices for the invalid add and connect actions
        invalid_idc, max_node_idc = self.get_invalid_actions(f_add_idc, f_conn_idc)

        # change "connect to" index for graphs trying to add more than max n nodes
        f_add_idc[5][max_node_idc] = 0

        return f_add_idc, f_conn_idc, f_term_idc, invalid_idc, likelihoods

    def get_invalid_actions(self,
                            f_add_idc : Tuple[torch.Tensor, ...],
                            f_conn_idc : Tuple[torch.Tensor, ...]) \
                            -> Tuple[torch.Tensor, torch.Tensor]:
        """
        Gets the indices corresponding to any invalid sampled actions.

        Args:
        ----
            f_add_idc (torch.Tensor)  : Indices for "add" actions for batch of
                                        graphs.
            f_conn_idc (torch.Tensor) : Indices for the "connect" actions for
                                        batch of graphs.

        Returns:
        -------
            invalid_action_idc (torch.Tensor) : Indices corresponding to all invalid
                                                actions (include the indices below).
            invalid_action_idc_needing_reset (torch.Tensor) : Indices corresponding
                                                              to add actions attempting
                                                              to add more than the
                                                              maximum number of nodes.
                                                              These must be treated
                                                              separately because
                                                              the "connect to" index
                                                              needs to be reset.
        """
        n_max_nodes = constants.dim_nodes[0]

        # empty graphs for which "add" action sampled
        f_add_empty_graphs = torch.nonzero(self.n_nodes[f_add_idc[0]] == 0)

        # get invalid indices for when adding a new node to a non-empty graph
        invalid_add_idx_tmp = torch.nonzero(f_add_idc[1] >= self.n_nodes[f_add_idc[0]])
        combined            = torch.cat((invalid_add_idx_tmp, f_add_empty_graphs)).squeeze(1)
        uniques, counts     = combined.unique(return_counts=True)
        invalid_add_idc     = uniques[counts == 1].unsqueeze(dim=1)  # set diff

        # get invalid indices for when adding a new node to an empty graph
        invalid_add_empty_idc = torch.nonzero(f_add_idc[1] != self.n_nodes[f_add_idc[0]])
        combined              = torch.cat((invalid_add_empty_idc, f_add_empty_graphs)).squeeze(1)
        uniques, counts       = combined.unique(return_counts=True)
        invalid_add_empty_idc = uniques[counts > 1].unsqueeze(dim=1)  # set intersection

        # get invalid indices for when adding more nodes than possible
        invalid_madd_idc = torch.nonzero(f_add_idc[5] >= n_max_nodes)

        # get invalid indices for when connecting a node to nonexisting node
        invalid_conn_idc = torch.nonzero(f_conn_idc[1] >= self.n_nodes[f_conn_idc[0]])

        # get invalid indices for when "connecting" a node in a graph with zero nodes
        invalid_conn_nonex_idc = torch.nonzero(self.n_nodes[f_conn_idc[0]] == 0)

        # get invalid indices for when creating self-loops
        invalid_sconn_idc = torch.nonzero(f_conn_idc[1] == f_conn_idc[3])

        # get invalid indices for when attemting to add multiple edges
        invalid_dconn_idc = torch.nonzero(
            torch.sum(self.edges, dim=-1)[f_conn_idc[0].long(),
                                          f_conn_idc[1].long(),
                                          f_conn_idc[-1].long()] == 1
        )

        # only need one invalid index per graph
        invalid_action_idc =torch.unique(
            torch.cat(
                (f_add_idc[0][invalid_add_idc],
                 f_add_idc[0][invalid_add_empty_idc],
                 f_conn_idc[0][invalid_conn_idc],
                 f_conn_idc[0][invalid_conn_nonex_idc],
                 f_conn_idc[0][invalid_sconn_idc],
                 f_conn_idc[0][invalid_dconn_idc],
                 f_add_idc[0][invalid_madd_idc])
            )
        )

        # keep track of invalid indices which require reseting during the final
        # `apply_action()`
        invalid_action_idc_needing_reset = torch.unique(
            torch.cat(
                (invalid_madd_idc, f_add_empty_graphs)
            )
        )

        return invalid_action_idc, invalid_action_idc_needing_reset

    def graph_to_graph(self, idx : int) -> GenerationGraph:
        """
        Converts a molecular graph representation from the individual node and
        edge feature tensors into `GenerationGraph` objects.

        Args:
        ----
            idx (int) : Index for the molecular graph to convert.

        Returns:
        -------
            graph (GenerationGraph) : Generated graph.
        """
        def _features_to_atom(node_idx : int, node_features : torch.Tensor) -> \
            rdkit.Chem.Atom:
            """
            Converts the node feature vector corresponding to the specified node
            into an atom object.

            Args:
            ----
                node_idx (int)               : Index denoting the specific node
                                               on the graph to convert.
                node_features (torch.Tensor) : Node features tensor for one graph.

            Returns:
            -------
                new_atom (rdkit.Atom) : Atom object corresponding to specified
                                        node features.
            """
            # get all the nonzero indices in the specified node feature vector
            nonzero_idc = torch.nonzero(node_features[node_idx])

            # determine atom symbol
            atom_idx = nonzero_idc[0]
            atom_type = constants.atom_types[atom_idx]

            # initialize atom
            new_atom = rdkit.Chem.Atom(atom_type)

            # determine formal charge
            fc_idx = nonzero_idc[1] - constants.n_atom_types
            formal_charge = constants.formal_charge[fc_idx]

            new_atom.SetFormalCharge(formal_charge)  # set property

            # determine number of implicit Hs (if used)
            if not constants.use_explicit_H and not constants.ignore_H:
                total_num_h_idx = (nonzero_idc[2] -
                                   constants.n_atom_types -
                                   constants.n_formal_charge)
                total_num_h = constants.imp_H[total_num_h_idx]

                new_atom.SetUnsignedProp("_TotalNumHs", total_num_h)  # set property
            elif constants.ignore_H:
                # Hs will be set with structure is "sanitized" (corrected) later
                # in `mol_to_graph()`
                pass

            # determine chirality (if used)
            if constants.use_chirality:
                cip_code_idx = (
                    nonzero_idc[-1]
                    - constants.n_atom_types
                    - constants.n_formal_charge
                    - (not constants.use_explicit_H and not
                       constants.ignore_H) * constants.n_imp_H
                )
                cip_code = constants.chirality[cip_code_idx]
                new_atom.SetProp("_CIPCode", cip_code)  # set property

            return new_atom

        def _graph_to_mol(node_features : torch.Tensor,
                         edge_features : torch.Tensor,
                         n_nodes : int) -> rdkit.Chem.Mol:
            """
            Converts input graph represenetation (node and edge features) into
            an `rdkit.Mol` object.

            Args:
            ----
                node_features (torch.Tensor) : Node features tensor.
                edge_features (torch.Tensor) : Edge features tensor.
                n_nodes (int)                : Number of nodes in the graph representation.

            Returns:
            -------
                molecule (rdkit.Chem.Mol) : Molecule object.
            """

            # create empty editable `rdkit.Chem.Mol` object
            molecule = rdkit.Chem.RWMol()
            node_to_idx = {}

            # add atoms to editable mol object
            for node_idx in range(n_nodes):
                atom_to_add = _features_to_atom(node_idx, node_features)
                molecule_idx = molecule.AddAtom(atom_to_add)
                node_to_idx[node_idx] = molecule_idx

            # add bonds to atoms in editable mol object; to not add the same bond twice
            # (which leads to an error), mask half of the edge features beyond diagonal
            n_max_nodes = constants.dim_nodes[0]
            edge_mask = torch.triu(
                torch.ones((n_max_nodes, n_max_nodes), device=constants.device),
                diagonal=1
            )
            edge_mask = edge_mask.view(n_max_nodes, n_max_nodes, 1)
            edges_idc = torch.nonzero(edge_features * edge_mask)

            for node_idx1, node_idx2, bond_idx in edges_idc:
                molecule.AddBond(
                    node_to_idx[node_idx1.item()],
                    node_to_idx[node_idx2.item()],
                    constants.int_to_bondtype[bond_idx.item()],
                )

            try:  # convert editable mol object to non-editable mol object
                molecule.GetMol()
            except AttributeError:  # will throw an error if molecule is `None`
                pass

            if constants.ignore_H and molecule:
                try:  # correct for ignored Hs
                    rdkit.Chem.SanitizeMol(molecule)
                except ValueError:  # throws exception if molecule is too ugly to correct
                    pass

            return molecule

        try:
            # first get the `rdkit.Mol` object corresponding to the selected graph
            mol = _graph_to_mol(self.generated_nodes[idx],
                                self.generated_edges[idx],
                                self.generated_n_nodes[idx])
        except (IndexError, AttributeError):  # raised when graph is empty
            mol = None

        # use the `rdkit.Mol` object, and node and edge features tensors, to get
        # the `GenerationGraph` object
        graph = GenerationGraph(constants=constants,
                                molecule=mol,
                                node_features=self.generated_nodes[idx],
                                edge_features=self.generated_edges[idx])
        return graph

max_stars_repo_name: gooaah/GraphINVENT

max_issues_repo_name: gooaah/GraphINVENT

max_forks_repo_name: gooaah/GraphINVENT


text:
import numpy
import score_calc
import json

def load_labels(file_path):
    file = open(file_path, 'r')
    lines = file.readlines()

    feature_labels = []

    for line in lines:
        feature_labels.append(line.strip())

    return feature_labels

def load_data(file_path):
    data = numpy.loadtxt(file_path, delimiter=',')
    return data

def calc_scores(features, n):
    labels = []
    standardized = []
    non_standardized = []

    labels.append('Laplacian Score')
    result_lap_score = score_calc.calc_lap_score(features)
    min_lap_score =  numpy.min(result_lap_score)
    range_lap_score = numpy.ptp(result_lap_score)
    result_lap_score_standardized = (1 - (result_lap_score - min_lap_score) / range_lap_score)
    standardized.append(result_lap_score_standardized)
    non_standardized.append(result_lap_score)

    labels.append('MCFS')
    result_mcfs = score_calc.calc_MCFS(features, n)
    min_mcfs =  numpy.min(result_mcfs)
    range_mcfs = numpy.ptp(result_mcfs)
    result_mcfs_standardized = (result_mcfs - min_mcfs) / range_mcfs
    standardized.append(result_mcfs_standardized)
    non_standardized.append(result_mcfs)

    labels.append('NDFS')
    result_ndfs = score_calc.calc_NDFS(features)
    min_ndfs =  numpy.min(result_ndfs)
    range_ndfs = numpy.ptp(result_ndfs)
    result_ndfs_standardized = 1 - (result_ndfs - min_ndfs) / range_ndfs
    standardized.append(result_ndfs_standardized)
    non_standardized.append(result_ndfs)

    labels.append('SPEC')
    result_spec = score_calc.calc_SPEC(features)
    min_spec =  numpy.min(result_spec)
    range_spec = numpy.ptp(result_spec)
    # result_spec_standardized = 1 - (result_spec - min_spec) / range_spec
    result_spec_standardized = (result_spec - min_spec) / range_spec
    standardized.append(result_spec_standardized)
    non_standardized.append(result_spec)

    labels.append('UDFS')
    result_udfs = score_calc.calc_UDFS(features)
    min_udfs =  numpy.min(result_udfs)
    range_udfs = numpy.ptp(result_udfs)
    result_udfs_standardized = (result_udfs - min_udfs) / range_udfs
    standardized.append(result_udfs_standardized)
    non_standardized.append(result_udfs)

    return standardized, non_standardized, labels

def generate_dict(feature_labels, score_labels, standardized, non_standardized, title=''):
    values_keys = list(score_labels)
    values_keys.insert(0, 'name')

    json = []
    values = [[0 for x in range(len(score_labels))] for y in range(len(feature_labels))]

    for k in  range(0, len(feature_labels), 1):
        json_element = {}
        json_element['name'] = feature_labels[k]
        for i in range(0, len(standardized), 1):
            data = standardized[i].tolist()
            values[k][i] = data[k]
            json_element[score_labels[i]] = non_standardized[i][k]
        json.append(json_element)

    result_dict = {}
    result_dict['scoreLabels'] = score_labels
    result_dict['featureLabels'] = feature_labels
    result_dict['title'] = title
    result_dict['values'] = values
    result_dict['json'] = json

    return result_dict

def save_as_json(dict_data, file_path):
    file = open(file_path, 'w')
    json.dump(dict_data, file, sort_keys=False, indent=4)

max_stars_repo_name: rafaelssantos/spas

max_issues_repo_name: rafaelssantos/spas

max_forks_repo_name: rafaelssantos/spas


text:
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.utils.data import Dataset, DataLoader
import pandas as pd
import SimpleITK as sitk
from skimage.measure import label, regionprops
import math
import pdb

class CMRDataset(Dataset):
    def __init__(self, dataset_dir, mode='train', domain='A', crop_size=256, scale=0.1, rotate=10, debug=False):

        self.mode = mode
        self.dataset_dir = dataset_dir
        self.crop_size = crop_size
        self.scale = scale
        self.rotate = rotate

        if self.mode == 'train':
            pre_face = 'Training'
            if 'C' in domain or 'D' in domain:
                print('No domain C or D in Training set')
                raise StandardError

        elif self.mode == 'test':
            pre_face = 'Testing'

        else:
            print('Wrong mode')
            raise StandardError
        if debug:
            # validation set is the smallest, need the shortest time for load data.
           pre_face = 'Testing'

        path = self.dataset_dir + pre_face + '/'
        print('start loading data')

        name_list = []

        if 'A' in domain:
            df = pd.read_csv(self.dataset_dir+pre_face+'_A.csv')
            name_list += np.array(df['name']).tolist()
        if 'B' in domain:
            df = pd.read_csv(self.dataset_dir+pre_face+'_B.csv')
            name_list += np.array(df['name']).tolist()
        if 'C' in domain:
            df = pd.read_csv(self.dataset_dir+pre_face+'_C.csv')
            name_list += np.array(df['name']).tolist()
        if 'D' in domain:
            df = pd.read_csv(self.dataset_dir+pre_face+'_D.csv')
            name_list += np.array(df['name']).tolist()

        img_list = []
        lab_list = []
        spacing_list = []

        for name in name_list:
            for name_idx in os.listdir(path+name):
                if 'gt' in name_idx:
                    continue
                else:
                    idx = name_idx.split('_')[2].split('.')[0]

                    itk_img = sitk.ReadImage(path+name+'/%s_sa_%s.nii.gz'%(name, idx))
                    itk_lab = sitk.ReadImage(path+name+'/%s_sa_gt_%s.nii.gz'%(name, idx))

                    spacing = np.array(itk_lab.GetSpacing()).tolist()
                    spacing_list.append(spacing[::-1])

                    assert itk_img.GetSize() == itk_lab.GetSize()
                    img, lab = self.preprocess(itk_img, itk_lab)

                    img_list.append(img)
                    lab_list.append(lab)

        self.img_slice_list = []
        self.lab_slice_list = []
        if self.mode == 'train':
            for i in range(len(img_list)):
                tmp_img = img_list[i]
                tmp_lab = lab_list[i]

                z, x, y = tmp_img.shape

                for j in range(z):
                    self.img_slice_list.append(tmp_img[j])
                    self.lab_slice_list.append(tmp_lab[j])

        else:
            self.img_slice_list = img_list
            self.lab_slice_list = lab_list
            self.spacing_list = spacing_list

        print('load done, length of dataset:', len(self.img_slice_list))

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

    def preprocess(self, itk_img, itk_lab):

        img = sitk.GetArrayFromImage(itk_img)
        lab = sitk.GetArrayFromImage(itk_lab)

        max98 = np.percentile(img, 98)
        img = np.clip(img, 0, max98)

        z, y, x = img.shape
        if x < self.crop_size:
            diff = (self.crop_size + 10 - x) // 2
            img = np.pad(img, ((0,0), (0,0), (diff, diff)))
            lab = np.pad(lab, ((0,0), (0,0), (diff,diff)))
        if y < self.crop_size:
            diff = (self.crop_size + 10 -y) // 2
            img = np.pad(img, ((0,0), (diff, diff), (0,0)))
            lab = np.pad(lab, ((0,0), (diff, diff), (0,0)))

        img = img / max98
        tensor_img = torch.from_numpy(img).float()
        tensor_lab = torch.from_numpy(lab).long()

        return tensor_img, tensor_lab

    def __getitem__(self, idx):
        tensor_image = self.img_slice_list[idx]
        tensor_label = self.lab_slice_list[idx]

        if self.mode == 'train':
            tensor_image = tensor_image.unsqueeze(0).unsqueeze(0)
            tensor_label = tensor_label.unsqueeze(0).unsqueeze(0)

            # Gaussian Noise
            tensor_image += torch.randn(tensor_image.shape) * 0.02
            # Additive brightness
            rnd_bn = np.random.normal(0, 0.7)#0.03
            tensor_image += rnd_bn
            # gamma
            minm = tensor_image.min()
            rng = tensor_image.max() - minm
            gamma = np.random.uniform(0.5, 1.6)
            tensor_image = torch.pow((tensor_image-minm)/rng, gamma)*rng + minm

            tensor_image, tensor_label = self.random_zoom_rotate(tensor_image, tensor_label)
            tensor_image, tensor_label = self.randcrop(tensor_image, tensor_label)
        else:
            tensor_image, tensor_label = self.center_crop(tensor_image, tensor_label)

        assert tensor_image.shape == tensor_label.shape

        if self.mode == 'train':
            return tensor_image, tensor_label
        else:
            return tensor_image, tensor_label, np.array(self.spacing_list[idx])

    def randcrop(self, img, label):
        _, _, H, W = img.shape

        diff_H = H - self.crop_size
        diff_W = W - self.crop_size

        rand_x = np.random.randint(0, diff_H)
        rand_y = np.random.randint(0, diff_W)

        croped_img = img[0, :, rand_x:rand_x+self.crop_size, rand_y:rand_y+self.crop_size]
        croped_lab = label[0, :, rand_x:rand_x+self.crop_size, rand_y:rand_y+self.crop_size]

        return croped_img, croped_lab

    def center_crop(self, img, label):
        D, H, W = img.shape

        diff_H = H - self.crop_size
        diff_W = W - self.crop_size

        rand_x = diff_H // 2
        rand_y = diff_W // 2

        croped_img = img[:, rand_x:rand_x+self.crop_size, rand_y:rand_y+self.crop_size]
        croped_lab = label[:, rand_x:rand_x+self.crop_size, rand_y:rand_y+self.crop_size]

        return croped_img, croped_lab

    def random_zoom_rotate(self, img, label):
        scale_x = np.random.random() * 2 * self.scale + (1 - self.scale)
        scale_y = np.random.random() * 2 * self.scale + (1 - self.scale)

        theta_scale = torch.tensor([[scale_x, 0, 0],
                                    [0, scale_y, 0],
                                    [0, 0, 1]]).float()
        angle = (float(np.random.randint(-self.rotate, self.rotate)) / 180.) * math.pi

        theta_rotate = torch.tensor( [  [math.cos(angle), -math.sin(angle), 0],
                                        [math.sin(angle), math.cos(angle), 0],
                                        ]).float()

        theta_rotate = theta_rotate.unsqueeze(0)
        grid = F.affine_grid(theta_rotate, img.size())
        img = F.grid_sample(img, grid, mode='bilinear')
        label = F.grid_sample(label.float(), grid, mode='nearest').long()

        return img, label

max_stars_repo_name: yhygao/UTNet


text:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))
if (np.dot(a, b)):
    print('No')
else:
    print('Yes')

max_stars_repo_name: KoukiNAGATA/c-

max_issues_repo_name: KoukiNAGATA/c-

max_forks_repo_name: KoukiNAGATA/c-


text:
import numpy as np
import tensorflow as tf
from dnc.controller import BaseController

"""
A 1-layer LSTM recurrent neural network with 256 hidden units
Note: the state of the LSTM is not saved in a variable because we want
the state to reset to zero on every input sequence
"""

class RecurrentController(BaseController):

    def network_vars(self):
        self.lstm_cell = tf.compat.v1.nn.rnn_cell.BasicLSTMCell(256)
        self.state = self.lstm_cell.zero_state(self.batch_size, tf.float32)

    def network_op(self, X, state):
        X = tf.convert_to_tensor(value=X)
        return self.lstm_cell(X, state)

    def get_state(self):
        return self.state

    @staticmethod
    def update_state(new_state):
        return tf.no_op()

max_stars_repo_name: egoetz/DNC-tensorflow

max_issues_repo_name: egoetz/DNC-tensorflow

max_forks_repo_name: egoetz/DNC-tensorflow


text:
#%% NEWWWWW
from uWebSockets import Server
import numpy as np
import msgpack
from turbojpeg import TurboJPEG
import time
jpeg = TurboJPEG()

s = Server(7777)
s.run()

npts = 500

dataPlotly = {
    'figure': {
        'x': np.random.rand(1, npts).flatten().tolist(),
        'y': np.random.rand(1, npts).flatten().tolist(),
        'z': np.random.rand(1, npts).flatten().tolist(),
        'mode': 'markers',
        'marker': {
            'size': 12,
            'line': {
                'color': 'rgba(217, 217, 217, 0.14)',
                'width': 0.5
                },
            'opacity': 0.5
        },
        'type': 'scatter3d',
        'showscale': False
    }
}

s.sendStringAsBinary(msgpack.packb(dataPlotly))

dataPlotlyConfig = {
    'figureConfiguration': {
            'scene': {
                'xaxis': {'range': [-1, 1]},
                'yaxis': {'range': [-2, 2]},
                'zaxis': {'range': [-3, 3]},
                'aspectmode':'manual',
                'aspectratio':{
                    'x': 1,
                    'y': 1,
                    'z': 1,
                }
            }
    }
}
s.sendStringAsBinary(msgpack.packb(dataPlotlyConfig))

dataFigureAndConfig = {
'figure': {
        'x': np.random.rand(1, npts).flatten().tolist(),
        'y': np.random.rand(1, npts).flatten().tolist(),
        'z': np.random.rand(1, npts).flatten().tolist(),
        'mode': 'markers',
        'marker': {
            'size': 12,
            'line': {
                'color': 'rgba(217, 217, 217, 0.14)',
                'width': 0.5
                },
            'opacity': 0.5
        },
        'type': 'scatter3d',
        'showscale': False
    },
'figureConfiguration': {
            'scene': {
                'xaxis': {'range': [-1, 1]},
                'yaxis': {'range': [-2, 2]},
                'zaxis': {'range': [-3, 3]},
                'aspectmode':'manual',
                'aspectratio':{
                    'x': 1,
                    'y': 1,
                    'z': 1,
                }
            }
    }
}
s.sendStringAsBinary(msgpack.packb(dataFigureAndConfig))

for _ in range(100):
    img = ((np.random.rand(1000, 1000, 3)) * 256).astype(np.uint8)
    dataImage = {
        'image': jpeg.encode(img, quality=80)
    }

    s.sendStringAsBinary(msgpack.packb(dataImage))
    time.sleep(0.01)

import numpy as np
import msgpack
from turbojpeg import TurboJPEG
import time
from webplot.webplot import webplot

wp = webplot()
for _ in range(100):
    img = ((np.random.rand(1000, 1000, 3)) * 256).astype(np.uint8)
    wp.show(img)

max_stars_repo_name: evgenyslab/remoteImageVisualizer

max_issues_repo_name: evgenyslab/remoteImageVisualizer

max_forks_repo_name: evgenyslab/remoteImageVisualizer