text:
import os
import numpy as np
import time
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable

from models.cnn_rnn import CNN_RNN, RNN, CNN_RNN_1, CNN_RNN_EF2_PS
from models.all_conv import AllConv

from utils.utils import *
from utils.data_manager import DataManager

from sklearn.metrics import accuracy_score, f1_score, mean_squared_error, classification_report

os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

result_dir = './results_all/'
data_dir = './data'
merged_data_dir = '../keyboard_cnn/data'
checkpoint_dir = './checkpoints/'

num_runs = 20
batch_size = 256
num_epochs = 200
learning_rate = 1e-3

mode = 'rgs_hdrs'
use_ctrl = False

parse_label = False

model_list = ['2cnn_rnn_sin_id', '2cnn_rnn_sin', '2cnn_rnn', 'cnn', 'rnn']

if use_ctrl:
    ctrl = ''
else:
    ctrl = '_NoCtrl'

if not os.path.isdir(result_dir):
    os.system('mkdir ' + result_dir)

print('loading data...')
train_data_d, test_data_d, num_users = load_data_dir(data_dir=data_dir, ttype='hour_sum', shuffle=False,
                                                     multi_class=False, parse_label=parse_label, with_ctrl=use_ctrl)

log_file = './logs/log_lf_stats_' + mode + ctrl + '.txt'

def main(model='2cnn_rnn_sin_id', pid=0):
    if parse_label:
        pl = '_norm'
    else:
        pl = ''
    pl += ('_' + str(pid))
    checkpoint_path = checkpoint_dir + model + '_' + mode + ctrl + pl + '.ckpt'
    if model == 'DeeperMood_24h':
        log_file = './results/log_DM_24h2_' + mode + ctrl + pl + '.txt'
        ab_log = './results/hours_emb_2_' + mode + ctrl + pl + '.txt'
        net = CNN_RNN(use_time='24h2')
    elif model == '2cnn_rnn_sin_id':
        log_file = result_dir + 'log_2cnn_rnn_sin_id_' + mode + ctrl + pl + '.txt'
        ab_log = result_dir + 'alpha_beta_id_' + mode + ctrl + pl + '.txt'
        net = CNN_RNN(use_time='sin_id', num_user=num_users)
    elif model == '2cnn_rnn_sin':
        log_file = result_dir + 'log_2cnn_rnn_sin_' + mode + ctrl + pl + '.txt'
        net = CNN_RNN(use_time='sin', num_user=num_users)
    elif model == '2cnn_rnn':
        log_file = result_dir + 'log_2cnn_rnn_' + mode + ctrl + pl + '.txt'
        net = CNN_RNN(use_time=None)
    elif model == 'cnn_rnn':
        log_file = result_dir + 'log_cnn_rnn_' + mode + ctrl + pl + '.txt'
        net = CNN_RNN_1()
    elif model == 'cnn':
        log_file = result_dir + 'log_cnn_' + mode + ctrl + pl + '.txt'
        net = AllConv()
    elif model == 'cnn2':
        log_file = result_dir + 'log_cnn2_' + mode + ctrl + pl + '.txt'
        net = AllConv(use_special=False)
    else: # model == 'rnn':
        log_file = result_dir + 'log_rnn_' + mode + ctrl + pl + '.txt'
        net = RNN()

    train_data = train_data_d
    test_data = test_data_d

    net.cuda()
    if mode == 'clf':
        criterion = nn.BCEWithLogitsLoss()
    else:
        criterion = nn.MSELoss()
    optimizer = optim.RMSprop(net.parameters(), lr=learning_rate, weight_decay=0.001)
    print('training...')

    best_epoch = 0
    best_acc = 0
    best_f1 = 0
    best_rmse = 1e10
    results = []
    train_data_manager = DataManager(len(train_data), num_epoch=num_epochs, batch_size=batch_size)

    for epoch in range(num_epochs):
        num_batch = train_data_manager.num_batch_per_epoch
        net.train()
        for batch in range(num_batch):
            optimizer.zero_grad()

            t0 = time.time()
            batch_data = train_data_manager.get_batch(train_data)
            accel = Variable(torch.from_numpy(np.transpose(np.asarray([item[0] for item in batch_data]), [0, 2, 1])).cuda())
            alphanum = Variable(torch.from_numpy(np.transpose(np.asarray([item[1] for item in batch_data]), [0, 2, 1])).cuda())
            special = Variable(torch.from_numpy(np.transpose(np.asarray([item[2] for item in batch_data]), [0, 2, 1])).cuda())
            timestamp = Variable(torch.from_numpy(np.asarray([item[4] for item in batch_data])).cuda().float())
            users = Variable(torch.from_numpy(np.asarray([item[5] for item in batch_data])).cuda())
            labels = [item[3] for item in batch_data]
            label_HDRS = Variable(torch.from_numpy(np.asarray([item[0] for item in labels])).cuda()).float()
            label_YMRS = Variable(torch.from_numpy(np.asarray([item[1] for item in labels])).cuda()).float()
            t1 = time.time()

            outputs = net.forward(accel, alphanum, special, timestamp, users)

            if mode == 'clf' or mode == 'rgs_hdrs':
                loss = criterion(outputs, label_HDRS)
            else:
                loss = criterion(outputs, label_YMRS)

            loss.backward()
            optimizer.step()
            t2 = time.time()

        res = evaluate(test_data, net)
        results.append(res)

        if res[0] < best_rmse:
            best_rmse = res[0]
            best_epoch = epoch + 1
            #torch.save(net.state_dict(), checkpoint_path)
        #print 'best: epoch %d, rmse: %f' % (best_epoch, best_rmse)

    if mode == 'clf':
        print('best: epoch %d, acc: %f, f1: %f'%(best_epoch, best_acc, best_f1))
    else:
        print('best: epoch %d, rmse: %f'%(best_epoch, best_rmse))

    save_log_data(results, log_file)

    if model == '2cnn_rnn_sin_id':
        idxs = Variable(torch.from_numpy(np.asarray([i for i in range(num_users)])).long().cuda())
        alpha = net.alpha(idxs).data
        beta = net.beta(idxs).data
        gamma = net.gamma(idxs).data
        delta = net.delta(idxs).data
        with open(ab_log, 'w') as fout:
            for i in range(num_users):
                fout.write(str(alpha[i][0]) + '\t' + str(beta[i][0]) + '\t' + str(gamma[i][0]) + '\t' + str(delta[i][0]) + '\n')

    return best_rmse

def evaluate(test_data, net, filepath=None):
    net.eval()
    test_data_manager = DataManager(len(test_data), num_epoch=1, batch_size=batch_size)
    num_batch = test_data_manager.num_batch_per_epoch
    pred = []
    truth = []
    logits = []
    for batch in range(num_batch):
        t0 = time.time()
        batch_data = test_data_manager.get_batch(test_data)
        accel = Variable(torch.from_numpy(np.transpose(np.asarray([item[0] for item in batch_data]), [0, 2, 1])).cuda())
        alphanum = Variable(
            torch.from_numpy(np.transpose(np.asarray([item[1] for item in batch_data]), [0, 2, 1])).cuda())
        special = Variable(
            torch.from_numpy(np.transpose(np.asarray([item[2] for item in batch_data]), [0, 2, 1])).cuda())
        labels = [item[3] for item in batch_data]
        label_HDRS = [item[0] for item in labels]
        label_YMRS = [item[1] for item in labels]

        timestamp = Variable(torch.from_numpy(np.asarray([item[4] for item in batch_data])).cuda().float())
        users = Variable(torch.from_numpy(np.asarray([item[5] for item in batch_data])).cuda())

        t1 = time.time()
        outputs = net.forward(accel, alphanum, special, timestamp, users)
        if mode == 'clf':
            probs = torch.sigmoid(outputs)
            logits += list(probs.data.cpu().numpy())
            pred += list(probs.data.cpu().numpy() > 0.5)
            truth += label_HDRS
        else:
            pred += list(outputs.data.cpu().numpy())
            if mode == 'rgs_hdrs':
                truth += label_HDRS
            else:
                truth += label_YMRS

    if mode == 'clf':
        acc = accuracy_score(y_true=truth, y_pred=pred)
        f1 = f1_score(y_true=truth, y_pred=pred)
        if filepath is not None:
            save_log_data([logits, truth], filepath)
        return [acc, f1]
    else:
        rmse = np.sqrt(mean_squared_error(y_true=truth, y_pred=pred))
        return [rmse]

if __name__ == '__main__':
    import gc
    from utils.logger import *
    logger = Logger(filename=log_file).get_logger()
    #torch.manual_seed(1234)
    #torch.cuda.manual_seed_all(1234)
    for m in model_list:
        rmse_list = []
        logger.info('running model: ' + m)
        for n in range(num_runs):
            rmse = main(model=m, pid=n)
            rmse_list.append(rmse)
            logger.info('run %d, rmse %f'%(n, rmse))
        rmse_mean = float(np.mean(rmse_list))
        rmse_max = max(rmse_list)
        rmse_min = min(rmse_list)
        rmse_std = float(np.std(rmse_list))
        logger.info('mean: %f, std: %f, max: %f, min: %f'%(rmse_mean, rmse_std, rmse_max, rmse_min))
        gc.collect()

max_stars_repo_name: stevehuanghe/dpMood

max_issues_repo_name: stevehuanghe/dpMood

max_forks_repo_name: stevehuanghe/dpMood


text:
import unittest, os, sys, copy, random

import tiledb
from tiledb import TileDBError, core
from tiledb.tests.common import DiskTestCase, rand_ascii

import numpy as np
from numpy.testing import assert_array_equal

class CoreCCTest(DiskTestCase):

    def test_pyquery_basic(self):
        ctx = tiledb.default_ctx()
        uri = self.path("test_pyquery_basic")
        with tiledb.from_numpy(uri, np.random.rand(4)) as A:
            pass

        with tiledb.open(uri) as a:
            with self.assertRaises(ValueError):
                testctx = tiledb.Ctx(config={'py.init_buffer_bytes': 'abcd'})
                core.PyQuery(testctx, a, ("",), False, 0)

            q = core.PyQuery(ctx, a, ("",), False, 0)

            try:
                q._test_err("bad foo happened")
            except Exception as exc:
                assert isinstance(exc, tiledb.TileDBError)
                assert exc.message == "bad foo happened"

            q.set_ranges([[(0, 3)]])

            with self.assertRaises(TileDBError):
                q.set_ranges([[(0, 3.0)]])

            q.set_ranges([[(0, np.int32(3))]])

            with self.assertRaises(TileDBError):
                q.set_ranges([[(3, "a")]])

            if sys.hexversion >= 0x3000000:
                # assertRaisesRegex is not available in 2.7
                with self.assertRaisesRegex(
                    TileDBError,
                    "Failed to cast dim range '\\(1.2344, 5.6789\\)' to dim type UINT64.*$",
                ):
                    q.set_ranges([[(1.2344, 5.6789)]])

                with self.assertRaisesRegex(
                    TileDBError,
                    "Failed to cast dim range '\\('aa', 'bbbb'\\)' to dim type UINT64.*$",
                ):
                    q.set_ranges([[("aa", "bbbb")]])

        with tiledb.open(uri) as a:
            q2 = core.PyQuery(ctx, a, ("",), False, 0)

            q2.set_ranges([[(0,3)]])
            q2.submit()
            res = q2.results()[''][0]
            res.dtype = np.double
            assert_array_equal(res, a[:])

    def test_pyquery_init(self):
        uri = self.path("test_pyquery_basic")
        intmax = np.iinfo(np.int64).max
        config_dict = {
            "sm.tile_cache_size": '100',
            "py.init_buffer_bytes": str(intmax)
            }
        ctx = tiledb.Ctx(config=config_dict)

        with tiledb.from_numpy(uri, np.random.rand(4)) as A:
            pass

        with tiledb.open(uri) as a:
            q = core.PyQuery(ctx, a, ("",), False, 0)
            self.assertEqual(q._test_init_buffer_bytes, intmax)

    def test_import_buffer(self):
        uri = self.path("test_import_buffer")

        ctx = tiledb.Ctx()

        dom = tiledb.Domain(tiledb.Dim(domain=(0, 3), tile=1,
                                       dtype=np.int64, ctx=ctx),
                            tiledb.Dim(domain=(0, 3), tile=1,
                                       dtype=np.int64, ctx=ctx),
                            ctx=ctx)
        attrs = [
            tiledb.Attr(name='', dtype=np.float64, ctx=ctx),
            tiledb.Attr(name='foo', dtype=np.int32, ctx=ctx),
            tiledb.Attr(name='str', dtype=np.str, ctx=ctx)
        ]
        schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=False, ctx=ctx)
        tiledb.DenseArray.create(uri, schema)

        data_orig = {
            '': 2.5 * np.identity(4, dtype=np.float64),
            'foo': 8 * np.identity(4, dtype=np.int32),
            'str': np.array([rand_ascii(random.randint(0,5)) for _ in range(16)]).reshape(4,4)
        }

        with tiledb.open(uri, 'w') as A:
           A[:] = data_orig

        with tiledb.open(uri) as B:
            assert_array_equal(B[:][''], data_orig['']),
            assert_array_equal(B[:]['foo'], data_orig['foo'])

        data_mod = {
            '': 5 * np.identity(4, dtype=np.float64),
            'foo': 32 * np.identity(4, dtype=np.int32),
            'str': np.array([rand_ascii(random.randint(1,7))
                             for _ in range(16)], dtype='U0').reshape(4,4)
        }

        str_offsets = np.array([0] + [len(x) for x in
                                      data_mod['str'].flatten()[:-1]],
                               dtype=np.uint64)
        str_offsets = np.cumsum(str_offsets)

        str_raw = np.array([ord(c) for c in
                            ''.join([x for x in data_mod['str'].flatten()])],
                           dtype=np.uint8)

        data_mod_bfr = {
            '': (data_mod[''].flatten().view(np.uint8),
                 np.array([], dtype=np.uint64)),
            'foo': (data_mod['foo'].flatten().view(np.uint8),
                    np.array([], dtype=np.uint64)),
            'str': (str_raw.flatten().view(np.uint8), str_offsets)
        }

        with tiledb.open(uri) as C:
            res = C.multi_index[0:3,0:3]
            assert_array_equal(res[''], data_orig[''])
            assert_array_equal(res['foo'], data_orig['foo'])
            assert_array_equal(res['str'], data_orig['str'])

            C._set_buffers(copy.deepcopy(data_mod_bfr))
            res = C.multi_index[0:3,0:3]
            assert_array_equal(res[''], data_mod[''])
            assert_array_equal(res['foo'], data_mod['foo'])
            assert_array_equal(res['str'], data_mod['str'])

        with tiledb.open(uri) as D:
            D._set_buffers(copy.deepcopy(data_mod_bfr))
            res = D[:,:]
            assert_array_equal(res[''], data_mod[''])
            assert_array_equal(res['foo'], data_mod['foo'])
            assert_array_equal(res['str'], data_mod['str'])

ext: py

max_stars_repo_name: gatesn/TileDB-Py

max_issues_repo_name: georgeSkoumas/TileDB-Py

max_forks_repo_name: georgeSkoumas/TileDB-Py

reason: import numpy,from numpy


text:
import numpy as np
import torchvision.utils as vutils
import torch
import torch.nn.functional as F

def print_args(opt):
    message = ''
    message += '----------------- Options ---------------\n'
    for k, v in sorted(vars(opt).items()):
        comment = ''
        message += '{:>25}: {:<30}{}\n'.format(str(k), str(v), comment)
    message += '----------------- End -------------------'
    print(message)

# torch.no_grad warpper for functions
def make_nograd_func(func):
    def wrapper(*f_args, **f_kwargs):
        with torch.no_grad():
            ret = func(*f_args, **f_kwargs)
        return ret

    return wrapper

# convert a function into recursive style to handle nested dict/list/tuple variables
def make_recursive_func(func):
    def wrapper(vars):
        if isinstance(vars, list):
            return [wrapper(x) for x in vars]
        elif isinstance(vars, tuple):
            return tuple([wrapper(x) for x in vars])
        elif isinstance(vars, dict):
            return {k: wrapper(v) for k, v in vars.items()}
        else:
            return func(vars)

    return wrapper

@make_recursive_func
def tensor2float(vars):
    if isinstance(vars, float):
        return vars
    elif isinstance(vars, torch.Tensor):
        return vars.data.item()
    else:
        raise NotImplementedError("invalid input type {} for tensor2float".format(type(vars)))

@make_recursive_func
def tensor2numpy(vars):
    if isinstance(vars, np.ndarray):
        return vars
    elif isinstance(vars, torch.Tensor):
        return vars.detach().cpu().numpy().copy()
    else:
        raise NotImplementedError("invalid input type {} for tensor2numpy".format(type(vars)))

@make_recursive_func
def tocuda(vars):
    if isinstance(vars, torch.Tensor):
        return vars.cuda()
    elif isinstance(vars, str):
        return vars
    else:
        raise NotImplementedError("invalid input type {} for tensor2numpy".format(type(vars)))

def save_scalars(logger, mode, scalar_dict, global_step):
    scalar_dict = tensor2float(scalar_dict)
    for key, value in scalar_dict.items():
        if not isinstance(value, (list, tuple)):
            name = '{}/{}'.format(mode, key)
            logger.add_scalar(name, value, global_step)
        else:
            for idx in range(len(value)):
                name = '{}/{}_{}'.format(mode, key, idx)
                logger.add_scalar(name, value[idx], global_step)

def save_images(logger, mode, images_dict, global_step):
    images_dict = tensor2numpy(images_dict)

    def preprocess(name, img):
        if not (len(img.shape) == 3 or len(img.shape) == 4):
            raise NotImplementedError("invalid img shape {}:{} in save_images".format(name, img.shape))
        if len(img.shape) == 3:
            img = img[:, np.newaxis, :, :]
        img = torch.from_numpy(img[:1])
        return vutils.make_grid(img, padding=0, nrow=1, normalize=True, scale_each=True)

    for key, value in images_dict.items():
        if not isinstance(value, (list, tuple)):
            name = '{}/{}'.format(mode, key)
            logger.add_image(name, preprocess(name, value), global_step)
        else:
            for idx in range(len(value)):
                name = '{}/{}_{}'.format(mode, key, idx)
                logger.add_image(name, preprocess(name, value[idx]), global_step)

class DictAverageMeter(object):
    def __init__(self):
        self.data = {}
        self.count = 0

    def update(self, new_input):
        self.count += 1
        if len(self.data) == 0:
            for k, v in new_input.items():
                if not isinstance(v, float):
                    raise NotImplementedError("invalid data {}: {}".format(k, type(v)))
                self.data[k] = v
        else:
            for k, v in new_input.items():
                if not isinstance(v, float):
                    raise NotImplementedError("invalid data {}: {}".format(k, type(v)))
                self.data[k] += v

    def mean(self):
        return {k: v / self.count for k, v in self.data.items()}

# a wrapper to compute metrics for each image individually
def compute_metrics_for_each_image(metric_func):
    def wrapper(depth_est, depth_gt, mask, *args):
        batch_size = depth_gt.shape[0]
        results = []
        # compute result one by one
        for idx in range(batch_size):
            ret = metric_func(depth_est[idx], depth_gt[idx], mask[idx], *args)
            results.append(ret)
        return torch.stack(results).mean()

    return wrapper

@make_nograd_func
@compute_metrics_for_each_image
def Thres_metrics(depth_est, depth_gt, mask, thres):
    assert isinstance(thres, (int, float))
    depth_est, depth_gt = depth_est[mask], depth_gt[mask]
    errors = torch.abs(depth_est - depth_gt)
    err_mask = errors > thres
    return torch.mean(err_mask.float())

# NOTE: please do not use this to build up training loss
@make_nograd_func
@compute_metrics_for_each_image
def AbsDepthError_metrics(depth_est, depth_gt, mask):
    depth_est, depth_gt = depth_est[mask], depth_gt[mask]
    return torch.mean((depth_est - depth_gt).abs())

max_stars_repo_name: ToughStoneX/U-MVS

max_issues_repo_name: ToughStoneX/U-MVS

max_forks_repo_name: ToughStoneX/U-MVS

reason: import numpy


text:
from collections import defaultdict

import cv2
import numpy as np
import pytest
import torch
from torch import nn

from pytorch_toolbelt.inference import tta
from pytorch_toolbelt.utils.torch_utils import to_numpy
from pytorch_toolbelt.zoo import resnet34_unet32

skip_if_no_cuda = pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA is not available")

class NoOp(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input):
        return input

class SumAll(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input):
        return input.sum(dim=[1, 2, 3])

def test_d4_image2mask():
    x = torch.rand((4, 3, 224, 224))
    model = NoOp()

    output = tta.d4_image2mask(model, x)
    np.testing.assert_allclose(to_numpy(output), to_numpy(x), atol=1e-6, rtol=1e-6)

def test_d4_image2mask_v2():
    x = torch.rand((4, 3, 224, 224))
    x_a = tta.d4_image_augment(x)
    y = tta.d4_image_deaugment(x_a)

    np.testing.assert_allclose(to_numpy(y), to_numpy(x), atol=1e-6, rtol=1e-6)

@torch.no_grad()
@skip_if_no_cuda()
def test_d4_speed():
    df = defaultdict(list)
    n = 100

    model = resnet34_unet32().cuda().eval()
    x = torch.rand((4, 3, 224, 224)).float().cuda()
    y1 = tta.d4_image2mask(model, x)
    y2 = tta.d4_image_deaugment(model(tta.d4_image_augment(x)))
    np.testing.assert_allclose(to_numpy(y1), to_numpy(y2), atol=1e-6, rtol=1e-6)

    for deterministic in [False, True]:
        for benchmark in [False, True]:
            for dtype in [torch.float16, torch.float32]:
                torch.cuda.empty_cache()
                torch.backends.cuda.deterministic = deterministic
                torch.backends.cuda.benchmark = benchmark

                model = resnet34_unet32().to(dtype).cuda().eval()

                speed_v1 = 0
                speed_v2 = 0
                for i in range(n):
                    x = torch.rand((4, 3, 224, 224)).to(dtype).cuda(non_blocking=False)
                    start = cv2.getTickCount()
                    y = tta.d4_image2mask(model, x)
                    v = y.sum().item()
                    finish = cv2.getTickCount()
                    speed_v1 += finish - start
                    np.testing.assert_allclose(v, v, atol=1e-6, rtol=1e-6)

                for i in range(n):
                    x = torch.rand((4, 3, 224, 224)).to(dtype).cuda(non_blocking=False)
                    start = cv2.getTickCount()
                    x_a = tta.d4_image_augment(x)
                    x_a = model(x_a)
                    y = tta.d4_image_deaugment(x_a)
                    v = y.sum().item()
                    finish = cv2.getTickCount()
                    speed_v2 += finish - start
                    np.testing.assert_allclose(v, v, atol=1e-6, rtol=1e-6)

                df["mode"].append("fp16" if dtype == torch.float16 else "fp32")
                df["deterministic"].append(deterministic)
                df["benchmark"].append(benchmark)
                df["d4_image2mask (ms)"].append(1000.0 * speed_v1 / (cv2.getTickFrequency() * n))
                df["d4_augment (ms)"].append(1000.0 * speed_v2 / (cv2.getTickFrequency() * n))

    import pandas as pd

    df = pd.DataFrame.from_dict(df)
    pd.set_option("display.max_columns", None)
    pd.set_option("display.max_rows", None)
    print(df)
    df.to_csv("tta_eval.csv", index=False)

def test_fliplr_image2mask():
    x = torch.rand((4, 3, 224, 224))
    model = NoOp()

    output = tta.fliplr_image2mask(model, x)
    np.testing.assert_allclose(to_numpy(output), to_numpy(x), atol=1e-6, rtol=1e-6)

def test_d4_image2label():
    x = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 1, 2], [3, 4, 5, 6]]).unsqueeze(0).unsqueeze(0).float()
    model = SumAll()

    output = tta.d4_image2label(model, x)
    expected = int(x.sum())

    assert int(output) == expected

def test_fliplr_image2label():
    x = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 1, 2], [3, 4, 5, 6]]).unsqueeze(0).unsqueeze(0).float()
    model = SumAll()

    output = tta.fliplr_image2label(model, x)
    expected = int(x.sum())

    assert int(output) == expected

def test_fivecrop_image2label():
    x = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 1, 2], [3, 4, 5, 6]]).unsqueeze(0).unsqueeze(0).float()
    model = SumAll()

    output = tta.fivecrop_image2label(model, x, (2, 2))
    expected = ((1 + 2 + 5 + 6) + (3 + 4 + 7 + 8) + (9 + 0 + 3 + 4) + (1 + 2 + 5 + 6) + (6 + 7 + 0 + 1)) / 5

    assert int(output) == expected

def test_tencrop_image2label():
    x = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 1, 2], [3, 4, 5, 6]]).unsqueeze(0).unsqueeze(0).float()
    model = SumAll()

    output = tta.tencrop_image2label(model, x, (2, 2))
    expected = (2 * ((1 + 2 + 5 + 6) + (3 + 4 + 7 + 8) + (9 + 0 + 3 + 4) + (1 + 2 + 5 + 6) + (6 + 7 + 0 + 1))) / 10

    assert int(output) == expected

max_stars_repo_name: papkov/pytorch-toolbelt

max_issues_repo_name: papkov/pytorch-toolbelt

max_forks_repo_name: papkov/pytorch-toolbelt


text:
import numpy as np
import pytest

from dnnv.nn.graph import OperationGraph
from dnnv.nn import operations
from dnnv.properties.expressions import *
from dnnv.verifiers.common.reductions.iopolytope import *

def test_non_existential():
    reduction = IOPolytopeReduction()

    phi = Constant(True)
    with pytest.raises(NotImplementedError):
        properties = list(reduction.reduce_property(phi))

def test_no_network():
    reduction = IOPolytopeReduction()

    phi = Exists(Symbol("x"), Symbol("x") > Constant(0))
    with pytest.raises(IOPolytopeReductionError):
        properties = list(reduction.reduce_property(phi))

def test_non_concrete_network():
    reduction = IOPolytopeReduction()

    phi = Exists(Symbol("x"), Network("N")(Symbol("x")) > Constant(0))
    with pytest.raises(IOPolytopeReductionError):
        properties = list(reduction.reduce_property(phi))

# TODO : finish this
def test_simple_property():
    reduction = IOPolytopeReduction()

    phi = Exists(
        Symbol("x"),
        And(
            Constant(0) <= Symbol("x"),
            Symbol("x") <= Constant(1),
            Network("N")(Symbol("x")) > Constant(0),
        ),
    )
    input_op = operations.Input((1,), np.dtype(np.float64))
    output_op = operations.Add(input_op, operations.Mul(np.float64(-2), input_op))
    op_graph = OperationGraph([output_op])
    phi.concretize(N=op_graph)

    properties = list(reduction.reduce_property(phi))
    assert len(properties) == 1
    prop = properties[0]

    assert len(prop.networks) == 1

    assert np.all(prop.input_constraint._lower_bound == np.array([0]))
    assert np.all(prop.input_constraint._upper_bound == np.array([1]))

    assert np.all(prop.output_constraint._lower_bound == np.array([np.nextafter(0, 1)]))
    assert np.all(prop.output_constraint._upper_bound == np.array([np.inf]))

max_stars_repo_name: samysweb/dnnv

max_issues_repo_name: samysweb/dnnv

max_forks_repo_name: samysweb/dnnv


text:
import csv
import cv2
import numpy as np

images = []
measurements = []

data_dirs = ['track1-normal', 'track1-reverse']
dirs_lr = {'track1-normal': True,
           'track1-reverse': True,
           'track1-weaving': False}

for d in data_dirs:
    lines = []
    with open('/home/workspace/' + d + '/driving_log.csv') as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            lines.append(line)
    for line in lines:
        if line[0] == 'center':
            continue
        correction = 0.3
        image = cv2.imread(line[0])
        image_left = cv2.imread(line[1])
        image_right = cv2.imread(line[2])
        measurement = float(line[3])

        images.append(image)
        measurements.append(measurement)
        images.append(image_left)
        measurements.append(measurement + correction)
        images.append(image_right)
        measurements.append(measurement - correction)
        if dirs_lr[d]:
            images.append(cv2.flip(image,1))
            measurements.append(measurement*-1.0)
            images.append(cv2.flip(image_left,1))
            measurements.append((measurement + correction)*-1.0)
            images.append(cv2.flip(image_right,1))
            measurements.append((measurement - correction)*-1.0)

X_train = np.array(images)
y_train = np.array(measurements)

from keras.models import Sequential, Model
from keras.layers import Flatten, Dense, Lambda, Dropout
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Cropping2D

###### FIRST MODEL
# model = Sequential()
# model.add(Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160,320,3))) # normalize and mean center
# model.add(Flatten(input_shape=(160,320,3)))
# model.add(Dense(1))

# ###### LENET
# model = Sequential()
# model.add(Cropping2D(cropping=((70,25), (0,0)), input_shape=(160,320,3)))
# model.add(Lambda(lambda x: (x / 255.0) - 0.5))
# model.add(Convolution2D(6,5,5,activation="relu"))
# model.add(MaxPooling2D())
# model.add(Convolution2D(6,5,5,activation="relu"))
# model.add(MaxPooling2D())
# model.add(Flatten())
# model.add(Dense(120))
# model.add(Dropout(0.5))
# model.add(Dense(84))
# model.add(Dropout(0.5))
# model.add(Dense(1))

###### NVIDIA Model
model = Sequential()
model.add(Cropping2D(cropping=((70,25), (0,0)), input_shape=(160,320,3)))
model.add(Lambda(lambda x: (x / 255.0) - 0.5))
model.add(Convolution2D(24,(5,5),strides=(2,2),activation="relu"))
model.add(Convolution2D(36,(5,5),strides=(2,2),activation="relu"))
model.add(Convolution2D(64,(3,3),activation="relu"))
model.add(Convolution2D(64,(3,3),activation="relu"))
model.add(Flatten())
model.add(Dense(100))
model.add(Dense(50))
model.add(Dense(10))
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
model.fit(X_train, y_train, validation_split=0.3, shuffle=True, epochs=3)

model.save('model.h5')


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

import argparse
import numpy as np
from PIL import Image
import tensorflow as tf
from nets.nasnet import nasnet
from matplotlib import pyplot as plt
import logging
import os

model_clazz = __import__("model")
slim = tf.contrib.slim

image_size = nasnet.build_nasnet_mobile.default_image_size  # 224

"""
    测试模型
"""

def check_accuracy(sess):
    sess.run(model.test_init_op)
    num_correct, num_samples = 0, 0
    i = 0
    while True:
        i += 1
        CONSOLE.info("i: %d" % i)
        try:
            correct_prediction, accuracy, logits = sess.run([model.correct_prediction, model.accuracy, model.logits])
            num_correct += correct_prediction.sum()
            num_samples += correct_prediction.shape[0]
            CONSOLE.info("accuracy {} {}".format(accuracy, logits))
        except tf.errors.OutOfRangeError:
            CONSOLE.info("over")
            break
    acc = float(num_correct) / num_samples
    return acc

def check_digital(image_dir, sess):
    image = Image.open(image_dir)
    if "RGB" != image.mode:
        image = image.convert("RGB")
    image = np.asarray(image.resize((image_size, image_size)), dtype=np.float32).reshape(1, image_size, image_size, 3)
    image = 2 * (image / 255.0) - 1.0
    prediction = sess.run(model.logits, {model.images: image})
    # CONSOLE.info(prediction)
    pre = prediction.argmax()
    CONSOLE.info(pre)
    content = ""
    if 1 == pre:
        content = "cat"
    elif 2 == pre:
        content = "dog"
    elif 3 == pre:
        content = "panda"
    else:
        content = "None"
    plt.imshow(np.asarray((image[0] + 1) * 255 / 2, np.uint8))
    plt.show()
    CONSOLE.info("%s -- %s" % (content, image_dir))
    return pre

if "__main__" == __name__:
    logging.basicConfig(level=logging.INFO,
                        format='[%(asctime)8s][%(filename)s][%(levelname)s] - %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S')
    CONSOLE = logging.getLogger("dev")
    ap = argparse.ArgumentParser()
    ap.add_argument("-d", "--dir", required=True, type=str, help="检测训练集")
    ap.add_argument("-e", "--eval-dir", required=True, type=str, help="测试数据集")
    ap.add_argument("-s", "--batch-size", default=32, type=int, help="批处理次数")
    args = vars(ap.parse_args())
    model = model_clazz.CustomizeNASNetModel()
    model.build_model("test", test_data_dir=args["eval_dir"])
    with tf.Session() as session:
        model.load_cpk(model.global_step, session, 1, model.saver, model.save_path)
        val_acc = check_accuracy(session)
        CONSOLE.info("val accuracy: %f" % val_acc)
        CONSOLE.info("--------------------------------")
        img_dir = "/mount/data/afterloe resources/12485.jpg"
        check_digital(img_dir, session)
        CONSOLE.info("--------------------------------")
        img_dir = os.path.sep.join([args["dir"], "cat", "cat.27.jpg"])
        check_digital(img_dir, session)
        CONSOLE.info("--------------------------------")
        img_dir = os.path.sep.join([args["dir"], "dog", "dog.93.jpg"])
        check_digital(img_dir, session)
        CONSOLE.info("--------------------------------")
        img_dir = os.path.sep.join([args["dir"], "panda", "00000005.jpg"])
        check_digital(img_dir, session)
        CONSOLE.info("--------------------------------")
        img_dir = "/mount/data/afterloe resources/2934349b033b5bb5391070c437d3d539b700bcc1.jpg"
        check_digital(img_dir, session)
        CONSOLE.info("--------------------------------")
        img_dir = "/mount/data/afterloe resources/6.png"
        check_digital(img_dir, session)

max_stars_repo_name: afterloe/opencv-practice

max_issues_repo_name: afterloe/Opencv-practice

max_forks_repo_name: afterloe/Opencv-practice


text:
# Copyright 2021 Baidu 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.

"""Image classification model wrapper for paddle hub models."""

from __future__ import absolute_import
from perceptron.models.base import Model
import paddlehub as hub
import numpy as np

class MobilenetV2AnimalsModel(Model):
    """Create a :class:`Model` instance from an `MobulenetV2AnimalsModel` model.
    """

    def __init__(
            self,
            bounds=(0, 255),
            channel_axis=3,
            preprocessing=(0, 1)):

        super(MobilenetV2AnimalsModel, self).__init__(
            bounds=bounds,
            channel_axis=channel_axis,
            preprocessing=preprocessing)

        self._task = 'cls'
        self.model = hub.Module(name="mobilenet_v2_animals")

    def predictions(self, image):
        """Get prediction for input image
        Parameters
        ----------
        image : `numpy.ndarray`
            The input image in [h, w, c] ndarry format, BGR, 0 ~ 255
        Returns
        -------
        """
        image = image.astype(np.uint8)
        predictions = self.model.classification(images=[image])
        return predictions

    def model_task(self):
        """Get the task that the model is used for."""
        return self._task

max_stars_repo_name: honshj/PaddleSleeve

max_issues_repo_name: honshj/PaddleSleeve

max_forks_repo_name: honshj/PaddleSleeve


text:
from timeit import default_timer as timer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score,confusion_matrix,roc_curve,auc
import config as cf
from  sklearn.feature_selection import  chi2 , f_classif , mutual_info_classif , SelectKBest
import numpy as np

# def feature_methods(df,labels):
#   '''
#   This function will apply feature selection on the input set
#   :param df: The raw dataFrame passed to be feature selected.
#   :param labels: labers of the raw dataset.
#   '''
#   chi_2 = SelectKBest(chi2, k = 10000).fit_transform( df , labels )
#   f_measure = SelectKBest(f_classif, k = 10000).fit_transform( df , labels )
#   mutual_info = SelectKBest(mutual_info_classif, k = 10000).fit_transform( df , labels )
#   return [chi_2,f_measure,mutual_info]

def feature_methods(df,labels ):
    '''
    This function will apply feature selection on the input set
    :param df: The raw dataFrame passed to be feature selected.
    :param labels: labers of the raw dataset.
    '''
    import numpy as np
    chi_2 = SelectKBest(chi2, k = df.shape[1]).fit( df , labels )
    f_measure = SelectKBest(f_classif, k = df.shape[1]).fit( df , labels )
    mutual_info = SelectKBest(mutual_info_classif, k = df.shape[1]).fit( df , labels )
    #return [chi_2. ,f_measure. get_feature_names_out(input_features=columns),mutual_info. get_feature_names_out(input_features=columns)]
    #return [chi_2. get_feature_names_out(input_features=columns),f_measure. get_feature_names_out(input_features=columns),mutual_info. get_feature_names_out(input_features=columns)]
    return [df.iloc[:,(chi_2.pvalues_).argsort()].to_numpy(),df.iloc[:,(f_measure.pvalues_).argsort()].to_numpy(),df.iloc[:,(-mutual_info.scores_).argsort()].to_numpy()]
    #return [np.argsort(chi_2.pvalues_),np.argsort(f_measure.pvalues_),(-mutual_info.scores_).argsort()]

def different_size( size  , classifier , index , index_fs, each_fs,labels):
  '''
  This function will be able compute accuracy based on the classifier, feature_Size and feature_method
  :param size: the feature size to be used.
  :param classifier: the classifier to be used.
  :param index: index for a item in global_list list
  :param index_fs: index for a item in global_fs list
  :param each_fs: The feature selection method to employ
  '''
  local_dict = {}
  #X_train, X_test, y_train, y_test =  train_test_split(each_fs[:,:size] , labels , random_state = 0 );
  X_train, X_test, y_train, y_test =  train_test_split(each_fs[:,:int(each_fs.shape[1] * size)] , labels , random_state = 0 );
  start = timer()
  clf =  classifier.fit( X_train , y_train );
  end = timer()
  preds =  clf.predict(X_test) ;
  local_dict['clf'] = cf.global_list[index]
  local_dict['fs'] = cf.global_fs[index_fs]
  local_dict['size'] = size
  local_dict['model_time'] =  float(format(end - start , '.2f'))
  local_dict['accuracy'] = float(format((accuracy_score( preds , y_test)  * 100),'.2f'))
  return local_dict

def compute_accuracy( clf  , name ,X_train, y_train,X_test,y_test):
  result = {}
  result['clf'] = name
  start = timer()
  model =  clf.fit( X_train , y_train );
  end = timer()
  preds =  model.predict(X_test);
  result['Time'] =  float(format(end - start , '.2f'))
  result['accuracy'] =  float(format((accuracy_score( preds , y_test)  * 100),'.2f'))
  return result

def compute_roc_auc( clf,name ,df, labels):
  X_train,X_test, y_train,y_test = train_test_split( df , labels , random_state = 0 )
  result = {}
  result['clf'] = name
  start = timer()
  model =  clf.fit( X_train , y_train );
  end = timer()
  preds =  model.predict(X_test);
  result['Time'] =  float(format(end - start , '.2f'))
  result['accuracy'] =  float(format((accuracy_score( preds , y_test)  * 100),'.2f'))
  result['Conf'] = confusion_matrix(y_test,preds)
  result['fprRD'], result['tprRD'], threshold = roc_curve(y_test, preds)
  result['roc_aucRD'] = auc(result['fprRD'], result['tprRD'])
  return result


text:
from scipy.stats import invwishart, multivariate_normal
from scipy.special import digamma, loggamma
from itertools import repeat
import numpy as np

def random_normal_invw(mu, lam, psi, nu, rng=None):
    if rng is None:
        rng = np.random.default_rng()
    ret_sigma = invwishart.rvs(nu, psi,
                               random_state=rng)
    ret_mu = multivariate_normal.rvs(mu, ret_sigma / lam,
                                     random_state=rng)
    return ret_mu, ret_sigma

def posterior_norm_invw_params(y, mu, lam, psi, nu):
    n, _ = y.shape
    ret_mu = (lam * mu + n * y.mean(axis=0)) / (lam + n)
    ret_lam = lam + n
    ret_psi = psi + n * np.cov(y.T, bias=True) + (
            (lam * n) / (lam + n) *
            ((y.mean(axis=0) - mu) @ (y.mean(axis=0) - mu)))
    ret_nu = nu + n
    return {"mu": ret_mu, "lambda": ret_lam, "psi": ret_psi, "nu": ret_nu}

def gumbel_max_sampling(logp, size=None, *, rng=None):
    if size is None:
        ret = np.argmax(logp -
                        np.log(-np.log(rng.uniform(size=logp.shape))), axis=0)
    else:
        ret = []
        for i in range(size):
            ret.append(np.argmax(logp -
                                 np.log(-np.log(rng.uniform(size=len(logp))))))
        ret = np.array(ret)
    return ret

def rejection_sample(f, max_y, a=0, b=1, size=None, *, rng=None):
    if size is None:
        x = rng.uniform(a, b)
        y = rng.uniform(0, max_y)
        while y > f(x):
            x = rng.uniform(a, b)
            y = rng.uniform(0, max_y)
        return x
    else:
        x = rng.uniform(a, b, size)
        y = rng.uniform(0, max_y, size)
        while np.any(y > f(x)):
            x[y > f(x)] = rng.uniform(a, b, np.sum(y > f(x)))
            y[y > f(x)] = rng.uniform(0, max_y, np.sum(y > f(x)))
        return x

def mixture_density(x, w, mu, sigma, u):
    k = len(w)

    ret = []
    for j in range(k):
        ret.append(multivariate_normal.pdf(x,
                                           mu[j],
                                           sigma[j],
                                           1))

    ret = np.array(ret).T
    mask = (np.array(list(repeat(u, k))) <
            np.array(list(repeat(w, len(u)))).transpose())

    ret = np.atleast_2d(ret.dot(mask / mask.sum(0))).mean(1)
    return ret

def cluster(x, w, mu, sigma, u=None):
    k = len(w)
    assign_prob = []
    for j in range(k):
        assign_prob.append(multivariate_normal.pdf(x,
                                                   mu[j],
                                                   sigma[j],
                                                   1))
    assign_prob = np.array(assign_prob).T
    if u is not None:
        mask = (np.array(list(repeat(u, k))) <
                np.array(list(repeat(w, len(u)))).transpose())

        w = (mask / mask.sum(0)).sum(1) / len(u)
    assign_prob = assign_prob * w
    grp = np.argmax(assign_prob, axis=1)
    uncertainty = 1 - assign_prob[range(len(x)), grp]
    u_grp, ret = np.unique(grp, return_inverse=True)
    return ret, uncertainty

def log_wishart_normalization_term(precision, scale):
    dim = precision.shape[0]
    res = np.log(np.linalg.norm(precision)) * (-scale / 2)
    inverse_term = np.log(2) * (scale * dim / 2)
    inverse_term += np.log(np.pi) * (dim * (dim - 1) / 4)
    for i in range(dim):
        inverse_term += loggamma((scale - i) / 2)
    res -= inverse_term
    return res

def e_log_norm_wishart(precision, scale):
    dim = precision.shape[0]
    res = np.log(np.linalg.norm(precision))
    res += dim * np.log(2)
    for i in range(dim):
        res += digamma((scale - i) / 2)
    return res

def entropy_wishart(precision, scale):
    dim = precision.shape[0]
    res = -log_wishart_normalization_term(precision, scale)
    res -= (scale - dim - 1) / 2 * e_log_norm_wishart(precision, scale)
    res += scale * dim / 2
    return res

reason: import numpy,from scipy


text:
import os

import numpy as np

from nn_analysis import utils

def main():
    print(f"Process PID: {os.getpid()}")
    tmp_filename_A = "tmp/tmp_file_A.pkl"
    tmp_filename_B = "tmp/tmp_file_B.pkl"
    print("Creating objects...")
    A = np.random.normal(size=(10000,5000))
    B = np.random.normal(size=(10000,5000))
    print("Finished creating object.")
    with utils.SimulFileHandler(tmp_filename_A, tmp_filename_B):
        print("Dumping A...")
        utils.save_data(tmp_filename_A, A)
        print("Done A. Dumping B...")
        utils.save_data(tmp_filename_B, B)
        print("Done B.")

if __name__ == '__main__':
    main()


text:
""" Shared fixtures for tests """

import logging

import pytest
import numpy as np
import pandas as pd

from resqpy.model import Model
from resqpy.organize import WellboreFeature, WellboreInterpretation
from resqpy.well import Trajectory, MdDatum
from resqpy.crs import Crs

@pytest.fixture(autouse=True)
def capture_logs(caplog):
   """Always capture log messages from respy"""

   caplog.set_level(logging.DEBUG, logger="resqpy")

@pytest.fixture
def example_model(tmp_path):
   """ Returns a fresh RESQML Model and Crs, in a temporary directory """

   # TODO: parameterise with m or feet
   xyz_uom = 'm'

   # Create a model with a coordinate reference system
   model = Model(create_basics=True)
   crs = Crs(parent_model=model, z_inc_down=True, xy_units=xyz_uom, z_units=xyz_uom)
   crs.create_xml()

   # Using tmp_path to get a temporary directory unique to each test
   model_path = str(tmp_path / f'test_well_{crs.uuid}.epc')
   model.set_epc_file_and_directory(model_path)

   # Add an hdf5 reference part in anticipation of some arrays being used later on
   model.create_hdf5_ext(add_as_part=True, file_name=model_path[:-4] + '.h5')

   return model, crs

@pytest.fixture
def example_model_with_well(example_model):
   """ Model with a single well with a vertical trajectory """

   wellname = 'well A'
   elevation = 100
   md_uom = 'm'

   model, crs = example_model

   # Create a single well feature and interpretation
   well_feature = WellboreFeature(parent_model=model, feature_name=wellname)
   well_feature.create_xml()
   well_interp = WellboreInterpretation(parent_model=model, wellbore_feature=well_feature, is_drilled=True)
   well_interp.create_xml()

   # Create a measured depth datum
   datum = MdDatum(
      parent_model=model, crs_root=crs.crs_root, location=(0, 0, -elevation), md_reference='kelly bushing'
   )
   datum.create_xml()

   # Create trajectory of a vertical well
   mds = np.array([0, 1000, 2000])
   zs = mds - elevation
   traj = Trajectory(
      parent_model=model,
      md_datum=datum,
      data_frame=pd.DataFrame(dict(MD=mds, X=0, Y=0, Z=zs)),
      length_uom=md_uom,
      represented_interp=well_interp
   )
   traj.write_hdf5(mode='w')
   traj.create_xml()

   return model, well_interp, datum, traj

max_stars_repo_name: berland/resqpy

max_issues_repo_name: berland/resqpy

max_forks_repo_name: berland/resqpy


text:
from base_processor.timeseries import BaseTimeSeriesProcessor
import base_processor.timeseries.utils as utils

from nptdms import TdmsFile
import numpy as np

class TDMSProcessor(BaseTimeSeriesProcessor):

    def task(self):
        """
        Provides primitive parsing of .tdms files. Returns a TimeSeries object
        which represents the time-series metadata as stored on Pennsieve platform.

        Note #1: this uses npTDMS library which does not support DAQmx format.

        Note #2: this does not properly parse segments, as there were problems
                 where duplicate segments were found (tdms_file.segments). For now,
                 a single segment is expected per channel/object.

        Note #3: increment, n_samples, and length(data) were not consistent
                 for the sample data file I was using, so I'm not sure which value
                 was inconsistent. Right now, I'm trusting len(data) and increment
        """

        # load tdms file
        tdms_file = TdmsFile(self.inputs['file'])

        # find all groups in tdms file
        for g in tdms_file.groups():
            # find all channels in group
            for ch in tdms_file.group_channels(g):

                if not ch.has_data: continue

                # ignore if decimated signal
                decimated_level = ch.property('DecimationLevel')
                if decimated_level != 0:
                    continue

                # important properties in file header
                increment  = ch.property('wf_increment')
                start_time = ch.property('wf_start_time').replace(tzinfo=None)
                unit = ch.properties.get('unit_string', 'V')

                # assumptions:
                #  * increment in seconds
                #  * wf_samples is not accurate; using len(data) instead
                n_samples = ch.data.size

                # get times
                start_time_us = utils.usecs_since_epoch(start_time)

                # length of time
                length_sec = (n_samples-1) * increment
                length_us  = int(round(utils.secs_to_usecs(length_sec)))

                # end time
                end_time_us  = start_time_us + length_us

                # get time range
                timestamps = np.linspace(start_time_us, end_time_us, num=n_samples)
                timestamps = timestamps.astype(int)

                # create channel object
                channel = self.get_or_create_channel(
                        name = str(ch.channel).strip(),
                        unit = str(unit),
                        rate = 1.0/increment,
                        type = 'continuous')

                self.write_channel_data(
                    channel = channel,
                    timestamps = timestamps,
                    values = ch.data
                    )

        self.finalize()

max_stars_repo_name: Pennsieve/timeseries-processor

max_issues_repo_name: Pennsieve/timeseries-processor

max_forks_repo_name: Pennsieve/timeseries-processor


text:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import sys
sys.path.append(r"../..")
##This is your Project Root##
from pushkin_gs.definition import ROOT_DIR
# ROOT_DIR = os.path.abspath('..') #This is the father folder
# ROOT_DIR = os.path.abspath('.') #This is your current Work Root
import re
from nltk import word_tokenize
import unidecode
from string import punctuation
import numpy as np
from nltk.corpus import stopwords
from nltk.corpus import wordnet as wn

from nltk.stem import SnowballStemmer

class readLoadData():
    def read(readdata):
        ln = readdata.split("\n")
        return ln

class dataPreprocessing():

    def toLower(data):  ##not sure it is useful or not？##
        """
        :return: twitter sentences to lowercase
        """
        for index in range(len(data)):
            data[index] = data[index].lower()

        return data

    def NoiceReduction(data):
        """
        :description: 1.remove url,media etc/ 2.remove words with accents or emoticons/
                      3.remove non-printable ascII characters /4.Finally, the cleaned text is tokenized
        :return:
        """
        ##define emoji emoticons##
        emoji_pattern = re.compile("["
                                   u"\U0001F600-\U0001F64F"  # emoticons
                                   u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                                   u"\U0001F680-\U0001F6FF"  # transport & map symbols
                                   u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                                   "]+", flags=re.UNICODE)
        Data = []
        for sentence in data:

            ##remove url##
            sentence = re.sub(r'\w+:\/{2}[\d\w-]+(\.[\d\w-]+)*(?:(?:\/[^\s/]*))*', '', sentence)
            ##remove punctuations##
            sentence = ' '.join(word for word in sentence.split(" ") if word not in punctuation)
            ##modify accent(Málaga->Malaga)##
            sentence = unidecode.unidecode(sentence)
            ##remove emoticons##
            sentence = emoji_pattern.sub(r'', sentence)
            ##remove @people remove #hashtag##
            sentence = re.sub(r'[@#][ ]?([A-Za-z0-9_-]+)','',sentence)

            ##tokenization using nltk##
            # sentence = word_tokenize(sentence)
            ##there have an issue if use tokenization: words like 'gonna' will be split to 'gon','na', then next step remove abbreviation will make some noize data##
            sentence = sentence.split(" ")
            Data.append(sentence)

        return Data

    def combineAbbreviation(data):
        with open(ROOT_DIR + "/support/slangDic.txt", "r") as f1:
            slangDic = f1.read()

        # print (slangDic.split("\n"))
        slang = {}
        slangKey = []
        for index in slangDic.split("\n"):
            try:
                slang[index.split(":")[0]] = index.split(":")[1]
                slangKey.append(index.split(":")[0])
            except:
                pass
        # print (slangKey)

        dataWithOutAbb = []
        for sent in data:
            abb = [x for x in slangKey if x in sent]
            if abb:
                for item in abb:
                    sent = [x if (x != item) else slang[item] for x in sent]
                    sent = ' '.join(sent).split(" ")
                dataWithOutAbb.append(sent)
            else:
                dataWithOutAbb.append(sent)
        # print (dataWithOutAbb)
        return dataWithOutAbb

    def combinePhrase(data):
        """
        #Natural Language Corpus Data: Beautiful Data
        #http://norvig.com/ngrams/
        combine word phrase, like 'surface_water', but we need to find Synsets from wordNet, so cannot combine at this stage
        """
        pass
    def removeStopWord(data):
        ##remove stop word also stemmer word##

        dataWithOutStops = []
        snowball_stemmer = SnowballStemmer("english")
        for sent in data:

            filtered_words = [word for word in sent if word not in stopwords.words('english')]
            stemmer_words = [snowball_stemmer.stem(word) for word in filtered_words]
            dataWithOutStops.append(stemmer_words)
        print(dataWithOutStops)
        return dataWithOutStops

    def wordNet(data):
        """
        find Synsets
        many problems: Polysemy word will change sentence meaning, really time consuming
        """
        pass
        # print("1", wn.synsets('going'))  ##find similar words
        # print("2", wn.synset('miss.n.01').lemma_names())

if __name__ == "__main__":

    with open("text.txt","r") as f1:
        data = f1.read()

    readdata = readLoadData.read(data)
    lowerData = dataPreprocessing.toLower(readdata)
    dataNoiceReduction = dataPreprocessing.NoiceReduction(lowerData)
    dataWithoutAbb = dataPreprocessing.combineAbbreviation(dataNoiceReduction)
    processedData = dataPreprocessing.removeStopWord(dataWithoutAbb)
    # print (processedData)

"""
    print ("3",wn.synsets('car'))
    motorcar = wn.synset('car.n.01')
    types_of_motorcar = motorcar.hyponyms()
    print ("4",types_of_motorcar)
    print (sorted([lemma.name()
                   for synset in types_of_motorcar
                   for lemma in synset.lemmas()]))

    input = [[('A', 1), ('B', 2)], ('C', 3), ('D', 4)]
    from functools import reduce
    reduce(lambda x, y: x + y, input)
    print (list(itertools.chain(*input)))
"""

# if __name__ == "__main__":
#     #open all rawData & combine together
#
#     parser = argparse.ArgumentParser()
#     parser.add_argument('--corpus',  default='',action='store', nargs='+',help='sentiment file')
#
#
#     with open(ROOT_DIR+"/data/Subtasks_BD/twitter-2015testBD.txt", "r") as f1, \
#             open(ROOT_DIR+"/data/Subtasks_BD/twitter-2015train-BD.txt", "r") as f2:
#         rawData1,rawData2,rawData3,rawData4,rawData5,rawData6 = f1.read(),f2.read()
#


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 15 12:32:20 2022

@author: emgordy
"""

# make nice figure of

# top row MAE all predictions
# middle row MAE on 20% most confident predictions
# bottom row MAE difference from persistence

import xarray as xr
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import cmasher as cmr
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import glob
import sys
sys.path.append('functions/')
import nndata
import matplotlib.colors as colors
import pickle

mpl.rcParams['figure.facecolor'] = 'white'
mpl.rcParams['figure.dpi']= 150
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = ['Helvetica']
mpl.rcParams['font.size'] = 12

params = {"ytick.color" : "k",
          "xtick.color" : "k",
          "axes.labelcolor" : "k",
          "axes.edgecolor" : "k"}
plt.rcParams.update(params)

runin = 60
hiddens = [60,4]
lr = 1e-4
drate = 0.8

lys = [[1,5],[3,7]]

metricstrs = 'ExperimentMetricsAll_ly*_hiddens'+str(hiddens[0])+str(hiddens[1])+'_lr'+str(lr)+'_drate'+str(drate)+'.nc'
metricstrs = glob.glob(metricstrs)
metricstrs = sorted(metricstrs)

#%% load in metric ds

metricds1 = xr.open_dataset(metricstrs[0])
testloss = np.asarray(metricds1.testloss)
arrdims = np.shape(testloss)

bestMAEall = np.empty((arrdims[0],arrdims[1],2))+np.nan
bestMAE20 = np.empty((arrdims[0],arrdims[1],2))+np.nan

persistenceMAE = np.empty((arrdims[0],arrdims[1],2))+np.nan

# loop through the two lead year ranges
for ily in range(2):

    metricstr = metricstrs[ily]
    print(metricstr)
    ly1 = lys[ily][0]
    ly2 = lys[ily][1]

    metricsds = xr.open_dataset(metricstr)

    testloss = np.asarray(metricsds.testloss)
    testpercentiles = np.asarray(metricsds.testpercentiles)
    test_muvar = np.asarray(metricsds.test_muvar)
    testMAEscaled = np.asarray(metricsds.testMAEscaled)
    sCC = np.asarray(metricsds.testsCC)
    pvals = np.asarray(metricsds.testpvals)
    classacc = np.asarray(metricsds.testaccuracy)

    MAE = np.asarray(metricsds.testMAE)

    lat = np.asarray(metricsds.lat)
    lon = np.asarray(metricsds.lon)
    seeds = metricsds.seed
    percentiles = metricsds.percentile

    #calculate variance in Y_test
    Y_train, Y_val, Y_test, _, _ = nndata.makedata_heatx3_SSTonly_ALL(ly1,ly2)
    Y_testvar = np.expand_dims(np.std(Y_test,axis=0),axis=2)

    #get rid of land + flatliners
    test_muvar = test_muvar/Y_testvar

    nomodel = np.isnan(testloss)
    flatliner = test_muvar<0.1

    testloss[nomodel | flatliner] = np.nan
    testpercentiles[nomodel | flatliner,:] = np.nan
    test_muvar[nomodel | flatliner] = np.nan
    MAE[nomodel | flatliner] = np.nan
    sCC[nomodel | flatliner,:] = np.nan
    pvals[nomodel | flatliner,:] = np.nan
    classacc[nomodel | flatliner,:] = np.nan
    testMAEscaled[nomodel | flatliner,:] = np.nan

    with open('bestind_val_ly'+str(ly1)+'-'+str(ly2)+'_array.pkl', 'rb') as f: # load in best model seed numbers
        bestind = pickle.load(f)

    for ilat,latloop in enumerate(lat):
        for ilon,lonloop in enumerate(lon):
            if ~np.isnan(bestind[ilat,ilon]):
                bestMAEall[ilat,ilon,ily] = testpercentiles[ilat,ilon,int(bestind[ilat,ilon]),0]
                bestMAE20[ilat,ilon,ily] = testpercentiles[ilat,ilon,int(bestind[ilat,ilon]),8]

    # persistence model
    per_train, per_val, per_test, latsel, lonsel = nndata.sstpersistence_ALL(ly1,ly2)
    persistenceMAE[:,:,ily] = np.nanmean(np.abs(per_test-Y_test),axis=0)

errdiff = bestMAEall-persistenceMAE

#%% plotting

bestMAEall[np.isnan(bestMAEall)] = 36
bestMAE20[np.isnan(bestMAE20)] = 36
errdiff[np.isnan(errdiff)] = 0

bounds1 = np.arange(0.2,1.05,0.05)
bounds2 = np.arange(-0.7,0.8,0.1)

cmapMAE = cmr.torch_r
cmapdiffMAE = cmr.redshift_r

norm1 = colors.BoundaryNorm(boundaries=bounds1, ncolors=cmapMAE.N)
norm2 = colors.BoundaryNorm(boundaries=bounds2, ncolors=cmapdiffMAE.N)

projection=ccrs.EqualEarth(central_longitude=255)
transform=ccrs.PlateCarree()

plt.figure(figsize=(12,9))

a1=plt.subplot(3,2,1,projection=projection)
c1=a1.pcolormesh(lon,lat,bestMAEall[:,:,0],vmin=bounds1[0],vmax=bounds1[-1],cmap=cmapMAE,norm=norm1,transform=transform)
a1.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '110m', facecolor='gray'))
plt.title('lead year 1-5')
plt.text(-0.07,0.25,'all predictions',transform=a1.transAxes,rotation='vertical')
plt.text(0.02,0.97,'a',transform=a1.transAxes)

a2=plt.subplot(3,2,2,projection=projection)
a2.pcolormesh(lon,lat,bestMAEall[:,:,1],vmin=bounds1[0],vmax=bounds1[-1],cmap=cmapMAE,norm=norm1,transform=transform)
a2.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '110m', facecolor='gray'))
plt.title('lead year 3-7')
plt.text(0.02,0.97,'b',transform=a2.transAxes)

a3=plt.subplot(3,2,3,projection=projection)
c3=a3.pcolormesh(lon,lat,bestMAE20[:,:,0],vmin=bounds1[0],vmax=bounds1[-1],cmap=cmapMAE,norm=norm1,transform=transform)
a3.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '110m', facecolor='gray'))
plt.text(-0.07,0.15,'20% most confident',transform=a3.transAxes,rotation='vertical')
plt.text(0.02,0.97,'c',transform=a3.transAxes)

a4=plt.subplot(3,2,4,projection=projection)
a4.pcolormesh(lon,lat,bestMAE20[:,:,1],vmin=bounds1[0],vmax=bounds1[-1],cmap=cmapMAE,norm=norm1,transform=transform)
a4.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '110m', facecolor='gray'))
plt.text(0.02,0.97,'d',transform=a4.transAxes)

cax1 = plt.axes((0.91,0.46,0.015,0.35))
cbar1=plt.colorbar(c1,cax=cax1,ticks=np.arange(0.2,1.2,0.2))
cbar1.ax.set_ylabel('MAE')

a5=plt.subplot(3,2,5,projection=projection)
c5=a5.pcolormesh(lon,lat,errdiff[:,:,0],vmin=bounds2[0],vmax=bounds2[-1],cmap=cmapdiffMAE,norm=norm2,transform=transform)
a5.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '110m', facecolor='gray'))
plt.text(-0.07,0.2,'MAE difference',transform=a5.transAxes,rotation='vertical')
plt.text(0.02,0.97,'e',transform=a5.transAxes)

a6=plt.subplot(3,2,6,projection=projection)
a6.pcolormesh(lon,lat,errdiff[:,:,1],vmin=bounds2[0],vmax=bounds2[-1],cmap=cmapdiffMAE,norm=norm2,transform=transform)
a6.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '110m', facecolor='gray'))
plt.text(0.02,0.97,'f',transform=a6.transAxes)

cax2 = plt.axes((0.91,0.14,0.015,0.2))
cbar2 = plt.colorbar(c5,cax=cax2,ticks=np.arange(-0.6,0.9,0.3),extend='both')
cbar2.ax.set_ylabel(r'$\Delta$MAE ')

plt.tight_layout()

plt.savefig('figures/nicemaps.png',dpi=300)

plt.show()

max_stars_repo_name: emily-gordy/Decadal-SST-prediction

max_issues_repo_name: emily-gordy/Decadal-SST-prediction

max_forks_repo_name: emily-gordy/Decadal-SST-prediction


text:
# Load libraries.
import json
import numpy as np
import matplotlib.pyplot as plt
import pystan
from tqdm import trange
from scipy.spatial import distance_matrix
from scipy.stats import norm
from sklearn.datasets import make_moons
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import WhiteKernel, RBF

# Create data dictionary.
def create_stan_data(X, y, m_rho=0, s_rho=1, m_alpha=0, s_alpha=1, eps=1e-6):
    N, P = X.shape
    assert (N, ) == y.shape

    return dict(y=y, X=X, N=N, P=P,
                m_rho=m_rho, s_rho=s_rho,
                m_alpha=m_alpha, s_alpha=s_alpha, eps=eps)

# GP binary classification STAN model code.
model_code = """
data {
  int<lower=0> N;
  int<lower=0> P;
  int<lower=0, upper=1> y[N];
  matrix[N, P] X;
  real m_rho;
  real<lower=0> s_rho;
  real m_alpha;
  real<lower=0> s_alpha;
  real<lower=0> eps;
}

parameters {
  real<lower=0> rho;   // range parameter in GP covariance fn
  real<lower=0> alpha; // covariance scale parameter in GP covariance fn
  vector[N] eta;
  real beta;
}

transformed parameters {
  vector[N] f;
  {
    matrix[N, N] K;   // GP covariance matrix
    matrix[N, N] LK;  // cholesky of GP covariance matrix
    row_vector[N] row_x[N];

    // Using exponential quadratic covariance function
    for (n in 1:N) {
      row_x[n] = to_row_vector(X[n, :]);
    }
    K = cov_exp_quad(row_x, alpha, rho);

    // Add small values along diagonal elements for numerical stability.
    for (n in 1:N) {
        K[n, n] = K[n, n] + eps;
    }

    // Cholesky of K (lower triangle).
    LK = cholesky_decompose(K);

    f = LK * eta;
  }
}

model {
  // Priors.
  alpha ~ lognormal(m_alpha, s_alpha);
  rho ~ lognormal(m_rho, s_rho);
  eta ~ std_normal();
  beta ~ std_normal();

  // Model.
  y ~ bernoulli_logit(beta + f);
}
"""

# Compile model. This takes about a minute.
sm = pystan.StanModel(model_code=model_code)

# Make data.
X, y = make_moons(n_samples=50, shuffle=True, noise=0.1, random_state=1)

# Generate stan data dictionary.
stan_data = create_stan_data(X, y)

# Fit via ADVI.
vb_fit = sm.vb(data=stan_data, iter=1000, seed=1,
               grad_samples=1, elbo_samples=1)

# Fit via HMC.
# - stepsize = 0.05
# - num leapfrog steps = 20
# - burn in: 500
# - samples: 500
hmc_fit = sm.sampling(
    data=stan_data, iter=1000, warmup=500, thin=1,
    seed=1, algorithm='HMC', chains=1,
    control=dict(stepsize=0.05, int_time=1, adapt_engaged=False))

# Fit via NUTS.
nuts_fit = sm.sampling(data=stan_data, iter=1000, warmup=500, thin=1,
                       seed=1, algorithm='NUTS', chains=1)

max_stars_repo_name: luiarthur/TuringBnpBenchmarks

max_issues_repo_name: luiarthur/TuringBnpBenchmarks

max_forks_repo_name: luiarthur/TuringBnpBenchmarks


text:
import conx as cx
import numpy as np
import matplotlib.pyplot as plt
from scipy import misc
import glob, random

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

def save_files(images, labels, dir_name, start=0):
    assert type(dir_name) is str and len(images) == len(labels)
    for i in range(len(images)):
        category = labels[i]
        filename = '%s/%d_%d.png' % (dir_name, category, start+i)
        misc.imsave(filename, images[i])
        if i > 0 and i % 5000 == 0:
            print('%d images saved' % i)
    print('all done')

# returns images and labels in dir_name folder, split and randomly shuffled
def myload_data(dir_name, limit=None, split=None):
    filenames = glob.glob('%s/*.png' % dir_name)
    num_files = len(filenames)
    if num_files == 0:
        print('ERROR: no files found')
        return None
    if limit is None:
        limit = num_files
    elif type(limit) is not int or not 0 < limit <= num_files:
        print('ERROR: invalid limit')
        return None
    if split is None or type(split) is int and 0 < split < limit:
        pass
    elif type(split) is float and 0 < split < 1:
        split = int(split*limit)
    else:
        print('ERROR: invalid split')
        return None
    random.shuffle(filenames)
    img = misc.imread(filenames[0])
    images = np.empty((limit,) + img.shape).astype('uint8')
    labels = np.empty((limit,)).astype('uint8')
    print('loading images...')
    for i, filename in enumerate(filenames):
        if i >= limit: break
        images[i] = misc.imread(filename)
        labels[i] = int(filename.split('/')[1].split('_')[0])
        if i > 0 and i % 1000 == 0:
            print('%d images loaded' % i)
    if split is None:
        print('images:', images.shape)
        print('image bytes:', images.nbytes)
        print('labels:', labels.shape)
        print('label bytes:', labels.nbytes)
        return images, labels
    else:
        train_images = images[:split]
        train_labels = labels[:split]
        test_images = images[split:]
        test_labels = labels[split:]
        print('train_images: %s, test_images: %s' % (train_images.shape, test_images.shape))
        print('image bytes:', train_images.nbytes + test_images.nbytes)
        print('train_labels %s, test_labels: %s' % (train_labels.shape, test_labels.shape))
        print('label bytes:', train_labels.nbytes + test_labels.nbytes)
        return (train_images, train_labels), (test_images, test_labels)

def info():
    print('images: %s %s' % (images.shape, images.dtype))
    print('image bytes: %d' % images.nbytes)
    print('labels: %s %s' % (labels.shape, labels.dtype))
    print('label bytes: %d' % labels.nbytes)

def info2():
    print('train_images: %s %s' % (train_images.shape, train_images.dtype))
    print('test_images: %s %s' % (test_images.shape, test_images.dtype))
    print('total image bytes: %d' % (train_images.nbytes + test_images.nbytes))
    print('train_labels: %s %s' % (train_labels.shape, train_labels.dtype))
    print('test_labels: %s %s' % (test_labels.shape, test_labels.dtype))
    print('total label bytes: %d' % (train_labels.nbytes + test_labels.nbytes))

def reshuffle(images, labels):
    num_images = images.shape[0]
    num_labels = labels.shape[0]
    assert num_images == num_labels
    indices = np.random.permutation(num_images)
    return images[indices], labels[indices]

def view(images, start=0, cmap=None, interpolation=None):
    plt.axis('off')
    k = start
    while True:
        print('image %d' % k)
        plt.imshow(images[k], cmap=cmap, interpolation=interpolation)
        plt.draw()
        k += 1
        answer = raw_input('RETURN to continue, q to quit...')
        if answer == 'q': break

# to save:
#np.savez_compressed('mydataset.npz', images=images, labels=labels)

plt.ion()
#---------------------------------------------------------------------------

net = cx.Network("MNIST")
net.add(cx.Layer("input", shape=784, vshape=(28, 28)))
net.add(cx.Layer("hidden1", shape=512, vshape=(16,32), activation='relu', dropout=0.2))
net.add(cx.Layer("hidden2", shape=512, vshape=(16,32), activation='relu', dropout=0.2))
net.add(cx.Layer("output", shape=10, activation='softmax'))

net.connect('input', 'hidden1')
net.connect('hidden1', 'hidden2')
net.connect('hidden2', 'output')

net.compile(loss='mean_squared_error',
            optimizer='sgd')

ds = cx.Dataset()
ds.get("mnist")
net.set_dataset(ds)
#net.rescale_inputs((0,255), (0,1), 'float32')
#net.shuffle_dataset()
ds.inputs.reshape(784)
ds.slice(100)
#net.set_targets_to_categories(10)
ds.summary()

net.train(10)
#net.test()

max_stars_repo_name: Quantitative-Big-Imaging/conx

max_issues_repo_name: Quantitative-Big-Imaging/conx

max_forks_repo_name: Quantitative-Big-Imaging/conx


text:
import mysql.connector
from mysql.connector import Error
import numpy as np
import pandas as pd
from typing import Optional

import constants as c

class Connector:
    """
        Allows inserting and retrieving training data from a MySQL database.
    """

    def __init__(self):
        self.connection = None
        try:
            self.connection = mysql.connector.connect(
                host=c.HOST_NAME,
                user=c.USER_NAME,
                passwd=c.PASSWORD,
                database=c.DATABASE,
            )
        except Error as e:
            print(f"The error '{e}' occurred")

    def _send_query(self, query: str, print_out: bool = False, print_out_errors: bool = True) -> Optional[list]:
        cursor = self.connection.cursor()
        try:
            cursor.execute(query)
            result = cursor.fetchall()
            self.connection.commit()
            if print_out:
                print('send query successfully')
            return result
        except Error as e:
            if print_out_errors:
                print(f"The error '{e}' occurred")

    def retrieve_data(self, query: str) -> Optional[pd.DataFrame]:
        try:
            df = pd.read_sql(query, self.connection)
            self.connection.commit()
            return df
        except Error as e:
            print(f"The error '{e}' occurred")

    # column number still hardcoded
    def insert_examples(self, examples: list, count_factor: float = 1.0, table: str = 'training_data') -> None:
        for ex in examples:
            ex = self._example_to_dict(ex)
            data = self.retrieve_data(f"SELECT * FROM {table} WHERE state = '{ex['state']}'")
            if not data.empty:
                old_dict = data.to_dict('records')[0]
                ex = self._merge_examples(old_dict, ex, count_factor=count_factor)
                self._send_query(
                    f"UPDATE {table} SET state = '{ex['state']}', val = {ex['val']},"
                    f"p0 = {ex['p0']}, p1 = {ex['p1']}, p2 = {ex['p2']}, p3 = {ex['p3']},"
                    f"p4 = {ex['p4']}, p5 = {ex['p5']}, p6 = {ex['p6']},"
                    f"counter = {ex['counter']}, last_edited = CURRENT_TIMESTAMP WHERE state = '{ex['state']}';"
                )
            else:
                self._send_query(
                    f"INSERT INTO {table} (state, val, p0, p1, p2, p3, p4, p5, p6, counter)"
                    f" VALUES ('{ex['state']}', {ex['val']}, {ex['p0']}, {ex['p1']}, {ex['p2']}, {ex['p3']},"
                    f"{ex['p4']}, {ex['p5']}, {ex['p6']}, 1);"
                )

    @staticmethod
    def _merge_examples(old_dict: dict, new_dict: dict, count_factor: float = 1.0) -> dict:
        counter = old_dict['counter']
        scaled_counter = counter * count_factor
        merged_dict = {'state': old_dict['state'], 'counter': counter + 1}
        for column in ['val'] + [f'p{i}' for i in range(c.COLUMNS)]:
            merged_dict.update({
                column: (1 / (scaled_counter + 1)) * new_dict[column] +
                        (scaled_counter / (scaled_counter + 1)) * old_dict[column]
            })
        return merged_dict

    def df_to_examples(self, df: pd.DataFrame) -> list:
        dictionary = df.to_dict('records')
        examples = []
        for row in dictionary:
            state = self._string_to_state(row['state'])
            value = row['val']
            pi = np.array([row[f'p{i}'] for i in range(c.COLUMNS)])
            examples.append((state, (pi, value)))
        return examples

    def _example_to_dict(self, example: tuple) -> dict:
        state = self._state_to_string(example[0])
        value = example[1][1]
        pi = example[1][0]
        return {'state': state, 'val': value, **dict((f'p{i}', pi[i]) for i in range(7))}

    @staticmethod
    def _state_to_string(state: np.array) -> str:
        return ''.join(str(int(state[i, j] + 1)) for i, j in np.ndindex(np.shape(state)))

    @staticmethod
    def _string_to_state(string: str) -> np.array:
        return np.reshape(np.array([int(char) for char in string]), (c.COLUMNS, c.ROWS)) - 1

    def _create_training_data_table(self, table: str) -> None:
        self._send_query(
            f"CREATE TABLE IF NOT EXISTS {table} ("
            f"id INT(10) NOT NULL AUTO_INCREMENT,"
            f"state VARCHAR(42) UNIQUE NOT NULL,"
            f"val DECIMAL(7,6) NOT NULL,"
            f"p0 DECIMAL (7,6) NOT NULL,"
            f"p1 DECIMAL (7,6) NOT NULL,"
            f"p2 DECIMAL (7,6) NOT NULL,"
            f"p3 DECIMAL (7,6) NOT NULL,"
            f"p4 DECIMAL (7,6) NOT NULL,"
            f"p5 DECIMAL (7,6) NOT NULL,"
            f"p6 DECIMAL (7,6) NOT NULL,"
            f"counter INT(10) NOT NULL"
            f"last_edited TIMESTAMP DEFAULT CURRENT_TIMESTAMP,"
            f"PRIMARY KEY (id)"
            f");"
        )
        self._send_query(
            f"CREATE UNIQUE INDEX state_index ON {table}(state);",
            print_out_errors=False
        )


text:
import os
import random
import numpy as np
import cv2
import paddle.fluid as fluid
from PIL import Image
class Transform():
    def __init__(self,size=32):
        self.size=size

    def __call__(self,image,label):
        img = image/255.0
        return img,label

class DataLoader():
    def __init__(self,
                 image_folder,
                 image_list_file,
                 transform=None,
                 shuffle=True):
        self.image_folder = image_folder
        self.image_list_file = image_list_file
        self.transform = transform
        self.shuffle = shuffle
        self.paths = self.read_list()

    def read_list(self):
        fp = open(self.image_list_file)
        path_list = []
        for line in fp.readlines():
            paths = line.split()
            image_path = paths[0]
            image_path = os.path.join(self.image_folder,image_path)
            path_list.append(image_path)
        if self.shuffle:
            random.shuffle(path_list)
        return path_list

    def preprocess(self, data,label):
        if self.transform:
            data,label = self.transform(data,label)
        #print(data.shape)
        data = data[:,:,np.newaxis]
        return data,label      ## must be a list or tuple to compatible with function,from_generator()

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

    def __call__(self):
        for image_path in self.paths:
            data = np.array(Image.open(image_path))
            label = np.ones(1)
            yield self.preprocess(data,label)

def make_train_dataset(image_path,folder):
    img = Image.open(image_path)
    img = np.array(img)
    img = cv2.blur(img,(3,3))
    print(img.shape)
    fp = open("fabric_list.txt",'a+')
    for i in range(2,16):
        for j in range(500):
            x,y = np.random.rand(2)*(256-32)
            x,y = int(x),int(y)
            temp = img[y:y+32,i*256+x:i*256+x+32]
            temp2 = Image.fromarray(np.uint8(temp))
            image_path = f"{folder}/{i}_{j}.png"
            temp2.save(image_path)
            fp.write(image_path)
            fp.write("\n")
            fp.flush()

def main():
    batch_size = 3
    #place = fluid.CPUPlace()
    transform = Transform()
    #place = fluid.CUDAPlace(0)
    with fluid.dygraph.guard():
        # TODO: craete BasicDataloder instance
        image_folder=""
        image_list_file="dummy_data/fabric_list.txt"
        data = DataLoader(image_folder,image_list_file,transform=transform)
        #z = ZLoader(len(data))
        #print("size of data: ",len(z))

        dataloader = fluid.io.DataLoader.from_generator(capacity=2, return_list=True)
        dataloader.set_sample_generator(data,batch_size)

        num_epoch = 2
        for epoch in range(1, num_epoch+1):
            print(f'Epoch [{epoch}/{num_epoch}]:')
            for idx, (data,label) in enumerate(dataloader):
                print(f'Iter {idx}, Data shape: {data.shape},{label.shape} ')
                img = np.squeeze(data.numpy()[0])*255
                cv2.imwrite(f"{idx}.png",img)

if __name__ == "__main__":

    main()

    # image_path = "dummy_data/fabric/0001_000_02.png"
    # save_folder = "dummy_data/fabric/train"
    # make_train_dataset(image_path,save_folder)

    # image_path = "dummy_data/fabric/0011_006_02.png"
    # save_folder = "dummy_data/fabric/test"
    # make_dataset(image_path,save_folder)


text:
from sklearn import datasets
from sklearn.model_selection import validation_curve
from sklearn.svm import SVC
import matplotlib.pyplot as plt
import numpy as np

#  加载数据集
loaded_digits = datasets.load_digits()
X = loaded_digits.data
y = loaded_digits.target

# 建立参数集
param_range = np.logspace(-6, -2.3, 5)

train_loss, test_loss = validation_curve(SVC(), X, y, param_name="gamma", param_range=param_range, cv=10, scoring='neg_mean_squared_error')

print(train_loss)

train_loss_mean = -np.mean(train_loss, axis=1)`
test_loss_mean = -np.mean(test_loss, axis=1)

plt.plot(param_range, train_loss_mean, 'o-', color='g', label="Traing_loss_mean")
plt.plot(param_range, test_loss_mean, 'o-', color='r', label='Test_loss_mean')

plt.xlabel('Training example')
plt.ylabel('loss')
plt.legend(loc="best")
plt.show()

max_stars_repo_name: zjhdota/practice

max_issues_repo_name: zjhdota/practice

max_forks_repo_name: zjhdota/practice


text:
from matplotlib import pyplot as plt
from numpy import array, zeros, amax, amin
from matplotlib.ticker import MultipleLocator, AutoMinorLocator

def cm_to_inch(val:float):
    return val * 0.393700787

def plot_SP_data(model, overlay=False):
    t = []
    SP = []
    A = []
    with open("C:\\Users\\luukv\\Documents\\Studie\\Masters\\Jaar2\\MSc_thesis\\PAPER\\forcedata\\{}_SPdata.txt".format(model), 'r') as f:
    # with open("C:\\Users\\luukv\\Documents\\ThesisData\\{}\\{}_SPdata.txt".format(model, model), 'r') as f:
        for line in f.readlines():
            if line.startswith('time'):
                pass
            else:
                data = line.split()
                if float(data[0]) / 1e6 < 0.01:
                    pass
                else:
                    t.append(float(data[0]) / 1e6)
                    SP.append(float(data[1]))
                    A.append(float(data[2]))
    if overlay:
        return array(t), array(SP), array(A)
    else:
        fig, ax = plt.subplots(2, figsize=(8, 4.0))
        #cm_to_inch(14.0), cm_to_inch(15.2))
        ax[0].plot(t, SP, 'r*-', label=model)
        ax[1].plot(t, A, 'r*-', label=model)
        for axis in ax:
            axis.xaxis.set_major_locator(MultipleLocator(10))
            axis.xaxis.set_major_formatter('{x:.0f}')
            axis.xaxis.set_minor_locator(MultipleLocator(5))
        # ax[0].set_xlabel('Time [Myr]')
        ax[1].set_xlabel('Time [Myr]')
        ax[0].set_ylabel('Slab pull force [N/m]')
        ax[1].set_ylabel(r'Slab area [$m^2$]')

        ax[0].grid(b=True)
        ax[1].grid(b=True)
        return fig, ax

if __name__ =="__main__":
    # dictionary to hold colours and labels for each model to prevent mistakes.
    idclrs = {"ER": ['r', 'ref'], "FI": ['b', 'oc510'], "FI_shortpush": [(0, 0, 0.67), 'oc510sp'], "FJ": ['k', 'oc410'], "FK": ['g', 'oc310'],
              "FP_rerun": [(1, 153 / 255, 153 / 255), 'peierls'], "FQ": ['c', 'LM25'], "FR": ['y', 'LM50'],
              "FS": ['m', 'LM75'], "FT": [(139 / 255, 69 / 255, 19 / 255), 'LMoc510'], "FX": [(0.8, 0.8, 0.8),
                                                                                'slowref']}

    Models = ["ER", 'FI', 'FJ', 'FK', 'FP_rerun', 'FQ', 'FR', 'FS', 'FT', 'FX']
    # Models = ["ER", "FQ", "FR", "FS", "FT"]
    # Models = ["ER", "FI", "FJ", "FK"]
    # Models = ["FP"]
    # filename = "oceanlength_old"
    filename = "selection"
    # fig, ax = plot_SP_data("ER", overlay=False)
    fig, ax = plt.subplots(2, figsize=(8, 3.5))
    for axis in ax:
        axis.xaxis.set_major_locator(MultipleLocator(10))
        axis.xaxis.set_major_formatter('{x:.0f}')
        axis.xaxis.set_minor_locator(MultipleLocator(5))
    for i, model in enumerate(Models):
        t, SP, A = plot_SP_data(model, overlay=True)
        if model != "ER":
            ax[0].plot(t, SP, '-', color=idclrs[model][0], label= idclrs[model][1])
            ax[1].plot(t, A, '-', color=idclrs[model][0])
            print("Peak slab pull of {:.3e} N/m reaced at t = {} for model {}".format(min(SP), t[SP.argmin()], model))
        else:
            ax[0].plot(t, SP, '-', color=idclrs[model][0], marker='*', label=idclrs[model][1])
            ax[1].plot(t, A, '-', color=idclrs[model][0], marker='*')
            print("Peak slab pull of {:.3e} N/m reaced at t = {} for model {}".format(min(SP), t[SP.argmin()], model))
    # fig, ax = plot_SP_data("FI", overlay=False)
    # t, SP, A = plot_SP_data("ER", overlay=True)
    # ax[0].plot(t, SP,'g--')
    # ax[1].plot(t, A, 'g--')
    # for a in ax:
        # a.set_xlim([0, 20])
    # ax[0].set_xlabel('Time [Myr]')
    ax[1].set_xlabel('Time [Myr]')
    ax[0].set_ylabel('Slab pull force [N/m]')
    ax[1].set_ylabel(r'Slab area [$m^2$]')
    [_.set_xlim([0, 115]) for _ in ax] if "FX" in Models else [_.set_xlim([0, 60]) for _ in ax]
    ax[0].set_ylim([-4.7e14, 0.8e14]) if "FX" in Models else ax[0].set_ylim([-1.7e14, 0.7e14])
    ax[0].plot([0, 120], [0, 0], 'k:', linewidth=2)
    ax[1].set_ylim([0, 8.1e11]) if "FX"in Models else ax[1].set_ylim([0, 5e11])
    ax[0].legend(bbox_to_anchor=(0.5, 1.25), ncol=5, loc='upper center', borderaxespad=0.1, fancybox=True)
    # plt.subplots_adjust(bottom=0.15)  # place legend below figure
    # plt.show()
    # plt.gca().set_axis_off()
    plt.subplots_adjust(top=1, bottom=0, right=1, left=0,
                        hspace=0.2, wspace=0)
    plt.margins(0, 0)
    plt.savefig('C:/Users/luukv/Documents/Studie/Masters/Jaar2/MSc_thesis/PAPER/modelfigs/forces/{}.png'.format(filename), bbox_inches='tight', pad_inches=0, dpi=300)

reason: from numpy


text:
# coding=utf-8
# Copyright 2021 The Google Research Authors.
#
# 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.

"""Tests for make_surrogate_posteriors."""

import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
from automatic_structured_vi import make_surrogate_posteriors

tfd = tfp.distributions

def _make_prior_dist():

  def _prior_model_fn():
    innovation_noise = 0.1
    prior_loc = 0.
    new = yield tfd.Normal(loc=prior_loc, scale=innovation_noise)
    for _ in range(4):
      new = yield tfd.Normal(loc=new, scale=innovation_noise)

  return tfd.JointDistributionCoroutineAutoBatched(_prior_model_fn)

class MakeSurrogatePosteriorsTest(tf.test.TestCase):

  def assertAllEqualNested(self, struct_a, struct_b):
    tf.nest.map_structure(self.assertAllEqual, struct_a, struct_b)

  def assertAllFinite(self, a):
    is_finite = np.isfinite(a.numpy())
    all_true = np.ones_like(is_finite, dtype=np.bool)
    self.assertAllEqual(all_true, is_finite)

  def test_make_flow_posterior(self):
    prior_dist = _make_prior_dist()

    iaf_surrogate_dist = make_surrogate_posteriors.make_flow_posterior(
        prior_dist, num_hidden_units=8, invert=True)

    iaf_log_prob = prior_dist.log_prob(iaf_surrogate_dist.sample())
    self.assertAllFinite(iaf_log_prob)

    self.assertAllEqualNested(prior_dist.event_shape_tensor(),
                              iaf_surrogate_dist.event_shape_tensor())

    maf_surrogate_dist = make_surrogate_posteriors.make_flow_posterior(
        prior_dist, num_hidden_units=8, invert=False)

    iaf_log_prob = prior_dist.log_prob(maf_surrogate_dist.sample())
    self.assertAllFinite(iaf_log_prob)

    self.assertAllEqualNested(prior_dist.event_shape_tensor(),
                              maf_surrogate_dist.event_shape_tensor())

  def test_make_mvn_posterior(self):
    prior_dist = _make_prior_dist()
    surrogate_dist = make_surrogate_posteriors.make_mvn_posterior(
        prior_dist)

    self.assertAllEqualNested(
        prior_dist.event_shape_tensor(),
        surrogate_dist.event_shape_tensor())

    self.assertAllFinite(prior_dist.log_prob(surrogate_dist.sample()))

  def test_build_autoregressive_surrogate_posterior(self):
    prior_dist = _make_prior_dist()
    surrogate_dist = make_surrogate_posteriors.build_autoregressive_surrogate_posterior(
        prior_dist, make_surrogate_posteriors.make_conditional_linear_gaussian)

    self.assertAllEqualNested(
        prior_dist.event_shape_tensor(),
        surrogate_dist.event_shape_tensor())

    self.assertAllFinite(prior_dist.log_prob(surrogate_dist.sample()))

if __name__ == '__main__':
  tf.test.main()

max_stars_repo_name: DionysisChristopoulos/google-research

max_issues_repo_name: davidfitzek/google-research

max_forks_repo_name: davidfitzek/google-research


text:
#
#    Author: David Futschik
#    Provided as part of the Chunkmogrify project, 2021.
#

import math
import torch
import numpy as np
from torch.nn import functional as F

def torch_grad(x):
    a = torch.tensor([[-1, 0, 1]], dtype=torch.float32, device=x.device, requires_grad=False).view((1, 1, 1, 3))
    b = torch.tensor([[-1, 0, 1]], dtype=torch.float32, device=x.device, requires_grad=False).view((1, 1, 3, 1))
    G_x = F.conv2d(x, a) / 2
    G_y = F.conv2d(x, b) / 2
    G_x = F.pad(G_x, (1, 1, 0, 0), 'constant', 0.)
    G_y = F.pad(G_y, (0, 0, 1, 1), 'constant', 0.)
    return [G_x, G_y]

def torch_normgrad_curv(x):
    G_x, G_y = torch_grad(x)
    G = torch.sqrt(torch.pow(G_x,2) + torch.pow(G_y,2))
    # div
    div = torch_grad(G_x / (G + 1e-8) )[0] + torch_grad(G_y / (G + 1e-8))[1]
    return G, div

def contrast_magnify(x, min=0, max=64, fromval=0., toval=255.):
    mul_by = (toval - min) / max
    x = ((x - min) * mul_by).clip(fromval, toval)
    return x

def mask_refine(mask, image1, image2, dt_A=0.001, dt_B=0.1, iters=300):

    S = (mask - 0.5)
    P = S[:, 0:1, :, :]

    Pg = torch_normgrad_curv(P)[0]

    I_1_mean = ((Pg * image1)).sum(dim=(2,3)) / Pg.sum()
    I_2_mean = ((Pg * image2)).sum(dim=(2,3)) / Pg.sum()

    assert I_1_mean.shape == (1, 3), "Mean wrong shape"
    I_1 = image1 - I_1_mean[:, :, None, None]
    I_2 = image2 - I_2_mean[:, :, None, None]
    Fn = I_1 - I_2
    Fn = Fn.norm(dim=1) / math.sqrt(12)

    P = P * 255
    Fn = Fn * 255

    Fn = contrast_magnify(Fn)

    with torch.no_grad():
        for _ in range(iters):
            ng, div = torch_normgrad_curv(P)
            P -= dt_A * Fn * ng - dt_B * ng * div

    P = torch.where(P < 0, 0., 1.)

    new_mask = P
    return new_mask

if __name__ == "__main__":
    import PIL.Image

    i1 = PIL.Image.open("_I1_.png")
    i2 = PIL.Image.open("_I2_.png")
    s = PIL.Image.open("_P_.png")

    i1 = (torch.tensor( np.array(i1).astype(np.float), dtype=torch.float32, device='cuda:0' ) / 127.5).permute((2,0,1)).unsqueeze(0)
    i2 = (torch.tensor( np.array(i2).astype(np.float), dtype=torch.float32, device='cuda:0' ) / 127.5).permute((2,0,1)).unsqueeze(0)
    s = (torch.tensor( np.array(s).astype(np.float), dtype=torch.float32, device='cuda:0' ) / 255.).unsqueeze(0).unsqueeze(0)

    m = mask_refine(s, i1 - 1, i2 - 1)

    d = m.cpu().numpy()[0].transpose((1,2,0)).__mul__(255.).astype(np.uint8)
    PIL.Image.fromarray(d[:, :, 0]).save("maskrefine.png")

max_stars_repo_name: forkbabu/Chunkmogrify

max_issues_repo_name: bycloudai/Chunkmogrify

max_forks_repo_name: bycloudai/Chunkmogrify


text:
from collections import deque, Counter
from functools import lru_cache
from typing import List, Deque, NamedTuple, Tuple, Dict

import numpy as np

from shor.gates import _Gate
from shor.quantum import Circuit

class _GateTuple(NamedTuple):
    qubits: List[int]
    matrix: np.ndarray
    order: int

class _QuantumBackend(object):
    def run(self, initial_state, gates, measure_bits, num_shots):
        pass

class QuantumSimulator(_QuantumBackend):

    @staticmethod
    @lru_cache(maxsize=10)
    def combine_gates(gates: Tuple[_Gate]) -> _GateTuple:
        to_combine: Deque[_GateTuple] = deque(_GateTuple(g.qubits, g.to_matrix(), i) for i, g in enumerate(gates))
        combined: Deque[_GateTuple] = deque()

        while len(to_combine) > 1:
            while len(to_combine) > 1:
                left_gate = to_combine.popleft()
                right_gate = to_combine.popleft()

                # Swap if out of order
                if right_gate.order < left_gate.order:
                    left_gate, right_gate = right_gate, left_gate

                can_combine_no_tensor = set(left_gate.qubits) == set(right_gate.qubits)
                can_combine_with_tensor = not has_common_qbits(left_gate, right_gate)

                if can_combine_no_tensor:
                    combined.append(_GateTuple(
                        left_gate.qubits,
                        change_qubit_order(right_gate.matrix, right_gate.qubits, left_gate.qubits).dot(left_gate.matrix),
                        left_gate.order
                    ))
                elif can_combine_with_tensor:
                    combined.append(_GateTuple(
                        left_gate.qubits + right_gate.qubits,
                        np.kron(
                            left_gate.matrix,
                            right_gate.matrix
                        ),
                        right_gate.order
                    ))
                else:
                    common_qubits = [q for q in left_gate.qubits if q in right_gate.qubits]
                    left_only_qbits = [q for q in left_gate.qubits if q not in common_qubits]
                    right_only_qbits = [q for q in right_gate.qubits if q not in common_qubits]
                    all_qubits = left_only_qbits + common_qubits + right_only_qbits

                    new_left_qbit_order = left_only_qbits + common_qubits
                    new_left_gate = change_qubit_order(left_gate.matrix, left_gate.qubits, new_left_qbit_order)

                    # Tensor product with I to fill missing qbits
                    qbits_to_add = len(all_qubits) - len(left_gate.qubits)
                    if qbits_to_add > 0:
                        new_left_gate = np.kron(new_left_gate, np.eye(np.power(2, qbits_to_add)))

                    new_right_qbit_order = common_qubits + right_only_qbits
                    new_right_gate = change_qubit_order(right_gate.matrix, right_gate.qubits, new_right_qbit_order)

                    # Tensor product with I to fill missing qbits
                    qbits_to_add = len(all_qubits) - len(right_gate.qubits)
                    if qbits_to_add > 0:
                        new_right_gate = np.kron(np.eye(np.power(2, qbits_to_add)), new_right_gate)

                    combined.append(_GateTuple(
                        all_qubits,
                        new_right_gate.dot(new_left_gate),
                        right_gate.order
                    ))

            to_combine += combined
            combined = deque()

        return to_combine.pop()

    def run(self, initial_state: np.ndarray, gates: List[_Gate], measure_bits: List[int], num_shots):
        combined = self.combine_gates(tuple(gates))
        new_qubit_order = combined.qubits
        state_vector = get_entangled_initial_state(initial_state, new_qubit_order)

        probabilities = np.square(np.absolute(combined.matrix.dot(state_vector))).real
        probabilities /= np.sum(probabilities)

        measured = Counter([np.random.choice(state_vector.shape[0], p=probabilities) for m in range(num_shots)])
        return measured

class QSession(object):
    def __init__(self, **kwargs):
        self.backend = kwargs.get('backend', QuantumSimulator())

    def run(self, circuit: Circuit, **kwargs):
        num_shots = kwargs.get('num_shots', 1024)

        initial_state = circuit.initial_state()
        gates = circuit.to_gates()
        measure_bits = circuit.measure_bits()

        counts = self.backend.run(initial_state, gates, measure_bits, num_shots)

        return QResult(counts, sig_bits=len(circuit.initial_state()))

class QResult(object):
    def __init__(self, counts: Dict[int, int], sig_bits: int):
        self.counts: Dict[int, int] = counts
        self.sig_bits: int = sig_bits

    def get(self, key, default=0):
        if isinstance(key, str):
            idx = int_from_bit_string(key)
        else:
            idx = key

        return self.counts.get(idx, default)

    def __getitem__(self, item):
        return self.get(item)

    def __repr__(self):
        return repr({int_to_bit_string(k, self.sig_bits): v for k, v in self.counts.items()})

def get_entangled_initial_state(initial_state, new_qubit_order):
    states_to_entangle: Deque[np.ndarray] = deque()

    states = list(map(lambda s: np.asarray([1 if s == 0 else 0, 1 if s == 1 else 0], dtype='complex64'), initial_state))

    for q in new_qubit_order:
        states_to_entangle.append(states[q])

    while len(states_to_entangle) > 1:
        entangled_states: Deque[np.ndarray] = deque()
        while len(states_to_entangle) > 1:
            entangled_states.append(np.kron(states_to_entangle.popleft(), states_to_entangle.popleft()))
        if len(states_to_entangle) == 1:
            entangled_states.append(states_to_entangle.popleft())
        states_to_entangle = entangled_states

    return states_to_entangle.pop()

def change_qubit_order(matrix: np.ndarray, old_order, new_order) -> np.ndarray:
    reorder_qubits = [new_order.index(q2) for q2 in old_order]
    initial_row_order = [i for i in range(np.power(2, len(old_order)))]

    reorder_rows = list(map(lambda i: rearrange_bits(i, reorder_qubits), initial_row_order))

    return matrix[reorder_rows][:, reorder_rows]

def rearrange_bits(num: int, new_bit_order: List[int]):
    sig_bits = len(new_bit_order)
    bit_string = int_to_bit_string(num, sig_bits)
    result = int_from_bit_string(''.join(bit_string[b] for b in new_bit_order))
    return result

def int_to_bits(num: int, sig_bits: int) -> str:
    return format(num, '#0%sb' % np.power(2, sig_bits))

def int_from_bit_string(bits: str) -> int:
    return int(bits, 2) if bits.startswith('0b') else int('0b' + bits, 2)

def int_to_bit_string(i: int, sig_bits: int) -> str:
    return '{0:b}'.format(i).zfill(sig_bits)

def has_common_qbits(gate1, gate2):
    return len(set(gate1.qubits).intersection(set(gate2.qubits))) > 0

max_stars_repo_name: jywyq/shor

max_issues_repo_name: jywyq/shor

max_forks_repo_name: jywyq/shor


text:
from PIL import Image, ImageDraw, ImageFont
import matplotlib.pyplot as plt
import numpy as np
import os
import sys

class button_self:
    def add_num(self,img):
        draw = ImageDraw.Draw(img)
        myfont = ImageFont.truetype('simhei.ttf', size=40)
        fillcolor = "#ff0000"
        width,height = img.size
        draw.text((width-400,8),'数字图像处理演示', font=myfont, fill=fillcolor)
        # img.save('result.jpg','jpeg')
        return img

    def scan_pic(self,path):#图片浏览
        im_before = Image.open(path)
        plt.imshow(im_before)
        plt.title('scan_img')
        plt.show()

    def mark_pic(self,path):#添加水印
        im_before = Image.open(path)
        plt.subplot(121),plt.imshow(im_before),plt.title('origin_img')
        im_after = self.add_num(im_before)
        plt.subplot(122),plt.imshow(im_after),plt.title('mark_img')
        plt.show()

    def zoomout_pic(self,path):#图片放大
        im=Image.open(path)
        (x, y) = im.size
        newimg=im.resize((x+200,y+200), Image.ANTIALIAS)
        plt.subplot(121),plt.imshow(im),plt.title('origin_img')
        plt.subplot(122),plt.imshow(newimg),plt.title('resize_img')
        plt.show()

    def cw_pic(self,path):#顺时针旋转90°
        img=Image.open(path)
        region = img.transpose(Image.ROTATE_270) #翻转
        plt.subplot(121),plt.imshow(img),plt.title('origin_img')
        plt.subplot(122),plt.imshow(region),plt.title('cw90_img')
        plt.show()

    def rcw_pic(self,path):#逆时针旋转90°
        img=Image.open(path)
        region = img.transpose(Image.ROTATE_90) #翻转
        plt.subplot(121),plt.imshow(img),plt.title('origin_img')
        plt.subplot(122),plt.imshow(region),plt.title('ccw90_img')
        plt.show()

    def file_save(self,path,savepath):#图片保存
        img = Image.open(path)
        img.save(savepath)

    def pre_file(self,file_path):#上一张
        file_dir=os.path.dirname(file_path)
        L=[]
        for root, dirs, files in os.walk(file_dir):
            for file in files:
                L.append(file)
        index=L.index(file_path.split("/")[-1])#查找本文件的索引
        if index==0:
            index=len(L)-1
        else:
            index=index-1
        pre_file_path=file_dir+'/'+L[index]
        return pre_file_path

    def next_file(self,file_path):#下一张
        file_dir=os.path.dirname(file_path)
        L=[]
        for root, dirs, files in os.walk(file_dir):
            for file in files:
                L.append(file)
        index=L.index(file_path.split("/")[-1])#查找本文件的索引
        if index==len(L)-1:
            index=0
        else:
            index=index+1
        pre_file_path=file_dir+'/'+L[index]
        return pre_file_path

max_stars_repo_path: 1.A GUI for various opreations/bottombutton.py