text:
import numpy as np
import os
import scipy.ndimage

inDir = "ADD: path to downloaded and extracted scalarflow directory"
sims = 100

outDir = "data/128_test/scalarflow_step13/"
size = 128
timeStep = 13 #1,5,10,13
timeStart = 150 - 11*timeStep

for sim in range(sims):
    print("scalarflow/sim_%d" % sim)
    outPath = os.path.join(outDir, "sim_%06d" % sim)
    if not os.path.isdir(outPath):
        os.makedirs(outPath)

    results = []
    for i in range(11):
        loadPath = os.path.join(inDir, "sim_%06d/reconstruction/velocity_%06d.npz" % (sim, timeStart + i*timeStep))
        result = np.load(loadPath)['data']
        result = result[:,18:178,:,:] #cut off inflow

        zoom = [size/result.shape[0], size/result.shape[1], size/result.shape[2], 1]
        results += [ scipy.ndimage.zoom(result, zoom, order=1) ]

    outFile = os.path.join(outPath, "velocity_000000.npz")
    np.savez_compressed(outFile, np.stack(results, axis=0))

print("Done")

ext: py

max_stars_repo_name: tum-pbs/VOLSIM

max_issues_repo_name: tum-pbs/VOLSIM

max_forks_repo_name: tum-pbs/VOLSIM

reason: import numpy,import scipy


text:
#!/usr/bin/env python

import logging
from itertools import combinations

import networkx as nx

from pgmpy.estimators import StructureEstimator, ScoreCache
from pgmpy.estimators import K2Score
from pgmpy.utils.mathext import powerset
from pgmpy.base import DAG

class ExhaustiveSearch(StructureEstimator):
    def __init__(self, data, scoring_method=None, use_cache=True, **kwargs):
        """
        Search class for exhaustive searches over all DAGs with a given set of variables.
        Takes a `StructureScore`-Instance as parameter; `estimate` finds the model with maximal score.

        Parameters
        ----------
        data: pandas DataFrame object
            dataframe object where each column represents one variable.
            (If some values in the data are missing the data cells should be set to `numpy.NaN`.
            Note that pandas converts each column containing `numpy.NaN`s to dtype `float`.)

        scoring_method: Instance of a `StructureScore`-subclass (`K2Score` is used as default)
            An instance of `K2Score`, `BDeuScore`, or `BicScore`.
            This score is optimized during structure estimation by the `estimate`-method.

        state_names: dict (optional)
            A dict indicating, for each variable, the discrete set of states (or values)
            that the variable can take. If unspecified, the observed values in the data set
            are taken to be the only possible states.

        use_caching: boolean
            If True, uses caching of score for faster computation.
            Note: Caching only works for scoring methods which are decomposible. Can
            give wrong results in case of custom scoring methods.

        complete_samples_only: bool (optional, default `True`)
            Specifies how to deal with missing data, if present. If set to `True` all rows
            that contain `np.Nan` somewhere are ignored. If `False` then, for each variable,
            every row where neither the variable nor its parents are `np.NaN` is used.
            This sets the behavior of the `state_count`-method.
        """
        if scoring_method is not None:
            if use_cache:
                self.scoring_method = ScoreCache.ScoreCache(scoring_method, data)
            else:
                self.scoring_method = scoring_method
        else:
            self.scoring_method = ScoreCache.ScoreCache(K2Score(data, **kwargs), data)

        super(ExhaustiveSearch, self).__init__(data, **kwargs)

    def all_dags(self, nodes=None):
        """
        Computes all possible directed acyclic graphs with a given set of nodes,
        sparse ones first. `2**(n*(n-1))` graphs need to be searched, given `n` nodes,
        so this is likely not feasible for n>6. This is a generator.

        Parameters
        ----------
        nodes: list of nodes for the DAGs (optional)
            A list of the node names that the generated DAGs should have.
            If not provided, nodes are taken from data.

        Returns
        -------
        dags: Generator object for nx.DiGraphs
            Generator that yields all acyclic nx.DiGraphs, ordered by number of edges. Empty DAG first.

        Examples
        --------
        >>> import pandas as pd
        >>> from pgmpy.estimators import ExhaustiveSearch
        >>> s = ExhaustiveSearch(pd.DataFrame(data={'Temperature': [23, 19],
                                                    'Weather': ['sunny', 'cloudy'],
                                                    'Humidity': [65, 75]}))
        >>> list(s.all_dags())
        [<networkx.classes.digraph.DiGraph object at 0x7f6955216438>,
         <networkx.classes.digraph.DiGraph object at 0x7f6955216518>,
        ....
        >>> [dag.edges() for dag in s.all_dags()]
        [[], [('Humidity', 'Temperature')], [('Humidity', 'Weather')],
        [('Temperature', 'Weather')], [('Temperature', 'Humidity')],
        ....
        [('Weather', 'Humidity'), ('Weather', 'Temperature'), ('Temperature', 'Humidity')]]

        """
        if nodes is None:
            nodes = sorted(self.state_names.keys())
        if len(nodes) > 6:
            logging.info("Generating all DAGs of n nodes likely not feasible for n>6!")
            logging.info(
                "Attempting to search through {n} graphs".format(
                    n=2 ** (len(nodes) * (len(nodes) - 1))
                )
            )

        edges = list(combinations(nodes, 2))  # n*(n-1) possible directed edges
        edges.extend([(y, x) for x, y in edges])
        all_graphs = powerset(edges)  # 2^(n*(n-1)) graphs

        for graph_edges in all_graphs:
            graph = nx.DiGraph(graph_edges)
            graph.add_nodes_from(nodes)
            if nx.is_directed_acyclic_graph(graph):
                yield graph

    def all_scores(self):
        """
        Computes a list of DAGs and their structure scores, ordered by score.

        Returns
        -------
        list: a list of (score, dag) pairs
            A list of (score, dag)-tuples, where score is a float and model a acyclic nx.DiGraph.
            The list is ordered by score values.

        Examples
        --------
        >>> import pandas as pd
        >>> import numpy as np
        >>> from pgmpy.estimators import ExhaustiveSearch, K2Score
        >>> # create random data sample with 3 variables, where B and C are identical:
        >>> data = pd.DataFrame(np.random.randint(0, 5, size=(5000, 2)), columns=list('AB'))
        >>> data['C'] = data['B']
        >>> searcher = ExhaustiveSearch(data, scoring_method=K2Score(data))
        >>> for score, model in searcher.all_scores():
        ...   print("{0}\t{1}".format(score, model.edges()))
        -24234.44977974726      [('A', 'B'), ('A', 'C')]
        -24234.449760691063     [('A', 'B'), ('C', 'A')]
        -24234.449760691063     [('A', 'C'), ('B', 'A')]
        -24203.700955937973     [('A', 'B')]
        -24203.700955937973     [('A', 'C')]
        -24203.700936881774     [('B', 'A')]
        -24203.700936881774     [('C', 'A')]
        -24203.700936881774     [('B', 'A'), ('C', 'A')]
        -24172.952132128685     []
        -16597.30920265254      [('A', 'B'), ('A', 'C'), ('B', 'C')]
        -16597.30920265254      [('A', 'B'), ('A', 'C'), ('C', 'B')]
        -16597.309183596342     [('A', 'B'), ('C', 'A'), ('C', 'B')]
        -16597.309183596342     [('A', 'C'), ('B', 'A'), ('B', 'C')]
        -16566.560378843253     [('A', 'B'), ('C', 'B')]
        -16566.560378843253     [('A', 'C'), ('B', 'C')]
        -16268.324549347722     [('A', 'B'), ('B', 'C')]
        -16268.324549347722     [('A', 'C'), ('C', 'B')]
        -16268.324530291524     [('B', 'A'), ('B', 'C')]
        -16268.324530291524     [('B', 'C'), ('C', 'A')]
        -16268.324530291524     [('B', 'A'), ('C', 'B')]
        -16268.324530291524     [('C', 'A'), ('C', 'B')]
        -16268.324530291524     [('B', 'A'), ('B', 'C'), ('C', 'A')]
        -16268.324530291524     [('B', 'A'), ('C', 'A'), ('C', 'B')]
        -16237.575725538434     [('B', 'C')]
        -16237.575725538434     [('C', 'B')]
        """

        scored_dags = sorted(
            [(self.scoring_method.score(dag), dag) for dag in self.all_dags()],
            key=lambda x: x[0],
        )
        return scored_dags

    def estimate(self):
        """
        Estimates the `DAG` structure that fits best to the given data set,
        according to the scoring method supplied in the constructor.
        Exhaustively searches through all models. Only estimates network structure, no parametrization.

        Returns
        -------
        model: `DAG` instance
            A `DAG` with maximal score.

        Examples
        --------
        >>> import pandas as pd
        >>> import numpy as np
        >>> from pgmpy.estimators import ExhaustiveSearch
        >>> # create random data sample with 3 variables, where B and C are identical:
        >>> data = pd.DataFrame(np.random.randint(0, 5, size=(5000, 2)), columns=list('AB'))
        >>> data['C'] = data['B']
        >>> est = ExhaustiveSearch(data)
        >>> best_model = est.estimate()
        >>> best_model
        <pgmpy.base.DAG.DAG object at 0x7f695c535470>
        >>> best_model.edges()
        [('B', 'C')]
        """

        best_dag = max(self.all_dags(), key=self.scoring_method.score)

        best_model = DAG()
        best_model.add_nodes_from(sorted(best_dag.nodes()))
        best_model.add_edges_from(sorted(best_dag.edges()))
        return best_model

max_stars_repo_name: akleinau/pgmpy

max_issues_repo_name: akleinau/pgmpy

max_forks_repo_name: akleinau/pgmpy

reason: import numpy,import networkx


text:
#Source: https://www.kaggle.com/yasufuminakama/panda-pytorch-grad-cam

import matplotlib.pyplot as plt
import torch.nn.functional as F
import numpy as np
import torch
import torch.nn as nn
from torch.optim import Adam, SGD
import torchvision.models as models
from torch.nn.parameter import Parameter
import cv2
import PIL
print(PIL.__version__)

class SaveFeatures():
    """ Extract pretrained activations"""
    features = None
    def __init__(self, m):
        self.hook = m.register_forward_hook(self.hook_fn)
    def hook_fn(self, module, input, output):
        self.features = ((output.cpu()).data).numpy()
    def remove(self):
        self.hook.remove()

def getCAM(feature_conv, weight_fc, class_idx):
    _, nc, h, w = feature_conv.shape
    cam = weight_fc[class_idx].dot(feature_conv[0,:, :, ].reshape((nc, h*w)))
    cam = cam.reshape(h, w)
    cam = cam - np.min(cam)
    cam_img = cam / np.max(cam)
    return cam_img

def plotGradCAM(model, final_conv, fc_params, train_loader,
                row=1, col=8, img_size=256, device='cpu', original=False):
    for param in model.parameters():
        param.requires_grad = False
    model.to(device)
    model.eval()
    # save activated_features from conv
    activated_features = SaveFeatures(final_conv)
    # save weight from fc
    weight = np.squeeze(fc_params[0].cpu().data.numpy())
    # original images
    if original:
        fig = plt.figure(figsize=(20, 15))
        for i, (img, target, org_img) in enumerate(train_loader):
            output = model(img.to(device))
            pred_idx = output.to('cpu').numpy().argmax(1)
            cur_images = org_img.numpy().transpose((0, 2, 3, 1))
            ax = fig.add_subplot(row, col, i+1, xticks=[], yticks=[])
            plt.imshow(cv2.cvtColor(cur_images[0], cv2.COLOR_BGR2RGB))
            ax.set_title('Label:%d, Predict:%d' % (target, pred_idx), fontsize=14)
            if i == row*col-1:
                break
        plt.show()
    # heatmap images
    fig = plt.figure(figsize=(20, 15))
    for i, (img, target, _) in enumerate(train_loader):
        output = model(img.to(device))
        pred_idx = output.to('cpu').numpy().argmax(1)
        cur_images = img.cpu().numpy().transpose((0, 2, 3, 1))
        heatmap = getCAM(activated_features.features, weight, pred_idx)
        ax = fig.add_subplot(row, col, i+1, xticks=[], yticks=[])
        plt.imshow(cv2.cvtColor(cur_images[0], cv2.COLOR_BGR2RGB))
        plt.imshow(cv2.resize(heatmap, (img_size, img_size), interpolation=cv2.INTER_LINEAR), alpha=0.4, cmap='jet')
        ax.set_title('Label:%d, Predict:%d' % (target, pred_idx), fontsize=14)
        if i == row*col-1:
            break
    plt.show()

reason: import numpy


text:
# # Notation: Converter Numeric

import mechkit
import numpy as np
import sympy as sp
import itertools

np.set_printoptions(
    linewidth=140,
    precision=3,
    # suppress=False,
)
converter = mechkit.notation.Converter()

# ### Mandel6: symmetric, Mandel9: Mandel6 + asymmetric parts
basics = mechkit.tensors.Basic()

# Symmetric identity
print(converter.to_mandel6(basics.I4s))
print(converter.to_mandel9(basics.I4s))

# Asymmetric identity
print(converter.to_mandel6(basics.I4a))
print(converter.to_mandel9(basics.I4a))

# ### Convenient autodetection of the notation: Specify only target

tensor = np.ones((3, 3, 3, 3))
mandel6 = converter.to_mandel6(tensor)
# Pass through
print(converter.to_tensor(mandel6))
print(converter.to_mandel6(mandel6))
print(converter.to_mandel9(mandel6))

# ### Vectorized explicit converter

expl_converter = mechkit.notation.ExplicitConverter()

tensors = np.ones((2, 5, 3, 3, 3, 3))  # We have 5 times 2 tensors of fourth order

mandel6s = expl_converter.convert(
    inp=tensors, source="tensor", target="mandel6", quantity="stiffness"
)

for i in range(2):
    for j in range(5):
        print(f"Tensor at position {i}, {j} in Mandel6 notation")
        print(mandel6s[i, j, ...])
        print()

max_stars_repo_name: jonashund/mechkit

max_issues_repo_name: jonashund/mechkit

max_forks_repo_name: jonashund/mechkit

reason: import numpy,import sympy


text:
import argparse
import numpy as np
import os
import torch
import time
import cv2
from data import collate_fn, CorrespondencesDataset
from evaluation import eval_decompose, compute_metric

def str2bool(v):
    return v.lower() in ("true", "1")
# Parse command line arguments.
parser = argparse.ArgumentParser(description='eval essential matrix')
parser.add_argument('--data_te', type=str, default='../../data_dump/yfcc-sift-2000-test.hdf5',
  help='datasets path.')
parser.add_argument('--ratio_test_th', type=float, default=0.8,
  help='ratio test threshold')
parser.add_argument("--ransac_type", type=int, default=2, help=""
    "0:opencv ransac, 1:magsac, 2:gcransac")
parser.add_argument("--ransac_th", type=float, default=0.00025, help=""
    "threshold of ransac. 0.00025 for gcransac")
parser.add_argument("--obj_geod_th", type=float, default=1e-4, help=""
    "theshold for the good geodesic distance")

config = parser.parse_args()

def eval(data_loader, config):
    loader_iter = iter(data_loader)
    metrics = ['err_q', 'err_t', 'precision', 'recall', 'fscore']
    eval_res= {}
    t_net = 0
    t_est = 0

    for met in metrics:
        eval_res[met] = []
    for idx, test_data in enumerate(loader_iter):
        test_xs = test_data['xs'][0].numpy()
        x1, x2 = test_xs[0,:,:2], test_xs[0,:,2:4]
        K1, K2 = test_data['K1s'][0].numpy(), test_data['K2s'][0].numpy()
        dR, dt = test_data['Rs'][0].numpy(), test_data['ts'][0].numpy()
        y = test_data['ys'][0].numpy()
        gt = y[:,0] < config.obj_geod_th
        mask = np.logical_and((test_data['sides'][0,:,0].numpy() < config.ratio_test_th),
                              (test_data['sides'][0,:,1].numpy().astype(bool)))

        err_q, err_t, loss_q, loss_t, inliers, mask_updated, R, t = eval_decompose(x1, x2, dR, dt, mask=mask, ransac_type=config.ransac_type, ransac_th=config.ransac_th, K1=K1, K2=K2)
        precision = np.logical_and(mask_updated, gt).sum() / mask_updated.sum()
        recall = np.logical_and(mask_updated, gt).sum() / gt.sum()
        fscore = 2*precision*recall/(precision+recall+1e-15)
        print('idx '+str(idx)+' err_q '+str(err_q)+' err_t '+str(err_t)+' precision '+str(precision)+' recall '+str(recall)+' fscore '+str(fscore))
        eval_res['err_q'].append(err_q)
        eval_res['err_t'].append(err_t)
        eval_res['precision'].append(precision)
        eval_res['recall'].append(recall)
        eval_res['fscore'].append(fscore)
    ths, q_acc, t_acc, qt_acc = compute_metric(eval_res)
    for idx_th in range(1, len(ths)):
        print(str(ths[idx_th])+'deg: '+str(np.mean(qt_acc[:idx_th])))
    print('precision: ' +str(np.mean(np.asarray(eval_res['precision']))))
    print('recall: ' +str(np.mean(np.asarray(eval_res['recall']))))
    print('fscore: ' +str(np.mean(np.asarray(eval_res['fscore']))))

test_dataset = CorrespondencesDataset(config.data_te)
test_loader = torch.utils.data.DataLoader(
        test_dataset, batch_size=1, shuffle=False,
        num_workers=8, pin_memory=False, collate_fn=collate_fn)

eval(test_loader, config)

max_stars_repo_name: zjhthu/graph-cut-ransac

max_issues_repo_name: zjhthu/graph-cut-ransac

max_forks_repo_name: zjhthu/graph-cut-ransac


text:
import numpy as np
from keras import backend as K
from keras.layers import Layer, Input, Activation
from keras.models import Model
from keras.optimizers import SGD
from keras.initializers import RandomNormal, Identity, Orthogonal, Zeros, Constant, Ones
from keras.callbacks import Callback
from sklearn.svm import LinearSVC
import argparse

class StopAtThreshold(Callback):

    def __init__(self,
                 monitor='loss',
                 threshold=0.001):
        super(StopAtThreshold, self).__init__()

        self.monitor = monitor
        self.threshold = threshold

    def on_epoch_end(self, epoch, logs=None):
        current = logs.get(self.monitor)
        if current is None:
            return

        if current < self.threshold:
            self.model.stop_training = True
            return

class RecordValues(Callback):

    def __init__(self, depth):
        self.depth = depth
        super(RecordValues, self).__init__()

    def on_epoch_end(self, epoch, logs=None):

        w = get_canonical_model(self.model, self.depth)
        w = np.abs(w)
        top_5_idx = np.argsort(w)[-5:]
        top_5_values = [w[i] for i in top_5_idx]
        logs['unnormalized_ev1'] = top_5_values[0]
        logs['unnormalized_ev2'] = top_5_values[1]
        logs['unnormalized_ev3'] = top_5_values[2]
        logs['unnormalized_ev4'] = top_5_values[3]
        logs['unnormalized_ev5'] = top_5_values[4]

        w /= np.linalg.norm(w)
        top_5_idx = np.argsort(w)[-5:]
        top_5_values = [w[i] for i in top_5_idx]
        logs['ev1'] = top_5_values[0]
        logs['ev2'] = top_5_values[1]
        logs['ev3'] = top_5_values[2]
        logs['ev4'] = top_5_values[3]
        logs['ev5'] = top_5_values[4]

        return

class Separator(Layer):

    def __init__(self, d, init, depth, **kwargs):
        """
        The initializer for the linear deparator class (neural network with diagonal weight matrices)
        :param d: the input dimension
        :param init: the scale of initialization (sigme_0)
        :param depth: the model's depth
        """
        self.d = d
        self.init = init
        self.depth = depth
        super(Separator, self).__init__(**kwargs)

    def build(self, input_shape):

        self.w_plus = self.add_weight(name='w_plus',
                                    shape=(self.d, ),
                                     initializer=Constant(init ** (1./self.depth)),
                                     trainable=True)

        self.w_minus = self.add_weight(name='w_minus',
                                    shape=(self.d, ),
                                     initializer=Constant(init ** (1./self.depth)),
                                     trainable=True)

        super(Separator, self).build(input_shape)

    def call(self, x, **kwargs):

        w_plus = K.pow(self.w_plus, self.depth)
        w_minus = K.pow(self.w_minus, self.depth)

        return K.dot(x, K.reshape(w_plus - w_minus, (-1,1)))

    def compute_output_shape(self, input_shape):
        return (input_shape[0], 1)

def get_canonical_model(model, depth):
    w_plus = model.get_weights()[0]
    w_minus = model.get_weights()[1]
    return w_plus**depth - w_minus**depth

def exp_loss(y_true, y_pred):
    return K.exp((1-2*y_true) * y_pred)

def parse_input():
    p = argparse.ArgumentParser()
    p.add_argument('lr', type=float, default=0.01)
    p.add_argument('init_size', type=float, default=0.01)
    p.add_argument('epochs', type=int, default=100000)
    p.add_argument('d', type=int, default=100)
    p.add_argument('data_size', type=int, default=200)
    p.add_argument('convergence_threshold', type=float, default=0.01)
    p.add_argument('loss', type=float, default=True, help="True for exponential loss, False for Logistic loss.")
    p.add_argument('verbosity', type=int, default=2)
    args = p.parse_args()
    return args

if __name__ == "__main__":

    args = parse_input()
    lr = args.lr
    epochs = args.epochs
    init = args.init_size
    d = args.d
    data_size = args.data_size
    threshold = args.convergence_threshold
    exponential_loss = args.loss
    verbosity = args.verbosity

    # generate the normalized sparse labeling function:
    Wstar = np.zeros((d,))
    Wstar[0] = 8
    Wstar[1] = 4
    Wstar[2] = 2
    Wstar[3] = 1
    Wstar /= np.linalg.norm(Wstar)

    # create the dataset:
    x_train = np.random.randn(data_size, d)
    logits = x_train @ Wstar
    y_train = np.sign(logits)
    y_train[y_train<0] = 0

    # create the models:
    i1 = Input((d,))
    out1 = Separator(d=d, init=init, depth=1)(i1)
    if exponential_loss:
        model1 = Model(inputs=i1, outputs=out1)
        model1.compile(SGD(lr), loss=exp_loss)
    else:
        out1 = Activation('sigmoid')(out1)
        model1 = Model(inputs=i1, outputs=out1)
        model1.compile(SGD(lr), loss='binary_crossentropy', metrics=['acc'])

    i2 = Input((d,))
    out2 = Separator(d=d, init=init, depth=2)(i2)
    if exponential_loss:
        model2 = Model(inputs=i2, outputs=out2)
        model2.compile(SGD(lr,), loss=exp_loss)
    else:
        out2 = Activation('sigmoid')(out2)
        model2 = Model(inputs=i2, outputs=out2)
        model2.compile(SGD(lr), loss='binary_crossentropy', metrics=['acc'])

    i3 = Input((d,))
    out3 = Separator(d=d, init=init, depth=3)(i3)
    if exponential_loss:
        model3 = Model(inputs=i3, outputs=out3)
        model3.compile(SGD(lr), loss=exp_loss)
    else:
        out3 = Activation('sigmoid')(out3)
        model3 = Model(inputs=i3, outputs=out3)
        model3.compile(SGD(lr), loss='binary_crossentropy', metrics=['acc'])

    i4 = Input((d,))
    out4 = Separator(d=d, init=init, depth=4)(i4)
    if exponential_loss:
        model4 = Model(inputs=i4, outputs=out4)
        model4.compile(SGD(lr), loss=exp_loss)
    else:
        out4 = Activation('sigmoid')(out4)
        model4 = Model(inputs=i4, outputs=out4)
        model4.compile(SGD(lr), loss='binary_crossentropy', metrics=['acc'])

    i5 = Input((d,))
    out5 = Separator(d=d, init=init, depth=5)(i5)
    if exponential_loss:
        model5 = Model(inputs=i5, outputs=out5)
        model5.compile(SGD(lr), loss=exp_loss)
    else:
        out5 = Activation('sigmoid')(out5)
        model5 = Model(inputs=i5, outputs=out5)
        model5.compile(SGD(lr), loss='binary_crossentropy', metrics=['acc'])

    model6 = LinearSVC()

    # train the models:
    print("Training Depth 1 Model...")
    hist1 = model1.fit(x=x_train, y=y_train,
              batch_size=data_size,
              epochs = epochs,
              shuffle=False,
              callbacks=[StopAtThreshold(threshold=threshold), RecordValues(depth=1)],
              verbose = verbosity)
    print("Training Depth 2 Model...")
    hist2 = model2.fit(x=x_train, y=y_train,
              batch_size=data_size,
              epochs = epochs,
              shuffle=False,
              callbacks=[StopAtThreshold(threshold=threshold), RecordValues(depth=2)],
              verbose = verbosity)
    print("Training Depth 3 Model...")
    hist3 = model3.fit(x=x_train, y=y_train,
              batch_size=data_size,
              epochs = epochs,
              shuffle=False,
              callbacks=[StopAtThreshold(threshold=threshold), RecordValues(depth=3)],
              verbose = verbosity)
    print("Training Depth 4 Model...")
    hist4 = model4.fit(x=x_train, y=y_train,
              batch_size=data_size,
              epochs = epochs,
              shuffle=False,
              callbacks=[StopAtThreshold(threshold=threshold), RecordValues(depth=4)],
              verbose = verbosity)
    print("Training Depth 5 Model...")
    hist5 = model5.fit(x=x_train, y=y_train,
              batch_size=data_size,
              epochs = epochs,
              shuffle=False,
              callbacks=[StopAtThreshold(threshold=threshold), RecordValues(depth=5)],
              verbose = verbosity)

    print("Training SVM Model...")
    model6.fit(x_train, y_train)

    # extract and normalize the final weight vectors:
    w1 = get_canonical_model(model1, 1)
    w2 = get_canonical_model(model2, 2)
    w3 = get_canonical_model(model3, 3)
    w4 = get_canonical_model(model4, 4)
    w5 = get_canonical_model(model5, 5)
    w6 = model6.coef_
    w1 /= np.linalg.norm(w1)
    w2 /= np.linalg.norm(w2)
    w3 /= np.linalg.norm(w3)
    w4 /= np.linalg.norm(w4)
    w5 /= np.linalg.norm(w5)
    w6 /= np.linalg.norm(w6)

    # print the correlation to the labeling function:
    print("W1 Correlation:  " + str(Wstar @ w1))
    print("W2 Correlation:  " + str(Wstar @ w2))
    print("W3 Correlation:  " + str(Wstar @ w3))
    print("W4 Correlation:  " + str(Wstar @ w4))
    print("W5 Correlation:  " + str(Wstar @ w5))
    print("SVM Correlation: " + str(Wstar @ w6.squeeze()))

max_stars_repo_name: dsgissin/Incremental-Learning

max_issues_repo_name: dsgissin/Incremental-Learning

max_forks_repo_name: dsgissin/Incremental-Learning


text:
from __future__ import division

import numpy as np

class Policy:

    def __init__(self,basis, num_theta, theta=None ):
        self.basis_function=basis
        self.actions = [0, 1, 2]

        self.num_theta=num_theta

        # uniform distribution of the actions

        self.theta_behavior= theta

        if theta is None:
            self.weights = np.random.uniform(-1.0, 1.0, size=(num_theta,))

        else:
            self.weights=theta

    def set_theta(self, theta):
        self.weights = (self.weights+theta)*0*5

    def behavior(self,state,action):
        prob=0.0
        if self.theta_behavior is None:
            self.theta_behavior = np.random.uniform(-1.0, 1.0, size=(self.num_theta,))

        vector_basis = self.basis_function.evaluate(state, action)
        return np.dot(vector_basis, self.theta_behavior)

    def q_value_function(self, state, action ):
        vector_basis = self.basis_function.evaluate(state,action)
        return np.dot(vector_basis,self.weights)

    def get_actions(self, state):

        q_state_action=[self.q_value_function(state,self.actions[i]) for i in range(len(self.actions))]
        q_state_action = np.reshape(q_state_action,[len(q_state_action),1])# convert to column vector

        index = np.argmax(q_state_action)
        q_max = q_state_action[index]

        best_actions = [self.actions[index]]
        ind =[index]

        for i in range(len(q_state_action)):
            if q_state_action[i]==q_max and index!=i:
                best_actions.append(self.actions[i])
                ind.append(i)

        return best_actions


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np

def point_distance(a, b):
    """Возвращает список расстояний между двумя массивами точек"""
    return np.linalg.norm(a - b, axis=1)

def route_distance(cities):
    """Возвращает длинну маршрута"""
    points = cities[['x', 'y']]
    return np.sum(point_distance(points, np.roll(points, 1, axis=0)))


text:
from __future__ import print_function, division

import sys
import time
import torch
from helper.util import plot_tensor
from .util import AverageMeter, accuracy
import os
import numpy as np

def train_vanilla(epoch, train_loader, model, criterion, optimizer, opt, warmup_scheduler):
    device = opt.device
    """vanilla training"""
    model.train()

    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    end = time.time()
    for idx, (input, target) in enumerate(train_loader):

        if epoch < 5 + 1:
            warmup_scheduler.step()

        data_time.update(time.time() - end)

        input = input.float()
        input = input.to(device)
        target = target.to(device)

        # ===================forward=====================
        output = model(input)
        loss = criterion(output, target)

        acc1, acc5 = accuracy(output, target, topk=(1, 5))
        losses.update(loss.item(), input.size(0))
        top1.update(acc1[0], input.size(0))
        top5.update(acc5[0], input.size(0))

        # ===================backward=====================
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # ===================meters=====================
        batch_time.update(time.time() - end)
        end = time.time()

        # tensorboard logger
        pass

        # print info
        if idx % opt.print_freq == 0:
            print('Epoch: [{0}][{1}/{2}]\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                  'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                  'Acc@1 {top1.val:.3f} ({top1.avg:.3f})\t'
                  'Acc@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
                epoch, idx, len(train_loader), batch_time=batch_time,
                data_time=data_time, loss=losses, top1=top1, top5=top5))
            sys.stdout.flush()

    print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'
          .format(top1=top1, top5=top5))

    return top1.avg, losses.avg

def convert_time(seconds):
    seconds = seconds % (24 * 3600)
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60
    return [hour, minutes, seconds]

def train_distill(epoch, train_loader, val_loader, module_list, criterion_list, optimizer, opt, best_acc, logger,
                  device, warmup_scheduler, total_t):
    t_0 = time.time()

    """One epoch distillation"""
    # set modules as train()
    for module in module_list:
        module.train()
    # set teacher as eval()
    module_list[-1].eval()

    if opt.distill == 'abound':
        module_list[1].eval()
    elif opt.distill == 'factor':
        module_list[2].eval()

    criterion_cls = criterion_list[0]
    criterion_div = criterion_list[1]
    criterion_kd = criterion_list[2]

    model_s = module_list[0]
    model_t = module_list[-1]

    batch_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()
    xentm = AverageMeter()
    kdm = AverageMeter()
    otherm = AverageMeter()

    end = time.time()

    t_data = time.time()

    ag_time = 0
    for idx, data_combined in enumerate(train_loader):

        ag_time += time.time() - t_data

        if epoch < opt.epochs_warmup + 1:
            warmup_scheduler.step()

        model_s.train()
        model_t.eval()

        if opt.aug_type is None:
            data = data_combined
        else:
            data = data_combined[0]
            data_aug = data_combined[1]

        if opt.distill in ['crd']:
            input, target, index, contrast_idx = data
        else:
            input, target, index = data
            if opt.aug_type is not None and opt.aug_type != 'cutmix':
                input_aug = data_aug[0]

        input = input.float()
        input = input.to(device)
        target = target.to(device)
        index = index.to(device)
        bs = input.size(0)

        if opt.distill in ['crd']:
            contrast_idx = contrast_idx.to(device)

        if opt.aug_type is not None:
            # construct augmentation samples using mixup or cropmix
            if opt.aug_type == 'mixup':
                input_aug = input_aug.to(device)
                # shift samples in the batch to make pairs
                idx_aug = torch.arange(bs)
                idx_aug[0:bs - 1] = idx_aug[1:bs].clone()
                idx_aug[-1] = 0
                input_aug_b = input_aug[idx_aug]
                if opt.aug_lambda > 0:
                    # compute mixup samples using fixed lambda
                    input_aug = opt.aug_lambda * input_aug + (1 - opt.aug_lambda) * input_aug_b
                elif opt.aug_lambda == -1:
                    # compute mixup samples using the beta distribution
                    lambda_aug = np.random.beta(opt.aug_alpha, opt.aug_alpha, size=[bs, 1, 1, 1])
                    lambda_aug = torch.from_numpy(lambda_aug).type(torch.FloatTensor).to(opt.device)
                    input_aug = lambda_aug * input_aug + (1 - lambda_aug) * input_aug_b
            elif opt.aug_type == 'cutmix':
                input_aug = data_aug[0]
                mask = data_aug[2].view(bs, 1, 32, 32)
                input_aug, mask = input_aug.to(device), mask.to(device)
                # shift samples in the batch to make pairs
                idx_aug = torch.arange(bs)
                idx_aug[0:bs - 1] = idx_aug[1:bs].clone()
                idx_aug[-1] = 0
                input_aug_b = input_aug[idx_aug]

                input_aug = mask * input_aug + (1 - mask) * input_aug_b
                # for i in range(10):
                #     plot_tensor([input_aug[i], mask[i]])
            input_aug = input_aug.to(device)

        # ===================forward=====================
        preact = False
        if opt.distill in ['abound']:
            preact = True
        feat_s, logit_s = model_s(input, is_feat=True, preact=preact)

        # make training faster when there is no need to the prediction of the teacher for nat samples
        if not (opt.distill in ['kd'] and opt.alpha == 0):
            feat_t, logit_t = model_t(input, is_feat=True, preact=preact)
            feat_t = [f.detach() for f in feat_t]

        # compute the predicted label of the teacher for the augmented samples
        if opt.aug_type is not None:
            logit_aug_t = model_t(input_aug)
            logit_aug_s = model_s(input_aug)
            pred_lbl_t = logit_aug_t.argmax(1)

        # cls + kl div
        loss_cls_nat = criterion_cls(logit_s, target)

        loss_cls_aug = 0
        if opt.aug_type is not None:
            loss_cls_aug = criterion_cls(logit_aug_s, pred_lbl_t)

        loss_cls = loss_cls_nat + loss_cls_aug

        if opt.alpha > 0:
            # if opt.aug_type is not None:
            #     loss_div = criterion_div(logit_aug_s, logit_aug_t)
            # else:
            loss_div = criterion_div(logit_s, logit_t)
        else:
            loss_div = torch.zeros([1])
            loss_div = loss_div.to(device)

        # other kd beyond KL divergence
        if opt.distill == 'kd':
            loss_kd = 0
        elif opt.distill == 'hint':
            f_s = module_list[1](feat_s[opt.hint_layer])
            f_t = feat_t[opt.hint_layer]
            loss_kd = criterion_kd(f_s, f_t)
        elif opt.distill == 'crd':
            f_s = feat_s[-1]
            f_t = feat_t[-1]
            loss_kd = criterion_kd(f_s, f_t, index, contrast_idx)
        elif opt.distill == 'attention':
            g_s = feat_s[1:-1]
            g_t = feat_t[1:-1]
            loss_group = criterion_kd(g_s, g_t)
            loss_kd = sum(loss_group)
        elif opt.distill == 'nst':
            g_s = feat_s[1:-1]
            g_t = feat_t[1:-1]
            loss_group = criterion_kd(g_s, g_t)
            loss_kd = sum(loss_group)
        elif opt.distill == 'similarity':
            g_s = [feat_s[-2]]
            g_t = [feat_t[-2]]
            loss_group = criterion_kd(g_s, g_t)
            loss_kd = sum(loss_group)
        elif opt.distill == 'rkd':
            f_s = feat_s[-1]
            f_t = feat_t[-1]
            loss_kd = criterion_kd(f_s, f_t)
        elif opt.distill == 'pkt':
            f_s = feat_s[-1]
            f_t = feat_t[-1]
            loss_kd = criterion_kd(f_s, f_t)
        elif opt.distill == 'kdsvd':
            g_s = feat_s[1:-1]
            g_t = feat_t[1:-1]
            loss_group = criterion_kd(g_s, g_t)
            loss_kd = sum(loss_group)
        elif opt.distill == 'correlation':
            f_s = module_list[1](feat_s[-1])
            f_t = module_list[2](feat_t[-1])
            loss_kd = criterion_kd(f_s, f_t)
        elif opt.distill == 'vid':
            g_s = feat_s[1:-1]
            g_t = feat_t[1:-1]
            loss_group = [c(f_s, f_t) for f_s, f_t, c in zip(g_s, g_t, criterion_kd)]
            loss_kd = sum(loss_group)
        elif opt.distill == 'abound':
            # can also add loss to this stage
            loss_kd = 0
        elif opt.distill == 'fsp':
            # can also add loss to this stage
            loss_kd = 0
        elif opt.distill == 'factor':
            factor_s = module_list[1](feat_s[-2])
            factor_t = module_list[2](feat_t[-2], is_factor=True)
            loss_kd = criterion_kd(factor_s, factor_t)
        else:
            raise NotImplementedError(opt.distill)

        loss = opt.gamma * loss_cls + opt.alpha * loss_div + opt.beta * loss_kd

        acc1, acc5 = accuracy(logit_s, target, topk=(1, 5))
        losses.update(loss.item(), bs)
        top1.update(acc1.item(), bs)
        top5.update(acc5.item(), bs)
        xentm.update(loss_cls.item(), bs)
        kdm.update(loss_div.item())
        otherm.update(loss_kd)

        # ===================backward=====================
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # ===================meters=====================
        total_t += time.time() - end
        batch_time.update(time.time() - end, 1)
        end = time.time()

        # print info
        if idx % opt.print_freq == 0 and idx > 0:
            for param_group in optimizer.param_groups:
                lr = param_group['lr']
            # compute the remaining time
            epoch_remaining = opt.epochs - epoch
            # total_iters_remaining = len(train_loader) * (opt.epochs - epoch + 1) - idx
            iters_passed = len(train_loader) * (epoch - 1) + idx
            iters_remaining = len(train_loader) * (opt.epochs - epoch + 1) - idx

            ert = total_t * iters_remaining / iters_passed
            ert = convert_time(ert)

            print(
                'Epoch: %d [%03d, %03d], l_xent: %.4f, l_kd: %.4f, l_other: %.4f, acc: %.2f, lr: %.4f, time: %.1f, ert: %d:%02d:%02d' % (
                    epoch, idx, len(train_loader), xentm.avg, kdm.avg, otherm.avg, top1.avg, lr,
                    batch_time.avg * opt.print_freq, ert[0], ert[1], ert[2]))

        if idx % opt.test_freq == 0 and idx > 0:
            test_acc, tect_acc_top5, test_loss = validate(val_loader, model_s, criterion_cls, opt)
            model_s.train()
            if test_acc > best_acc:
                best_acc = test_acc
                state = {
                    'epoch': epoch,
                    'model': model_s.state_dict(),
                    'best_acc': best_acc,
                }
                save_file = os.path.join(opt.save_folder, '{}.pth'.format(opt.model_s))
                torch.save(state, save_file)
            print("\nTest acc: %.2f, best: %.2f\n" % (test_acc, best_acc))

            logger.store([epoch, xentm.avg, kdm.avg, otherm.avg, top1.avg, test_acc, best_acc, lr], log=True)

            xentm.reset()
            kdm.reset()
            top1.reset()
            otherm.reset()
            batch_time.reset()
            end = time.time()

        t_data = time.time()

    return best_acc, total_t

def validate(val_loader, model, criterion, opt):
    device = opt.device
    """validation"""
    batch_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    # switch to evaluate mode
    model.eval()

    with torch.no_grad():
        end = time.time()
        for idx, (input, target) in enumerate(val_loader):
            input = input.float()
            input = input.to(device)
            target = target.to(device)

            # compute output
            output = model(input)
            loss = criterion(output, target)

            # measure accuracy and record loss
            acc1, acc5 = accuracy(output, target, topk=(1, 5))
            losses.update(loss.item(), input.size(0))
            top1.update(acc1.item(), input.size(0))
            top5.update(acc5.item(), input.size(0))

            # measure elapsed time
            batch_time.update(time.time() - end)
            end = time.time()

    model.train()
    return top1.avg, top5.avg, losses.avg

max_stars_repo_name: anonyrep/SuperMix

max_issues_repo_name: anonyrep/SuperMix

max_forks_repo_name: anonyrep/SuperMix


text:
from copy import copy
import numpy as np
import itertools
from ..base import Model, ParamMixin, PhaseMixin, float_
from ..input import Input

class SiameseNetwork(Model, PhaseMixin):
    def __init__(self, siamese_layers, loss):
        self.layers = siamese_layers
        self.loss = loss
        # Create second array of layers
        self.layers2 = [copy(layer) for layer in self.layers]
        for layer1, layer2 in zip(self.layers, self.layers2):
            if isinstance(layer1, ParamMixin):
                # Replace weights in layers2 with shared weights
                layer2._params = [p.share() for p in layer1._params]
        self.bprop_until = next((idx for idx, l in enumerate(self.layers)
                                 if isinstance(l, ParamMixin)), 0)
        self.layers[self.bprop_until].bprop_to_x = False
        self.layers2[self.bprop_until].bprop_to_x = False
        self._initialized = False

    def _setup(self, x_shape, y_shape=None):
        # Setup layers sequentially
        if self._initialized:
            return
        next_shape = x_shape
        for layer in self.layers:
            layer._setup(next_shape)
            next_shape = layer.y_shape(next_shape)
        next_shape = x_shape
        for layer in self.layers2:
            layer._setup(next_shape)
            next_shape = layer.y_shape(next_shape)
        next_shape = self.loss.y_shape(next_shape)
        self._initialized = True

    @property
    def _params(self):
        all_params = [layer._params for layer in self.layers
                      if isinstance(layer, ParamMixin)]
        # Concatenate lists in list
        return list(itertools.chain.from_iterable(all_params))

    @PhaseMixin.phase.setter
    def phase(self, phase):
        if self._phase == phase:
            return
        self._phase = phase
        for layer in self.layers + self.layers2:
            if isinstance(layer, PhaseMixin):
                layer.phase = phase

    def _update(self, x1, x2, y):
        self.phase = 'train'

        # Forward propagation
        for layer in self.layers:
            x1 = layer.fprop(x1)
        for layer in self.layers2:
            x2 = layer.fprop(x2)

        # Back propagation of partial derivatives
        grad1, grad2 = self.loss.grad(y, x1, x2)
        layers = self.layers[self.bprop_until:]
        for layer in reversed(layers[1:]):
            grad1 = layer.bprop(grad1)
        layers[0].bprop(grad1)

        layers2 = self.layers2[self.bprop_until:]
        for layer in reversed(layers2[1:]):
            grad2 = layer.bprop(grad2)
        layers2[0].bprop(grad2)

        return self.loss.loss(y, x1, x2)

    def features(self, input):
        self.phase = 'test'
        input = Input.from_any(input)
        next_shape = input.x.shape
        for layer in self.layers:
            next_shape = layer.y_shape(next_shape)
        feats = np.empty(next_shape)
        idx = 0
        for batch in input.batches():
            x_batch = batch['x']
            x_next = x_batch
            for layer in self.layers:
                x_next = layer.fprop(x_next)
            feats_batch = np.array(x_next)
            batch_size = x_batch.shape[0]
            feats[idx:idx+batch_size, ...] = feats_batch
            idx += batch_size
        return feats

    def distances(self, input):
        self.phase = 'test'
        dists = np.empty((input.n_samples,), dtype=float_)
        offset = 0
        for batch in input.batches():
            x1, x2 = batch
            for layer in self.layers:
                x1 = layer.fprop(x1)
            for layer in self.layers2:
                x2 = layer.fprop(x2)
            dists_batch = self.loss.fprop(x1, x2)
            dists_batch = np.ravel(np.array(dists_batch))
            batch_size = x1.shape[0]
            dists[offset:offset+batch_size, ...] = dists_batch
            offset += batch_size
        return dists

max_stars_repo_name: DmitryUlyanov/deeppy


text:
###############################################################################
# Version: 1.1
# Last modified on: 3 April, 2016
# Developers: Michael G. Epitropakis
#      email: m_(DOT)_epitropakis_(AT)_lancaster_(DOT)_ac_(DOT)_uk
###############################################################################
import numpy as np
# UNCOMMENT APPROPRIATELY
#MINMAX = 1		# Minimization
MINMAX = -1		# Maximization

class CFunction(object):
	__dim_ = -1
	__nofunc_ = -1
	__C_ = 2000.0
	__lambda_ = None
	__sigma_ = None
	__bias_ = None
	__O_ = None
	__M_ = None
	__weight_ = None
	__lbound_ = None
	__ubound_ = None
	__fi_ = None
	__z_ = None
	__f_bias_ = 0
	__fmaxi_ = None
	__tmpx_ = None
	__function_ = None

	def __init__(self, dim, nofunc):
		self.__dim_ = dim
		self.__nofunc_ = nofunc

	def evaluate(self, x):
		pass

	def get_lbound(self, ivar):
		assert (ivar >= 0 and ivar < self.__dim_), ["ivar is not in valid variable range: %d not in [0,%d]" % ivar,self.__dim_]
		return self.__lbound_[ivar];

	def get_ubound(self, ivar):
		assert (ivar >= 0 and ivar < self.__dim_), ["ivar is not in valid variable range: %d not in [0,%d]" % ivar,self.__dim_]
		return self.__ubound_[ivar];

	def __evaluate_inner_(self, x):
		if self.__function_ == None:
			raise NameError('Composition functions\' dict is uninitialized')
		self.__fi_ = np.zeros(self.__nofunc_)

		self.__calculate_weights(x)

		for i in range(self.__nofunc_):
			self.__transform_to_z(x, i)
			self.__fi_[i] = self.__function_[i](self.__z_)

		tmpsum = np.zeros(self.__nofunc_)
		for i in range(self.__nofunc_):
			tmpsum[i] = self.__weight_[i] * (self.__C_ * self.__fi_[i] / self.__fmaxi_[i] + self.__bias_[i])
		#print("VERIFICANDO...")
		#print(tmpsum, MINMAX, self.__f_bias_)
		return sum(tmpsum) * MINMAX + self.__f_bias_

	def __calculate_weights(self, x):
		self.__weight_ = np.zeros(self.__nofunc_)
		for i in range(self.__nofunc_):
			mysum = sum( (x-self.__O_[i])**2 )
			self.__weight_[i] = np.exp( -mysum/(2.0 * self.__dim_ * self.__sigma_[i] * self.__sigma_[i]) )
		maxw = np.max(self.__weight_)
		#maxi = self.__weight_.argmax(axis=0)

		maxw10 = maxw**10
		for i in range(self.__nofunc_):
			if self.__weight_[i] != maxw:
			#if i != maxi:
				self.__weight_[i] = self.__weight_[i] * (1.0 - maxw10)

		mysum = np.sum( self.__weight_ )
		for i in range(self.__nofunc_):
			if mysum == 0.0:
				self.__weight_[i] = 1.0 / (1.0 * self.__nofunc_ )
			else:
				self.__weight_[i] = self.__weight_[i] / mysum

	def __calculate_fmaxi(self):
		self.__fmaxi_ = np.zeros(self.__nofunc_)
		if self.__function_ == None:
			raise NameError('Composition functions\' dict is uninitialized')

		x5 = 5 * np.ones(self.__dim_)

		for i in range(self.__nofunc_):
			self.__transform_to_z_noshift(x5, i)
			self.__fmaxi_[i] = self.__function_[i](self.__z_)

	def __transform_to_z_noshift(self, x, index):
		# z_i = (x)/\lambda_i
		tmpx = np.divide(x, self.__lambda_[index])
		# Multiply z_i * M_i
		self.__z_ = np.dot( tmpx, self.__M_[index] )

	def __transform_to_z(self, x, index):
		# Calculate z_i = (x - o_i)/\lambda_i
		tmpx = np.divide((x - self.__O_[index]), self.__lambda_[index])
		# Multiply z_i * M_i
		self.__z_ = np.dot( tmpx, self.__M_[index] )

	def __load_rotmat(self, fname):
		self.__M_ = []

		with open(fname, 'r') as f:
			tmp = np.zeros( (self.__dim_,self.__dim_) )
			cline = 0
			ctmp = 0
			for line in f:
				line = line.split()
				if line:
					line = [float(i) for i in line]
					# re initialize array when reached dim
					if (ctmp%self.__dim_ == 0):
						tmp = np.zeros( (self.__dim_,self.__dim_) )
						ctmp = 0

					# add line to tmp
					tmp[ctmp] = line[:self.__dim_]
					# if we loaded self.__nofunc_ * self.__dim_-1 lines break
					if cline >= self.__nofunc_ * self.__dim_-1:
						break
					# add array to M_ when it is fully created
					if (cline%self.__dim_ == 0):
						self.__M_.append(tmp)
					ctmp = ctmp + 1
					cline = cline + 1

# Sphere function
def FSphere(x):
    return (x**2).sum()

# Rastrigin's function
def FRastrigin(x):
    return np.sum( x**2-10.*np.cos( 2.*np.pi*x )+10)

# Griewank's function
def FGrienwank(x):
    i = np.sqrt(np.arange(x.shape[0])+1.0)
    return np.sum(x**2)/4000.0 - np.prod(np.cos(x/i)) + 1.0

# Weierstrass's function
def FWeierstrass(x):
    alpha = 0.5
    beta = 3.0
    kmax = 20
    D = x.shape[0]
    exprf = 0.0

    c1 = alpha**np.arange(kmax+1)
    c2 = 2.0*np.pi*beta**np.arange(kmax+1)
    f = 0
    c = -D*np.sum(c1*np.cos(c2*0.5))

    for i in range(D):
        f += np.sum(c1*np.cos(c2*(x[i]+0.5)))
    return f + c

def F8F2(x):
    f2 = 100.0 * ( x[0]**2 - x[1] )**2 + (1.0 - x[0])**2
    return 1.0 + (f2**2)/4000.0 - np.cos(f2)

# FEF8F2 function
def FEF8F2(x):
    D = x.shape[0]
    f = 0
    for i in range(D-1):
        f += F8F2( x[ [i,i+1] ] + 1 )
    f += F8F2( x[ [D-1,0] ] + 1 )
    return f

max_stars_repo_name: krowck/ISDA-NCjDE-HJ

max_issues_repo_name: krowck/ISDA-NCjDE-HJ

max_forks_repo_name: krowck/ISDA-NCjDE-HJ


text:
"""
@author:    Guan'an Wang
@contact:   guan.wang0706@gmail.com
"""

import numpy as np

def hamming_distance(x, y):
    """
    compute hamming distance (NOT hamming similarity)
    between x and y
    Args:
        x(np.ndarray): [num_x, dim] in {0, 1}
        y(np.ndarray): [num_y, dim] in {0, 1}
    Return:
        (np.ndarray): [num_x, num_y]
    """
    assert min(x.min(), y.min())==0 and max(x.max(), y.max())==1, \
        'expect binary codes in \{0, 1\}, but got {{}, {}}'.format(min(x.min(), y.min()), max(x.max(), y.max()))

    assert x.shape[1] == y.shape[1], \
        'expect x and y have the same dimmension, but got x {} and y {}'.format(x.shape[1], y.shape[1])
    code_len = x.shape[1]

    x = (x-0.5)*2
    y = (y-0.5)*2
    return code_len - (np.matmul(x, y.transpose([1,0])) + code_len) / 2

max_stars_repo_name: wangguanan/light-reid

max_issues_repo_name: Rockdow/light-reid

max_forks_repo_name: Rockdow/light-reid


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 16 10:35:20 2020

@author: p20coupe
"""

import argparse
import sys

import joblib
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import os
import math
import statistics

import torch
from torch import nn
from torch.optim import Adam
from torch.utils.data import DataLoader, Dataset
import torch.backends.cudnn as cudnn
import torch.backends.cudnn
from torch.autograd import Variable
from torch.nn import functional as F
from torchvision.transforms import ToTensor, Normalize, Compose
import torch.optim as optim

from ResNet_Reconstruction import *

parser = argparse.ArgumentParser()
parser.add_argument("TestData", help="PATH to testing data")
parser.add_argument("NetworkPATH",help="PATH to the network to use")
parser.add_argument("ResultsDirectory",help="PATH to the results storage directory")
args = parser.parse_args()

use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")

Datadirectory = sys.argv[1]
ResultsDirectory = sys.argv[3]
NetworkPath = sys.argv[2]

def psnr(lP,lT):
    mse = np.mean( (lP - lT) ** 2 )
    if mse == 0:
        return 100
    PIXEL_MAX = 3.0
    return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))

def imshow(img,corr):
	n_rows = 2
	n_cols = int(len(img)/2)
	plt.figure(figsize=(n_cols, n_rows))
	for i in range(n_rows):
		for j in range(n_cols):
			sub = plt.subplot(n_rows, n_cols, i*n_cols+1+j)
			sub.imshow(img[j+n_rows*i,0,:,:].numpy(),
		 	cmap=plt.cm.gray,
		 	interpolation="nearest",
		 	vmin=-3,vmax=2)
			sub.set_title('%.3f' %(corr[j+n_rows*i]))
			sub.axis('off')

def imshow_difMap(img,label):
	n_rows = 2
	n_cols = int(len(img)/2)
	plt.figure(figsize=(n_cols, n_rows))
	for i in range(n_rows):
		for j in range(n_cols):
			sub = plt.subplot(n_rows, n_cols, i*n_cols+1+j)
			sub.imshow(img[j+n_rows*i,0,:,:].numpy()-label[j+n_rows*i,0,:,:].numpy(),
		 	cmap=plt.cm.gray,
		 	interpolation="nearest",
		 	vmin=-3,vmax=2)
			sub.axis('off')

def ValidRed2D(testloader,path):
    psnr_value=[]

    net = ResNet(BasicBlock, [3,4,6]).to(device)
    net.load_state_dict(torch.load(path))

    for i, data in enumerate(testloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels, correlation = data
        inputs= inputs.to(device)
        labels = labels.to(device)
        correlation = correlation.to(device)

        outputs = net(inputs)

        psnr_val = 0

        pr= labels[0].cpu().detach().numpy()
        gt = outputs[0].cpu().detach().numpy()
        psnr_val = psnr_val + psnr(gt[0,:,:],pr[0,:,:])

        if i == 800:
            imshow(inputs.cpu().detach(),correlation.cpu().detach().numpy())
            plt.savefig(os.path.join(ResultsDirectory,'Images','inputs_myloss_test.png'),dpi=150)
            imshow(labels.cpu().detach(),correlation.cpu().detach().numpy())
            plt.savefig(os.path.join(ResultsDirectory,'Images','labels_myloss_test.png'),dpi=150)
            imshow(outputs.cpu().detach(),correlation.cpu().detach().numpy())
            plt.savefig(os.path.join(ResultsDirectory,'Images','outputs_myloss_test.png'),dpi=150)
            imshow_difMap(outputs.cpu().detach(),labels.cpu().detach())
            plt.savefig(os.path.join(ResultsDirectory,'Images','DifMap_myloss_test.png'),dpi=150)

        psnr_value.append(psnr_val)

    np.savetxt(os.path.join(ResultsDirectory,'PSNR_test.txt'),psnr_value)
    print('Standard Deviation:' + str(statistics.stdev(psnr_value)))
    print('Mean:' + str(statistics.mean(psnr_value)))

bones=['calcaneus','talus','tibia']
X_test=[[]]
Y_test=[[]]
corr=[]

sujets = os.listdir(Datadirectory)
sujets = np.sort(sujets)

for i in range(len(sujets)):

    #Dataset Validation
    for bone in bones:
        patches= os.listdir(os.path.join(Datadirectory,sujets[i],'DatasetReconstruction_patches',bone))
        for k in range(len(patches)):
            if(patches[k].find('BR')!=-1):
                if X_test[0]==[]:
                    X_test[0] = joblib.load(os.path.join(Datadirectory,sujets[i],'DatasetReconstruction_patches',bone,patches[k]))
                else:
                    X_test[0] = X_test[0]+joblib.load(os.path.join(Datadirectory,sujets[i],'DatasetReconstruction_patches',bone,patches[k]))

            if(patches[k].find('HR')!=-1):
                if Y_test[0]==[]:
                    Y_test[0] = joblib.load(os.path.join(Datadirectory,sujets[i],'DatasetReconstruction_patches',bone,patches[k]))
                else:
                    Y_test[0] = Y_test[0]+joblib.load(os.path.join(Datadirectory,sujets[i],'DatasetReconstruction_patches',bone,patches[k]))

            if(patches[k].find('corr')!=-1):
                corr = np.append(corr,joblib.load(os.path.join(Datadirectory,sujets[i],'DatasetReconstruction_patches',bone,patches[k])))

Y_test = np.moveaxis(Y_test,0,1)
X_test = np.moveaxis(X_test,0,1)
print(np.shape(Y_test))
print(np.shape(corr))

testset = torch.utils.data.TensorDataset(torch.Tensor(X_test),torch.Tensor(Y_test),torch.Tensor(corr))
testloader = torch.utils.data.DataLoader(testset, batch_size=8,
		                                  shuffle=False, pin_memory=use_cuda, num_workers=2)

#Create directores for the results
if not os.path.exists(os.path.join(ResultsDirectory,'Images')):
    os.mkdir(os.path.join(ResultsDirectory, 'Images'))

ValidRed2D(testloader,NetworkPath)

max_stars_repo_name: Benjamin-Fouquet/dynMRI

max_issues_repo_name: Benjamin-Fouquet/dynMRI

max_forks_repo_name: Benjamin-Fouquet/dynMRI


text:
# -*- coding:utf-8 -*-
# =========================================================================== #
# Project : Ames House Prediction Model                                       #
# File    : \analysis.py                                                      #
# Python  : 3.9.1                                                             #
# --------------------------------------------------------------------------- #
# Author  : John James                                                        #
# Company : nov8.ai                                                           #
# Email   : john.james@nov8.ai                                                #
# URL     : https://github.com/john-james-sf/Ames/                            #
# --------------------------------------------------------------------------- #
# Created       : Tuesday, March 23rd 2021, 10:30:24 pm                       #
# Last Modified : Tuesday, March 23rd 2021, 10:30:24 pm                       #
# Modified By   : John James (john.james@nov8.ai)                             #
# --------------------------------------------------------------------------- #
# License : BSD                                                               #
# Copyright (c) 2021 nov8.ai                                                  #
# =========================================================================== #
#%%
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# --------------------------------------------------------------------------- #
class Analysis:
    def __init__(self, filepath="../reports/scores.csv"):
        self._filepath = filepath
        self.read()

    def read(self):
        self.scores_ = pd.read_csv(self._filepath)

    def performance_by_cv(self, return_best=False):
        #If multiple trials, group by group and estimator and take means before
        df = self.scores_.pivot(index=["Group","Estimator"], columns="FileSplit", values="Test RMSE")
        df["Average"] = df.mean(axis=1)
        df.sort_values(by="Average", inplace=True)
        if return_best:
            best = df.groupby("Group").head(1)
            return best
        return df

    def grouped_summary(self):
        df = self.scores_.groupby(by=["Group","Estimator"]).mean()
        return df[["Score","Fit Time", "Test RMSE", "Test Time"]].sort_values(by="Test RMSE")

def main():
    analysis = Analysis()
    print(analysis.performance_by_cv(return_best=True))
    print(analysis.performance_by_cv())
    print(analysis.grouped_summary())

if __name__ == "__main__":
    main()

#%%

max_stars_repo_name: john-james-sf/Ames

max_issues_repo_name: john-james-sf/Ames

max_forks_repo_name: john-james-sf/Ames


text:
# -*- coding: utf-8 -*-
"""
Created on 08-16-2020

@author: Xin Huang
"""

from numpy import vstack
from numpy import argmax
from pandas import read_csv
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from torch import Tensor
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torch.utils.data import random_split
from torch.nn import Linear
from torch.nn import ReLU
from torch.nn import Sigmoid
from torch.nn import Softmax
from torch.nn import Module
from torch.nn import Dropout
from torch.nn import BatchNorm1d
from torch.optim import SGD,RMSprop,Adam,AdamW
from torch.nn import CrossEntropyLoss
from torch.nn import MSELoss
from torch.nn.init import kaiming_uniform_
from torch.nn.init import xavier_uniform_
from torch.utils.data import TensorDataset
import torch

_device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("torch.cuda.is_available:")
print(torch.cuda.is_available())

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
import numpy as np

from google.colab import drive
drive.mount('content/')

NUM_LALBELS = 3

prefix = '/content/content/My Drive/Colab Notebooks/ieeebigdata/weak/'

def equalRate(a, b):
  c = a-b
  d = np.where(c==0)
  print("equal labels:")
  print(d[0].shape[0])
  print(d[0].shape[0] * 1.0 / a.shape[0])

def loadData(prefix, filename):
  data = np.load(prefix + filename)
  #load common data
  latlon = data['latlon']
  iff = data['iff']

  X_v = data['viirs']
  Y_v = data['label']
  print ('X_v shape:')
  print (X_v.shape)
  print ('Y_v.shape:')
  print(Y_v)
  Y_v = np.delete(Y_v, 0, 1)
  print ('Y_v.shape:')

  X_c = data['calipso']
  Y_c = data['label']
  print ('X_c shape:')
  print (X_c.shape)
  print ('Y_c.shape:')
  Y_c = np.delete(Y_c, 1, 1)
  print ('Y_c.shape:')

  equalRate(data['label'][:,0], data['label'][:,1])

  inds_v,vals_v = np.where(Y_v>0)
  Y_v = Y_v[inds_v]
  X_v = X_v[inds_v]
  print ('X_v')
  print (X_v)

  inds_c,vals_c = np.where(Y_v>0)
  Y_c = Y_c[inds_c]
  X_c = X_c[inds_c]
  print ('X_c')
  print (X_c)

  # process common data
  Latlon = latlon[inds_v]
  Iff = iff[inds_v]

  print('original X_v: ', X_v.shape)
  rows = np.where((X_v[:,0] >= 0) & (X_v[:,0] <= 83) & (X_v[:,15] > 100) & (X_v[:,15] < 400) & (X_v[:,16] > 100) & (X_v[:,16] < 400) & (X_v[:,17] > 100) & (X_v[:,17] < 400) & (X_v[:,18] > 100) & (X_v[:,18] < 400) & (X_v[:,19] > 100) & (X_v[:,19] < 400) & (X_v[:,10] > 0))
  print("rows:", rows)
  print("rows.shape:", len(rows))

  Latlon = Latlon[rows]
  Iff = Iff[rows]

  Y_v = Y_v[rows]
  X_v = X_v[rows]

  Y_c = Y_c[rows]
  X_c = X_c[rows]

  print('after SZA X_v: ', X_v.shape)
  print('after SZA X_c: ', X_c.shape)

  #concanate common data
  X_c = np.concatenate((X_c, Latlon, Iff), axis=1)
  print (X_v.shape)
  print (X_c.shape)

  X_v = np.nan_to_num(X_v)
  X_c = np.nan_to_num(X_c)
  return X_v, X_c, Y_v, Y_c

train_file = '4yr_jan.npz'
X_v, X_c, Y_v, Y_c = loadData(prefix, train_file)
Y = np.concatenate((Y_v, Y_c), axis=1)
print (X_v.shape)
print (X_c.shape)
print (Y.shape)

# combine data and split latter to define ground truth for MLR
n1=20
n2=25
X=np.concatenate((X_v, X_c), axis=1)
print (X.shape)
x_train, x_valid, y_train, y_valid = train_test_split(X, Y,
                                                    test_size=0.3,
                                                    random_state=0,
                                                    stratify=Y)

# feature scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
x_train=sc_X.fit_transform(x_train)
x_valid=sc_X.transform(x_valid)
# x_test=sc_X.fit_transform(x_test)

x_train_v = x_train[:, 0:20]
x_train_c = x_train[:, 20:45]
x_train_comm = x_train[:, 45:51]
x_train_src = x_train[:, 20:51]
y_train_src = np.delete(y_train, 0, 1)
y_train_tgt = np.delete(y_train, 1, 1)

print(x_train_v.shape)
print(x_train_c.shape)
print(x_train_comm.shape)
print(x_train_src.shape)
print(y_train.shape)
print(y_train_src.shape)
print(y_train_tgt.shape)

x_valid_src = x_valid[:, 20:51]
x_valid_v = x_valid[:, 0:20]
x_valid_c = x_valid[:, 20:45]
x_valid_comm = x_valid[:, 45:51]
y_valid_src = np.delete(y_valid, 0, 1)
y_valid_tgt = np.delete(y_valid, 1, 1)

print(x_valid_v.shape)
print(x_valid_c.shape)
print(x_valid_comm.shape)
print(x_valid_src.shape)
print(y_valid.shape)
print(y_valid_src.shape)
print(y_valid_tgt.shape)

x_train_c_pt = x_train_v
x_valid_c_pt = x_valid_v

print(x_train_c_pt.shape)
print(x_valid_c_pt.shape)

# DLR imputed target domain
x_train_pt = np.concatenate((x_train_c_pt, x_train_comm),axis=1)
print(x_train_pt.shape)

x_valid_pt = np.concatenate((x_valid_c_pt, x_valid_comm),axis=1)
print(x_valid_pt.shape)

def load_test_data(prefix, filename):
  data_test = np.load(prefix + filename)

  passive =1

  #load common data
  latlon_test = data_test['latlon']
  iff_test = data_test['iff']

  x_t_test = data_test['viirs']
  y_t_test = data_test['label']
  y_t_test = np.delete(y_t_test, 0, 1)

  x_s_test = data_test['calipso']
  y_s_test = data_test['label']
  y_s_test = np.delete(y_s_test, 1 , 1)

  inds_test,vals_test = np.where(y_t_test>0)

  # process common data
  Latlon_test = latlon_test[inds_test]
  Iff_test = iff_test[inds_test]

  Y_t_test = y_t_test[inds_test]
  X_t_test = x_t_test[inds_test]

  Y_s_test = y_s_test[inds_test]
  X_s_test = x_s_test[inds_test]

  # 0 =< SZA <= 83
  print('original X_t_test: ', X_t_test.shape)
  rows_test = np.where((X_t_test[:,0] >= 0) & (X_t_test[:,0] <= 83) & (X_t_test[:,15] > 100) & (X_t_test[:,15] < 400) & (X_t_test[:,16] > 100) & (X_t_test[:,16] < 400) & (X_t_test[:,17] > 100) & (X_t_test[:,17] < 400) & (X_t_test[:,18] > 100) & (X_t_test[:,18] < 400) & (X_t_test[:,19] > 100) & (X_t_test[:,19] < 400) & (X_t_test[:,10] > 0))
  print("rows_test:", rows_test)
  print("rows_test.shape:", len(rows_test))

  Latlon_test = Latlon_test[rows_test]
  Iff_test = Iff_test[rows_test]

  Y_t_test = Y_t_test[rows_test]
  X_t_test = X_t_test[rows_test]

  Y_s_test = Y_s_test[rows_test]
  X_s_test = X_s_test[rows_test]

  X_s_test = np.nan_to_num(X_s_test)
  X_t_test = np.nan_to_num(X_t_test)

  print('after SZA X_t_test: ', X_t_test.shape)
  print('after SZA X_s_test: ', X_s_test.shape)

  #concanate common data
  X_s_test = np.concatenate((X_s_test, Latlon_test, Iff_test), axis=1)

  print (X_s_test.shape)
  print (X_t_test.shape)

  X_test=np.concatenate((X_t_test, X_s_test), axis=1)

  x_test2=sc_X.transform(X_test)

  X_t_test = x_test2[:, 0:20]
  x_test_c2 = x_test2[:, 20:45]
  x_test_comm2 = x_test2[:, 45:51]

  x_test_t_pt = X_t_test
  print(x_test_t_pt.shape)

  x_test_pt_test = np.concatenate((x_test_t_pt, x_test_comm2),axis=1)
  print(x_test_pt_test.shape)

  return X_s_test, Y_s_test, x_test_pt_test, Y_t_test

X_s_test, Y_s_test, x_test_pt_test, Y_t_test = load_test_data(prefix, '2017_jan_day_005.npz')

# run the Correlation based DA
# pytorch mlp for multiclass classification

n_epochs = 200
lambda_ = 0.001
lambda_l2 = 0.05
DDM_NUM = 20
NUM = 26
DIFFERECE_COL = 5
BATCH_SIZE = 2048

class EarlyStopping(object):
    def __init__(self, mode='min', min_delta=0, patience=10, percentage=False):
        self.mode = mode
        self.min_delta = min_delta
        self.patience = patience
        self.best = None
        self.num_bad_epochs = 0
        self.is_better = None
        self._init_is_better(mode, min_delta, percentage)

        if patience == 0:
            self.is_better = lambda a, b: True
            self.step = lambda a: False

    def step(self, metrics):
        if self.best is None:
            self.best = metrics
            return False

        if np.isnan(metrics):
            return True

        if self.is_better(metrics, self.best):
            self.num_bad_epochs = 0
            self.best = metrics
        else:
            self.num_bad_epochs += 1

        if self.num_bad_epochs >= self.patience:
            return True

        return False

    def _init_is_better(self, mode, min_delta, percentage):
        if mode not in {'min', 'max'}:
            raise ValueError('mode ' + mode + ' is unknown!')
        if not percentage:
            if mode == 'min':
                self.is_better = lambda a, best: a < best - min_delta
            if mode == 'max':
                self.is_better = lambda a, best: a > best + min_delta
        else:
            if mode == 'min':
                self.is_better = lambda a, best: a < best - (
                            best * min_delta / 100)
            if mode == 'max':
                self.is_better = lambda a, best: a > best + (
                            best * min_delta / 100)

# dataset definition
class CSVDataset(Dataset):
    # load the dataset
    def __init__(self, X1, Y1):
        # load the csv file as a dataframe
        self.X=X1
        self.y=Y1
        print("self.X before fit_transform")
        print(self.X)
        print("self.y before fit_transform")
        print(self.y)
        self.X = self.X.astype('float32')
        # label encode target and ensure the values are floats
        self.y = LabelEncoder().fit_transform(self.y)
        print("self.X before fit_transform")
        print(self.X)
        print("self.y after fit_transform")
        print(self.y)

    # number of rows in the dataset
    def __len__(self):
        return len(self.X)

    # get a row at an index
    def __getitem__(self, idx):
        return [self.X[idx], self.y[idx]]

    # get indexes for train and test rows
    def get_splits(self, n_test=0.33):
        # determine sizes
        test_size = round(n_test * len(self.X))
        train_size = len(self.X) - test_size
        # calculate the split
        return random_split(self, [train_size, test_size])

def CORAL(src,tgt):
    d = src.size(1)
    src_c = coral(src)
    tgt_c = coral(tgt)

    loss = torch.sum(torch.mul((src_c-tgt_c),(src_c-tgt_c)))
    loss = loss/(4*d*d)
    return loss

def LOG_CORAL(src,tgt):
    d = src.size(1)
    src_c = coral(src)
    tgt_c = coral(tgt)
    src_vals, src_vecs = torch.symeig(src_c,eigenvectors = True)
    tgt_vals, tgt_vecs = torch.symeig(tgt_c,eigenvectors = True)
    src_cc = torch.mm(src_vecs,torch.mm(torch.diag(torch.log(src_vals)),src_vecs.t()))
    tgt_cc = torch.mm(tgt_vecs,torch.mm(torch.diag(torch.log(tgt_vals)),tgt_vecs.t()))
    loss = torch.sum(torch.mul((src_cc - tgt_cc), (src_cc - tgt_cc)))
    loss = loss / (4 * d * d)
    return loss

def coral(data):
    n = data.size(0)
    id_row = torch.ones(n).resize(1,n)
    if torch.cuda.is_available():
        id_row = id_row.cuda()
    sum_column = torch.mm(id_row,data)
    mean_column = torch.div(sum_column,n)
    mean_mean = torch.mm(mean_column.t(),mean_column)
    d_d = torch.mm(data.t(),data)
    coral_result = torch.add(d_d,(-1*mean_mean))*1.0/(n-1)
    return coral_result

class Deep_coral(Module):
    def __init__(self,num_classes = NUM_LALBELS):
        super(Deep_coral,self).__init__()

        self.ddm = DDM(n_inputs=DDM_NUM,n_outputs=DDM_NUM+DIFFERECE_COL)
        self.feature = MLP(n_inputs=NUM+DIFFERECE_COL)
        self.central = Linear(64,32) # correlation layer
        xavier_uniform_(self.central.weight)

        self.fc = CLASSIFY(32, num_classes)

    def forward(self,src,tgt):
        src = self.feature(src)
        centr1 = self.central(src)
        src = self.fc(centr1)
        # output layer
        viirs_d = tgt[:, 0:20]
        common_d = tgt[:, 20:26]
        dmval = self.ddm(viirs_d)
        combine_d = torch.cat((dmval, common_d), 1)
        tgt = self.feature(combine_d)
        centr2 = self.central(tgt)

        tgt = self.fc(centr2)
        return src,tgt,dmval,centr1,centr2

    def pretrain(self,tgt):
        # output layer
        viirs_d = tgt[:, 0:20]
        common_d = tgt[:, 20:26]
        dmval = self.ddm(viirs_d)
        return dmval

# DDM model definition
class DDM(Module):
    # define model elements
    def __init__(self, n_inputs=DDM_NUM, n_outputs = DDM_NUM+DIFFERECE_COL):
        super(DDM, self).__init__()
        # # input to very beginning hidden layer
        self.hidden = Linear(n_inputs, 256)
        xavier_uniform_(self.hidden.weight)
        self.act = Sigmoid()
        # input to beginning hidden layer
        self.hidden0 = Linear(256, 256)
        xavier_uniform_(self.hidden0.weight)
        self.act0 = Sigmoid()
        # input to first hidden layer
        self.hidden1 = Linear(256, 256)
        xavier_uniform_(self.hidden1.weight)
        self.act1 = Sigmoid()
        # second hidden layer
        self.hidden2 = Linear(256, 128)
        xavier_uniform_(self.hidden2.weight)
        self.act2 = Sigmoid()
        # third hidden layer
        self.hidden3 = Linear(128, 64)
        xavier_uniform_(self.hidden3.weight)
        self.act3 = Sigmoid()
        # 4th hidden layer
        self.hidden4 = Linear(64, n_outputs)
        xavier_uniform_(self.hidden4.weight)

        self.dropout = Dropout(p=0.5)
        self.batchnorm = BatchNorm1d(256)
        self.batchnorm0 = BatchNorm1d(256)
        self.batchnorm1 = BatchNorm1d(256)
        self.batchnorm2 = BatchNorm1d(128)
        self.batchnorm3 = BatchNorm1d(64)
        self.batchnorm4 = BatchNorm1d(n_outputs)

    # forward propagate input
    def forward(self, X):
        # input to very first hidden layer
        X = self.hidden(X)
        X = self.batchnorm(X)
        X = self.act(X)
        X = self.dropout(X)
        # input to first hidden layer
        X = self.hidden0(X)
        X = self.batchnorm0(X)
        X = self.act0(X)
        X = self.dropout(X)
        # input to second hidden layer
        X = self.hidden1(X)
        X = self.batchnorm1(X)
        X = self.act1(X)
        X = self.dropout(X)
        # third hidden layer
        X = self.hidden2(X)
        X = self.batchnorm2(X)
        X = self.act2(X)
        X = self.dropout(X)
        # fourth hidden layer
        X = self.hidden3(X)
        X = self.batchnorm3(X)
        X = self.act3(X)
        X = self.dropout(X)
        # fifth hidden layer
        X = self.hidden4(X)
        X = self.batchnorm4(X)
        return X

# model definition n_inputs = 31
class MLP(Module):
    # define model elements
    def __init__(self, n_inputs=NUM+DIFFERECE_COL):
        super(MLP, self).__init__()
        # # input to very beginning hidden layer
        self.hidden = Linear(n_inputs, 128)
        kaiming_uniform_(self.hidden.weight, nonlinearity='relu')
        self.act = ReLU()
        # input to beginning hidden layer
        self.hidden0 = Linear(128, 256)
        kaiming_uniform_(self.hidden0.weight, nonlinearity='relu')
        self.act0 = ReLU()
        # input to first hidden layer
        self.hidden1 = Linear(256, 128)
        kaiming_uniform_(self.hidden1.weight, nonlinearity='relu')
        self.act1 = ReLU()
        # second hidden layer
        self.hidden2 = Linear(128, 64)
        kaiming_uniform_(self.hidden2.weight, nonlinearity='relu')
        self.act2 = ReLU()
        self.dropout = Dropout(p=0.5)
        self.batchnorm = BatchNorm1d(128)
        self.batchnorm0 = BatchNorm1d(256)
        self.batchnorm1 = BatchNorm1d(128)
        self.batchnorm2 = BatchNorm1d(64)

    # forward propagate input
    def forward(self, X):
        # input to very first hidden layer
        X = self.hidden(X)
        X = self.batchnorm(X)
        X = self.act(X)
        X = self.dropout(X)
        # input to first hidden layer
        X = self.hidden0(X)
        X = self.batchnorm0(X)
        X = self.act0(X)
        X = self.dropout(X)
        # input to second hidden layer
        X = self.hidden1(X)
        X = self.batchnorm1(X)
        X = self.act1(X)
        X = self.dropout(X)
        # third hidden layer
        X = self.hidden2(X)
        X = self.batchnorm2(X)
        X = self.act2(X)
        return X

# classifer model definition n_inputs = 32 from the coral layer
class CLASSIFY(Module):
    # define model elements
    def __init__(self, n_inputs=32, n_outputs=6):
        super(CLASSIFY, self).__init__()
        # # input to very beginning hidden layer
        self.hidden = Linear(n_inputs, 64)
        kaiming_uniform_(self.hidden.weight, nonlinearity='relu')
        self.act = ReLU()
        # input to beginning hidden layer
        self.hidden0 = Linear(64, 128)
        kaiming_uniform_(self.hidden0.weight, nonlinearity='relu')
        self.act0 = ReLU()
        # input to first hidden layer
        self.hidden1 = Linear(128, 64)
        kaiming_uniform_(self.hidden1.weight, nonlinearity='relu')
        self.act1 = ReLU()
        # second hidden layer
        self.hidden2 = Linear(64, n_outputs)
        kaiming_uniform_(self.hidden2.weight, nonlinearity='relu')
        self.dropout = Dropout(p=0.5)
        self.batchnorm = BatchNorm1d(64)
        self.batchnorm0 = BatchNorm1d(128)
        self.batchnorm1 = BatchNorm1d(64)
        self.batchnorm2 = BatchNorm1d(n_outputs)

    # forward propagate input
    def forward(self, X):
        # input to very first hidden layer
        X = self.hidden(X)
        X = self.batchnorm(X)
        X = self.act(X)
        X = self.dropout(X)
        # input to first hidden layer
        X = self.hidden0(X)
        X = self.batchnorm0(X)
        X = self.act0(X)
        X = self.dropout(X)
        # input to second hidden layer
        X = self.hidden1(X)
        X = self.batchnorm1(X)
        X = self.act1(X)
        X = self.dropout(X)
        # third hidden layer
        X = self.hidden2(X)
        X = self.batchnorm2(X)
        return X

def prepare_data(X_src, Y_src, X_tgt, Y_tgt):
    # load the train dataset
    # src_train = CSVDataset(X_src, Y_src)
    # tgt_train = CSVDataset(X_tgt, Y_tgt)
    print("Y_src:")
    print(Y_src)
    print("Y_tgt:")
    print(Y_tgt)
    Y_src_1 = LabelEncoder().fit_transform(Y_src)
    Y_tgt_1 = LabelEncoder().fit_transform(Y_tgt)

    print("Y_src_1:")
    print(Y_src_1)
    print("Y_tgt_1:")
    print(Y_tgt_1)

    print("X_src:")
    print(X_src)
    print("X_tgt:")
    print(X_tgt)

    x_src = torch.from_numpy(X_src)
    y_src = torch.from_numpy(Y_src_1)
    x_tgt = torch.from_numpy(X_tgt)
    y_tgt = torch.from_numpy(Y_tgt_1)

    datasets = TensorDataset(x_src.float(), y_src, x_tgt.float(), y_tgt)
    # prepare data loaders
    train_dl = DataLoader(datasets, batch_size=BATCH_SIZE, shuffle=True)
    return train_dl

aggre_losses = []
aggre_losses_l2 = []
aggre_losses_coral = []
aggre_losses_classifier = []
aggre_losses_classifier_tgt = []

aggre_losses_valid = []
aggre_losses_l2_valid = []
aggre_losses_classifier_valid = []
aggre_losses_classifier_valid_tgt = []
aggre_losses_coral_valid = []

# aggre_losses_l2_ddm = []

aggre_train_acc = []
aggre_test_acc = []
aggre_valid_acc = []
aggre_train_tgt_acc = []

# train the model
def train_model(train_dat, valid_dat, test_dat, model, device):

    model.train()
    # define the optimization
    criterion = CrossEntropyLoss()
    l2loss = MSELoss()

    optimizer1 = AdamW(model.ddm.parameters(), lr=0.001, weight_decay=0.01)
    optimizer2 = AdamW([{'params': model.feature.parameters()}, {'params': model.central.parameters()}, {'params': model.fc.parameters()}], lr=0.000005, weight_decay=0.01)

    es = EarlyStopping(patience=15)

    if torch.cuda.is_available():
      model = model.cuda()

    # enumerate epochs for DA
    j = 0
    for epoch in range(n_epochs):
        j += 1
        # enumerate mini batches of src domain and target domain
        train_steps = len(train_dat)
        print("DA train_steps:", train_steps)

        epoch_loss = 0
        epoch_loss_l2 = 0
        epoch_loss_classifier = 0
        epoch_loss_classifier_tgt = 0
        epoch_loss_coral = 0

        i = 0
        for it, (src_data, src_label, tgt_data, tgt_label) in enumerate(train_dat):
            # clear the gradients
            optimizer1.zero_grad()
            optimizer2.zero_grad()

            # optimizer.zero_grad()
            if torch.cuda.is_available():
              tgt_data = tgt_data.to(device)
              src_data = src_data.to(device)
              src_label = src_label.to(device)
              tgt_label = tgt_label.to(device)

            # compute the model output
            src_out, tgt_out, dm_out, centr1, centr2 = model(src_data, tgt_data)

            # calculate loss
            loss_classifier = criterion(src_out, src_label)
            loss_classifier_tgt = criterion(tgt_out, tgt_label) * 0.5
            loss_coral = CORAL(centr1, centr2)

            loss_l2 = lambda_l2 * l2loss(dm_out, src_data[:, 0:DDM_NUM+DIFFERECE_COL])
            sum_loss = lambda_ * loss_coral + loss_classifier + loss_classifier_tgt
            epoch_loss += sum_loss.item()
            epoch_loss_l2 += loss_l2.item()
            epoch_loss_classifier += loss_classifier.item()
            epoch_loss_classifier_tgt += loss_classifier_tgt.item()
            epoch_loss_coral += loss_coral.item()

            # credit assignment
            sum_loss.backward(retain_graph=True)
            loss_l2.backward()
            # update model weights
            optimizer2.step()
            optimizer1.step()
            i = i+1

        print('DA Train Epoch: {:2d} [{:2d}/{:2d}]\t'
              'Lambda: {:.4f}, Class: {:.6f}, CORAL: {:.6f}, l2_loss: {:.6f}, Total_Loss: {:.6f}'.format(
            epoch,
            i + 1,
            train_steps,
            lambda_,
            loss_classifier.item(),
            loss_coral.item(),
            loss_l2.item(),
            sum_loss.item()
        ))

        print('DA Train ith Epoch %d result:' % epoch)
        # calculate train src accuracy
        train_acc = evaluate_model_src(train_dat, model, device)
        aggre_train_acc.append(train_acc)
        print('DA train_acc: %.3f' % train_acc)

        # calculate train tgt accuracy
        train_tgt_acc = evaluate_model_tgt(train_dat, model, device)
        aggre_train_tgt_acc.append(train_tgt_acc)
        print('DA train_tgt_acc: %.3f' % train_tgt_acc)

        # # calculate valid accuracy
        valid_acc = evaluate_model_tgt(valid_dat, model, device)
        aggre_valid_acc.append(valid_acc)
        print('DA valid_tgt_acc: %.3f' % valid_acc)

        # # calculate test accuracy
        test_acc = evaluate_model_tgt(test_dat, model, device)
        aggre_test_acc.append(test_acc)
        print('DA test_acc: %.3f' % test_acc)

        epoch_loss = epoch_loss / train_steps
        aggre_losses.append(epoch_loss)
        print(f'DA epoch: {j:3} sum loss: {epoch_loss:6.4f}')

        epoch_loss_l2 = epoch_loss_l2 / train_steps
        aggre_losses_l2.append(epoch_loss_l2)
        print(f'DA epoch: {j:3} l2 loss: {epoch_loss_l2:6.4f}')

        epoch_loss_classifier = epoch_loss_classifier / train_steps
        aggre_losses_classifier.append(epoch_loss_classifier)
        print(f'DA epoch: {j:3} classifier src loss: {epoch_loss_classifier:6.4f}')

        epoch_loss_classifier_tgt= epoch_loss_classifier_tgt / train_steps
        aggre_losses_classifier_tgt.append(epoch_loss_classifier_tgt)
        print(f'DA epoch: {j:3} classifier tgt loss: {epoch_loss_classifier_tgt:6.4f}')

        epoch_loss_coral = epoch_loss_coral / train_steps
        aggre_losses_coral.append(epoch_loss_coral)
        print(f'DA epoch: {j:3} coral loss: {epoch_loss_coral:6.4f}')

        # # calculate validate accuracy
        epoch_loss_valid, epoch_loss_l2_valid, epoch_loss_classifier_valid,  epoch_loss_coral_valid, epoch_loss_classifier_valid_tgt= evaluate_model_stop(valid_dat, model, device)  # evalution on dev set (i.e., holdout from training)
        aggre_losses_valid.append(epoch_loss_valid)
        aggre_losses_l2_valid.append(epoch_loss_l2_valid)
        aggre_losses_classifier_valid.append(epoch_loss_classifier_valid)
        aggre_losses_classifier_valid_tgt.append(epoch_loss_classifier_valid_tgt)
        aggre_losses_coral_valid.append(epoch_loss_coral_valid)

        print(f'DA epoch: {j:3} valid sum loss: {epoch_loss_valid:6.4f}')
        print(f'DA epoch: {j:3} valid l2 loss: {epoch_loss_l2_valid:6.4f}')
        print(f'DA epoch: {j:3} valid classifier loss: {epoch_loss_classifier_valid:6.4f}')
        print(f'DA epoch: {j:3} valid tgt classifier loss: {epoch_loss_classifier_valid_tgt:6.4f}')
        print(f'DA epoch: {j:3} valid coral loss: {epoch_loss_coral_valid:6.4f}')

        if es.step(np.array(epoch_loss_classifier_valid_tgt)):
            print(f'Early Stopping Criteria Met!')
            break  # early stop criterion is met, we can stop now

# evaluate the validation loss for early stop
def evaluate_model_stop(valid_dl, model, device):
  model.eval()
  # define the optimization
  criterion = CrossEntropyLoss()
  l2loss = MSELoss()
  epoch_loss = 0
  epoch_loss_l2 = 0
  epoch_loss_classifier = 0
  epoch_loss_classifier_valid = 0
  epoch_loss_coral = 0

  valid_steps = len(valid_dl)
  print("DA train_steps:", valid_steps)

  for i, (src_data, src_label, tgt_data, tgt_label) in enumerate(valid_dl):
    if torch.cuda.is_available():
      tgt_data = tgt_data.to(device)
      src_data = src_data.to(device)
      src_label = src_label.to(device)
      tgt_label = tgt_label.to(device)

    with torch.no_grad():
      src_out, tgt_out, dm_out, centr1, centr2 = model(src_data, tgt_data)
    # calculate loss
    loss_classifier = criterion(src_out, src_label)
    loss_classifier_valid = criterion(tgt_out, tgt_label) * 0.5
    # loss_coral = CORAL(src_out, tgt_out)
    loss_coral = CORAL(centr1, centr2)
    loss_l2 = l2loss(dm_out, src_data[:, 0:DDM_NUM+DIFFERECE_COL]) * lambda_l2
    # sum_loss = lambda_ * loss_coral + loss_classifier + lambda_l2 * loss_l2 + loss_classifier_valid * 0.5
    sum_loss = lambda_ * loss_coral + loss_classifier
    epoch_loss += sum_loss.item()
    epoch_loss_l2 += loss_l2.item()
    epoch_loss_classifier += loss_classifier.item()
    epoch_loss_classifier_valid += loss_classifier_valid.item()
    epoch_loss_coral += loss_coral.item()

  return epoch_loss / valid_steps, epoch_loss_l2 / valid_steps, epoch_loss_classifier / valid_steps, epoch_loss_coral / valid_steps, epoch_loss_classifier_valid / valid_steps

# evaluate the model source
def evaluate_model_src(test_dl, model, device):
    model.eval()
    predictions, actuals = list(), list()
    for i, (src_data, src_label, tgt_data, tgt_label) in enumerate(test_dl):
    # for i in range(test_steps):
        # evaluate the model on the test set
        if torch.cuda.is_available():
          src_data = src_data.to(device)
          src_label = src_label.to(device)

        tgt_data = src_data
        targets = src_label

        with torch.no_grad():
          yhat, _, _, _, _ = model(tgt_data, tgt_data[:,0:26])
        # retrieve numpy array
        yhat = yhat.detach().cpu().numpy()
        actual = targets.cpu().numpy()
        # convert to class labels
        yhat = argmax(yhat, axis=1)
        # reshape for stacking
        actual = actual.reshape((len(actual), 1))
        yhat = yhat.reshape((len(yhat), 1))
        # store
        predictions.append(yhat)
        actuals.append(actual)
    predictions, actuals = vstack(predictions), vstack(actuals)
    # calculate accuracy
    acc = accuracy_score(actuals, predictions)
    return acc

# evaluate the model target
def evaluate_model_tgt(test_dl, model, device):
    model.eval()
    predictions, actuals = list(), list()
    for i, (src_data, src_label, target_data, target_label) in enumerate(test_dl):
        # evaluate the model on the test set
        if torch.cuda.is_available():
          target_data = target_data.to(device)
          target_label = target_label.to(device)

        tgt_data = target_data
        targets = target_label
        temp = torch.zeros((tgt_data.shape[0], DIFFERECE_COL))
        temp = temp.to(device)
        tmp_data = torch.cat((tgt_data, temp), 1)
        with torch.no_grad():
          _, yhat, _, _, _ = model(tmp_data, tgt_data)
        # retrieve numpy array
        yhat = yhat.detach().cpu().numpy()
        actual = targets.cpu().numpy()
        # convert to class labels
        yhat = argmax(yhat, axis=1)
        # reshape for stacking
        actual = actual.reshape((len(actual), 1))
        yhat = yhat.reshape((len(yhat), 1))
        # store
        predictions.append(yhat)
        actuals.append(actual)
    predictions, actuals = vstack(predictions), vstack(actuals)
    # calculate accuracy
    acc = accuracy_score(actuals, predictions)
    return acc

# train data
X_s = x_train_src
Y_s = y_train_src
X_t = x_train_pt
Y_t = y_train_tgt

# valid data
X_s_valid = x_valid_src
Y_s_valid = y_valid_src
X_t_valid = x_valid_pt
Y_t_valid = y_valid_tgt

# # test data
X_s_test = X_s_test
Y_s_test = Y_s_test
X_t_test = x_test_pt_test
Y_t_test = Y_t_test

train_dat = prepare_data(X_s, Y_s, X_t, Y_t)
valid_dat = prepare_data(X_s_valid, Y_s_valid, X_t_valid, Y_t_valid)
test_dat = prepare_data(X_s_test, Y_s_test, X_t_test, Y_t_test)

model = Deep_coral(num_classes=NUM_LALBELS)
# train the model
train_model(train_dat, valid_dat,test_dat, model, _device)

# evaluate the model
acc = evaluate_model_tgt(test_dat, model, _device)

print('Accuracy: %.3f' % acc)

#plot the sum loss
plt.figure()
plt.plot(range(len(aggre_losses)), aggre_losses, '-', label='train')
plt.plot(range(len(aggre_losses_valid)), aggre_losses_valid, '-', label='valid')
plt.title('Sum loss history')
plt.ylabel('Sum Loss in da')
plt.xlabel('epoch');
plt.legend()

#plot the L2 loss
plt.figure()
plt.plot(range(len(aggre_losses_l2)), aggre_losses_l2, '-', label='train')
plt.plot(range(len(aggre_losses_l2_valid)), aggre_losses_l2_valid, '-', label='valid')
plt.title('L2 loss history')
plt.ylabel('L2 Loss in da')
plt.xlabel('epoch');
plt.legend()

#plot the classifier loss
plt.figure()
plt.plot(range(len(aggre_losses_classifier)), aggre_losses_classifier, '-', label='train src')
plt.plot(range(len(aggre_losses_classifier_tgt)), aggre_losses_classifier_tgt, '-', label='train tgt')
plt.plot(range(len(aggre_losses_classifier_valid)), aggre_losses_classifier_valid, '-', label='valid src')
plt.plot(range(len(aggre_losses_classifier_valid_tgt)), aggre_losses_classifier_valid_tgt, '-', label='valid tgt')
plt.title('Classifier loss history')
plt.ylabel('Classifier Loss in da')
plt.xlabel('epoch');
plt.legend()

#plot the coral loss
plt.figure()
plt.plot(range(len(aggre_losses_coral)), aggre_losses_coral, '-', label='train')
plt.plot(range(len(aggre_losses_coral_valid)), aggre_losses_coral_valid, '-', label='valid')
plt.title('Coral loss history')
plt.ylabel('coral Loss in da')
plt.xlabel('epoch');
plt.legend()

#plot the train accuracy
plt.figure()
plt.plot(range(len(aggre_train_acc)), aggre_train_acc, '-', label='train src')
plt.plot(range(len(aggre_train_tgt_acc)), aggre_train_tgt_acc, '-', label='train tgt')
plt.plot(range(len(aggre_valid_acc)), aggre_valid_acc, '-', label='valid tgt')
plt.plot(range(len(aggre_test_acc)), aggre_test_acc, '-', label='test tgt')
plt.title('Classification accuracy history')
plt.xlabel('Epochs')
plt.ylabel('Clasification accuracy')
plt.legend()
plt.gcf().set_size_inches(14, 4)

#plot the combined
plt.figure()
plt.subplot(1, 2, 1)
plt.plot(range(len(aggre_losses)), aggre_losses, '-')
plt.title('Loss history')
plt.xlabel('Epochs')
plt.ylabel('Loss')

plt.subplot(1, 2, 2)
plt.plot(range(len(aggre_train_acc)), aggre_train_acc, '-', label='train src')
plt.plot(range(len(aggre_train_tgt_acc)), aggre_train_tgt_acc, '-', label='train tgt')
plt.plot(range(len(aggre_test_acc)), aggre_test_acc, '-', label='test tgt')
plt.title('Classification accuracy history')
plt.xlabel('Epochs')
plt.ylabel('Clasification accuracy')
plt.legend()

plt.gcf().set_size_inches(14, 4)
plt.show()

all_results = []

def test_all(prefix, filenames, device):
  for i in range(len(filenames)):
    X_s_test, Y_s_test, X_t_test, Y_t_test = load_test_data(prefix, filenames[i])
    test_dat = prepare_data(X_s_test, Y_s_test, X_t_test, Y_t_test)
    # evaluate the model
    acc = evaluate_model_tgt(test_dat, model, device)
    all_results.append(acc)
    print(filenames[i])
    print('test Accuracy: %.3f' % acc)

test_filenames = ['2017_jan_day_005.npz', '2017_jan_day_013.npz',  '2017_jan_day_019.npz', '2017_jan_day_024.npz', '2017_jan_day_030.npz', '2017_mon1.npz']
test_all(prefix, test_filenames, _device)
print("test_filenames:")
print (test_filenames)
print("all_results:")
print(all_results)

reason: import numpy,from numpy


text:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import t

if __name__ == "__main__":
    # PDFs for the exponential distribution
    sns.set_palette("deep", desat=.6)
    sns.set_context(rc={"figure.figsize": (8, 4)})

    x = np.linspace(0.0, 5.0, 100)
    lambdas = [0.5, 1.0, 2.0]
    # lambdas = [0.5, 1.0, 1.5, 2.0, 2.5, 4.0]

    for lamb in lambdas:
        y = lamb*np.exp(-lamb*x)
        ax = plt.plot(x, y, label="$\\lambda=%s$" % lamb)
    plt.xlabel("x")
    plt.ylabel("P(x)")
    plt.legend(title="Parameters")
    plt.show()

max_stars_repo_name: spideynolove/Other-repo

max_issues_repo_name: spideynolove/Other-repo

max_forks_repo_name: spideynolove/Other-repo

reason: import numpy,from scipy