text:
import sys
sys.path.append('/home/cjy/FudanOCR/recognition_model')
sys.path.append('/home/cjy/FudanOCR/maskrcnn_benmark_architecture')

import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import os
import requests
from io import BytesIO
from PIL import Image
import numpy as np
import cv2

# 调用GRCNN的部分，用于生成一个模型
from GRCNN.models import crann
from GRCNN.utils import util
from GRCNN.utils import keys
import yaml

import torch
from torch.autograd import Variable
import torch.nn.functional as F
import torchvision.transforms as transforms

# ?
# this makes our figures bigger
pylab.rcParams['figure.figsize'] = 20, 12

from maskrcnn_benchmark.config import cfg
from predictor import COCODemo

from PIL import Image

_DEBUG = True

# 这里两个部分处理的是模型的初始化

############## det model init ##############
# 检测部分的生成调用高封装接口，较为简单
# 可以通过传入一个yaml文件的形式

print('Initializing detection model...')

config_file = "/home/cjy/FudanOCR/maskrcnn_benmark_architecture/configs/text_maskrcnn_res50_fpn_lsvt.yaml"

# update the config options with the config file
cfg.merge_from_file(config_file)
# manual override some options
cfg.merge_from_list(["MODEL.DEVICE", "cpu"])

coco_demo = COCODemo(
    cfg,
    min_image_size=800,
    confidence_threshold=0.8,
)

print('done')

############################################

############## rec model init ##############

# 初始化识别模型
# 加载参数,用yaml.load的形式就变成字典了
print('Initializing recognition model...')

config_yaml = '/home/cjy/FudanOCR/config/GRCNN_test.yaml'

f = open(config_yaml)
opt = yaml.load(f)

# 超参数
fixed_height = 32

if opt['N_GPU'] > 1:
    opt['RNN']['multi_gpu'] = True
else:
    opt['RNN']['multi_gpu'] = False

alphabet = keys.alphabet
nClass = len(alphabet) + 1
converter = util.strLabelConverter(alphabet)

# 用这个方式去定义一个模型
rec_model = crann.CRANN(opt, nClass).cuda()

# 这个已经准备好了
model_path = opt['CRANN']

# 加载已有的参数
if os.path.isfile(opt['CRANN']):
    print("=> loading checkpoint '{}'".format(model_path))
    checkpoint = torch.load(model_path)
    start_epoch = checkpoint['epoch']
    # best_pred = checkpoint['best_pred']
    rec_model.load_state_dict(checkpoint['state_dict'])
    # print("=> loaded checkpoint '{}' (epoch {} accuracy {})"
    #       .format(model_path, checkpoint['epoch'], best_pred))

# if _DEBUG:
#    print('rec_model:', rec_model)

# 设置为 eval模式进行测试
rec_model.eval()
print('done')

############################################

def normalize(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # img = Image.fromarray(img)

    img = torch.from_numpy(img).float().div(255)
    img.sub_(0.5).div_(0.5)
    return img

def load(url):
    """
    Given an url of an image, downloads the image and
    returns a PIL image
    """
    response = requests.get(url)
    pil_image = Image.open(BytesIO(response.content)).convert("RGB")
    # convert to BGR format
    image = np.array(pil_image)[:, :, [2, 1, 0]]
    return image

def load_img(impath):
    """
    Given an url of an image, downloads the image and
    returns a PIL image
    """
    # response = requests.get(url)
    pil_image = Image.open(impath).convert("RGB")
    # convert to BGR format
    image = np.array(pil_image)[:, :, [2, 1, 0]]
    return image

def imshow(img):
    plt.imshow(img[:, :, [2, 1, 0]])
    plt.axis("off")

def rotate(image, angle, ctr):
    # convert to cv2 image
    image = np.array(image)
    (h, w) = image.shape[:2]
    scale = 1.0
    # set the rotation center
    center = (int(ctr[0]), int(ctr[1]))
    # anti-clockwise angle in the function
    M = cv2.getRotationMatrix2D(center, angle, scale)
    image = cv2.warpAffine(image, M, (w, h))

    return image

# image process
# 核心函数
def improc(impath):

    # image_dir = ''
    # imlist = os.listdir(image_dir)

    # for imname in imlist:
        # impath = os.path.join(image_dir, imname)

    # from http://cocodataset.org/#explore?id=345434
    image = load_img(impath)
    # imshow(image)

    # 返回结果的字典
    result_dict = {}

    # compute predictions
    # 得到一个检测结果
    res_im, predictions = coco_demo.run_on_opencv_image(image)

    # print('predictions:', predictions.shape)

    # 从predictions抽取mask域
    masks = predictions.get_field('mask')
    masks_np = masks.data.cpu().numpy()

    print('masks_np:', masks_np.shape)

    # 返回得到三个数组
    rboxes = []
    rcrops = []
    rpolys = []

    recs = []

    for i in range(masks_np.shape[0]):
        mask_np = masks_np[i][0]
        contours = cv2.findContours((mask_np * 1).astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        for pts in contours[1]:
            poly = pts.reshape(-1, 2)
            if poly.shape[0] >= 4:
                # print('polygon:', poly.shape[0])
                rect = cv2.minAreaRect(poly)
                poly_q = np.array(cv2.boxPoints(rect), np.int)
                # print('rect:', rect)

                poly_q = poly_q.reshape(-1)
                pt1 = (int(poly_q[0]), int(poly_q[1]))
                pt2 = (int(poly_q[2]), int(poly_q[3]))
                pt3 = (int(poly_q[4]), int(poly_q[5]))
                pt4 = (int(poly_q[6]), int(poly_q[7]))

                edge1 = np.sqrt((pt1[0] - pt2[0]) * (pt1[0] - pt2[0]) + (pt1[1] - pt2[1]) * (pt1[1] - pt2[1]))
                edge2 = np.sqrt((pt2[0] - pt3[0]) * (pt2[0] - pt3[0]) + (pt2[1] - pt3[1]) * (pt2[1] - pt3[1]))

                angle = 0

                if edge1 > edge2:

                    width = edge1
                    height = edge2
                    if pt1[0] - pt2[0] != 0:
                        angle = -np.arctan(float(pt1[1] - pt2[1]) / float(pt1[0] - pt2[0])) / 3.1415926 * 180
                    else:
                        angle = 90.0
                elif edge2 >= edge1:
                    width = edge2
                    height = edge1
                    # print pt2[0], pt3[0]
                    if pt2[0] - pt3[0] != 0:
                        angle = -np.arctan(float(pt2[1] - pt3[1]) / float(pt2[0] - pt3[0])) / 3.1415926 * 180
                    else:
                        angle = 90.0
                if angle < -45.0:
                    angle = angle + 180

                x_ctr = float(pt1[0] + pt3[0]) / 2  # pt1[0] + np.abs(float(pt1[0] - pt3[0])) / 2
                y_ctr = float(pt1[1] + pt3[1]) / 2  # pt1[1] + np.abs(float(pt1[1] - pt3[1])) / 2

                # if height * width * (800 / float(img.shape[0])) < 16 * 16 and mode == "train":
                #    continue

                rboxes.append([x_ctr, y_ctr, width, height, angle])

                rcrops.append(
                    rotate(image, -angle, (x_ctr, y_ctr))
                    [int(y_ctr-height/2):int(y_ctr+height/2), int(x_ctr-width/2):int(x_ctr+width/2)]
                )

                rpolys.append(poly.tolist())

    result_dict['polys'] = rpolys

    cnt = 0

    # crop 应该是裁剪好的图片
    # 对每张crop进行识别
    # 限制图片的高度为32，然后按比例放缩

    for crop in rcrops:
        # rec model infer

        try:
            re_img = cv2.resize(crop, (int(fixed_height / crop.shape[0] * crop.shape[1]), fixed_height))
        except Exception as e:
            # 发生异常
            print('From rec:', e)
            recs.append('')
            continue

        # 在_DEBUG模式下保存图片
        if _DEBUG:
            # cv2.imwrite('demo_img/crops' + str(cnt) + '.img', re_img)
            re_img_pil = Image.fromarray(cv2.cvtColor(re_img, cv2.COLOR_RGB2BGR))
            re_img_pil.save('demo_img/crops' + str(cnt) + '.jpg')
            # cv2.waitKey(0)
        cnt += 1
        # re_img_th = torch.from_numpy(np.transpose(np.expand_dims(re_img, 0), (0, 3, 1, 2))).float().cuda()
        re_img_th = normalize(re_img).unsqueeze(0).permute(0, 3, 1, 2).cuda()
        bsz = re_img_th.size(0)

        # 调用识别模型
        predict = rec_model(re_img_th)
        predict_len = torch.IntTensor([predict.size(0)] * bsz)

        # if _DEBUG:
        #    print('predict:', predict.size())

        # Compute accuracy
        _, acc = predict.max(2)
        # if int(torch.__version__.split('.')[1]) < 2:
        #    acc = acc.squeeze(2)
        acc = acc.transpose(1, 0).contiguous().view(-1)
        prob, _ = F.softmax(predict, dim=2).max(2)
        probilities = torch.mean(prob, dim=1)
        sim_preds = converter.decode(acc.data, predict_len.data, raw=False)
        # if _DEBUG:
        #    print('sim_preds:', sim_preds)

        recs.append(sim_preds)

    # organize results in a dict

    result_dict['recs'] = recs

    return result_dict

max_stars_repo_name: JinGyeSetBirdsFree/FudanOCR


text:
import numpy as np
from PIL import ImageGrab
import mss
import cv2
from pynput.mouse import Button, Controller
import time
import sys

# origin position of the game play screen
# you can modify this parameter according to your own computer
ox = 44
oy = 132
# the position and size for screen capture
# you can modify this parameter according to your own computer
gameBox = {"top": 132, "left": 44, "width": 390, "height": 650}
# four x positions to check piano key dropping
lines = [98, 294, 490, 686]
mouse = Controller()

# find and update mouse position and perform clicking
def click_box_new(new_screen):
    # find the unclicked black piano keys
    ii = np.nonzero(new_screen[:, lines] < 100)
    # return if all keys have been already clicked
    if len(ii[0]) == 0:
        return
    # find the button position of the key
    j = np.argmax(ii[0])
    y = ii[0][j]
    x = lines[ii[1][j]]
    # compute the realy mouse position
    xx = ox + x / 2
    yy = oy + y / 2 + 10
    # only click the position inside the game play screen
    # you can modify this parameter according to your own computer
    if yy > 786 or yy < 180:
        return
    if xx < 60  or xx > 425:
        return
    # update mouse position and perform mouse clicking
    mouse.position = (xx, yy)
    print mouse.position, xx, yy
    mouse.click(Button.left, 1)
    #time.sleep(0.1)
    return

def main():

    # wait for 2 seconds before press start button
    time.sleep(2)
    mouse.click(Button.left, 1)

    # main loop
    for i in range(100000):
        # screen capture
        with mss.mss() as sct:
            screen = np.array(sct.grab(gameBox))
        # convert the color screen to gray
        new_screen = cv2.cvtColor(screen, cv2.COLOR_BGRA2GRAY)
        # perform mouse clicking
        click_box_new(new_screen)
        # move the mouse away to stop the program
        if mouse.position[0] > 500:
            break
        #if cv2.waitKey(25) & 0xFF == ord('q'):
        #    cv2.destroyAllWindows()
        #    break

if __name__ == "__main__":
    main()

ext: py

reason: import numpy


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

from copy import deepcopy
from sys import stdout
from time import sleep
from typing import Callable, List, Tuple

from matplotlib import pyplot as plt
import numpy as np
import torch
import torch.utils.data
from tqdm import tqdm

from util.losses import get_gan_loss

class Discriminator(torch.nn.Module):
    def __init__(self,
                 in_channels: int = 1,
                 channels: List[int] or Tuple[int] = (64, 128, 256, 512, 1),
                 kernels: List[int] or Tuple[int] = (4, 4, 4, 4, 4),
                 strides: List[int] or Tuple[int] = (2, 2, 2, 2, 1),
                 padding: List[int] or Tuple[int] = (1, 1, 1, 1, 0),
                 leaky_relu_slope: float = 0.2,
                 use_batch_norm: bool = True):
        """
        The Discriminator constructor.

        :param in_channels:
            (Optional) An int representing the number of input channels.
            Default: 1.

        :param channels:
            (Optional) A List[int] or Tuple[int] representing the channels of
            every convolutional layer. Default: (64, 128, 256, 512, 1).

        :param kernels:
            (Optional) A List[int] or Tuple[int] representing the kernels of
            every convolutional layer. Default: (4, 4, 4, 4, 4).

        :param strides:
            (Optional) A List[int] or Tuple[int] representing the strides of
            every convolutional layer. Default: (2, 2, 2, 2, 1).

        :param padding:
            (Optional) A List[int] or Tuple[int] representing the padding of
            every convolutional layer. Default: (1, 1, 1, 1, 0).

        :param leaky_relu_slope:
            (Optional) A float representing the slope of the leaky ReLu
            activation functions. Default: 0.2.

        :param use_batch_norm:
            (Optional) A bool: True if you wish to use batch normalization,
            False otherwise. Batch normalization is applied after every layer
            that isn't the input or an output. Default: True.
        """
        super().__init__()

        self._conv = torch.nn.ModuleList()
        self._batch_norm = torch.nn.ModuleList() if use_batch_norm else None
        self._leaky_relu = torch.nn.LeakyReLU(leaky_relu_slope)

        last_out = in_channels

        for i, (chan, kern, stri, padd) in enumerate(zip(channels,
                                                         kernels,
                                                         strides,
                                                         padding)):
            self.conv.append(torch.nn.Conv2d(in_channels=last_out,
                                             out_channels=chan,
                                             kernel_size=kern,
                                             stride=stri,
                                             padding=padd))

            if use_batch_norm and i != 0 and i != (len(channels) - 1):
                self.batch_norm.append(torch.nn.BatchNorm2d(num_features=chan))

            last_out = chan

        self.reset_parameters()

    # region Properties
    @property
    def conv(self) -> List[torch.nn.Conv2d]:
        return self._conv

    @property
    def batch_norm(self) -> List[torch.nn.BatchNorm2d]:
        return self._batch_norm

    @property
    def leaky_relu(self) -> Callable:
        return self._leaky_relu

    # endregion

    def reset_parameters(self):
        """
        Resets this instance's parameters.

        :return:
            Nothing.
        """
        for conv in self.conv[:-1]:
            torch.nn.init.kaiming_normal_(conv.weight,
                                          nonlinearity="leaky_relu")
            torch.nn.init.normal_(conv.bias, 0, 1e-6 / 3)

        torch.nn.init.xavier_normal_(self.conv[-1].weight)
        torch.nn.init.constant_(self.conv[-1].bias, 0.)

    def forward(self, x):
        """
        The forward method of a Discriminator instance.

        :param x:
            A torch.Tensor representing the network input.

        :return:
            A torch.Tensor of shape (B, 1) representing the network's
            confidence that the input is a real image.
        """
        y = self.conv[0](x)
        y = self.leaky_relu(y)

        for i, conv in enumerate(self.conv[1:-1]):
            y = conv(y)
            y = self.leaky_relu(y)

            if self.batch_norm is not None:
                y = self.batch_norm[i](y)

        y = self.conv[-1](y)
        y = y.view(-1)

        return torch.sigmoid(y)

class Generator(torch.nn.Module):
    def __init__(self,
                 input_size: int = 100,
                 channels: List[int] or Tuple[int] = (512, 256, 128, 64, 1),
                 kernels: List[int] or Tuple[int] = (4, 4, 4, 4, 4),
                 strides: List[int] or Tuple[int] = (1, 2, 2, 2, 2),
                 padding: List[int] or Tuple[int] = (0, 1, 1, 1, 1),
                 leaky_relu_slope: float = 0.2,
                 use_batch_norm: bool = True):
        """
        The Generator constructor.

        :param input_size:
            (Optional) An int representing the dimensionality of the samples
            generated. Default: 100.

        :param channels:
            (Optional) A List[int] or Tuple[int] representing the channels of
            every convolutional layer. Default: (512, 256, 128, 64, 1).

        :param kernels:
            (Optional) A List[int] or Tuple[int] representing the kernels of
            every convolutional layer. Default: (4, 4, 4, 4, 4).

        :param strides:
            (Optional) A List[int] or Tuple[int] representing the strides of
            every convolutional layer. Default: (1, 2, 2, 2, 2).

        :param padding:
            (Optional) A List[int] or Tuple[int] representing the padding of
            every convolutional layer. Default: (0, 1, 1, 1, 1).

        :param leaky_relu_slope:
            (Optional) A float representing the slope of the leaky ReLu
            activation functions. Default: 0.2.

        :param use_batch_norm:
            (Optional) A bool: True if you wish to use batch normalization,
            False otherwise. Batch normalization is applied after every layer
            except the output. Default: True.
        """
        super().__init__()

        self._input_size = input_size
        self._conv = torch.nn.ModuleList()
        self._batch_norm = torch.nn.ModuleList() if use_batch_norm else None
        self._leaky_relu = torch.nn.LeakyReLU(leaky_relu_slope)

        last_out = input_size

        for i, (chan, kern, stri, padd) in enumerate(zip(channels,
                                                         kernels,
                                                         strides,
                                                         padding)):
            self.conv.append(torch.nn.ConvTranspose2d(in_channels=last_out,
                                                      out_channels=chan,
                                                      kernel_size=kern,
                                                      stride=stri,
                                                      padding=padd))

            if use_batch_norm and i != (len(channels) - 1):
                self.batch_norm.append(torch.nn.BatchNorm2d(num_features=chan))

            last_out = chan

        self.reset_parameters()

    # region Properties
    @property
    def input_size(self):
        return self._input_size

    @property
    def conv(self) -> List[torch.nn.ConvTranspose2d]:
        return self._conv

    @property
    def batch_norm(self) -> List[torch.nn.BatchNorm2d]:
        return self._batch_norm

    @property
    def leaky_relu(self) -> Callable:
        return self._leaky_relu

    # endregion

    def reset_parameters(self):
        """
        Resets this instance's parameters.

        :return:
            Nothing.
        """
        for conv in self.conv[:-1]:
            torch.nn.init.kaiming_normal_(conv.weight,
                                          nonlinearity="leaky_relu")
            torch.nn.init.normal_(conv.bias, 0, 1e-6 / 3)

        torch.nn.init.xavier_normal_(self.conv[-1].weight)
        torch.nn.init.constant_(self.conv[-1].bias, 0.)

    def forward(self, x):
        """
        The forward method of a Generator instance.

        :param x:
            A torch.Tensor representing the network input.

        :return:
            A torch.Tensor of shape (B, 64, 64) representing the generator's
            output.
        """
        for i in range(len(self.conv) - 1):
            x = self.conv[i](x)
            x = self.leaky_relu(x)

            if self.batch_norm is not None:
                x = self.batch_norm[i](x)

        x = self.conv[-1](x)

        return torch.tanh(x)

class DCGAN(torch.nn.Module):
    def __init__(self,
                 discriminator: Discriminator,
                 generator: Generator):
        """
        The DCGAN constructor.

        :param discriminator:
            A Discriminator object representing the model's discriminator
            module.

        :param generator:
            A Generator object representing the model's generator module.
        """
        super().__init__()

        self._discriminator = deepcopy(discriminator)
        self._generator = deepcopy(generator)

        self._component_names = ("discriminator", "generator")

    # region Properties
    @property
    def discriminator(self) -> Discriminator:
        return self._discriminator

    @property
    def generator(self) -> Generator:
        return self._generator

    @property
    def component_names(self) -> Tuple[str, str]:
        return self._component_names

    # endregion

    def fit(self,
            dataset: torch.utils.data.Dataset,
            n_epochs: int = 1,
            batch_size: int = 1,
            learning_rate: float or Tuple[float, float] or List[float] = 3e-4,
            lr_gamma: float or Tuple[float, float] or List[float] = None,
            loss: Callable = None,
            device: str = "cpu",
            discriminator_batches_till_step: int = 1,
            generator_batches_till_step: int = 1,
            verbose: int = 1):
        """

        :param dataset:
            A torch.utils.data.Dataset representing the dataset your wish to
            fit the model on.

        :param n_epochs:
            (Optional) An int representing the number of epochs you wish to
            train the model for. Default: 1.

        :param batch_size:
            (Optional) An int representing the batch size. Default: 1.

        :param learning_rate:
            (Optional) A float representing the starting learning rate during
            training. Default: 3e-4.

        :param lr_gamma:
            (Optional) A float representing the learning rate decay multiplier
            per fit epoch. Default: None.

        :param loss:
            (Optional) A Callable representing the loss function for the VAE.
            Default: None (takes it from losses.get_gan_loss())

        :param device:
            (Optional) A string representing the device you wish to fit on.
            Default: "cpu".

        :param discriminator_batches_till_step:
            An int representing the number of batches to wait before updating
            the discriminator parameters.

        :param generator_batches_till_step:
            An int representing the number of batches to wait before updating
            the generator parameters.

        :param verbose:
            (Optional) An int representing the level of verbosity you wish to
            have which fitting the model. Default: 1 (progress bar).

        :return:
            Nothing.
        """
        self.train()
        self.to(device)

        if loss is None:
            loss = get_gan_loss()

        if isinstance(learning_rate, int) or isinstance(learning_rate, float):
            learning_rate = tuple([learning_rate] * 2)

        if lr_gamma is None:
            lr_gamma = 1.

        if isinstance(lr_gamma, int) or isinstance(lr_gamma, float):
            lr_gamma = tuple([lr_gamma] * 2)

        loss = {k: loss for k in self.component_names}
        losses = {k: list() for k in self.component_names}

        optimizer = dict()
        scheduler = dict()

        for key, component, lr, gamma in zip(self.component_names,
                                             [self.discriminator,
                                              self.generator],
                                             learning_rate,
                                             lr_gamma):
            optimizer[key] = torch.optim.Adam(component.parameters(),
                                              lr=lr)
            scheduler[key] = torch.optim \
                                  .lr_scheduler \
                                  .ExponentialLR(optimizer[key], gamma=gamma)

        tr_loader = torch.utils.data.DataLoader(dataset,
                                                batch_size=batch_size,
                                                shuffle=True)

        for epoch in range(n_epochs):
            iterator = tqdm(tr_loader, file=stdout)\
                       if verbose > 0\
                       else tr_loader

            for i, (x, _) in enumerate(iterator):
                curr_batch_size = x.shape[0]

                noise = torch.randn((curr_batch_size, self.generator.input_size,
                                    1, 1),
                                    device=device)

                x_real = x.to(device)
                x_fake = self.generator.forward(noise)

                y_real = torch.ones(curr_batch_size, device=device).float()
                y_fake = torch.zeros(curr_batch_size, device=device).float()
                y_dis_real = self.discriminator.forward(x_real)
                y_dis_fake = self.discriminator.forward(x_fake.detach())

                loss_dis = loss[self.component_names[0]](y_dis_real, y_real) +\
                           loss[self.component_names[0]](y_dis_fake, y_fake)
                loss_dis.backward()
                losses[self.component_names[0]].append(float(loss_dis))

                if (i + 1) % discriminator_batches_till_step == 0:
                    optimizer[self.component_names[0]].step()

                # --------------------------------------------------------------

                y_dis_fake = self.discriminator.forward(x_fake)

                loss_gen = loss[self.component_names[1]](y_dis_fake, y_real)
                loss_gen.backward()
                losses[self.component_names[1]].append(float(loss_gen))

                if (i + 1) % generator_batches_till_step == 0:
                    optimizer[self.component_names[1]].step()

                if verbose > 0:
                    iterator.set_description(
                        f"Epoch {epoch + 1}    "
                        f"DisLoss: "
                        f"{np.mean(losses[self.component_names[0]]):.04f}  "
                        f"GenLoss: "
                        f"{np.mean(losses[self.component_names[1]]):.04f}")

                for component_name in self.component_names:
                    optimizer[component_name].zero_grad()

            for component_name in self.component_names:
                scheduler[component_name].step()
                losses[component_name].clear()

    def plot_generations(self,
                         n_samples: int = 4,
                         shape: Tuple[int, int] = None,
                         base_size: Tuple[int, int] = (1.6, 1.6),
                         device: str = "cpu"):
        """
        Plots generated images given a number of samples from the dataset.

        :param n_samples:
            (Optional) An int representing the number of samples you wish to
            plot. Default: 4.

        :param shape:
            (Optional) The shape of the subplots. Default: None (calculates
            the shape dynamically, focusing on a square shape with a width of
            at most 10).

        :param base_size:
            (Optional) A Tuple[float, float] containing the base sizes of a
            subplot. Default: (1.6, 1.6).

        :param device:
            (Optional) A string representing the device you wish to fit on.
            Default: "cpu".

        :return:
            A Tuple[matplotlib.pyplot.Figure, matplotlib.pyplot.Axes]
            containing the plot information.
        """
        self.eval()
        self.to(device)

        if n_samples is None or n_samples < 3:
            n_samples = 4

        if shape is None or shape[0] * shape[1] < n_samples:
            width = min(int((n_samples ** 0.5) + 1e-6), 10)
            height = (n_samples + width - 1) // width

            shape = (height, width)

        fig, ax = plt.subplots(*shape, figsize=(base_size[0] * shape[0],
                                                base_size[1] * shape[1]))

        with torch.no_grad():
            samples = self.generator.forward(
                torch.randn(n_samples, 100, 1, 1, device=device))\
                     .view(n_samples, 64, 64)\
                     .data\
                     .cpu()\
                     .numpy()

            for i in range(n_samples):
                curr_axis = ax[i // shape[0]][i % shape[0]]

                curr_axis.axis("off")
                curr_axis.imshow(samples[i], vmin=0, vmax=1)

        return fig, ax

max_stars_repo_name: Yalfoosh/DUBUCE

max_issues_repo_name: Yalfoosh/DUBUCE

max_forks_repo_name: Yalfoosh/DUBUCE


text:
"""Contains the definition of an input layer for a neural network."""

import numpy as np

from emell.computation import constant, identity
from emell.neuralnetwork.layer import Layer

class InputLayer(Layer):
    """
    The input layer to the network.

    Used so that some operations inside the neural network can be implemented
    uniformly. For instance, adding weights to the layer being added to the
    network.
    """

    def __init__(self, input_count: int):
        """
        Initialize the input layer.

        Parameters
        ----------
        input_count : int
            The number of parameters that are added to the network.

        """
        super().__init__(
            neuron_count=input_count,
            activation=identity,
            activation_prime=constant(np.zeros(input_count)),
        )

    def add_weights(self, weights_count: int) -> None:
        """Raise an error, as there are no input weights to the network."""
        raise NotImplementedError("An input layer cannot be given weights")

    def compute(self, layer_input: np.ndarray) -> "Layer.Result":
        """
        Compute the output of the input layer.

        Parmaeters
        ----------
        layer_input : np.ndarray
            The input to the layer. Also the input to the network.

        """
        if layer_input.ndim != 1:
            raise ValueError("Can only work on vectors")

        if layer_input.shape[0] != self.neuron_count:
            raise ValueError("The layer input does not match the declared input size")

        return Layer.Result(
            layer=self, weighted_output=layer_input, output=layer_input,
        )

    def update_bias(self, delta: np.ndarray) -> None:
        """Update bias does nothing for an input layer."""

    def get_weights(self) -> np.ndarray:
        """Return a degenerate weight set."""
        return np.ones([self.neuron_count])

max_stars_repo_name: jakevoytko/emell

max_issues_repo_name: jakevoytko/emell

max_forks_repo_name: jakevoytko/emell


text:
import pickle
import numpy as np
from typing import Iterable

def to_list(x):
    """
    If x is not a list, convert it to list
    """
    if isinstance(x, Iterable):
        return list(x)
    elif isinstance(x, np.ndarray):
        return x.tolist()
    else:
        return [x]

def expand_1st(x):
    """
    Adding an extra first dimension

    Args:
        x: (np.array)
    Returns:
         (np.array)
    """
    return np.expand_dims(x, axis=0)

def fast_label_binarize(value, labels):
    """Faster version of label binarize

    `label_binarize` from scikit-learn is slow when run 1 label at a time.
    `label_binarize` also is efficient for large numbers of classes, which is not
    common in `megnet`

    Args:
        value: Value to encode
        labels (list): Possible class values
    Returns:
        ([int]): List of integers
    """

    if len(labels) == 2:
        return [int(value == labels[0])]
    else:
        output = [0] * len(labels)
        if value in labels:
            output[labels.index(value)] = 1
        return output

def ring_to_vector(l):
    """
    Convert the ring sizes vector to a fixed length vector
    For example, l can be [3, 5, 5], meaning that the atom is involved
    in 1 3-sized ring and 2 5-sized ring. This function will convert it into
    [ 0, 0, 1, 0, 2, 0, 0, 0, 0, 0].

    Args:
        l: (list of integer) ring_sizes attributes

    Returns:
        (list of integer) fixed size list with the i-1 th element indicates number of
            i-sized ring this atom is involved in.
    """
    return_l = [0] * 9
    if l:
        for i in l:
            return_l[i - 1] += 1
    return return_l

def write_file_to_pickle(file, file_path):
    with open(file_path, 'wb') as f:
        pickle.dump(file, f, pickle.HIGHEST_PROTOCOL)

def read_pickle_from_file(pickle_file):
    with open(pickle_file,'rb') as f:
        x = pickle.load(f)
    return x

class Struct(object):
    def __init__(self, is_copy=False, **kwargs):
        self.add(is_copy, **kwargs)

    def add(self, is_copy=False, **kwargs):
        #self.__dict__.update(kwargs)

        if is_copy == False:
            for key, value in kwargs.items():
                setattr(self, key, value)
        else:
            for key, value in kwargs.items():
                try:
                    setattr(self, key, copy.deepcopy(value))
                    #setattr(self, key, value.copy())
                except Exception:
                    setattr(self, key, value)

    def __str__(self):
        return str(self.__dict__.keys())


text:
"""Tests for neurodsp.utils.norm."""

import numpy as np

from neurodsp.utils.norm import *

###################################################################################################
###################################################################################################

def test_normalize_sig():

    sig1 = np.array([1, 2, 3, 4, 5, 6])
    sig2 = np.array([0, 1, 0, -1, 0, 1, 0, -1])

    new_mean = 2
    new_variance = 2

    for sig in [sig1, sig2]:
        out = normalize_sig(sig, new_mean, new_variance)
        assert np.isclose(out.mean(), new_mean)
        assert np.isclose(out.var(), new_variance)

def test_demean():

    d1 = np.array([1, 2, 3])
    d2 = np.array([0, 1, 2, 3, 0])

    # Check default, which demeans to 0
    out1 = demean(d1)
    assert np.isclose(out1.mean(), 0.)

    # Check demeaning with specified mean
    new_mean = 1.
    out2 = demean(d1, mean=new_mean)
    assert np.isclose(out2.mean(), new_mean)

def test_normalize_variance():

    d1 = np.array([1, 2, 3])
    d2 = np.array([0, 1, 2, 3, 0])

    # Check default, which normalizes variance to 1
    out1 = normalize_variance(d1)
    np.isclose(out1.var(), 1.)

    # Check normalizing with specified variance
    out2 = normalize_variance(d1, 2.)
    np.isclose(out2.var(), 2.)

max_stars_repo_name: bcmartinb/neurodsp

max_issues_repo_name: bcmartinb/neurodsp

max_forks_repo_name: bcmartinb/neurodsp


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

from dwave.embedding.pegasus import find_clique_embedding, find_biclique_embedding
from dwave.embedding.diagnostic import is_valid_embedding
from dwave_networkx.generators.pegasus import pegasus_graph
from random import shuffle
import networkx as nx
import unittest

class TestFindClique(unittest.TestCase):
    def test_k_parameter_int(self):
        k_int = 5
        m = 3

        # Find embedding
        pg = pegasus_graph(m, coordinates=True)
        embedding = find_clique_embedding(k_int, target_graph=pg)

        self.assertTrue(is_valid_embedding(embedding, nx.complete_graph(k_int), pg))

    def test_k_parameter_list(self):
        k_nodes = ['one', 'two', 'three']
        m = 4

        # Find embedding
        pg = pegasus_graph(m, coordinates=True)
        embedding = find_clique_embedding(k_nodes, target_graph=pg)

        self.assertTrue(is_valid_embedding(embedding, nx.complete_graph(k_nodes), pg))

    def test_k_parameter_graph(self):
        k_graph = nx.complete_graph(10)
        m = 4

        # Find embedding
        pg = pegasus_graph(m, coordinates=True)
        embedding = find_clique_embedding(k_graph, target_graph=pg)

        self.assertTrue(is_valid_embedding(embedding, nx.complete_graph(k_graph), pg))

    def test_zero_clique(self):
        k = 0
        m = 3

        # Find embedding
        pg = pegasus_graph(m)
        embedding = find_clique_embedding(k, target_graph=pg)

        self.assertEqual(k, len(embedding))
        self.assertEqual({}, embedding)

    def test_one_clique(self):
        k = 1
        m = 4

        # Find embedding
        pg = pegasus_graph(m, coordinates=True)
        embedding = find_clique_embedding(k, target_graph=pg)

        self.assertTrue(is_valid_embedding(embedding, nx.complete_graph(k), pg))

    def test_valid_clique_ints(self):
        k = nx.complete_graph(15)
        m = 4

        # Find embedding
        pg = pegasus_graph(m)
        embedding = find_clique_embedding(k, target_graph=pg)

        self.assertTrue(is_valid_embedding(embedding, k, pg))

    def test_valid_clique_nice_coordinates(self):
        k = nx.complete_graph(15)
        m = 4

        # Find embedding
        pg = pegasus_graph(m, nice_coordinates=True)
        embedding = find_clique_embedding(k, target_graph=pg)

        self.assertTrue(is_valid_embedding(embedding, k, pg))

    def test_valid_clique_coord(self):
        k = nx.complete_graph(15)
        m = 4

        # Find embedding
        pg = pegasus_graph(m, coordinates=True)
        embedding = find_clique_embedding(k, target_graph=pg)

        self.assertTrue(is_valid_embedding(embedding, k, pg))

    def test_impossible_clique(self):
        k = 55
        m = 2

        # Find embedding
        pg = pegasus_graph(m)

        with self.assertRaises(ValueError):
            find_clique_embedding(k, target_graph=pg)

    def test_clique_incomplete_graph(self):
        k = 5
        m = 2

        # Nodes in a known K5 embedding
        # Note: {0: [14, 32], 1: [33, 15], 2: [16, 34], 3: [35, 17], 4: [36, 12]}
        known_embedding_nodes = {14, 32, 33, 15, 16, 34, 35, 17, 36, 12}

        # Create graph with missing nodes
        incomplete_pg = pegasus_graph(m)
        removed_nodes = set(incomplete_pg.nodes) - known_embedding_nodes
        incomplete_pg.remove_nodes_from(removed_nodes)

        # See if clique embedding is found
        embedding = find_clique_embedding(k, target_graph=incomplete_pg)
        self.assertTrue(is_valid_embedding(embedding, nx.complete_graph(k), incomplete_pg))

    def test_clique_missing_edges(self):
        k = 9
        m = 2

        pg = pegasus_graph(m)

        #pick a random ordering of the edges
        edges = list(pg.edges())
        shuffle(edges)

        K = nx.complete_graph(k)

        #now delete edges, one at a time, until we can no longer embed K
        with self.assertRaises(ValueError):
            while 1:
                (u, v) = edges.pop()
                pg.remove_edge(u, v)

                # See if clique embedding is found
                embedding = find_clique_embedding(k, target_graph=pg)
                self.assertTrue(is_valid_embedding(embedding, K, pg))

class Test_find_biclique_embedding(unittest.TestCase):
    def test_full_yield_one_tile_k44(self):
        left, right = find_biclique_embedding(6, 6, 2)
        left, right = find_biclique_embedding(16, 16, 3)
        # smoke test for now

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

max_stars_repo_name: arcondello/dwave-system

max_issues_repo_name: arcondello/dwave-system

max_forks_repo_name: arcondello/dwave-system

reason: import networkx


text:
# Copyright (c) Microsoft Corporation and contributors.
# Licensed under the MIT License.

import logging
import numpy as np
import pandas as pd
import pickle
import scipy.optimize as opt
from sklearn.dummy import DummyClassifier
from time import time

from ._constants import _PRECISION, _INDENTATION, _LINE

from fairlearn.reductions._moments import ClassificationMoment
from fairlearn.reductions._moments import CDF_DemographicParity

logger = logging.getLogger(__name__)

class _Lagrangian:
    """Operations related to the Lagrangian.

    Parameters
    ----------
    X : {numpy.ndarray, pandas.DataFrame}
        the training features
    sensitive_features : {numpy.ndarray, pandas.Series, pandas.DataFrame, list}
        the sensitive features to use for constraints
    y : {numpy.ndarray, pandas.Series, pandas.DataFrame, list}
        the training labels
    estimator :
        the estimator to fit in every iteration of :meth:`best_h` using a
        :meth:`fit` method with arguments `X`, `y`, and `sample_weight`
    constraints : fairlearn.reductions.Moment
        Object describing the parity constraints. This provides the reweighting
        and relabelling.
    B : float
        bound on the L1-norm of the lambda vector
    opt_lambda : bool
        indicates whether to optimize lambda during the calculation of the
        Lagrangian; optional with default value True
    """

    def __init__(self, X, sensitive_features, y, estimator, constraints, B, opt_lambda=True):
        self.X = X
        self.n = self.X.shape[0]
        self.y = y
        self.constraints = constraints
        self.constraints.load_data(X, y, sensitive_features=sensitive_features)
        self.obj = self.constraints.default_objective()
        self.obj.load_data(X, y, sensitive_features=sensitive_features)
        self.pickled_estimator = pickle.dumps(estimator)
        self.B = B
        self.opt_lambda = opt_lambda
        self.hs = pd.Series(dtype="float64")
        self.predictors = pd.Series(dtype="float64")
        self.errors = pd.Series(dtype="float64")
        self.gammas = pd.DataFrame()
        self.lambdas = pd.DataFrame()
        self.n_oracle_calls = 0
        self.oracle_execution_times = []
        self.n_oracle_calls_dummy_returned = 0
        self.last_linprog_n_hs = 0
        self.last_linprog_result = None

    def _eval(self, Q, lambda_vec):
        """Return the value of the Lagrangian.

        Parameters
        ----------
        Q : {pandas.Series, callable}
            `Q` is either a series of weights summing up to 1 that indicate
            the weight of each `h` in contributing to the randomized
            predictor, or a callable corresponding to a deterministic
            `predict` function.
        lambda_vec : pandas.Series
            lambda vector

        Returns
        -------
        tuple
            tuple `(L, L_high, gamma, error)` where `L` is the value of the
            Lagrangian, `L_high` is the value of the Lagrangian under the best
            response of the lambda player, `gamma` is the vector of constraint
            violations, and `error` is the empirical error
        """
        if callable(Q):
            error = self.obj.gamma(Q)[0]
            gamma = self.constraints.gamma(Q)
        else:
            error = self.errors[Q.index].dot(Q)
            gamma = self.gammas[Q.index].dot(Q)

        if self.opt_lambda:
            lambda_projected = self.constraints.project_lambda(lambda_vec)
            L = error + np.sum(lambda_projected * (gamma - self.constraints.bound()))
        else:
            L = error + np.sum(lambda_vec * (gamma - self.constraints.bound()))

        max_constraint = (gamma - self.constraints.bound()).max()
        if max_constraint <= 0:
            L_high = error
        else:
            L_high = error + self.B * max_constraint
        return L, L_high, gamma, error

    def eval_gap(self, Q, lambda_hat, nu):
        r"""Return the duality gap object for the given :math:`Q` and :math:`\hat{\lambda}`."""
        L, L_high, gamma, error = self._eval(Q, lambda_hat)
        result = _GapResult(L, L, L_high, gamma, error)
        for mul in [1.0, 2.0, 5.0, 10.0]:
            h_hat, h_hat_idx = self.best_h(mul * lambda_hat)
            logger.debug("%smul=%.0f", _INDENTATION, mul)
            L_low_mul, _, _, _ = self._eval(pd.Series({h_hat_idx: 1.0}), lambda_hat)
            if L_low_mul < result.L_low:
                result.L_low = L_low_mul
            if result.gap() > nu + _PRECISION:
                break
        return result

    def solve_linprog(self, nu):
        n_hs = len(self.hs)
        n_constraints = len(self.constraints.index)
        if self.last_linprog_n_hs == n_hs:
            return self.last_linprog_result
        c = np.concatenate((self.errors, [self.B]))
        A_ub = np.concatenate((self.gammas.sub(self.constraints.bound(), axis=0),
                               -np.ones((n_constraints, 1))), axis=1)
        b_ub = np.zeros(n_constraints)
        A_eq = np.concatenate((np.ones((1, n_hs)), np.zeros((1, 1))), axis=1)
        b_eq = np.ones(1)
        result = opt.linprog(c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, method='simplex')
        Q = pd.Series(result.x[:-1], self.hs.index)
        dual_c = np.concatenate((b_ub, -b_eq))
        dual_A_ub = np.concatenate((-A_ub.transpose(), A_eq.transpose()), axis=1)
        dual_b_ub = c
        dual_bounds = [(None, None) if i == n_constraints else (0, None) for i in range(n_constraints + 1)]  # noqa: E501
        result_dual = opt.linprog(dual_c,
                                  A_ub=dual_A_ub,
                                  b_ub=dual_b_ub,
                                  bounds=dual_bounds,
                                  method='simplex')
        lambda_vec = pd.Series(result_dual.x[:-1], self.constraints.index)
        self.last_linprog_n_hs = n_hs
        self.last_linprog_result = (Q, lambda_vec, self.eval_gap(Q, lambda_vec, nu))
        return self.last_linprog_result

    def _call_oracle(self, lambda_vec):
        signed_weights = self.obj.signed_weights() + self.constraints.signed_weights(lambda_vec)
        if isinstance(self.constraints, ClassificationMoment):
            redY = 1 * (signed_weights > 0)
            redW = signed_weights.abs()
        elif isinstance(self.constraints,CDF_DemographicParity):
            redY = self.constraints.optimal_label(signed_weights)
            redW = pd.Series(1,index=redY.index)
        else:
            redY = self.y
            redW = signed_weights.abs()

        redW = self.n * redW / redW.sum()

        redY_unique = np.unique(redY)

        classifier = None
        if len(redY_unique) == 1:
            logger.debug("redY had single value. Using DummyClassifier")
            classifier = DummyClassifier(strategy='constant',
                                         constant=redY_unique[0])
            self.n_oracle_calls_dummy_returned += 1
        else:
            classifier = pickle.loads(self.pickled_estimator)

        oracle_call_start_time = time()
        classifier.fit(self.X, redY, sample_weight=redW)
        self.oracle_execution_times.append(time() - oracle_call_start_time)
        self.n_oracle_calls += 1

        return classifier

    def best_h(self, lambda_vec):
        """Solve the best-response problem.

        Returns the classifier that solves the best-response problem for
        the vector of Lagrange multipliers `lambda_vec`.
        """
        classifier = self._call_oracle(lambda_vec)
        def h(X): return classifier.predict(X)
        h_error = self.obj.gamma(h)[0]
        h_gamma = self.constraints.gamma(h)
        h_value = h_error + h_gamma.dot(lambda_vec)

        if not self.hs.empty:
            values = self.errors + self.gammas.transpose().dot(lambda_vec)
            best_idx = values.idxmin()
            best_value = values[best_idx]
        else:
            best_idx = -1
            best_value = np.PINF

        if h_value < best_value - _PRECISION:
            logger.debug("%sbest_h: val improvement %f", _LINE, best_value - h_value)
            h_idx = len(self.hs)
            self.hs.at[h_idx] = h
            self.predictors.at[h_idx] = classifier
            self.errors.at[h_idx] = h_error
            self.gammas[h_idx] = h_gamma
            self.lambdas[h_idx] = lambda_vec.copy()
            best_idx = h_idx

        return self.hs[best_idx], best_idx

class _GapResult:
    """The result of a duality gap computation."""

    def __init__(self, L, L_low, L_high, gamma, error):
        self.L = L
        self.L_low = L_low
        self.L_high = L_high
        self.gamma = gamma
        self.error = error

    def gap(self):
        return max(self.L - self.L_low, self.L_high - self.L)

reason: import numpy,import scipy


text:
import numpy
import numpy as np
import theano
import theano.tensor as T

from lasagne import init
from lasagne import nonlinearities

#from lasagne.base import *
from lasagne import *
from theano.tensor.shared_randomstreams import RandomStreams

__all__ = [
    "AutoEncoder",
]

class AutoEncoder(layers.Layer):
    """ asdsa """
    def __init__(self, incoming, num_units, n_hidden, W=init.Uniform(), bhid=init.Constant(0.), bvis=init.Constant(0.),
                 nonlinearity=nonlinearities.rectify, **kwargs):
        super(AutoEncoder, self).__init__(incoming, **kwargs)
        if nonlinearity is None:
            self.nonlinearity = nonlinearities.identity
        else:
            self.nonlinearity = nonlinearity

        self.num_units = num_units
        self.n_hidden = n_hidden
        self.x = incoming

        #num_inputs = int(np.prod(self.input_shape[1:]))
        num_inputs = num_units
        rng = np.random.RandomState(123)
        self.rng = RandomStreams(rng.randint(2 ** 30))

        initial_W = np.asarray(
            rng.uniform(
                    low=-4 * np.sqrt(6. / (n_hidden + num_units)),
                    high=4 * np.sqrt(6. / (n_hidden + num_units)),
                    size=(num_units, n_hidden)
            ),
            dtype=theano.config.floatX
        )

        #self.W = self.create_param(initial_W, (num_inputs, n_hidden), name="W")
        #self.bvis = self.create_param(bvis, (num_units,), name="bvis") if bvis is not None else None
        #self.bhid = self.create_param(bhid, (n_hidden,), name="bhid") if bhid is not None else None
        self.W = theano.shared(value=initial_W, name='W', borrow=True)

        bvis = theano.shared(
                value=np.zeros(
                    num_units,
                    dtype=theano.config.floatX
                ),
                borrow=True
            )

        bhid = theano.shared(
                value=np.zeros(
                    n_hidden,
                    dtype=theano.config.floatX
                ),
                name='b',
                borrow=True
            )

        # b corresponds to the bias of the hidden
        self.b = bhid
        # b_prime corresponds to the bias of the visible
        self.b_prime = bvis
        # tied weights, therefore W_prime is W transpose
        self.W_prime = self.W.T

    def get_corrupted_input(self, input, corruption_level = 0.1):
        print(("SIZE ======",input.shape))

        blah = theano.shared(value=np.zeros((28, 28),dtype=theano.config.floatX),)

        return self.rng.binomial(size=(28,28), n=1,
                                        p=1 - corruption_level,
                                        dtype=theano.config.floatX) #* input

    def get_hidden_values(self, input):
        """ Computes the values of the hidden layer """
        return T.nnet.sigmoid(T.dot(input, self.W) + self.b)

    def get_reconstructed_input(self, hidden):
        """ Computes the reconstructed input given the values of the hidden layer """
        return T.nnet.sigmoid(T.dot(hidden, self.W_prime) + self.b_prime)

    def get_params(self):
        return [self.W] + self.get_bias_params()

    def get_bias_params(self):
        return [self.b, self.b_prime] if self.b is not None else []

    def get_output_shape_for(self, input_shape):
        return (self.n_hidden,self.n_hidden)

    def get_output_for(self, input, *args, **kwargs):

        tilde_x = self.get_corrupted_input(self.x, corruption_level = 0.1)
        y = self.get_hidden_values(tilde_x)
        z = self.get_reconstructed_input(y)
        L = - T.sum(self.x * T.log(z) + (1 - self.x) * T.log(1 - z), axis=1)
        activation = T.mean(L)

        return self.nonlinearity(activation)

max_stars_repo_name: Keesiu/meta-kaggle

max_issues_repo_name: Keesiu/meta-kaggle

max_forks_repo_name: Keesiu/meta-kaggle

reason: import numpy,import theano,from theano


text:
import os
import scipy.misc
import numpy as np
import tensorflow as tf
from utils import pp, visualize, to_json

from models.dcgan_mnist import DCGAN_MNIST
from models.dcgan_celeba import DCGAN_CelebA
from models.dcwgan_mnist import DCWGAN_MNIST
from models.dcwgan_celeba import DCWGAN_CelebA
from models.bigan_celeba import BiGAN_CelebA
from models.biwgan_celeba import BiWGAN_CelebA

flags = tf.app.flags
FLAGS = flags.FLAGS

flags.DEFINE_integer("epoch", 25, "Epoch to train [25]")
flags.DEFINE_float("learning_rate", 0.0001, "Learning rate of for adam [0.0002]")
flags.DEFINE_float("beta1", 0.5, "Momentum term of adam [0.5]")
flags.DEFINE_integer("train_size", np.inf, "The size of train images [np.inf]")
flags.DEFINE_integer("batch_size", 64, "The size of batch images [64]")
flags.DEFINE_integer("input_height", 108, "The size of image to use (will be center cropped). [108]")
flags.DEFINE_integer("input_width", None, "The size of image to use (will be center cropped). If None, same value as input_height [None]")
flags.DEFINE_integer("output_height", 64, "The size of the output images to produce [64]")
flags.DEFINE_integer("output_width", None, "The size of the output images to produce. If None, same value as output_height [None]")
flags.DEFINE_integer("c_dim", 3, "Dimension of image color. [3]")
flags.DEFINE_string("stash_dir", ".", "The directory where all generated content goes")
flags.DEFINE_string("model", "dcgan_celeba", "The name of model to use [dcgan_celeba, dcgan_mnist, dcwgan_mnist]")
flags.DEFINE_string("input_fname_pattern", "*.jpg", "Glob pattern of filename of input images [*]")
flags.DEFINE_string("checkpoint_dir", FLAGS.stash_dir + "/checkpoint", "Directory name to save the checkpoints [checkpoint]")
flags.DEFINE_string("sample_dir", FLAGS.stash_dir + "/samples", "Directory name to save the image samples [samples]")
flags.DEFINE_boolean("is_train", False, "True for training, False for testing [False]")
flags.DEFINE_boolean("is_crop", False, "True for training, False for testing [False]")
flags.DEFINE_boolean("visualize", False, "True for visualizing, False for nothing [False]")

def main(_):
  pp.pprint(flags.FLAGS.__flags)

  if FLAGS.input_width is None:
    FLAGS.input_width = FLAGS.input_height
  if FLAGS.output_width is None:
    FLAGS.output_width = FLAGS.output_height

  if not os.path.exists(FLAGS.checkpoint_dir):
    os.makedirs(FLAGS.checkpoint_dir)
  if not os.path.exists(FLAGS.sample_dir):
    os.makedirs(FLAGS.sample_dir)

  #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
  run_config = tf.ConfigProto()
  run_config.gpu_options.allow_growth=True

  with tf.Session(config=run_config) as sess:
    if FLAGS.model == 'dcgan_mnist':
      FLAGS.dataset = 'mnist'
      model = DCGAN_MNIST(
          sess,
          batch_size=FLAGS.batch_size,
          y_dim=10,
          z_dim=100,
          c_dim=1,
          checkpoint_dir=FLAGS.checkpoint_dir,
          sample_dir=FLAGS.sample_dir)
    elif FLAGS.model == 'dcgan_celeba':
      FLAGS.dataset = 'celebA'
      model = DCGAN_CelebA(
          sess,
          batch_size=FLAGS.batch_size,
          c_dim=FLAGS.c_dim,
          input_fname_pattern=FLAGS.input_fname_pattern,
          is_crop=FLAGS.is_crop,
          checkpoint_dir=FLAGS.checkpoint_dir,
          sample_dir=FLAGS.sample_dir)
    elif FLAGS.model == 'dcwgan_mnist':
      FLAGS.dataset = 'mnist'
      model = DCWGAN_MNIST(
          sess,
          batch_size=FLAGS.batch_size,
          n_critic=5,
          y_dim=10,
          z_dim=100,
          c_dim=1,
          checkpoint_dir=FLAGS.checkpoint_dir,
          sample_dir=FLAGS.sample_dir)
    elif FLAGS.model == 'dcwgan_celeba':
      FLAGS.dataset = 'celebA'
      model = DCWGAN_CelebA(
          sess,
          batch_size=FLAGS.batch_size,
          n_critic=5,
          c_dim=FLAGS.c_dim,
          input_fname_pattern=FLAGS.input_fname_pattern,
          is_crop=FLAGS.is_crop,
          checkpoint_dir=FLAGS.checkpoint_dir,
          sample_dir=FLAGS.sample_dir)
    elif FLAGS.model == 'bigan_celeba':
      FLAGS.dataset = 'celebA'
      model = BiGAN_CelebA(
          sess,
          batch_size=FLAGS.batch_size,
          c_dim=FLAGS.c_dim,
          input_fname_pattern=FLAGS.input_fname_pattern,
          is_crop=FLAGS.is_crop,
          checkpoint_dir=FLAGS.checkpoint_dir,
          sample_dir=FLAGS.sample_dir)
    elif FLAGS.model == 'biwgan_celeba':
      FLAGS.dataset = 'celebA'
      model = BiWGAN_CelebA(
          sess,
          batch_size=FLAGS.batch_size,
          c_dim=FLAGS.c_dim,
          input_fname_pattern=FLAGS.input_fname_pattern,
          is_crop=FLAGS.is_crop,
          checkpoint_dir=FLAGS.checkpoint_dir,
          sample_dir=FLAGS.sample_dir)

    else:
      print('No such model.')

    if FLAGS.is_train:
      model.train(FLAGS)
    else:
      if not model.load(FLAGS.checkpoint_dir):
        raise Exception("[!] Train a model first, then run test mode")

    # to_json("./web/js/layers.js", [dcgan.h0_w, dcgan.h0_b, dcgan.g_bn0],
    #                 [dcgan.h1_w, dcgan.h1_b, dcgan.g_bn1],
    #                 [dcgan.h2_w, dcgan.h2_b, dcgan.g_bn2],
    #                 [dcgan.h3_w, dcgan.h3_b, dcgan.g_bn3],
    #                 [dcgan.h4_w, dcgan.h4_b, None])

    # Below is codes for visualization
    OPTION = 1
    visualize(sess, model, FLAGS, OPTION)

if __name__ == '__main__':
  tf.app.run()

max_stars_repo_name: adityab/generative

max_issues_repo_name: adityab/generative

max_forks_repo_name: adityab/generative


text:
import joblib
import numpy as np
from subroutines import load_data
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from imblearn.over_sampling import RandomOverSampler
from sklearn.ensemble import GradientBoostingClassifier

random_sampler = RandomOverSampler()

KNN = KNeighborsClassifier(
    n_neighbors = int(np.sqrt(200)),
    n_jobs = -1
)

RF = RandomForestClassifier(
    n_estimators = 10000,
    criterion = 'gini',
    min_samples_split = 0.01,
    min_samples_leaf = 0.01,
    max_features = 'sqrt',
    n_jobs = -1,
    random_state = 2,
    verbose = 9,
)

NB = GaussianNB()

GB = GradientBoostingClassifier(
    n_estimators = 1000,
    min_samples_split = 0.05,
    min_samples_leaf = 0.05,
    verbose = 9
)

def model_gen(asset, X, Y):
    X_train, Y_train = random_sampler.fit_resample(X, Y)
    KNN.fit(X_train, Y_train)
    RF.fit(X_train, Y_train)
    joblib.dump(KNN, 'models/KNN/'+asset+'.pkl')
    joblib.dump(RF, 'models/RF/'+asset+'.pkl')

if __name__ == "__main__":
    X, Y = load_data('BTC', 'data/')
    X_train, Y_train = random_sampler.fit_resample(X, Y)
    NB.fit(X_train, Y_train)
    joblib.dump(NB, "models/NB/BTC.pkl")
    GB.fit(X_train, Y_train)
    joblib.dump(GB, "models/GB/BTC.pkl")

max_stars_repo_name: SnowCheetos/BubbleMint

max_issues_repo_name: SnowCheetos/BubbleMint

max_forks_repo_name: SnowCheetos/BubbleMint


text:
import nose.tools
import numpy
from palm.probability_vector import make_prob_vec_from_state_ids
from palm.rate_matrix import make_rate_matrix_from_state_ids
from palm.blink_model import StateIDCollection
from palm.linalg import ScipyMatrixExponential2, QitMatrixExponential
from palm.util import ALMOST_ZERO

@nose.tools.istest
def qit_matches_scipy_for_matrix_with_values_near_zero():
    state_ids = StateIDCollection()
    state_ids.add_id('a')
    state_ids.add_id('b')
    state_ids.add_id('c')
    rate_matrix = make_rate_matrix_from_state_ids(state_ids, state_ids)
    rate_matrix.set_rate('a', 'b', 0.1)
    rate_matrix.set_rate('a', 'c', 0.01)
    rate_matrix.set_rate('b', 'c', 0.02)
    rate_matrix.set_rate('b', 'a', 0.01)
    rate_matrix.set_rate('c', 'a', 0.03)
    rate_matrix.set_rate('c', 'b', 0.05)
    rate_matrix.balance_transition_rates()
    prob_vec = make_prob_vec_from_state_ids(state_ids)
    prob_vec.set_state_probability('a', 0.2)
    prob_vec.set_state_probability('b', 0.1)
    prob_vec.set_state_probability('c', 0.7)
    compute_and_compare_expm(rate_matrix, 1.0, prob_vec)

@nose.tools.istest
def qit_matches_scipy_for_matrix_with_large_values():
    state_ids = StateIDCollection()
    state_ids.add_id('a')
    state_ids.add_id('b')
    state_ids.add_id('c')
    rate_matrix = make_rate_matrix_from_state_ids(state_ids, state_ids)
    rate_matrix.set_rate('a', 'b', 100.0)
    rate_matrix.set_rate('a', 'c', 50.0)
    rate_matrix.set_rate('b', 'c', 40.0)
    rate_matrix.set_rate('b', 'a', 30.0)
    rate_matrix.set_rate('c', 'a', 40.0)
    rate_matrix.set_rate('c', 'b', 200.)
    rate_matrix.balance_transition_rates()
    prob_vec = make_prob_vec_from_state_ids(state_ids)
    prob_vec.set_state_probability('a', 1e-6)
    prob_vec.set_state_probability('b', 0.0)
    prob_vec.set_state_probability('c', 10.0)
    compute_and_compare_expm(rate_matrix, 1.0, prob_vec)

def compute_and_compare_expm(rate_matrix, dwell_time, prob_vec):
    scipy_expm = ScipyMatrixExponential2()
    qit_expm = QitMatrixExponential()
    scipy_expQt_vec = scipy_expm.compute_matrix_expv(
                            rate_matrix, 10.0, prob_vec)
    qit_expQt_vec = qit_expm.compute_matrix_expv(
                            rate_matrix, 10.0, prob_vec)
    test_condition = scipy_expQt_vec.allclose(qit_expQt_vec)
    error_msg = "Vectors don't match.\nscipy\n%s\nqit\n%s\n"\
                "given rate matrix\n%s\nand vector\n%s\nas input" % \
                (str(scipy_expQt_vec), str(qit_expQt_vec),
                 str(rate_matrix), str(prob_vec))
    nose.tools.ok_(test_condition, error_msg)
    print error_msg
    print rate_matrix.compute_norm()
    print prob_vec.compute_norm()

max_stars_repo_name: milapour/palm

max_issues_repo_name: milapour/palm

max_forks_repo_name: milapour/palm


text:
import numpy as np
import pytest
from utils.datasets import DeepFashionDataset
from utils.datasets import Siamesize
from config.deep_fashion import root_dir
from config.fashion_mnist import FashionMNISTConfig
from utils.datasets import FashionMNIST

def test_siamesize_mnist_basic():
    cfg = FashionMNISTConfig

    ds_kwargs = {
        'root': cfg.root,
    }
    train_ds = FashionMNIST(train=True, download=True, **ds_kwargs)
    siamese_ds = Siamesize(train_ds)

    targets = list()
    for idx1 in range(5000):
        idx2, is_similar = siamese_ds._get_idx2_and_target(idx1)
        assert is_similar in [0, 1]
        assert idx1 != idx2
        # store the targets
        targets.append(is_similar)

    # empirically check probability of the target getting 0 or 1
    prob = np.mean(targets)
    assert 0.4 <= prob <= 0.6

def test_siamesize_train_ds_basic():
    """
    Basic testing
    """
    dataset = DeepFashionDataset(root_dir, 'train')
    siamese_ds = Siamesize(dataset)

    targets = list()
    for idx1 in range(5000):
        idx2, is_similar = siamese_ds._get_idx2_and_target(idx1)
        assert is_similar in [0, 1]
        assert idx1 != idx2
        # store the targets
        targets.append(is_similar)

    # empirically check probability of the target getting 0 or 1
    prob = np.mean(targets)
    assert 0.4 <= prob <= 0.6

def test_siamesize_train_ds():
    """
    One epoch testing
    """
    dataset = DeepFashionDataset(root_dir, 'train')
    siamese_ds = Siamesize(dataset)

    targets = list()
    for idx1 in range(len(siamese_ds)):
        idx2, is_similar = siamese_ds._get_idx2_and_target(idx1)
        # store the targets
        targets.append(is_similar)

    # empirically check probability of the target getting 0 or 1
    prob = np.mean(targets)
    assert 0.48 <= prob <= 0.52

def test_siamesize_val_ds():
    dataset = DeepFashionDataset(root_dir, 'val')
    siamese_ds = Siamesize(dataset)
    assert not siamese_ds.train

    # first round
    pairs = list()
    targets = list()
    for idx1 in range(len(siamese_ds)):
        idx2, is_similar = siamese_ds._get_idx2_and_target(idx1)
        assert is_similar in [0, 1]
        assert idx1 != idx2
        # store the targets
        pairs.append((idx1, idx2))
        targets.append(is_similar)

    prob = np.mean(targets)
    assert 0.48 <= prob <= 0.52

    # second round
    for idx1 in range(len(siamese_ds)):
        idx2, is_similar = siamese_ds._get_idx2_and_target(idx1)
        assert (idx1, idx2) in pairs

def test_siamesize_test_ds():
    dataset = DeepFashionDataset(root_dir, 'test')
    siamese_ds = Siamesize(dataset)
    assert not siamese_ds.train

    # first round
    pairs = list()
    targets = list()
    for idx1 in range(len(siamese_ds)):
        idx2, is_similar = siamese_ds._get_idx2_and_target(idx1)
        assert is_similar in [0, 1]
        assert idx1 != idx2
        # store the targets
        pairs.append((idx1, idx2))
        targets.append(is_similar)

    prob = np.mean(targets)
    assert 0.48 <= prob <= 0.52

    # second round
    for idx1 in range(len(siamese_ds)):
        idx2, is_similar = siamese_ds._get_idx2_and_target(idx1)
        assert (idx1, idx2) in pairs

@pytest.mark.timeout(200)
def test_siamesize_train_ds_epoch():
    n_epochs = 10
    dataset = DeepFashionDataset(root_dir, 'val')

    # make it trainable
    dataset.train = True
    siamese_ds = Siamesize(dataset)

    for epoch in range(n_epochs):
        for idx1 in range(len(siamese_ds)):
            idx2, is_similar = siamese_ds._get_idx2_and_target(idx1)


text:
import pandas as pd
import numpy as np
from pandas import ExcelWriter
from pandas import ExcelFile
import tensorflow as tf
import matplotlib.pylab as plt
import matplotlib.animation as animation
from scipy.interpolate import interp1d

def qhull(sample):
    link = lambda a,b: np.concatenate((a,b[1:]))
    edge = lambda a,b: np.concatenate(([a],[b]))

    def dome(sample,base):
        h, t = base
        dists = np.dot(sample-h, np.dot(((0,-1),(1,0)),(t-h)))
        outer = np.repeat(sample, dists>0, axis=0)

        if len(outer):
            pivot = sample[np.argmax(dists)]
            return link(dome(outer, edge(h, pivot)),
                        dome(outer, edge(pivot, t)))
        else:
            return base

    if len(sample) > 2:
        axis = sample[:,0]
        base = np.take(sample, [np.argmin(axis), np.argmax(axis)], axis=0)
        return link(dome(sample, base),
                    dome(sample, base[::-1]))
    else:
        return sample

df = pd.read_excel('Escondida_Combined.xlsx', sheetname='EscondidaMultiSensorDataset')

def normalize(array):
    # return (array - array.mean()) / array.std()
    return array

def lorentzian(x, height, center, width):
    """ defined such that height is the height when x==x0 """
    halfWSquared = (width/2.)**2
    return (height * halfWSquared) / ((x - center)**2 + halfWSquared)

def three_lorentzian(x, h1,c1,w1,h2,c2,w2,h3,c3,w3):
    return (lorentzian(x,h1,c1,w1)+lorentzian(x,h2,c2,w2)+lorentzian(x,h3,c3,w3))

def hull_to_spectrum(hull, wavelengths):
    f = interp1d(hull['wavelength'], hull['intensity'])
    return f(wavelengths)

if __name__=="__main__":
    col_name = df.columns
    name_list = list(col_name)
    xbegin = name_list.index(928.080017)
    wavelengths = np.array(name_list[xbegin:])

    col_value = df.values
    value_list = list(col_value)
    spectrum = col_value[0][xbegin:]
    sample = np.array([wavelengths,spectrum]).T

    hull = qhull(sample)
    hull = pd.DataFrame(hull, columns=['wavelength', 'intensity'])

    fig, axs = plt.subplots(1,2, figsize=(10,5)) # multiple plots in one figure

    axs[0].plot(wavelengths, spectrum, c='b')
    axs[0].plot(hull.iloc[:-1]['wavelength'], hull.iloc[:-1]['intensity'], color='k')

    hull_spectrum = hull_to_spectrum(hull[:-1], wavelengths)
    spectrum2 = spectrum / hull_spectrum
    axs[1].plot(spectrum2)

    plt.show()

    # TF graph input
    X = tf.placeholder(tf.float32, shape=[len(wavelengths)])
    Y = tf.placeholder(tf.float32, shape=[len(spectrum)])

    # Create a model

    # Set model weights

    # height = tf.Variable(numpy.random.randn(), name="height")
    # center = tf.Variable(numpy.random.randn(), name="center")
    # width = tf.Variable(numpy.random.randn(), name="width")
    h1=tf.Variable(0.,name="h1")
    c1=tf.Variable(1420.0,name="c1")
    w1=tf.Variable(10.0,name="w1")
    h2=tf.Variable(0.,name="h2")
    c2=tf.Variable(1920.0,name="c2")
    w2=tf.Variable(10.0,name="w2")
    h3=tf.Variable(0.,name="h3")
    c3=tf.Variable(2210.0,name="c3")
    w3=tf.Variable(10.0,name="w3")
    params = [h1,c1,w1,h2,c2,w2,h3,c3,w3]

    # Set parameters
    learning_rate = 0.3
    training_iteration = 3000

    # Construct a  model
    model = hull_spectrum + three_lorentzian(X, h1,c1,w1,h2,c2,w2,h3,c3,w3)

    # Minimize squared errors
    cost_function = tf.reduce_sum(tf.pow(model - Y, 2)) #L2 loss
    # optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function) #Gradient descent
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost_function)

    # Initialize variables
    init = tf.global_variables_initializer()

    # Launch a graph
    with tf.Session() as sess:
        sess.run(init)

        display_step = 20
        # Fit all training data
        for iteration in range(training_iteration):
            training_cost, _ = sess.run([cost_function, optimizer], feed_dict={X: wavelengths, Y: spectrum})

            # Display logs per iteration step
            if iteration % display_step == 0:
                _h1,_c1,_w1,_h2,_c2,_w2,_h3,_c3,_w3 = sess.run(params, feed_dict={X: wavelengths, Y: spectrum})

                print('Iteration: %04d, cost=%0.9f. [h1,c1,w1]: %0.3f,%0.3f,%0.3f [h2,c2,w2]: %0.3f,%0.3f,%0.3f [h3,c3,w3]: %0.3f,%0.3f,%0.3f' % (
                    iteration + 1,
                    training_cost,
                    _h1,_c1,_w1,_h2,_c2,_w2,_h3,_c3,_w3
                ))

            # Display fitted spectrum occasionally
            if iteration % 500 == 0:
                plt.plot(wavelengths, spectrum, c='k', label='observed spectrum')
                plt.plot(wavelengths, hull_spectrum + three_lorentzian(wavelengths, _h1,_c1,_w1,_h2,_c2,_w2,_h3,_c3,_w3), c='r', label='Fitted line')
                plt.legend()
                plt.show()

        # Final parameters
        _h1,_c1,_w1,_h2,_c2,_w2,_h3,_c3,_w3 = sess.run(params, feed_dict={X: wavelengths, Y: spectrum})
        print('Training completed: cost=%0.9f. [h1,c1,w1]: %0.3f,%0.3f,%0.3f [h2,c2,w2]: %0.3f,%0.3f,%0.3f [h3,c3,w3]: %0.3f,%0.3f,%0.3f' % (
            training_cost,
            _h1,_c1,_w1,_h2,_c2,_w2,_h3,_c3,_w3
        ))

    # Final plot
    plt.plot(wavelengths, spectrum, c='k', label='observed spectrum')
    plt.plot(wavelengths, hull_spectrum + three_lorentzian(wavelengths, _h1,_c1,_w1,_h2,_c2,_w2,_h3,_c3,_w3), c='r', label='Fitted line')
    plt.legend()
    plt.show()

reason: import numpy,from scipy


text:
import csv
from sys import prefix
from rppg import rppg
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats.kde import gaussian_kde

def processGroundData(file_name,second_unit = 10000):
    ground_relative_time = []
    ground_heart_rate = []
    with open(file_name) as csvfile:
        csvreader = csv.reader(csvfile, delimiter=",")
        for row in csvreader:
            _, relative_time, heart_rate = row
            ground_relative_time.append(relative_time)
            ground_heart_rate.append(heart_rate)

    ground_relative_time = ground_relative_time[1:]
    ground_heart_rate = ground_heart_rate[1:]
    ground_relative_time = list(map(int, ground_relative_time))
    ground_heart_rate = list(map(float, ground_heart_rate))
    ground_time = [(x - ground_relative_time[0]) /second_unit for x in ground_relative_time]
    ground_lenght = int(ground_time[-1])
    ground_time = np.array(ground_time)
    ground_heart_rate = np.array(ground_heart_rate)
    time = []
    heart_rate = []
    for i in range(0, ground_lenght):
        idx = np.where((ground_time >= i) & (ground_time <= i + 1))
        bmp = np.average(ground_heart_rate[idx])
        time.append(i + 1)
        heart_rate.append(bmp)

    return time, heart_rate

def processRppgData(file_name,fps):
    frame_list = []
    heart_rate_list = []
    with open(file_name, newline="") as csvfile:
        csvreader = csv.reader(csvfile, delimiter=",")
        for row in csvreader:
            frame, heart_rate = row
            frame_list.append(frame)
            heart_rate_list.append(heart_rate)

    frame_list = list(map(int, frame_list))
    heart_rate_list = list(map(float, heart_rate_list))
    heart_rate_rec = []
    seconds = []
    heart_rate_persecond = []
    for i, frame in enumerate(frame_list):
        heart_rate_rec.append(heart_rate_list[i])
        if frame % fps == 0:
            if len(heart_rate_rec) == 0:
                heart_rate_persecond.append(0)
            else:
                heart_rate_persecond.append(sum(heart_rate_rec) / len(heart_rate_rec))
            seconds.append(int(frame / fps))
            heart_rate_rec = []
    return seconds, heart_rate_persecond

def dataDiff(rppgData,groundData):
    ret = []
    _rppgData = np.array(rppgData)
    _groundData = np.array(groundData)
    for i, rate in enumerate(_rppgData):
        ret.append(_groundData[i]-rate)
    return ret

def percentWithinRange(rppgdata,groundData,range):
    diff = dataDiff(rppgdata, groundData)
    diff = np.array(diff)
    diff = diff[np.logical_not(np.isnan(diff))]
    idx = np.where((diff>=range[0])&(diff<=range[1]))
    if len(rppgdata)==0:
        return False
    else:
        return 100*len(idx[0])/len(diff)

ground_filename = "MPDataExport.csv"
output_filename = "video_front_1_output.csv"

fig, axs = plt.subplots(2,2)
seconds, heart_rate_persecond = processRppgData(output_filename, 30)
ground_time, ground_heart_rate = processGroundData(ground_filename)
print(ground_heart_rate)

##Process for my data
"""
window_size  = 50
ground_time = ground_time[window_size:]
ground_heart_rate = ground_heart_rate[window_size:]
"""

ground_length = len(ground_time)
rppg_time = seconds[-ground_length:]
rppg_heart_rate = heart_rate_persecond[-ground_length:]
ground_time = [(x + rppg_time[0]) for x in ground_time]

print("lenght of ground data" + str(len(ground_time)))
print("length of ground time"+ str(len(ground_heart_rate)))
print("length of estimate data" + str(len(rppg_time)))
diff = []
for i, rate in enumerate(rppg_heart_rate):
    diff.append(ground_heart_rate[i]-rate)

# axs[1].plot(time, heart_rate)
diff = np.array(diff)
diff_without_nan = diff[np.logical_not(np.isnan(diff))]
print(diff_without_nan)
print(np.std(diff_without_nan))
print(np.mean(diff_without_nan))
print(percentWithinRange(rppg_heart_rate,ground_heart_rate, [-5,5]))
#plot pdf

kde = gaussian_kde(diff_without_nan)
dist_space = np.linspace( min(diff_without_nan), max(diff_without_nan), 100 )
axs[0,0].plot(rppg_time, rppg_heart_rate)
axs[0,0].set_title("Ground Truth", fontsize = 15)
axs[0,0].set_xlabel("Time in video(seconds)", fontsize = 15)
axs[0,0].set_ylabel("Heart rate", fontsize = 15)
#, ylabel = "Heart rate")
axs[0,1].plot(ground_time, ground_heart_rate)
axs[0,1].set_title("Estimate Heart Rate", fontsize = 15)
axs[0,1].set_xlabel("Time in video(seconds)",fontsize = 15)
axs[0,1].set_ylabel("Heart rate",fontsize = 15)
axs[1,0].plot(ground_time,diff)
axs[1,0].set_title("Difference\Error", fontsize = 15)
axs[1,0].set_xlabel("Time in video(seconds)",fontsize = 15)
axs[1,0].set_ylabel("Difference/Error in heart rate",fontsize = 15)
axs[1,1].plot(dist_space, kde(dist_space))
axs[1,1].set_title("PDF of Differnce\Error", fontsize = 15)
axs[1,1].set_xlabel("Difference\Error",fontsize = 15)
axs[1,1].set_ylabel("Probability",fontsize = 15)

plt.show()


text:
import math
import numpy as np
import basis.robot_math as rm
import modeling.mesh_tools as mt

if __name__ == '__main__':
    '''
    author: weiwei
    date: 20201208osaka
    '''
    mt.convert_to_stl("shuidi_agv.stl", "shuidi_agv_meter.stl", pos=np.array([.2542, -.2542, 0]),
                      rotmat=rm.rotmat_from_axangle([0, 0, 1], math.pi / 2), scale_ratio=.001)

max_stars_repo_name: takuya-ki/wrs

max_issues_repo_name: takuya-ki/wrs

max_forks_repo_name: takuya-ki/wrs


text:
#coding=utf-8
import torch
import torch.utils.data as data
import torchvision.transforms as transforms

from PIL import Image
from PIL import ImageDraw

import os.path as osp
import numpy as np
import json

class CPDataset(data.Dataset):
    """Dataset for CP-VTON.
    """
    def __init__(self, opt):
        super(CPDataset, self).__init__()
        # base setting
        self.opt = opt
        self.root = opt.dataroot
        self.datamode = opt.datamode # train or test or self-defined
        self.data_list = opt.data_list
        self.fine_height = opt.fine_height
        self.fine_width = opt.fine_width
        self.data_path = osp.join(opt.dataroot, opt.datamode)
        self.transform = transforms.Compose([  \
                transforms.ToTensor(),   \
                transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

        # load data list
        im_names = []
        c_names = []
        with open(osp.join(opt.dataroot, opt.data_list), 'r') as f:
            for line in f.readlines():
                im_name, c_name = line.strip().split()
                im_names.append(im_name)
                c_names.append(c_name)

        self.im_names = im_names
        self.c_names = c_names

    def name(self):
        return "CPDataset"

    def __getitem__(self, index):
        c_name = self.c_names[index]
        im_name = self.im_names[index]

        # cloth image
        c = Image.open(osp.join(self.data_path, 'cloth', c_name))
        c = self.transform(c)  # [-1,1]

        # person image
        im = Image.open(osp.join(self.data_path, 'image', im_name))
        im = self.transform(im) # [-1,1]

        # cloth on person as groundtruth
        parse_name = im_name.replace('.jpg', '.png')
        im_parse = Image.open(osp.join(self.data_path, 'image-parse', parse_name))
        parse_array = np.array(im_parse)
        parse_cloth = (parse_array == 5).astype(np.float32) + \
                (parse_array == 6).astype(np.float32) + \
                (parse_array == 7).astype(np.float32)

        parse_cloth = torch.from_numpy(parse_cloth)
        c_gt = im * parse_cloth + (1 - parse_cloth)

        # masked person image
        mask_name = parse_name
        im_mask = Image.open(osp.join(self.data_path, 'image-mask', mask_name))
        im_mask = self.transform(im_mask)

        inputs = {
            'c': c,
            'im_mask': im_mask,
            'c_gt': c_gt,
            'im': im,
        }

        return inputs

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

class CPDataLoader(object):
    def __init__(self, opt, dataset):
        super(CPDataLoader, self).__init__()

        if opt.shuffle :
            train_sampler = torch.utils.data.sampler.RandomSampler(dataset)
        else:
            train_sampler = None

        self.data_loader = torch.utils.data.DataLoader(
                dataset, batch_size=opt.batch_size, shuffle=(train_sampler is None),
                num_workers=opt.workers, pin_memory=True, sampler=train_sampler)
        self.dataset = dataset
        self.data_iter = self.data_loader.__iter__()

    def next_batch(self):
        try:
            batch = self.data_iter.__next__()
        except StopIteration:
            self.data_iter = self.data_loader.__iter__()
            batch = self.data_iter.__next__()

        return batch

if __name__ == "__main__":
    print("Check the dataset for geometric matching module!")

    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--dataroot", default = "data")
    parser.add_argument("--datamode", default = "test")
    parser.add_argument("--data_list", default = "test_pairs.txt")
    parser.add_argument("--fine_width", type=int, default = 192)
    parser.add_argument("--fine_height", type=int, default = 256)
    parser.add_argument("--shuffle", action='store_true', help='shuffle input data')
    parser.add_argument('-b', '--batch-size', type=int, default=4)
    parser.add_argument('-j', '--workers', type=int, default=1)

    opt = parser.parse_args()
    dataset = CPDataset(opt)
    data_loader = CPDataLoader(opt, dataset)

    print('Size of the dataset: %05d, dataloader: %04d' \
            % (len(dataset), len(data_loader.data_loader)))
    first_item = dataset.__getitem__(0)
    first_batch = data_loader.next_batch()

max_stars_repo_name: Bingwen-Hu/hackaway

max_issues_repo_name: Bingwen-Hu/hackaway

max_forks_repo_name: Bingwen-Hu/hackaway


text:
import os
import io
import base64
import functools

from PIL import Image
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub

from helpers import *

class PythonPredictor:

    def __init__(self, config):
        # Define style image
        self.style_image_url = 'https://upload.wikimedia.org/wikipedia/commons/c/c5/Edvard_Munch%2C_1893%2C_The_Scream%2C_oil%2C_tempera_and_pastel_on_cardboard%2C_91_x_73_cm%2C_National_Gallery_of_Norway.jpg'

        # Import TF-Hub module
        hub_handle = 'https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2'
        self.hub_module = hub.load(hub_handle)

    def predict(self, payload):
        # Define content image
        content_image_url = payload["url"]

        # Load images
        content_img_size = (500, 500)
        style_img_size = (300, 300)

        style_image = load_image(self.style_image_url, style_img_size)
        content_image = load_image(content_image_url, content_img_size)
        style_image = tf.nn.avg_pool(
            style_image, ksize=[3, 3], strides=[1, 1], padding='SAME')

        # Stylize content image with given style image.
        outputs = self.hub_module(tf.constant(content_image),
                                  tf.constant(style_image))
        stylized_image = outputs[0]

        # get PIL image and convert to base64
        img = Image.fromarray(np.uint8(stylized_image.numpy()[0] * 255))
        im_file = io.BytesIO()
        img.save(im_file, format="PNG")
        im_bytes = base64.b64encode(im_file.getvalue()).decode("utf-8")

        return im_bytes

max_stars_repo_name: muchemwal/models

max_issues_repo_name: muchemwal/models

max_forks_repo_name: muchemwal/models


text:
import numpy as np

import speech.utils.wave as wave

def test_load():
    audio, samp_rate = wave.array_from_wave("test0.wav")

    assert samp_rate == 16000
    assert audio.dtype == np.int16

def test_duration():
    duration = wave.wav_duration("test0.wav")

    assert round(duration, 3) == 1.101


text:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
http://www.scipy.org/Cookbook/Least_Squares_Circle
"""

from numpy import *

# Coordinates of the 2D points

x = r_[  9, 35, -13,  10,  23,   0]
y = r_[ 34, 10,   6, -14,  27, -10]

# x = r_[ 16, -10, -15, -14,  23,   7,  13, -18, -17]
# y = r_[ 36,   0,   7,  -1,  30, -17, -15,  14,  18]

# x = r_[36, 36, 19, 18, 33, 26]
# y = r_[14, 10, 28, 31, 18, 26]

# # Code to generate random data points
# R0 = 25
# nb_pts = 81
# dR = 1
# angle =10*pi/5
# theta0 = random.uniform(0, angle, size=nb_pts)
# x = (10 + R0*cos(theta0) + dR*random.normal(size=nb_pts)).round()
# y = (10 + R0*sin(theta0) + dR*random.normal(size=nb_pts)).round()

# == METHOD 1 ==
method_1 = 'algebraic'

# coordinates of the barycenter
x_m = mean(x)
y_m = mean(y)

# calculation of the reduced coordinates
u = x - x_m
v = y - y_m

# linear system defining the center in reduced coordinates (uc, vc):
#    Suu * uc +  Suv * vc = (Suuu + Suvv)/2
#    Suv * uc +  Svv * vc = (Suuv + Svvv)/2
Suv  = sum(u*v)
Suu  = sum(u**2)
Svv  = sum(v**2)
Suuv = sum(u**2 * v)
Suvv = sum(u * v**2)
Suuu = sum(u**3)
Svvv = sum(v**3)

# Solving the linear system
A = array([ [ Suu, Suv ], [Suv, Svv]])
B = array([ Suuu + Suvv, Svvv + Suuv ])/2.0
uc, vc = linalg.solve(A, B)

xc_1 = x_m + uc
yc_1 = y_m + vc

# Calculation of all distances from the center (xc_1, yc_1)
Ri_1      = sqrt((x-xc_1)**2 + (y-yc_1)**2)
R_1       = mean(Ri_1)
residu_1  = sum((Ri_1-R_1)**2)
residu2_1 = sum((Ri_1**2-R_1**2)**2)

# Decorator to count functions calls
import functools
def countcalls(fn):
    "decorator function count function calls "

    @functools.wraps(fn)
    def wrapped(*args):
        wrapped.ncalls +=1
        return fn(*args)

    wrapped.ncalls = 0
    return wrapped

#  == METHOD 2 ==
# Basic usage of optimize.leastsq
from scipy      import optimize

method_2  = "leastsq"

def calc_R(c):
    """ calculate the distance of each 2D points from the center c=(xc, yc) """
    return sqrt((x-c[0])**2 + (y-c[1])**2)

@countcalls
def f_2(c):
    """ calculate the algebraic distance between the 2D points and the mean circle centered at c=(xc, yc) """
    Ri = calc_R(c)
    return Ri - Ri.mean()

center_estimate = x_m, y_m
center_2, ier = optimize.leastsq(f_2, center_estimate)

xc_2, yc_2 = center_2
Ri_2       = calc_R(center_2)
R_2        = Ri_2.mean()
residu_2   = sum((Ri_2 - R_2)**2)
residu2_2  = sum((Ri_2**2-R_2**2)**2)
ncalls_2   = f_2.ncalls

# == METHOD 2b ==
# Advanced usage, with jacobian
method_2b  = "leastsq with jacobian"

def calc_R(c):
    """ calculate the distance of each 2D points from the center c=(xc, yc) """
    return sqrt((x-c[0])**2 + (y-c[1])**2)

@countcalls
def f_2b(c):
    """ calculate the algebraic distance between the 2D points and the mean circle centered at c=(xc, yc) """
    Ri = calc_R(c)
    return Ri - Ri.mean()

@countcalls
def Df_2b(c):
    """ Jacobian of f_2b, with derivatives along the rows """
    xc, yc     = c
    df2b_dc    = empty((x.size, len(c)))

    Ri = calc_R(c).T
    df2b_dc[:, 0] = (xc - x.T)/Ri                   # dR/dxc
    df2b_dc[:, 1] = (yc - y.T)/Ri                   # dR/dyc
    df2b_dc       = df2b_dc - df2b_dc.mean(axis=0)

    return df2b_dc

center_estimate = x_m, y_m
center_2b, ier = optimize.leastsq(f_2b, center_estimate, Dfun=Df_2b)

xc_2b, yc_2b = center_2b
Ri_2b       = calc_R(center_2b)
R_2b        = Ri_2b.mean()
residu_2b   = sum((Ri_2b - R_2b)**2)
residu2_2b  = sum((Ri_2b**2-R_2b**2)**2)
ncalls_2b   = f_2b.ncalls

print "\nMethod 2b :"
print "Functions calls : f_2b=%d Df_2b=%d" % (f_2b.ncalls, Df_2b.ncalls)

# == METHOD 3 ==
# Basic usage of odr with an implicit function definition
from scipy      import  odr

method_3  = "odr"

@countcalls
def f_3(beta, x):
    """ implicit definition of the circle """
    return (x[0]-beta[0])**2 + (x[1]-beta[1])**2 -beta[2]**2

# initial guess for parameters
R_m = calc_R([x_m, y_m]).mean()
beta0 = [ x_m, y_m, R_m]

# for implicit function :
#       data.x contains both coordinates of the points
#       data.y is the dimensionality of the response
lsc_data  = odr.Data(row_stack([x, y]), y=1)
lsc_model = odr.Model(f_3, implicit=True)
lsc_odr   = odr.ODR(lsc_data, lsc_model, beta0)
lsc_out   = lsc_odr.run()

xc_3, yc_3, R_3 = lsc_out.beta
Ri_3       = calc_R([xc_3, yc_3])
residu_3   = sum((Ri_3 - R_3)**2)
residu2_3  = sum((Ri_3**2-R_3**2)**2)
ncalls_3   = f_3.ncalls

# == METHOD 3b ==
# Advanced usage, with jacobian
method_3b  = "odr with jacobian"

@countcalls
def f_3b(beta, x):
    """ implicit definition of the circle """
    return (x[0]-beta[0])**2 + (x[1]-beta[1])**2 -beta[2]**2

@countcalls
def jacb(beta, x):
    """ Jacobian function with respect to the parameters beta.
    return df3b/dbeta
    """
    xc, yc, r = beta
    xi, yi    = x

    df_db    = empty((beta.size, x.shape[1]))
    df_db[0] =  2*(xc-xi)                     # d_f/dxc
    df_db[1] =  2*(yc-yi)                     # d_f/dyc
    df_db[2] = -2*r                           # d_f/dr

    return df_db

@countcalls
def jacd(beta, x):
    """ Jacobian function with respect to the input x.
    return df3b/dx
    """
    xc, yc, r = beta
    xi, yi    = x

    df_dx    = empty_like(x)
    df_dx[0] =  2*(xi-xc)                     # d_f/dxi
    df_dx[1] =  2*(yi-yc)                     # d_f/dyi

    return df_dx

def calc_estimate(data):
    """ Return a first estimation on the parameter from the data  """
    xc0, yc0 = data.x.mean(axis=1)
    r0 = sqrt((data.x[0]-xc0)**2 +(data.x[1] -yc0)**2).mean()
    return xc0, yc0, r0

# for implicit function :
#       data.x contains both coordinates of the points
#       data.y is the dimensionality of the response
lsc_data  = odr.Data(row_stack([x, y]), y=1)
lsc_model = odr.Model(f_3b, implicit=True, estimate=calc_estimate, fjacd=jacd, fjacb=jacb)
lsc_odr   = odr.ODR(lsc_data, lsc_model)    # beta0 has been replaced by an estimate function
lsc_odr.set_job(deriv=3)                    # use user derivatives function without checking
lsc_out   = lsc_odr.run()

xc_3b, yc_3b, R_3b = lsc_out.beta
Ri_3b       = calc_R([xc_3b, yc_3b])
residu_3b   = sum((Ri_3b - R_3b)**2)
residu2_3b  = sum((Ri_3b**2-R_3b**2)**2)
ncalls_3b   = f_3b.ncalls

print "\nMethod 3b : ", method_3b
print "Functions calls : f_3b=%d jacb=%d jacd=%d" % (f_3b.ncalls, jacb.ncalls, jacd.ncalls)

# Summary
fmt = '%-22s %10.5f %10.5f %10.5f %10d %10.6f %10.6f %10.2f'
print ('\n%-22s' +' %10s'*7) % tuple('METHOD Xc Yc Rc nb_calls std(Ri) residu residu2'.split())
print '-'*(22 +7*(10+1))
print  fmt % (method_1 , xc_1 , yc_1 , R_1 ,        1 , Ri_1.std() , residu_1 , residu2_1 )
print  fmt % (method_2 , xc_2 , yc_2 , R_2 , ncalls_2 , Ri_2.std() , residu_2 , residu2_2 )
print  fmt % (method_2b, xc_2b, yc_2b, R_2b, ncalls_2b, Ri_2b.std(), residu_2b, residu2_2b)
print  fmt % (method_3 , xc_3 , yc_3 , R_3 , ncalls_3 , Ri_3.std() , residu_3 , residu2_3 )
print  fmt % (method_3b, xc_3b, yc_3b, R_3b, ncalls_3b, Ri_3b.std(), residu_3b, residu2_3b)

# plotting functions
from matplotlib                 import pyplot as p, cm, colors

def plot_all(residu2=False, basename='circle'):
    """ Draw data points, best fit circles and center for the three methods,
    and adds the iso contours corresponding to the fiel residu or residu2
    """

    f = p.figure(figsize=(7, 5.4), dpi=72, facecolor='white')
    p.axis('equal')

    theta_fit = linspace(-pi, pi, 180)

    x_fit1 = xc_1 + R_1*cos(theta_fit)
    y_fit1 = yc_1 + R_1*sin(theta_fit)
    p.plot(x_fit1, y_fit1, 'b-' , label=method_1, lw=2)

    x_fit2 = xc_2 + R_2*cos(theta_fit)
    y_fit2 = yc_2 + R_2*sin(theta_fit)
    p.plot(x_fit2, y_fit2, 'k--', label=method_2, lw=2)

    x_fit3 = xc_3 + R_3*cos(theta_fit)
    y_fit3 = yc_3 + R_3*sin(theta_fit)
    p.plot(x_fit3, y_fit3, 'r-.', label=method_3, lw=2)

    p.plot([xc_1], [yc_1], 'bD', mec='y', mew=1)
    p.plot([xc_2], [yc_2], 'gD', mec='r', mew=1)
    p.plot([xc_3], [yc_3], 'kD', mec='w', mew=1)

    # draw
    p.xlabel('x')
    p.ylabel('y')

    # plot the residu fields
    nb_pts = 100

    p.draw()
    xmin, xmax = p.xlim()
    ymin, ymax = p.ylim()

    vmin = min(xmin, ymin)
    vmax = max(xmax, ymax)

    xg, yg = ogrid[vmin:vmax:nb_pts*1j, vmin:vmax:nb_pts*1j]
    xg = xg[..., newaxis]
    yg = yg[..., newaxis]

    Rig    = sqrt( (xg - x)**2 + (yg - y)**2 )
    Rig_m  = Rig.mean(axis=2)[..., newaxis]

    if residu2 : residu = sum( (Rig**2 - Rig_m**2)**2 ,axis=2)
    else       : residu = sum( (Rig-Rig_m)**2 ,axis=2)

    lvl = exp(linspace(log(residu.min()), log(residu.max()), 15))

    p.contourf(xg.flat, yg.flat, residu.T, lvl, alpha=0.6, cmap=cm.Blues_r, norm=colors.LogNorm())
    cbar = p.colorbar(fraction=0.15, format='%.f')
    p.contour (xg.flat, yg.flat, residu.T, lvl, alpha=0.75, colors="purple")

    if residu2 : cbar.set_label('Residu_2 - algebraic approximation')
    else       : cbar.set_label('Residu')

    # plot data
    p.plot(x, y, 'ro', label='data', ms=8, mec='b', mew=1)
    p.legend(loc='best',labelspacing=0.1 )

    p.xlim(xmin=vmin, xmax=vmax)
    p.ylim(ymin=vmin, ymax=vmax)

    p.grid()
    p.title('Leasts Squares Circle')
    p.savefig('%s_residu%d.png' % (basename, 2 if residu2 else 1))

plot_all(residu2=False, basename='arc')
plot_all(residu2=True , basename='arc')

p.show()
# vim: set et sts=4 sw=4:

max_stars_repo_name: cassiasamp/scipy-cookbook

max_issues_repo_name: cassiasamp/scipy-cookbook

max_forks_repo_name: cassiasamp/scipy-cookbook

reason: from numpy,from scipy


text:
import numpy as np
import scipy.sparse as sp
import matplotlib.pyplot as plt
from scipy import signal
from scipy.ndimage.filters import gaussian_filter1d
from multiprocessing import Process, Manager

def blank_diagonal2(matr, strata = False):
    """
    in: edgelist, strata (n entries off main diagonal to zero)
    out:matrix with n blanked diagonal
    """
#     int_shape = (int(max(matr[:,1])+1), int(max(matr[:,1])+1))
#     coo = sp.coo_matrix((matr[:, 2], (matr[:, 0], matr[:, 1])), shape=int_shape, dtype=matr.dtype)
#     csr = coo.tocsr()
#     csr_org = csr.copy()
    coo = sp.coo_matrix(matr)
    csr = coo.tocsr()
    lil=csr.tolil()
    for i in range(lil.shape[0]):
        lil[i:i+strata+1,i:i+strata+1] = 0
    csr = lil.tocsr()
    return csr

def getPeakAndWidths(matrix_in, gap=600, sigma=12, rel_height=0.3):
#
    HMAT = [matrix_in[i,i:i+gap].mean() for i in range(0,matrix_in.shape[0])]
#
    hFiltered = gaussian_filter1d(HMAT, sigma=sigma)
    hMax = signal.argrelmax(hFiltered)[0]
    hMin = signal.argrelmin(hFiltered)[0]
    hwidths = signal.peak_widths(hFiltered, peaks=hMax, rel_height=rel_height)[0]
#
    VMAT = [matrix_in[:,i].mean() for i in range(0,gap)]
    VMAT += [matrix_in[i-gap:i,i].mean() for i in range(gap,matrix_in.shape[0]-gap)]
    VMAT += [matrix_in[:,i].mean() for i in range(matrix_in.shape[0]-gap,matrix_in.shape[0])]
#
    vFiltered = gaussian_filter1d(VMAT, sigma=sigma)
    vMax = signal.argrelmax(vFiltered)[0]
    vMin = signal.argrelmin(vFiltered)[0]
    vwidths = signal.peak_widths(vFiltered, peaks=vMax, rel_height=rel_height)[0]
#
    return hMax, hwidths, vMax, vwidths

# def matrix2peaks(mat, stride=2500, step=False, sigma=12, rel_height=0.3):
#     hM_res = list()
#     hW_res = list()
#     vM_res = list()
#     vW_res = list()
#     for i in range(1,mat.shape[0],stride):
#         lower = i-1
#         upper = i+stride-1
#         if upper > mat.shape[0]:
#             upper = mat.shape[0]
#         Z_tester = mat[lower:upper,lower:upper]
#         if not step:
#             step = Z_tester.shape[0]//12
#         try:
#             hM,hW,vM,vW = getPeakAndWidths(Z_tester, step, sigma, rel_height)
#             hM += lower
#             vM += lower
#             hM_res.extend(hM)
#             vM_res.extend(vM)
#             hW_res.extend(hW)
#             vW_res.extend(vW)
#         except ValueError:
#             continue
#     return hM_res, hW_res, vM_res, vW_res

max_stars_repo_name: seanpatrickmoran/StripeCaller

max_issues_repo_name: seanpatrickmoran/StripeCaller

max_forks_repo_name: seanpatrickmoran/StripeCaller

reason: import numpy,import scipy,from scipy