text:
import argparse
import collections
import time
import numpy as np
import torch as th
import torch.nn.functional as F
import torch.nn.init as INIT
import torch.optim as optim
from torch.utils.data import DataLoader

import dgl
from dgl.data.tree import SST, SSTBatch

from tree_lstm import TreeLSTM

SSTBatch = collections.namedtuple('SSTBatch', ['graph', 'mask', 'wordid', 'label'])
def batcher(device):
    def batcher_dev(batch):
        batch_trees = dgl.batch(batch)
        return SSTBatch(graph=batch_trees,
                        mask=batch_trees.ndata['mask'].to(device),
                        wordid=batch_trees.ndata['x'].to(device),
                        label=batch_trees.ndata['y'].to(device))
    return batcher_dev

def main(args):
    np.random.seed(args.seed)
    th.manual_seed(args.seed)
    th.cuda.manual_seed(args.seed)

    best_epoch = -1
    best_dev_acc = 0

    cuda = args.gpu >= 0
    device = th.device('cuda:{}'.format(args.gpu)) if cuda else th.device('cpu')
    if cuda:
        th.cuda.set_device(args.gpu)

    trainset = SST()
    train_loader = DataLoader(dataset=trainset,
                              batch_size=args.batch_size,
                              collate_fn=batcher(device),
                              shuffle=True,
                              num_workers=0)
    devset = SST(mode='dev')
    dev_loader = DataLoader(dataset=devset,
                            batch_size=100,
                            collate_fn=batcher(device),
                            shuffle=False,
                            num_workers=0)

    testset = SST(mode='test')
    test_loader = DataLoader(dataset=testset,
                             batch_size=100, collate_fn=batcher(device), shuffle=False, num_workers=0)

    model = TreeLSTM(trainset.num_vocabs,
                     args.x_size,
                     args.h_size,
                     trainset.num_classes,
                     args.dropout,
                     cell_type='childsum' if args.child_sum else 'nary',
                     pretrained_emb = trainset.pretrained_emb).to(device)
    print(model)
    params_ex_emb =[x for x in list(model.parameters()) if x.requires_grad and x.size(0)!=trainset.num_vocabs]
    params_emb = list(model.embedding.parameters())

    for p in params_ex_emb:
        if p.dim() > 1:
            INIT.xavier_uniform_(p)

    optimizer = optim.Adagrad([
        {'params':params_ex_emb, 'lr':args.lr, 'weight_decay':args.weight_decay},
        {'params':params_emb, 'lr':0.1*args.lr}])

    dur = []
    for epoch in range(args.epochs):
        t_epoch = time.time()
        model.train()
        for step, batch in enumerate(train_loader):
            g = batch.graph.to(device)
            n = g.number_of_nodes()
            h = th.zeros((n, args.h_size)).to(device)
            c = th.zeros((n, args.h_size)).to(device)
            if step >= 3:
                t0 = time.time() # tik

            logits = model(batch, g, h, c)
            logp = F.log_softmax(logits, 1)
            loss = F.nll_loss(logp, batch.label, reduction='sum')

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

            if step >= 3:
                dur.append(time.time() - t0) # tok

            if step > 0 and step % args.log_every == 0:
                pred = th.argmax(logits, 1)
                acc = th.sum(th.eq(batch.label, pred))
                root_ids = [i for i in range(g.number_of_nodes()) if g.out_degree(i)==0]
                root_acc = np.sum(batch.label.cpu().data.numpy()[root_ids] == pred.cpu().data.numpy()[root_ids])

                print("Epoch {:05d} | Step {:05d} | Loss {:.4f} | Acc {:.4f} | Root Acc {:.4f} | Time(s) {:.4f}".format(
                    epoch, step, loss.item(), 1.0*acc.item()/len(batch.label), 1.0*root_acc/len(root_ids), np.mean(dur)))
        print('Epoch {:05d} training time {:.4f}s'.format(epoch, time.time() - t_epoch))

        # eval on dev set
        accs = []
        root_accs = []
        model.eval()
        for step, batch in enumerate(dev_loader):
            g = batch.graph.to(device)
            n = g.number_of_nodes()
            with th.no_grad():
                h = th.zeros((n, args.h_size)).to(device)
                c = th.zeros((n, args.h_size)).to(device)
                logits = model(batch, g, h, c)

            pred = th.argmax(logits, 1)
            acc = th.sum(th.eq(batch.label, pred)).item()
            accs.append([acc, len(batch.label)])
            root_ids = [i for i in range(g.number_of_nodes()) if g.out_degree(i)==0]
            root_acc = np.sum(batch.label.cpu().data.numpy()[root_ids] == pred.cpu().data.numpy()[root_ids])
            root_accs.append([root_acc, len(root_ids)])

        dev_acc = 1.0*np.sum([x[0] for x in accs])/np.sum([x[1] for x in accs])
        dev_root_acc = 1.0*np.sum([x[0] for x in root_accs])/np.sum([x[1] for x in root_accs])
        print("Epoch {:05d} | Dev Acc {:.4f} | Root Acc {:.4f}".format(
            epoch, dev_acc, dev_root_acc))

        if dev_root_acc > best_dev_acc:
            best_dev_acc = dev_root_acc
            best_epoch = epoch
            th.save(model.state_dict(), 'best_{}.pkl'.format(args.seed))
        else:
            if best_epoch <= epoch - 10:
                break

        # lr decay
        for param_group in optimizer.param_groups:
            param_group['lr'] = max(1e-5, param_group['lr']*0.99) #10
            print(param_group['lr'])

    # test
    model.load_state_dict(th.load('best_{}.pkl'.format(args.seed)))
    accs = []
    root_accs = []
    model.eval()
    for step, batch in enumerate(test_loader):
        g = batch.graph.to(device)
        n = g.number_of_nodes()
        with th.no_grad():
            h = th.zeros((n, args.h_size)).to(device)
            c = th.zeros((n, args.h_size)).to(device)
            logits = model(batch, g, h, c)

        pred = th.argmax(logits, 1)
        acc = th.sum(th.eq(batch.label, pred)).item()
        accs.append([acc, len(batch.label)])
        root_ids = [i for i in range(g.number_of_nodes()) if g.out_degree(i)==0]
        root_acc = np.sum(batch.label.cpu().data.numpy()[root_ids] == pred.cpu().data.numpy()[root_ids])
        root_accs.append([root_acc, len(root_ids)])

    test_acc = 1.0*np.sum([x[0] for x in accs])/np.sum([x[1] for x in accs])
    test_root_acc = 1.0*np.sum([x[0] for x in root_accs])/np.sum([x[1] for x in root_accs])
    print('------------------------------------------------------------------------------------')
    print("Epoch {:05d} | Test Acc {:.4f} | Root Acc {:.4f}".format(
        best_epoch, test_acc, test_root_acc))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--seed', type=int, default=41)
    parser.add_argument('--batch-size', type=int, default=20)
    parser.add_argument('--child-sum', action='store_true')
    parser.add_argument('--x-size', type=int, default=300)
    parser.add_argument('--h-size', type=int, default=150)
    parser.add_argument('--epochs', type=int, default=100)
    parser.add_argument('--log-every', type=int, default=5)
    parser.add_argument('--lr', type=float, default=0.05)
    parser.add_argument('--weight-decay', type=float, default=1e-4)
    parser.add_argument('--dropout', type=float, default=0.5)
    args = parser.parse_args()
    print(args)
    main(args)


text:
# -*- coding: utf-8 -*-
"""
@Time          : 2020/08/07 15:07
@Author        : FelixFu
@File          : train.py
@Noice         :
@Modificattion : txt描述文件 image_name.jpg x y w h c x y w h c 这样就是说一张图片中有两个目标
    @Author    :
    @Time      :
    @Detail    :
"""

import os
import os.path

import random
import numpy as np

import torch
import torch.utils.data as data
import torchvision.transforms as transforms

import cv2
import matplotlib.pyplot as plt

class yoloDataset(data.Dataset):
    image_size = 448

    def __init__(self, root, list_file, train, transform):
        self.root = root    # 数据集根目录 datasests
        self.train = train  # 是否为训练
        self.transform = transform  # 转换
        self.fnames = []    # 文件名s [001.jpg, 002.jpg]
        self.boxes = []     # boxes  [ [box], [[x1,y1,x2,y2], ...], ... ]
        self.labels = []    # labels [ [1], [2], ... ]
        self.mean = (123, 117, 104)  # RGB
        self.num_samples = 0  # 样本总数

        if isinstance(list_file, list):
            # Cat multiple list files together.
            # This is especially useful for voc07/voc12 combination.
            tmp_file = os.path.join(root, 'images.txt')
            list_file = [os.path.join(root, list_file[0]), os.path.join(root, list_file[1])]
            os.system('cat %s > %s' % (' '.join(list_file), tmp_file))
            list_file = tmp_file
        else:
            list_file = os.path.join(root, list_file)

        # 处理标签
        with open(list_file) as f:
            lines = f.readlines()
        for line in lines:
            splited = line.strip().split()  # ['005246.jpg', '84', '48', '493', '387', '2']
            self.fnames.append(splited[0])
            num_boxes = (len(splited) - 1) // 5
            box = []
            label = []
            for i in range(num_boxes):
                x = float(splited[1+5*i])
                y = float(splited[2+5*i])
                x2 = float(splited[3+5*i])
                y2 = float(splited[4+5*i])
                c = splited[5+5*i]
                box.append([x, y, x2, y2])
                label.append(int(c)+1)
            self.boxes.append(torch.Tensor(box))
            self.labels.append(torch.LongTensor(label))
        self.num_samples = len(self.boxes)

    def __getitem__(self, idx):
        fname = self.fnames[idx]
        img = cv2.imread(os.path.join(self.root, "images", fname))
        boxes = self.boxes[idx].clone()
        labels = self.labels[idx].clone()

        # 数据增强
        # if self.train:
        #     img = self.random_bright(img)
        #     img, boxes = self.random_flip(img, boxes)
        #     img, boxes = self.randomScale(img, boxes)
        #     img = self.randomBlur(img)
        #     img = self.RandomBrightness(img)
        #     img = self.RandomHue(img)
        #     img = self.RandomSaturation(img)
        #     img, boxes, labels = self.randomShift(img, boxes, labels)
        #     img, boxes, labels = self.randomCrop(img, boxes, labels)

        # # debug
        # box_show = boxes.numpy().reshape(-1)
        # print(box_show)
        # img_show = self.BGR2RGB(img)
        # pt1 = (int(box_show[0]), int(box_show[1]))
        # pt2 = (int(box_show[2]), int(box_show[3]))
        # cv2.rectangle(img_show, pt1=pt1, pt2=pt2, color=(0, 255, 0), thickness=1)
        # print(type(img_show))
        # plt.figure()
        # plt.imshow(img_show)
        # plt.show()
        # plt.savefig("a.png")
        # #debug
        h, w, _ = img.shape
        boxes /= torch.Tensor([w, h, w, h]).expand_as(boxes)
        img = self.BGR2RGB(img)  # because pytorch pretrained model use RGB
        img = self.subMean(img, self.mean)  # 减去均值
        img = cv2.resize(img, (self.image_size, self.image_size))
        target = self.encoder(boxes, labels)  # 7x7x30
        for t in self.transform:
            img = t(img)

        return img, target

    def __len__(self):
        return self.num_samples

    def encoder(self, boxes, labels):
        """
        boxes (tensor) [[x1,y1,x2,y2],[]]
        labels (tensor) [...]
        return 14x14x30
        """
        grid_num = 14
        target = torch.zeros((grid_num, grid_num, 30))
        cell_size = 1./grid_num
        wh = boxes[:, 2:] - boxes[:, :2]    # 这张图片，有n=8个box，每个box的wh torch.Size([8, 2])
        cxcy = (boxes[:, 2:] + boxes[:, :2]) / 2    # n个box的中心xy  torch.Size([8, 2])
        for i in range(cxcy.size()[0]):
            cxcy_sample = cxcy[i]
            ij = (cxcy_sample/cell_size).ceil()-1  # 将cxcy_sample定位到cell（网格）中
            target[int(ij[1]), int(ij[0]), 4] = 1   # 将target中box设为1
            target[int(ij[1]), int(ij[0]), 9] = 1   # 将target中box设为1
            target[int(ij[1]), int(ij[0]), int(labels[i])+9] = 1    # 将target中class设为1
            xy = ij*cell_size  # 匹配到的网格的左上角相对坐标
            delta_xy = (cxcy_sample - xy)/cell_size
            target[int(ij[1]), int(ij[0]), 2:4] = wh[i]
            target[int(ij[1]), int(ij[0]), :2] = delta_xy
            target[int(ij[1]), int(ij[0]), 7:9] = wh[i]
            target[int(ij[1]), int(ij[0]), 5:7] = delta_xy
        return target

    def BGR2RGB(self, img):
        return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    def BGR2HSV(self, img):
        return cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    def HSV2BGR(self, img):
        return cv2.cvtColor(img, cv2.COLOR_HSV2BGR)

    def RandomBrightness(self, bgr):
        if random.random() < 0.5:
            hsv = self.BGR2HSV(bgr)
            h, s, v = cv2.split(hsv)
            adjust = random.choice([0.5, 1.5])
            v = v*adjust
            v = np.clip(v, 0, 255).astype(hsv.dtype)
            hsv = cv2.merge((h, s, v))
            bgr = self.HSV2BGR(hsv)
        return bgr

    def RandomSaturation(self, bgr):
        if random.random() < 0.5:
            hsv = self.BGR2HSV(bgr)
            h, s, v = cv2.split(hsv)
            adjust = random.choice([0.5, 1.5])
            s = s*adjust
            s = np.clip(s, 0, 255).astype(hsv.dtype)
            hsv = cv2.merge((h, s, v))
            bgr = self.HSV2BGR(hsv)
        return bgr

    def RandomHue(self, bgr):
        if random.random() < 0.5:
            hsv = self.BGR2HSV(bgr)
            h, s, v = cv2.split(hsv)
            adjust = random.choice([0.5, 1.5])
            h = h*adjust
            h = np.clip(h, 0, 255).astype(hsv.dtype)
            hsv = cv2.merge((h, s, v))
            bgr = self.HSV2BGR(hsv)
        return bgr

    def randomBlur(self, bgr):
        if random.random() < 0.5:
            bgr = cv2.blur(bgr, (5, 5))
        return bgr

    def randomShift(self, bgr, boxes, labels):
        # 平移变换
        center = (boxes[:, 2:]+boxes[:, :2])/2
        if random.random() < 0.5:
            height, width, c = bgr.shape
            after_shfit_image = np.zeros((height, width, c), dtype=bgr.dtype)
            after_shfit_image[:, :, :] = (104, 117, 123)  # bgr
            shift_x = random.uniform(-width*0.2, width*0.2)
            shift_y = random.uniform(-height*0.2, height*0.2)
            # print(bgr.shape,shift_x,shift_y)
            # 原图像的平移
            if shift_x >= 0 and shift_y >= 0:
                after_shfit_image[int(shift_y):, int(shift_x):, :] = bgr[:height-int(shift_y), :width-int(shift_x), :]
            elif shift_x >= 0 and shift_y < 0:
                after_shfit_image[:height+int(shift_y), int(shift_x):, :] = bgr[-int(shift_y):, :width-int(shift_x), :]
            elif shift_x < 0 and shift_y >= 0:
                after_shfit_image[int(shift_y):, :width+int(shift_x), :] = bgr[:height-int(shift_y), -int(shift_x):, :]
            elif shift_x < 0 and shift_y < 0:
                after_shfit_image[:height+int(shift_y), :width+int(shift_x), :] = bgr[-int(shift_y):, -int(shift_x):, :]

            shift_xy = torch.FloatTensor([[int(shift_x),int(shift_y)]]).expand_as(center)
            center = center + shift_xy
            mask1 = (center[:, 0] > 0) & (center[:, 0] < width)
            mask2 = (center[:, 1] > 0) & (center[: ,1] < height)
            mask = (mask1 & mask2).view(-1, 1)
            boxes_in = boxes[mask.expand_as(boxes)].view(-1,4)
            if len(boxes_in) == 0:
                return bgr, boxes, labels
            box_shift = torch.FloatTensor([[int(shift_x), int(shift_y),
                                            int(shift_x), int(shift_y)]]).expand_as(boxes_in)
            boxes_in = boxes_in+box_shift
            labels_in = labels[mask.view(-1)]
            return after_shfit_image, boxes_in, labels_in
        return bgr, boxes, labels

    def randomScale(self, bgr, boxes):
        # 固定住高度，以0.8-1.2伸缩宽度，做图像形变
        if random.random() < 0.5:
            scale = random.uniform(0.8, 1.2)
            height, width, c = bgr.shape
            bgr = cv2.resize(bgr, (int(width*scale), height))
            scale_tensor = torch.FloatTensor([[scale, 1, scale, 1]]).expand_as(boxes)
            boxes = boxes * scale_tensor
            return bgr, boxes
        return bgr, boxes

    def randomCrop(self, bgr, boxes, labels):
        if random.random() < 0.5:
            center = (boxes[:, 2:]+boxes[:, :2])/2
            height, width, c = bgr.shape
            h = random.uniform(0.6*height, height)
            w = random.uniform(0.6*width, width)
            x = random.uniform(0, width-w)
            y = random.uniform(0, height-h)
            x, y, h, w = int(x), int(y), int(h), int(w)

            center = center - torch.FloatTensor([[x, y]]).expand_as(center)
            mask1 = (center[:, 0] > 0) & (center[:, 0] < w)
            mask2 = (center[:, 1] > 0) & (center[:, 1] < h)
            mask = (mask1 & mask2).view(-1, 1)

            boxes_in = boxes[mask.expand_as(boxes)].view(-1, 4)
            if(len(boxes_in) == 0):
                return bgr, boxes, labels
            box_shift = torch.FloatTensor([[x, y, x, y]]).expand_as(boxes_in)

            boxes_in = boxes_in - box_shift
            boxes_in[:, 0] = boxes_in[:, 0].clamp_(min=0, max=w)
            boxes_in[:, 2] = boxes_in[:, 2].clamp_(min=0, max=w)
            boxes_in[:, 1] = boxes_in[:, 1].clamp_(min=0, max=h)
            boxes_in[:, 3] = boxes_in[:, 3].clamp_(min=0, max=h)

            labels_in = labels[mask.view(-1)]
            img_croped = bgr[y:y+h, x:x+w, :]
            return img_croped, boxes_in, labels_in
        return bgr, boxes, labels

    def subMean(self, bgr, mean):
        mean = np.array(mean, dtype=np.float32)
        bgr = bgr - mean
        return bgr

    def random_flip(self, im, boxes):
        if random.random() < 0.5:
            im_lr = np.fliplr(im).copy()
            h, w, _ = im.shape
            xmin = w - boxes[:, 2]
            xmax = w - boxes[:, 0]
            boxes[:, 0] = xmin
            boxes[:, 2] = xmax
            return im_lr, boxes
        return im, boxes

    def random_bright(self, im, delta=16):
        alpha = random.random()
        if alpha > 0.3:
            im = im * alpha + random.randrange(-delta, delta)
            im = im.clip(min=0, max=255).astype(np.uint8)
        return im

if __name__ == '__main__':
    from torch.utils.data import DataLoader
    import torchvision.transforms as transforms
    file_root = "../datasets"
    # train_dataset = yoloDataset(root=file_root, list_file=['voc2012.txt', 'voc2007.txt'],
    #                             train=True, transform=[transforms.ToTensor()])
    train_dataset = yoloDataset(root=file_root, list_file='images.txt',
                                train=True, transform=[transforms.ToTensor()])
    train_loader = DataLoader(train_dataset, batch_size=1, shuffle=False, num_workers=0)
    train_iter = iter(train_loader)
    for i in range(1):
        img, target = next(train_iter)
        print(img.shape, target.shape)
    print(train_dataset.num_samples)

ext: py

reason: import numpy


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

import numpy as np

class SpeedupFunction(object):

    def __init__(self, goodput_fn, max_batch_size=None, atomic_bsz_range=None,
                 accumulation=False, mem_size=32):
        self._goodput_fn = goodput_fn
        self._max_batch_size = max_batch_size
        self._atomic_bsz_range = atomic_bsz_range
        self._accumulation = accumulation
        self._mem_size = mem_size
        self._base_goodput, _, _ = goodput_fn.optimize(
            num_nodes=1, num_replicas=1, max_batch_size=max_batch_size,
            atomic_bsz_range=atomic_bsz_range, accumulation=accumulation)
        # Memoization for fast repeated queries.
        self._mem_speedup = -np.ones((mem_size, mem_size))
        self._mem_speedup[0, 0] = 0.0

    def __call__(self, num_nodes, num_replicas):
        assert np.all(np.less_equal(0, num_nodes))
        assert np.all(np.less_equal(num_nodes, num_replicas))
        assert np.all((num_nodes > 0) == (num_replicas > 0))
        # Remember what the output shape/format should be and flatten inputs.
        output_scalar = np.isscalar(num_nodes) and np.isscalar(num_replicas)
        output_shape = np.broadcast(num_nodes, num_replicas).shape
        num_nodes = np.broadcast_to(num_nodes, output_shape).flatten()
        num_replicas = np.broadcast_to(num_replicas, output_shape).flatten()
        # Return values which will be filled out.
        speedup = -np.ones(output_shape).flatten()
        # Fill in any previously memoized results first.
        indices = num_replicas < self._mem_size
        mem_idx = (num_nodes[indices], num_replicas[indices])
        speedup[indices] = self._mem_speedup[mem_idx]
        # Find the missing indices which still need to be computed.
        missing = speedup < 0
        if np.count_nonzero(missing) > 0:
            num_nodes, num_replicas = num_nodes[missing], num_replicas[missing]
            # Find unique inputs to reduce compuation.
            (num_nodes, num_replicas), inverse = np.unique(
                    np.stack([num_nodes, num_replicas]),
                    axis=1, return_inverse=True)
            goodput, _, _ = self._goodput_fn.optimize(
                num_nodes, num_replicas,
                max_batch_size=self._max_batch_size,
                atomic_bsz_range=self._atomic_bsz_range,
                accumulation=self._accumulation)
            # Memoize results.
            indices = num_replicas < self._mem_size
            mem_idx = (num_nodes[indices], num_replicas[indices])
            self._mem_speedup[mem_idx] = goodput[indices] / self._base_goodput
            # Fill in computed results.
            speedup[missing] = goodput[inverse] / self._base_goodput
        assert np.all(np.less_equal(0, speedup))
        speedup = speedup.reshape(output_shape)
        return speedup.item() if output_scalar else speedup

max_stars_repo_name: jessezbj/adaptdl

max_issues_repo_name: jessezbj/adaptdl

max_forks_repo_name: jessezbj/adaptdl


text:
import os
import warnings

import numpy as np
from astropy.table import Table, hstack
from astropy.stats import sigma_clipped_stats
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from astropy.utils.exceptions import AstropyWarning

import context

warnings.simplefilter('ignore', category=AstropyWarning)

def select_candidates(table, seeing, dflux_radius=0.5, field=None, redo=False,
                      ellip_max=0.3):
    """ Narrow the number of GC candidates according to their morphology. """
    outtable = os.path.join(os.getcwd(), "gc-candidates-catalog.fits")
    if os.path.exists(outtable) and not redo:
        table = Table.read(outtable)
        return table
    title = f"Field {field[-1]}"
    table = table[table["FLUX_RADIUS"] > 0]
    table =  table[table["FLUX_RADIUS"] < 5]
    candidates = table[table["FLUX_RADIUS"] < 4]
    # Remove outliers in flux
    mags = candidates["MAG_AUTO"]
    mmean, mmedian, mstd = sigma_clipped_stats(mags)
    candidates = candidates[np.abs(mags - mmedian) <= 5 * mstd]
    # Use only round sources
    ellip = candidates["ELLIPTICITY"]
    candidates = candidates[ellip <= ellip_max]
    # Use only sources with flux_radius similar to point sources
    flux_radius = candidates["FLUX_RADIUS"]
    candidates = candidates[np.abs(flux_radius - seeing) <= dflux_radius]
    candidates.write(outtable, overwrite=True)
    # Plotting results
    fig = plt.figure(figsize=(context.fig_width, 2.5))
    ax = plt.subplot(111)
    ax.grid(True)
    idx = [i for i, n in enumerate(table["NUMBER"])
             if n not in candidates["NUMBER"]]
    ax.plot(table["FLUX_RADIUS"][idx], table["MAG_AUTO"][idx], "x",
             label=r"$\varepsilon > {}$".format(ellip_max))
    ax.plot(candidates["FLUX_RADIUS"], candidates["MAG_AUTO"], ".",
             label=r"$\varepsilon \leq {}$".format(ellip_max))
    # ymin = candidates["MAG_AUTO"].max() + 1
    # ymax = candidates["MAG_AUTO"].min() - 1
    # ax.set_ylim(ymin, ymax)
    ax.set_ylim(ax.get_ylim()[::-1])
    # Including rectangle
    ymin = candidates["MAG_AUTO"].min() - 0.5
    ymax = candidates["MAG_AUTO"].max() + 0.5
    xmin = seeing - dflux_radius
    rect=mpatches.Rectangle((xmin, ymin), 2 * dflux_radius, ymax - ymin,
                            fill=False, color ="purple", linewidth = 2)
    plt.gca().add_patch(rect)
    plt.legend(title=title)
    plt.xlabel("Flux radius (arcsec)")
    plt.ylabel("mag (arbitrary units)")
    plt.subplots_adjust(left=0.1, top=0.99, bottom=0.13, right=0.99)
    plt.savefig(f"gc_candidates_{field}.png", dpi=250)
    plt.show()
    plt.close()
    return candidates

if __name__ == "__main__":
    redo=True
    for i, field in enumerate(context.fields):
        wdir = os.path.join(context.home_dir, f"data/{field}")
        os.chdir(wdir) # Changing to working directory
        table = Table.read("source-catalog.fits")
        # Converting table to arcsec
        table["FLUX_RADIUS"] *= context.PS
        # Offset for magnitude
        table["MAG_AUTO"] += 25
        # Select systems that are more likely GC candidates
        candidates = select_candidates(table, context.seeing[i] / 2,
                                       field=field, redo=redo)

reason: import numpy,from astropy


text:
import shutil
from functools import reduce
from pathlib import Path

import numpy as np
import pytest
import tensorflow as tf

from deeppavlov.core.layers.tf_layers import cudnn_lstm, cudnn_compatible_lstm, cudnn_gru, cudnn_compatible_gru

tests_dir = Path(__file__).parent
tf_layers_data_path = tests_dir / "tf_layers_data"

def setup_module():
    shutil.rmtree(str(tf_layers_data_path), ignore_errors=True)
    tf_layers_data_path.mkdir(parents=True)

def teardown_module():
    shutil.rmtree(str(tf_layers_data_path), ignore_errors=True)

class DPCudnnLSTMModel:
    def __init__(self, num_layers, num_units):
        sess_config = tf.ConfigProto(allow_soft_placement=True)
        sess_config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=sess_config)

        self.x = tf.placeholder(shape=(None, None, 50), dtype=tf.float32)
        with tf.variable_scope('cudnn_model'):
            h, (h_last, c_last) = cudnn_lstm(self.x, num_units, num_layers, trainable_initial_states=True)

            self.h = h
            self.h_last = h_last

        self.sess.run(tf.global_variables_initializer())

    def __call__(self, x):
        feed_dict = {
            self.x: x,
        }
        return self.sess.run([self.h, self.h_last], feed_dict=feed_dict)

    def save(self, path='model'):
        print('[saving model to {}]'.format(path))
        saver = tf.train.Saver()
        saver.save(self.sess, path)

    def load(self, path):
        saver = tf.train.Saver()
        saver.restore(self.sess, path)

class DPLSTMModel:
    def __init__(self, num_layers, num_units):
        sess_config = tf.ConfigProto(allow_soft_placement=True)
        sess_config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=sess_config)

        self.x = tf.placeholder(shape=(None, None, 50), dtype=tf.float32)
        with tf.variable_scope('cudnn_model'):
            h, (h_last, c_last) = cudnn_compatible_lstm(self.x, num_units, num_layers, trainable_initial_states=True)

            self.h = h
            self.h_last = h_last

        self.sess.run(tf.global_variables_initializer())

    def __call__(self, x):
        feed_dict = {
            self.x: x,
        }
        return self.sess.run([self.h, self.h_last], feed_dict=feed_dict)

    def save(self, path='model'):
        print('[saving model to {}]'.format(path))
        saver = tf.train.Saver()
        saver.save(self.sess, path)

    def load(self, path):
        saver = tf.train.Saver()
        saver.restore(self.sess, path)

class DPCudnnGRUModel:
    def __init__(self, num_layers, num_units):
        sess_config = tf.ConfigProto(allow_soft_placement=True)
        sess_config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=sess_config)

        self.x = tf.placeholder(shape=(None, None, 50), dtype=tf.float32)
        with tf.variable_scope('cudnn_model'):
            h, h_last = cudnn_gru(self.x, num_units, num_layers, trainable_initial_states=True)

            self.h = h
            self.h_last = h_last

        self.sess.run(tf.global_variables_initializer())

    def __call__(self, x):
        feed_dict = {
            self.x: x,
        }
        return self.sess.run([self.h, self.h_last], feed_dict=feed_dict)

    def save(self, path='model'):
        print('[saving model to {}]'.format(path))
        saver = tf.train.Saver()
        saver.save(self.sess, path)

    def load(self, path):
        saver = tf.train.Saver()
        saver.restore(self.sess, path)

class DPGRUModel:
    def __init__(self, num_layers, num_units):
        sess_config = tf.ConfigProto(allow_soft_placement=True)
        sess_config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=sess_config)

        self.x = tf.placeholder(shape=(None, None, 50), dtype=tf.float32)
        with tf.variable_scope('cudnn_model'):
            h, h_last = cudnn_compatible_gru(self.x, num_units, num_layers, trainable_initial_states=True)

            self.h = h
            self.h_last = h_last

        self.sess.run(tf.global_variables_initializer())

    def __call__(self, x):
        feed_dict = {
            self.x: x,
        }
        return self.sess.run([self.h, self.h_last], feed_dict=feed_dict)

    def save(self, path='model'):
        print('[saving model to {}]'.format(path))
        saver = tf.train.Saver()
        saver.save(self.sess, path)

    def load(self, path):
        saver = tf.train.Saver()
        saver.restore(self.sess, path)

class TestTFLayers:
    allowed_error_lvl = 0.01 * 2 ** 0.5

    @staticmethod
    def equal_values(a, b, round=5):
        a, b = np.round(a, round), np.round(b, round)
        return np.sum(a == b) / reduce(lambda x, y: x * y, a.shape)

    @pytest.mark.parametrize("num_layers", [1, 3])
    def test_cudnn_lstm_save_load(self, num_layers):
        x = np.random.normal(size=(10, 10, 50))
        tf.reset_default_graph()
        cdnnlstmmodel = DPCudnnLSTMModel(num_layers=num_layers, num_units=100)
        before_load_hidden, before_load_state = cdnnlstmmodel(x)[0], cdnnlstmmodel(x)[1]
        cdnnlstmmodel.save(str(tf_layers_data_path / 'dpcudnnlstmmodel' / 'model'))

        tf.reset_default_graph()
        cdnnlstmmodel = DPCudnnLSTMModel(num_layers=num_layers, num_units=100)
        cdnnlstmmodel.load(str(tf_layers_data_path / 'dpcudnnlstmmodel' / 'model'))
        after_load_hidden, after_load_state = cdnnlstmmodel(x)[0], cdnnlstmmodel(x)[1]

        equal_hidden = self.equal_values(after_load_hidden, before_load_hidden)
        equal_state = self.equal_values(after_load_state, before_load_state)

        assert equal_hidden > 1 - self.allowed_error_lvl
        assert equal_state > 1 - self.allowed_error_lvl

    @pytest.mark.parametrize("num_layers", [1, 3])
    def test_cudnn_lstm_save_and_cudnn_compatible_load(self, num_layers):
        x = np.random.normal(size=(10, 10, 50))
        tf.reset_default_graph()
        cdnnlstmmodel = DPCudnnLSTMModel(num_layers=num_layers, num_units=100)
        before_load_hidden, before_load_state = cdnnlstmmodel(x)[0], cdnnlstmmodel(x)[1]
        cdnnlstmmodel.save(str(tf_layers_data_path / 'dpcudnnlstmmodel' / 'model'))

        tf.reset_default_graph()
        cdnnlstmmodel = DPLSTMModel(num_layers=num_layers, num_units=100)
        cdnnlstmmodel.load(str(tf_layers_data_path / 'dpcudnnlstmmodel' / 'model'))
        after_load_hidden, after_load_state = cdnnlstmmodel(x)[0], cdnnlstmmodel(x)[1]

        equal_hidden = self.equal_values(after_load_hidden, before_load_hidden)
        equal_state = self.equal_values(after_load_state, before_load_state)

        assert equal_hidden > 1 - self.allowed_error_lvl
        assert equal_state > 1 - self.allowed_error_lvl

    @pytest.mark.parametrize("num_layers", [1, 3])
    def test_cudnn_gru_save_load(self, num_layers):
        x = np.random.normal(size=(10, 10, 50))
        tf.reset_default_graph()
        cdnngrumodel = DPCudnnGRUModel(num_layers=num_layers, num_units=100)
        before_load_hidden, before_load_state = cdnngrumodel(x)[0], cdnngrumodel(x)[1]
        cdnngrumodel.save(str(tf_layers_data_path / 'cdnngrumodel' / 'model'))

        tf.reset_default_graph()
        cdnngrumodel = DPCudnnGRUModel(num_layers=num_layers, num_units=100)
        cdnngrumodel.load(str(tf_layers_data_path / 'cdnngrumodel' / 'model'))
        after_load_hidden, after_load_state = cdnngrumodel(x)[0], cdnngrumodel(x)[1]

        equal_hidden = self.equal_values(after_load_hidden, before_load_hidden)
        equal_state = self.equal_values(after_load_state, before_load_state)

        assert equal_hidden > 1 - self.allowed_error_lvl
        assert equal_state > 1 - self.allowed_error_lvl

    @pytest.mark.parametrize("num_layers", [1, 3])
    def test_cudnn_gru_save_and_cudnn_compatible_load(self, num_layers):
        x = np.random.normal(size=(10, 10, 50))
        tf.reset_default_graph()
        cdnngrumodel = DPCudnnGRUModel(num_layers=num_layers, num_units=100)
        before_load_hidden, before_load_state = cdnngrumodel(x)[0], cdnngrumodel(x)[1]
        cdnngrumodel.save(str(tf_layers_data_path / 'cdnngrumodel' / 'model'))

        tf.reset_default_graph()
        cdnngrumodel = DPGRUModel(num_layers=num_layers, num_units=100)
        cdnngrumodel.load(str(tf_layers_data_path / 'cdnngrumodel' / 'model'))
        after_load_hidden, after_load_state = cdnngrumodel(x)[0], cdnngrumodel(x)[1]

        equal_hidden = self.equal_values(after_load_hidden, before_load_hidden)
        equal_state = self.equal_values(after_load_state, before_load_state)

        assert equal_hidden > 1 - self.allowed_error_lvl
        assert equal_state > 1 - self.allowed_error_lvl

max_stars_repo_name: xbodx/DeepPavlov

max_issues_repo_name: xbodx/DeepPavlov

max_forks_repo_name: xbodx/DeepPavlov


text:
# -*- coding: utf-8 -*-
#   This work is part of the Core Imaging Library (CIL) developed by CCPi
#   (Collaborative Computational Project in Tomographic Imaging), with
#   substantial contributions by UKRI-STFC and University of Manchester.

#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

from cil.framework import AcquisitionGeometry, ImageGeometry
import numpy as np

try:
    from tigre.utilities.geometry import Geometry
except ModuleNotFoundError:
    raise ModuleNotFoundError("This plugin requires the additional package TIGRE\n" +
            "Please install it via conda as tigre from the ccpi channel")

class CIL2TIGREGeometry(object):
    @staticmethod
    def getTIGREGeometry(ig, ag):
        tg = TIGREGeometry(ig, ag)

        #angles
        angles = ag.config.angles.angle_data + ag.config.angles.initial_angle

        if ag.config.angles.angle_unit == AcquisitionGeometry.DEGREE:
            angles *= (np.pi/180.)

        angles *= -1 #negate rotation
        angles -= np.pi/2 #rotate imagegeometry 90deg
        angles -= tg.theta #compensate for image geometry definitions

        return tg, angles

class TIGREGeometry(Geometry):

    def __init__(self, ig, ag):

        Geometry.__init__(self)

        ag_in = ag.copy()
        system = ag_in.config.system
        system.update_reference_frame()

        #rotate and translate system to match tigre definitions
        if ag_in.geom_type == 'cone':
            if ag_in.dimension=='3D':
                z = system.source.position[2]
                translate = np.asarray([0,0,-z])
                system.source.position += translate
                system.detector.position += translate
                system.rotation_axis.position += translate

                #align source along negative y
                a = system.source.position
                a = a / np.sqrt(a.dot(a))
                b = np.array([0.0, -1.0, 0.0])

                if np.allclose(a,b):
                    axis_rotation = np.eye(3)
                elif np.allclose(a,-b):
                    axis_rotation = np.eye(3) #pi rotation around either axis
                    axis_rotation[0][0] = -1
                    axis_rotation[2][2] = -1
                else:
                    v = np.cross(a, b)
                    s = np.linalg.norm(v)
                    c = np.dot(a, b)
                    vx = np.array([[0, -v[2], v[1]], [v[2], 0, -v[0]], [-v[1], v[0], 0]])
                    axis_rotation = np.eye(3) + vx + vx.dot(vx) * (1-c)/(s**2)

                r = np.array(axis_rotation)
                system.source.position = r.dot(system.source.position.reshape(3,1))
                system.detector.position = r.dot(system.detector.position.reshape(3,1))
                system.rotation_axis.position = r.dot(system.rotation_axis.position.reshape(3,1))
                new_x = r.dot(system.detector.direction_x.reshape(3,1))
                new_y = r.dot(system.detector.direction_y.reshape(3,1))
                system.detector.set_direction(new_x, new_y)
            else:
                #align source along negative y
                a = system.source.position
                a = a / np.sqrt(a.dot(a))
                b = np.array([0.0, -1.0])

                if np.allclose(a,b):
                    axis_rotation = np.eye(2)
                elif np.allclose(a,-b):
                    axis_rotation = np.eye(2)
                    axis_rotation[0][0] = -1
                else:
                    theta = np.arctan2(a[0], a[1]) + np.pi
                    axis_rotation = np.eye(2)
                    axis_rotation[0][0] = axis_rotation[1][1] = np.cos(theta)
                    axis_rotation[0][1] = -np.sin(theta)
                    axis_rotation[1][0] = np.sin(theta)

                r = np.array(axis_rotation)
                system.source.position = r.dot(system.source.position.reshape(2,1))
                system.detector.position = r.dot(system.detector.position.reshape(2,1))
                system.rotation_axis.position = r.dot(system.rotation_axis.position.reshape(2,1))
                system.detector.direction_x = r.dot(system.detector.direction_x.reshape(2,1))

            #distance source to origin
            DSO = -system.source.position[1]
            DSD = DSO + system.detector.position[1]

        else:
            raise NotImplementedError("TIGRE only wrapped for cone-beam tomography in CIL")

        self.DSD = DSD
        self.DSO = DSO

        # Detector parameters
        # (V,U) number of pixels        (px)
        self.nDetector = np.array(ag_in.config.panel.num_pixels[::-1])
        # size of each pixel            (mm)
        self.dDetector = np.array(ag_in.config.panel.pixel_size[::-1])
        self.sDetector = self.dDetector * self.nDetector    # total size of the detector    (mm)

        # number of voxels              (vx)
        self.nVoxel = np.array( [ig.voxel_num_z, ig.voxel_num_y, ig.voxel_num_x] )
        # size of each voxel            (mm)
        self.dVoxel = np.array( [ig.voxel_size_z, ig.voxel_size_y, ig.voxel_size_x]  )

        if ag_in.dimension == '2D':
            #fix IG
            self.nVoxel[0]=1
            self.dVoxel[0]= ag_in.config.panel.pixel_size[1] / ag_in.magnification

            self.is2D = True
            # Offsets Tigre (Z, Y, X) == CIL (X, -Y)
            self.offOrigin = np.array( [0, system.rotation_axis.position[0], -system.rotation_axis.position[1]])
            self.offDetector = np.array( [0, system.detector.position[0] , 0 ]) #y component in DSD

            #convert roll, pitch, yaw
            U = [0, system.detector.direction_x[0], -system.detector.direction_x[1]]
            roll = 0
            pitch = 0
            yaw = np.arctan2(-U[2],U[1])

        else:
            self.is2D = False
            # Offsets Tigre (Z, Y, X) == CIL (Z, X, -Y)
            ind = np.asarray([2, 0, 1])
            flip = np.asarray([1, 1, -1])

            self.offOrigin = np.array( system.rotation_axis.position[ind] * flip )
            self.offDetector = np.array( [system.detector.position[2], system.detector.position[0], 0]) #y component in DSD

            #convert roll, pitch, yaw
            U = system.detector.direction_x[ind] * flip
            V = system.detector.direction_y[ind] * flip

            roll = np.arctan2(-V[1], V[0])
            pitch = np.arcsin(V[2])
            yaw = np.arctan2(-U[2],U[1])

        self.theta = yaw
        panel_origin = ag_in.config.panel.origin
        if 'right' in panel_origin:
            yaw += np.pi
        if 'top' in panel_origin:
            pitch += np.pi

        self.rotDetector = np.array((roll, pitch, yaw))

        # total size of the image       (mm)
        self.sVoxel = self.nVoxel * self.dVoxel

        # Auxiliary
        self.accuracy = 0.5                        # Accuracy of FWD proj          (vx/sample)
        # Mode
        # parallel, cone
        self.mode = ag_in.config.system.geometry

max_stars_repo_name: mhquah/CCPi-Framework

max_issues_repo_name: mhquah/CCPi-Framework

max_forks_repo_name: mhquah/CCPi-Framework


text:
from multiprocessing import Process, Value, Array, Queue, Pipe
import time
from datetime import datetime
import cv2
import numpy
import RPi.GPIO as GPIO
import Iothub_client_functions as iot
import picamera
from picamera.array import PiRGBArray
import picamera.array

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]
    time.sleep(2)
    print(a[:])

def trip(conn):
    ballCounter = 0

    while True:
        ballCounter = ballCounter + 1
        # time.sleep(.001)
        #     print('Ball Timer Awake ', ballCounter)
        timesup = True
        # temp = q.get()
        if ballCounter % 100000 == 0:
            x = conn.recv()
            conn.send(ballCounter)
            print('Multi', ballCounter, type(x), x.size, x.shape)

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    q = Queue()
    trip_conn, other_conn = Pipe()

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()
    #     getRGB(q)
    #     p1 = Process(target=getRGB, args=(q,))
    #     p1.start()
    #     while True:
    #         if q.get() % 10 == 0:

    #             print('FrameNo', q.get())

    #     p1.join()
    #     getRGB()
    p2 = Process(target=trip, args=(other_conn,))
    p2.start()

    print(num.value)
    print(arr[:])
    frameNo = 0
    with picamera.PiCamera() as camera:
        # camera.resolution = setResolution()
        camera.video_stabilization = True
        camera.annotate_background = True
        camera.rotation = 180
        rawCapture = PiRGBArray(camera)
        # setup a circular buffer
        # stream = picamera.PiCameraCircularIO(camera, seconds = video_preseconds)
        stream = picamera.PiCameraCircularIO(camera, size=3000000)
        # video recording into circular buffer from splitter port 1
        camera.start_recording(stream, format='h264', splitter_port=1)
        #camera.start_recording('test.h264', splitter_port=1)
        # wait 2 seconds for stable video data
        camera.wait_recording(2, splitter_port=1)
        # motion_detected = False
        print(camera.resolution)
        startTime = time.time()
        print('Preframe')

        for frame in camera.capture_continuous(rawCapture, format="bgr",  use_video_port=True):
            # grab the raw NumPy array representing the image, then initialize the timestamp
            # and occupied/unoccupied text???????????????????
            rawCapture.truncate()
            rawCapture.seek(0)

            frame2 = frame.array
            frameNo = frameNo + 1

            trip_conn.send(frame2)
            print(frameNo)
            print('CONN', trip_conn.recv())
            cv2.imshow('RGB', frame2)

            key = cv2.waitKey(1) & 0xFF
            # if the `q` key was pressed, break from the loop
            if key == ord("q"):
                camera.close()
                break


text:
# -*- coding: utf-8 -*-
"""createcsv.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/0B6o9rnHzHPSIUFlrakp5UDY1S3V2a3RpVnJlVHEycEZPZEww
"""

# converting images to pixels (numpyarray)
import glob
import cv2
images=[]
for file in glob.glob("C:\Users\user\Desktop\ismriti\CK+48\anger\*.png"):
    img=cv2.imread(str(file))
    img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    images.append(img)

images

images.insert(0,'P')

images.remove('Pixels')

#creating csv file and storing label and pixels
import csv
with open('testck.csv', 'w') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    for i in range(len(images)):
        wr.writerow((0,list(images[i])))

import pandas as pd
ff= pd.read_csv('testck.csv')

ff

import numpy as np
for i in range(len(images)):
    images[i]=np.array(images[i]).flatten()
#     print(len(i))

images[0].shape

h=[]
for file in glob.glob(r"C:\Users\user\Desktop\ismriti\CK+48\contempt\*.png"):
    img=cv2.imread(str(file))
    img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    h.append(img)

for i in range(len(h)):
    h[i]=np.array(h[i]).flatten()

#opening the csv in append mode
with open('testck.csv','a', newline='') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    for i in range(len(h)):
        wr.writerow((6,list(h[i])))

ff= pd.read_csv('testck.csv')
ff


text:
import warnings

import virl_model

warnings.filterwarnings('ignore', category=DeprecationWarning)

import random
from pathlib import Path

import hydra
import numpy as np
import torch
import torch.utils.data
from hydra.utils import to_absolute_path

import datasets
import utils
import video
from logger import Logger

def _worker_init_fn(worker_id):
    seed = np.random.get_state()[1][0] + worker_id
    np.random.seed(seed)
    random.seed(seed)

def _make_agent(obs_spec, action_spec, cfg):
    cfg.obs_shape = obs_spec.shape
    cfg.action_shape = action_spec.shape
    return hydra.utils.instantiate(cfg)

class Workspace:
    def __init__(self, cfg, hyperparams_str=''):
        self.work_dir = Path.cwd() / hyperparams_str
        print(f'workspace: {self.work_dir}')

        self.cfg = cfg
        utils.set_seed_everywhere(cfg.seed)
        self.encoder: virl_model.ViRLNet = hydra.utils.instantiate(self.cfg.virl_model).to(utils.device())

        self.dataset = datasets.ViRLVideoDataset(to_absolute_path(self.cfg.train_video_dir), self.cfg.episode_len, self.cfg.train_cams)
        self.valid_dataset = datasets.ViRLVideoDataset(to_absolute_path(self.cfg.valid_video_dir), self.cfg.episode_len, self.cfg.train_cams)

        self.dataloader = torch.utils.data.DataLoader(
            self.dataset,
            batch_size=self.cfg.batch_size,
            num_workers=self.cfg.num_workers,
            worker_init_fn=_worker_init_fn,
        )
        self.valid_dataloader = torch.utils.data.DataLoader(
            self.valid_dataset,
            batch_size=self.cfg.batch_size
        )

        self.dataloader_iter = iter(self.dataloader)
        self.valid_dataloader_iter = iter(self.valid_dataloader)

        self.logger = Logger(self.work_dir, use_tb=self.cfg.use_tb)

        self._epoch = 0
        self._eval_loss = np.inf

    def train(self):
        train_until_epoch = utils.Until(self.cfg.num_epochs)
        eval_every_epoch = utils.Every(self.cfg.eval_every_epochs)

        self.encoder.train()

        while train_until_epoch(self._epoch):
            video_i, video_p, video_n = next(self.dataloader_iter)

            video_i = video_i.to(device=utils.device(), dtype=torch.float)
            video_p = video_p.to(device=utils.device(), dtype=torch.float)
            video_n = video_n.to(device=utils.device(), dtype=torch.float)

            video_i, video_p, video_n = datasets.ViRLVideoDataset.augment(video_i, video_p, video_n)

            metrics = self.encoder.update(video_i, video_p, video_n)

            self.logger.log_metrics(metrics, self._epoch, 'train')

            print(f'E: {self._epoch+1}', end='\t| ')
            for k, v in metrics.items():
                print(f'{k}: {v}', end='\t| ')
            print('')

            if eval_every_epoch(self._epoch):
                self.encoder.eval()

                with torch.no_grad():

                    metrics = None
                    for _ in range(self.cfg.num_evaluations):
                        video_i, video_p, video_n = next(self.valid_dataloader_iter)

                        video_i = video_i.to(device=utils.device())
                        video_p = video_p.to(device=utils.device())
                        video_n = video_n.to(device=utils.device())

                        m, _ = self.encoder.evaluate(video_i, video_p, video_n)

                        if metrics is None:
                            metrics = m
                        else:
                            for k, v in m.items():
                                metrics[k] += v

                    for k, v in metrics.items():
                        metrics[k] /= self.cfg.num_evaluations

                    self.logger.log_metrics(metrics, self._epoch, 'eval')

                    eval_loss = metrics['loss']
                    print('Eval loss: ', eval_loss, end='\t')
                    if eval_loss < self._eval_loss:
                        self._eval_loss = eval_loss
                        self.save_snapshot(as_optimal=True)
                        print('*** save ***', end='')
                    print('')

                    video_sample, _, _ = next(self.valid_dataloader_iter)
                    video_sample = video_sample[0]  # T x c x h x w
                    video_sample = video_sample.to(device=utils.device())
                    video0, video1 = self.encoder.encode_decode(video_sample)

                    video.make_video_from_frames(self.work_dir / f'eval_video/{self._epoch}.mp4',
                                                 video_sample.cpu().numpy())
                    video.make_video_from_frames(self.work_dir / f'eval_video/{self._epoch}_rae.mp4',
                                                 video0.cpu().numpy())
                    video.make_video_from_frames(self.work_dir / f'eval_video/{self._epoch}_vae.mp4',
                                                 video1.cpu().numpy())

                self.encoder.train()

            self.save_snapshot()
            self._epoch += 1

    def save_snapshot(self, as_optimal=False):
        if not as_optimal:
            snapshot = self.work_dir / 'snapshot.pt'
        else:
            snapshot = self.work_dir / 'opt_snapshot.pt'
        keys_to_save = ['encoder', '_epoch', '_eval_loss']
        payload = {k: self.__dict__[k] for k in keys_to_save}
        with snapshot.open('wb') as f:
            torch.save(payload, f)

    def load_snapshot(self):
        snapshot = self.work_dir / 'snapshot.pt'
        with snapshot.open('rb') as f:
            payload = torch.load(f)
        for k, v in payload.items():
            self.__dict__[k] = v

@hydra.main(config_path='virl_cfgs', config_name='config')
def main(cfg):
    root_dir = Path.cwd()
    workspace = Workspace(cfg)
    snapshot = root_dir / 'snapshot.pt'
    if snapshot.exists():
        print(f'resuming: {snapshot}')
        workspace.load_snapshot()
    workspace.train()

if __name__ == '__main__':
    main()


text:
import iris
import math
import numpy as np

# the name of coordinates about latitude should be "grid_latitude"
# the name of coordinates about longitude should be "grid_longitude"
# any dimension is o.k.

# grid points after changing resolution
def lat_id(cube, drop_rate):
    lat_num = len(cube.coord("grid_latitude").points)
    span = math.ceil((lat_num - math.ceil((lat_num*drop_rate))) /(math.ceil((lat_num*drop_rate)) + 1))
    last = math.ceil((lat_num - span)/(span+1))
    last_ind = span + (last - 1)*(span + 1)

    drop_ind = np.linspace(span, last_ind, last)
    return list(set(range(lat_num)).difference(set(drop_ind)))

def lon_id(cube, drop_rate):
    lon_num = len(cube.coord("grid_longitude").points)
    span = math.ceil((lon_num - math.ceil((lon_num*drop_rate))) /(math.ceil((lon_num*drop_rate)) + 1))
    last = math.ceil((lon_num - span)/(span+1))
    last_ind = span + (last - 1)*(span + 1)

    drop_ind = np.linspace(span, last_ind, last)
    return list(set(range(lon_num)).difference(set(drop_ind)))

# resolution shange
def change_resolution(cube, drop_rate):
    constlat = iris.Constraint(grid_latitude = cube.coord("grid_latitude").points[lat_id(cube, drop_rate)])
    constlon = iris.Constraint(grid_longitude = cube.coord("grid_longitude").points[lon_id(cube, drop_rate)])
    cube.extract(lat & lon)
    return cube.extract(lat & lon)

max_stars_repo_name: keiikegami/weather

max_issues_repo_name: keiikegami/weather

max_forks_repo_name: keiikegami/weather


text:
import math as mt
def derivRichardson(x):
    return x**(2)+mt.cos(x)
xi=[]
fxi=[]
a=0
b=2
h=0.01
hi=h
######
print("Nama : M. Thoriqul Aziz\n NIM = 081711733002 ")
while a<b+h:
    xi.append(a)
    a=a+h
print('nilai x = ',xi)
panjang=len(xi)
####
for i in range(0,panjang):
    fx=derivRichardson(xi[i])
    fxi.append(fx)
#print('Nilai fx=', fxi)
#####
num=eval(input('Masukan nilai x ='))
for i in range (0,panjang):
    if xi[i] >= num:
        urutan=i
        break
print('urutan keberapa = ', urutan)
lapisan=int(mt.log10(urutan)/mt.log10(2))
print('Jumlah Lapisan = ',lapisan)
####
Di=[]
print('Lapisan ke 1')
for i in range(0,lapisan+1):
    lapis=2**i
    x0=xi[urutan-lapis]
    x1=xi[urutan+lapis]
    D=(derivRichardson(x1)-derivRichardson(x0))/(2*h*lapis)
    Di.append(D)
print("Nilai semua D",Di)
panjangDi=len(Di)
####
if panjangDi>1:
    n=1
    for j in range(1,lapisan+1):
        print('Lapisan ke ',j+1)
        n=2*j
        Dj=[]
        print('Jumlah perkalian h =',n)
        for i in range (0,panjangDi-1):
            #print(Di[0],Di[1])
            Der=Di[i]+((Di[i]-Di[i+1])/(2**n-1))
            Dj.append(Der)
        Di=Dj #pengubah dalam perhitungan der
        print('Nilai Dj = ',Di)
        panjangDi=len(Di)
######
### Perhitungan dengan Metode Center
# Nilaix=num
# print('hasil num =', num)
# D2h=(derivRichardson(Nilaix+0.2) - derivRichardson(Nilaix-0.2))/0.4
# D4h=(derivRichardson(Nilaix+0.4) - derivRichardson(Nilaix-0.4))/0.8
# center=D2h+(D2h-D4h)/15
# print('Hasil metode center =', center)
######
from sympy import *
x=Symbol("x")
fd=x**(2)+cos(x)
dydx=fd.diff(x)
print("turunan pertama =",dydx)
####### Mengetahui Hasil Sebenarnya dari Perhitungan Turunan Kontinu ########
fx=lambdify(x,dydx)
print("f'(",num,")=",fx(num))
er=abs(fx(num)-Di)
# er1=abs(fx(num)-center)
print("Nilai Eror dari hasil perhitungan dengan Metode Richardson =", er)
# print('Nilai eror dibandingkan dengan metode center =',er1)

max_stars_repo_path: Chapter 10/Richardson Extrapolation Derivates.py

reason: from sympy


text:
import numpy as np
from scipy.stats import norm

def get_phase_amounts(eq_phases, phase_fractions, all_phases):
    """Return the phase fraction for each phase in equilibrium

    Parameters
    ----------
    eq_phases : Sequence[str]
        Equilibrium phases
    phase_fractions : Sequence[float]
        Phase amounts corresponding to the equilibrium phases
    all_phases : Sequence[str]
        All phases that we want to keep track of in the system.

    Returns
    -------
    Dict[str, float]

    """
    phase_amnts = {ph: 0.0 for ph in all_phases}
    for phase_name, fraction in zip(eq_phases, phase_fractions):
        if phase_name in all_phases:
            phase_amnts[phase_name] += float(fraction)
    return phase_amnts

def local_sample(sitefracs, comp_count, pdens=100, stddev=0.05):
    """Sample from a normal distribution around the optimal site fractions

    Parameters
    ----------
    sitefracs : np.ndarray[:, :]
        2d array of site fractions of shape (N, len(dof))
    comp_count : Sequence[int]
        Number of active components in each sublattice, e.g.
        (FE,NI,TI)(FE,NI)(FE,NI,TI) is [3, 2, 3]
    pdens : Optional[int]
        Number of points to add locally
    stddev : Optional[float]
        Standard deviation for the normal distribution to sample from

    Returns
    -------
    np.ndarray[:, :]
        Shape (pdens, len(dof))

    """
    # Need to take the absolute value, in order to make all the candidate site fractions positive
    # There's probably a better distribution, but this works. Vacancies may cause trouble with this kind of normal distribution, since they are often near zero.
    # Maybe like a gamma between Y=0 and Y=1, with a mean (or cdf of 0.5) optimal site fraction
    pts = np.concatenate(np.abs(norm(sitefracs, stddev).rvs([pdens, sitefracs.shape[0], sitefracs.shape[-1]])), axis=0)
    # add on the original points, since these are the actual minima
    pts = np.concatenate([pts, sitefracs], axis=0)
    # Need to normalize in each sublattice
    # comp_count is # of elements per sublattice, e.g.
    cur_idx = 0
    for ctx in comp_count:
        end_idx = cur_idx + ctx
        pts[:, cur_idx:end_idx] /= pts[:, cur_idx:end_idx].sum(axis=1)[:, None]
        cur_idx = end_idx
    return pts

max_stars_repo_name: pycalphad/scheil

max_issues_repo_name: pycalphad/scheil

max_forks_repo_name: pycalphad/scheil

reason: import numpy,from scipy


text:
import sys
from math import cos, sin, pi

from PySide import QtCore, QtGui, QtOpenGL
from OpenGL.GL import *
from numpy import array, int32, float32, cross, dot, tan, zeros, eye
from numpy.linalg import norm
import PIL.Image

def createColorTexture(width, height):
    tex = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, tex)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_INT, ctypes.c_void_p(0))
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glBindTexture(GL_TEXTURE_2D, 0)
    return tex

def createDepthTexture(width, height):
    tex = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, tex)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, ctypes.c_void_p(0))
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glBindTexture(GL_TEXTURE_2D, 0)
    return tex

def loadTextureIntoGL(filename):
    ''' Load an image file into GL as a 2D texture (using PIL) '''
    image = PIL.Image.open(filename)
    try:
        width, height, data = image.size[0], image.size[1], image.tobytes("raw", "RGBA", 0, -1)
    except SystemError:
        width, height, data = image.size[0], image.size[1], image.tobytes("raw", "RGBX", 0, -1) # ?
    glID = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, glID)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    return (width, height, glID)

def normalize(vec):
    n = norm(vec)
    return array([i / float(n) for i in vec])

def frustum( left, right, bottom, top, znear, zfar ):
    assert(right  != left)
    assert(bottom != top )
    assert(znear  != zfar)
    M = zeros((4,4), dtype=float32)
    M[0,0] = 2.0 * znear / (right - left)
    M[2,0] = (right + left) / (right - left)
    M[1,1] = 2.0 * znear / (top - bottom)
    M[3,1] = (top + bottom) / (top - bottom)
    M[2,2] = - (zfar + znear) / (zfar - znear)
    M[3,2] = - 2.0 * znear * zfar / (zfar - znear)
    M[2,3] = -1.0
    return M

def perspective(fovy, aspect, znear, zfar):
    assert( znear != zfar )
    h = tan(fovy / 360.0 * pi) * znear
    w = h * aspect
    return frustum( -w, w, -h, h, znear, zfar )

MOUSE_ZOOM_SPEED = 0.08
MOUSE_PAN_SPEED = 0.001
MOUSE_TURN_SPEED = 0.005

class RenderObject:
    def __init__(self):
        self.vao = -1
        self.vertexCount = 0
        self.drawMode = GL_TRIANGLES
        self.polygonMode = GL_FILL

    def draw(self):
        if glIsVertexArray(self.vao):
            glBindVertexArray(self.vao)
            if self.polygonMode != GL_FILL:
                glPolygonMode(GL_FRONT_AND_BACK, self.polygonMode)
            glDrawElements(self.drawMode, self.vertexCount, GL_UNSIGNED_INT, ctypes.c_void_p(0))
            if self.polygonMode != GL_FILL:
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
            glBindVertexArray(0)

    def setPolygonMode(self, polygonMode):
        self.polygonMode = polygonMode

    def freeRessources(self):
        if glIsVertexArray(self.vao):
            glDeleteVertexArrays(1, (GLuint)(self.vao))

class Quad2D(RenderObject):
    def __init__(self):
        RenderObject.__init__(self)

        self.vao = glGenVertexArrays(1)
        vbos = glGenBuffers(2)
        glBindVertexArray(self.vao)

        indices = array([0, 1, 2, 2, 0, 3], dtype=int32)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0])
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)
        self.vertexCount = len(indices)

        vertices = array([-1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0], dtype=float32)
        glBindBuffer(GL_ARRAY_BUFFER, vbos[1])
        glEnableVertexAttribArray(0) # shader layout location
        glVertexAttribPointer(0, 2, GL_FLOAT, False, 0, ctypes.c_void_p(0))
        glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)

        glBindVertexArray(0)
        glDisableVertexAttribArray(0)
        glBindBuffer(GL_ARRAY_BUFFER, 0)
        glDeleteBuffers(2, vbos)

class Cube3D(RenderObject):
    def __init__(self):
        RenderObject.__init__(self)

        self.vao = glGenVertexArrays(1)
        vbos = glGenBuffers(4)
        glBindVertexArray(self.vao)

        indices = [0, 1, 2, 2, 1, 3, 4, 5, 6, 6, 5, 7, 8, 9, 10, 10, 9, 11, 12, 13, 14, 14, 13, 15, 16, 17, 18, 19, 17, 20, 21, 22, 23, 24, 25, 26]
        indices = array(indices, dtype=int32)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0])
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)
        self.vertexCount = len(indices)

        vertices = [-0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5]
        vertices = array(vertices, dtype=float32)
        glBindBuffer(GL_ARRAY_BUFFER, vbos[1])
        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, ctypes.c_void_p(0))
        glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)

        normals = [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0]
        normals = array(normals, dtype=float32)
        glBindBuffer(GL_ARRAY_BUFFER, vbos[2])
        glEnableVertexAttribArray(1)
        glVertexAttribPointer(1, 3, GL_FLOAT, False, 0, ctypes.c_void_p(0))
        glBufferData(GL_ARRAY_BUFFER, normals.nbytes, normals, GL_STATIC_DRAW)

        uvs = [0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0]
        uvs = array(uvs, dtype=float32)
        glBindBuffer(GL_ARRAY_BUFFER, vbos[3])
        glEnableVertexAttribArray(2)
        glVertexAttribPointer(2, 2, GL_FLOAT, False, 0, ctypes.c_void_p(0))
        glBufferData(GL_ARRAY_BUFFER, uvs.nbytes, uvs, GL_STATIC_DRAW)

        glBindVertexArray(0)
        glDisableVertexAttribArray(0)
        glBindBuffer(GL_ARRAY_BUFFER, 0)
        glDeleteBuffers(4, vbos)

class Grid3D(RenderObject):
    def __init__(self, xres, yres):
        ''' Create a 3D grid which has xres column and yres lines. The grid corners are (-1, 1), (1, 1), (1, -1) and (-1, -1)'''
        RenderObject.__init__(self)

        self.vao = glGenVertexArrays(1)
        vbos = glGenBuffers(2)

        glBindVertexArray(self.vao)

        indices = []
        for j in range(0, yres):
            offset = j * (xres + 1)
            for i in range(0, xres):
                indices.extend((offset + i,
                                offset + i + 1,
                                offset + (xres + 1) + i + 1,
                                offset + i,
                                offset + (xres + 1) + i + 1,
                                offset + (xres + 1) + i))
        indices = array(indices, dtype=int32)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0])
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)
        self.vertexCount = len(indices)

        vertices = []
        for j in range(0, yres + 1):
            y = (0.5 - j / float(yres)) * 2.0
            for i in range(0, xres + 1):
                vertices.extend((((i / float(xres)) - 0.5) * 2.0, y, 0.0))
        vertices = array(vertices, dtype=float32)
        glBindBuffer(GL_ARRAY_BUFFER, vbos[1])
        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, ctypes.c_void_p(0))
        glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)

        glBindVertexArray(0)
        glDisableVertexAttribArray(0)
        glBindBuffer(GL_ARRAY_BUFFER, 0)
        glDeleteBuffers(2, vbos)
        # self.polygonMode = GL_LINE

class GLProgram():

    def __init__(self, vsFilename, fsFilename):
        self.id = glCreateProgram()
        vsObject = 0
        try:
            f = open(vsFilename, 'r')
            vsObject = self.createAndCompileShader(GL_VERTEX_SHADER, f.read())
            glAttachShader(self.id, vsObject)
            f.close()
        except IOError as e:
            print "Fail to load", vsFilename
            print "I/O error({0}): {1}".format(e.errno, e.strerror)

        fsObject = 0
        try:
            f = open(fsFilename, 'r')
            fsObject = self.createAndCompileShader(GL_FRAGMENT_SHADER, f.read())
            glAttachShader(self.id, fsObject)
            f.close()
        except IOError as e:
            print "Fail to load", fsFilename
            print "I/O error({0}): {1}".format(e.errno, e.strerror)

        glLinkProgram(self.id)
        glDeleteShader(vsObject)
        glDeleteShader(fsObject)

        status = glGetProgramiv(self.id, GL_LINK_STATUS)
        loglength = glGetProgramiv(self.id, GL_INFO_LOG_LENGTH)
        if(loglength > 1):
            print "Error in linking shaders (status = %s) : %s" % (str(status), glGetProgramInfoLog(self.id))
        print self.id

    def createAndCompileShader(self, shaderType, source):
        shader = glCreateShader(shaderType)
        glShaderSource(shader, source)
        glCompileShader(shader)
        status = glGetShaderiv(shader, GL_COMPILE_STATUS)
        loglength = glGetShaderiv(shader, GL_INFO_LOG_LENGTH)
        if(loglength > 1):
            print "Error in compiling %s (Status = %s): %s" % (str(shaderType), str(status), glGetShaderInfoLog(shader))
            return -1
        return shader

    def use(self):
        glUseProgram(self.id)

    def getLocation(self, uniform):
        return glGetUniformLocation(self.id, uniform)

    def sendUniform1i(self, uniform, i):
        glUniform1i(self.getLocation(uniform), i)

    def sendUniform2i(self, uniform, i1, i2):
        glUniform2i(self.getLocation(uniform), i1, i2)

    def sendUniform3i(self, uniform, i1, i2, i3):
        glUniform3i(self.getLocation(uniform), i1, i2, i3)

    def sendUniform4i(self, uniform, i1, i2, i3, i4):
        glUniform4i(self.getLocation(uniform), i1, i2, i3, i4)

    def sendUniform1iv(self, uniform, vec):
        glUniform1iv(self.getLocation(uniform), len(vec), vec)

    def sendUniform1f(self, uniform, f):
        glUniform1f(self.getLocation(uniform), f)

    def sendUniform2f(self, uniform, f1, f2):
        glUniform2f(self.getLocation(uniform), f1, f2)

    def sendUniform3f(self, uniform, f1, f2, f3):
        glUniform3f(self.getLocation(uniform), f1, f2, f3)

    def sendUniform4f(self, uniform, f1, f2, f3, f4):
        glUniform4f(self.getLocation(uniform), f1, f2, f3, f4)

    def sendUniform1fv(self, uniform, vec):
        glUniform1fv(self.getLocation(uniform), len(vec), vec)

    def sendUniform2fv(self, uniform, vec):
        glUniform2fv(self.getLocation(uniform), len(vec), vec)

    def sendUniform3fv(self, uniform, vec):
        glUniform3fv(self.getLocation(uniform), len(vec), vec)

    def sendUniformMatrix4fv(self, uniform, matrix):
        glUniformMatrix4fv(self.getLocation(uniform), 1, 0, matrix)

    def sendTexture2d(self, uniform, textureId, textureUnit):
        if textureUnit == GL_TEXTURE0:
            i = 0
        elif textureUnit == GL_TEXTURE1:
            i = 1
        elif textureUnit == GL_TEXTURE2:
            i = 2
        elif textureUnit == GL_TEXTURE3:
            i = 3
        elif textureUnit == GL_TEXTURE4:
            i = 4
        elif textureUnit == GL_TEXTURE5:
            i = 5
        elif textureUnit == GL_TEXTURE6:
            i = 6
        elif textureUnit == GL_TEXTURE7:
            i = 7
        else:
            print "bad texture unit"
        self.sendUniform1i(uniform, i)
        glActiveTexture(textureUnit)
        glBindTexture(GL_TEXTURE_2D, textureId)

class GLFramebuffer():
    def __init__(self):
        self.id = glGenFramebuffers(1)

    def bind(self):
        glBindFramebuffer(GL_FRAMEBUFFER, self.id)

    def unbind(self):
        glBindFramebuffer(GL_FRAMEBUFFER, 0)

    def check(self):
        self.bind()
        status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
        if (status == GL_FRAMEBUFFER_COMPLETE):
            print "GL_FRAMEBUFFER_COMPLETE"
        elif (status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT):
            print "!! GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"
        elif (status == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT):
            print "!! GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"
        elif (status == GL_FRAMEBUFFER_UNSUPPORTED):
            print "!! GL_FRAMEBUFFER_UNSUPPORTED"
        else:
            print "glCheckFramebufferStatus : unsupported result"

    def attachTex2D(self, attachement, tex):
        self.bind()
        glFramebufferTexture2D(GL_FRAMEBUFFER, attachement, GL_TEXTURE_2D,tex, 0)

    def freeRessources(self):
        glDeleteFramebuffers(GLuint(self.id))

class CameraControls:
    def __init__(self):
        self.altPressed = False
        self.turnLock = False
        self.zoomLock = False
        self.panLock = False
        self.lockPositionX = 0.0
        self.lockPositionY = 0.0
        self.leftButton = False
        self.rightButton = False
        self.middleButton = False

class Camera():
    def __init__(self):
        self.reset()

    def compute(self):
        self.eye[0] = self.target[0] + cos(self.theta) * sin(self.phi) * self.radius
        self.eye[1] = self.target[1] + cos(self.phi) * self.radius
        self.eye[2] = self.target[2] + sin(self.theta) * sin(self.phi) * self.radius
        self.up = array([0.0, 1.0 if self.phi < pi else -1.0, 0.0])

    def reset(self, eye=array([0.0, 0.0, -5.0], dtype=float32), target=array([0.0, 0.0, 0.0], dtype=float32)):
        self.target = target
        self.eye = eye
        self.phi = pi / 2.0
        self.theta = pi / 2.0
        self.radius = 5.0
        self.compute()

    def zoom(self, zoomFactor):
        self.radius += self.radius * zoomFactor
        if self.radius < 0.1:
            self.radius = 10.0
            EO = [x - y for x, y in zip(self.target, self.eye)]
            EO = normalize(EO)
            self.target = array([x + y * self.radius for x, y in zip(self.eye, EO)])
        self.compute()

    def turn(self, phi, theta):
        self.theta += theta
        self.phi -= phi
        if self.phi >= 2.0 * pi - 0.1:
            self.phi = 0.00001
        elif self.phi <= 0.0:
            self.phi = 2.0 * pi - 0.1
        self.compute()

    def pan(self, x, y):
        up = array([0.0, 1.0 if self.phi < pi else -1.0, 0.0])
        fwd = normalize(array([i - j for i, j in zip(self.target, self.eye)]))
        side = normalize(cross(fwd, up))
        self.up = normalize(cross(side, fwd))
        self.target[0] += up[0] * y * self.radius * 2
        self.target[1] += up[1] * y * self.radius * 2
        self.target[2] += up[2] * y * self.radius * 2
        self.target[0] -= side[0] * x * self.radius * 2
        self.target[1] -= side[1] * x * self.radius * 2
        self.target[2] -= side[2] * x * self.radius * 2
        self.compute()

    def getViewMatrix(self):
        f = normalize([x - y for x, y in zip(self.target, self.eye)])
        s = normalize(cross(f, normalize(self.up)))
        u = cross(s, f)
        result = eye(4)
        result[0][0] = s[0]
        result[1][0] = s[1]
        result[2][0] = s[2]
        result[0][1] = u[0]
        result[1][1] = u[1]
        result[2][1] = u[2]
        result[0][2] = -f[0]
        result[1][2] = -f[1]
        result[2][2] = -f[2]
        result[3][0] = -dot(s, self.eye)
        result[3][1] = -dot(u, self.eye)
        result[3][2] =  dot(f, self.eye)
        return result

class Window(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setMinimumWidth(800)
        self.setMinimumHeight(600)
        self.setCentralWidget(Viewport(self))

class Viewport(QtOpenGL.QGLWidget):
    def __init__(self, parent):
        super(Viewport, self).__init__(parent)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.setMouseTracking(True)

    def initializeGL(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)
        self.programs = {
            "blit_quad": GLProgram("blit_quad.vs.glsl", "blit_quad.fs.glsl"),
            "3d": GLProgram("3d.vs.glsl", "3d.fs.glsl")
        }
        self.rdo = {
            "grid": Grid3D(100, 100),
            "quad": Quad2D(),
            "cube": Cube3D()
        }
        self.textures = {
            "displacement": loadTextureIntoGL("displacemap.png")
        }
        self.camera = Camera()
        self.ctrl = CameraControls()

    def paintGL(self):
        glViewport(0, 0, self.width(), self.height())
        glEnable(GL_DEPTH_TEST)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        self.programs["3d"].use()
        projection = perspective(30, self.width() / float(self.height()), 0.1, 10000.0)
        self.programs["3d"].sendUniformMatrix4fv("u_projection", projection.astype(float32))
        self.programs["3d"].sendUniformMatrix4fv("u_view", self.camera.getViewMatrix().astype(float32))

        self.rdo["cube"].draw()

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.MouseButton.LeftButton:
            self.ctrl.leftButton = True
            self.ctrl.turnLock = True
        elif event.button() == QtCore.Qt.MouseButton.MiddleButton:
            self.ctrl.middleButton = True
            self.ctrl.panLock = True
        elif event.button() == QtCore.Qt.MouseButton.RightButton:
            self.ctrl.rightButton = True
            self.ctrl.zoomLock = True
        else:
            return
        if self.ctrl.altPressed:
            return
        self.ctrl.lockPositionX = event.x()
        self.ctrl.lockPositionY = event.y()
        self.updateGL()

    def mouseReleaseEvent(self, event):
        if event.button() == QtCore.Qt.MouseButton.LeftButton:
            self.ctrl.leftButton = False
            self.ctrl.turnLock = False
        elif event.button() == QtCore.Qt.MouseButton.MiddleButton:
            self.ctrl.middleButton = False
            self.ctrl.panLock = False
        elif event.button() == QtCore.Qt.MouseButton.RightButton:
            self.ctrl.rightButton = False
            self.ctrl.zoomLock = False
        self.updateGL()

    def mouseMoveEvent(self, event):
        if self.ctrl.altPressed:
            mouseX = event.x()
            mouseY = event.y()
            diffX = mouseX - self.ctrl.lockPositionX
            diffY = mouseY - self.ctrl.lockPositionY

            if self.ctrl.zoomLock:
                zoomDir = -1.0 if diffX > 0 else 1.0
                self.camera.zoom(zoomDir * MOUSE_ZOOM_SPEED)
            elif self.ctrl.turnLock:
                self.camera.turn(diffY * MOUSE_TURN_SPEED, diffX * MOUSE_TURN_SPEED)
            elif self.ctrl.panLock:
                self.camera.pan(diffX * MOUSE_PAN_SPEED, diffY * MOUSE_PAN_SPEED)
            self.ctrl.lockPositionX = mouseX
            self.ctrl.lockPositionY = mouseY
        self.updateGL()

    def wheelEvent(self, event):
        self.updateGL()

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key.Key_Alt:
            self.ctrl.altPressed = True
        elif event.key() == QtCore.Qt.Key.Key_P:
            mode = GL_FILL if self.grid.polygonMode == GL_POINT else GL_POINT
            self.grid.setPolygonMode(mode)
        self.updateGL()

    def keyReleaseEvent(self, event):
        if event.key() == QtCore.Qt.Key.Key_Alt:
            self.ctrl.altPressed = False
        self.updateGL()

    def freeRessources(self):
        pass

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

reason: from numpy


text:
class CounterInstance(type):
    def __new__(cls, name, bases, dict):
        cls.counter = 0
        return super().__new__(cls, name, bases, dict)

    def __call__(cls, *args, **kwarg):
        cls.counter += 1
        return super().__call__(*args, **kwarg)

class A(metaclass=CounterInstance):
    pass

class C(A):
    pass

a1 = A()
a2 = A()
c1 = C()

print(A.counter)
print(C.counter)

# import PIL
# import numpy as np
# import matplotlib.pyplot as plt
#
# image = PIL.Image.open("./tests/models/wood.jpg")
# width, height = image.size
# image = image.convert("RGBA")
# data = image.getdata()
# bytes = bytearray()
# for r,g,b,a in data:
#     bytes.append(r)
#     bytes.append(g)
#     bytes.append(b)
#     bytes.append(a)
# im_np = np.array(bytes, dtype=np.uint8)
#
# im_np = im_np.reshape((height, width, 4))
# plt.imshow(im_np)
# plt.show()


text:
# -*- coding: utf-8 -*-
"""
Defines the unit tests for the
:mod:`colour.models.rgb.transfer_functions.itur_bt_2020` module.
"""

import numpy as np
import unittest

from colour.models.rgb.transfer_functions import (eotf_inverse_BT2020,
                                                  eotf_BT2020)
from colour.utilities import domain_range_scale, ignore_numpy_errors

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-developers@colour-science.org'
__status__ = 'Production'

__all__ = ['TestEotfInverse_BT2020', 'TestEotf_BT2020']

class TestEotfInverse_BT2020(unittest.TestCase):
    """
    Defines :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_inverse_BT2020` definition unit tests methods.
    """

    def test_eotf_inverse_BT2020(self):
        """
        Tests :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_inverse_BT2020` definition.
        """

        self.assertAlmostEqual(eotf_inverse_BT2020(0.0), 0.0, places=7)

        self.assertAlmostEqual(
            eotf_inverse_BT2020(0.18), 0.409007728864150, places=7)

        self.assertAlmostEqual(eotf_inverse_BT2020(1.0), 1.0, places=7)

    def test_n_dimensional_eotf_inverse_BT2020(self):
        """
        Tests :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_inverse_BT2020` definition n-dimensional arrays support.
        """

        E = 0.18
        E_p = eotf_inverse_BT2020(E)

        E = np.tile(E, 6)
        E_p = np.tile(E_p, 6)
        np.testing.assert_almost_equal(eotf_inverse_BT2020(E), E_p, decimal=7)

        E = np.reshape(E, (2, 3))
        E_p = np.reshape(E_p, (2, 3))
        np.testing.assert_almost_equal(eotf_inverse_BT2020(E), E_p, decimal=7)

        E = np.reshape(E, (2, 3, 1))
        E_p = np.reshape(E_p, (2, 3, 1))
        np.testing.assert_almost_equal(eotf_inverse_BT2020(E), E_p, decimal=7)

    def test_domain_range_scale_eotf_inverse_BT2020(self):
        """
        Tests :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_inverse_BT2020` definition domain and range scale support.
        """

        E = 0.18
        E_p = eotf_inverse_BT2020(E)

        d_r = (('reference', 1), (1, 1), (100, 100))
        for scale, factor in d_r:
            with domain_range_scale(scale):
                np.testing.assert_almost_equal(
                    eotf_inverse_BT2020(E * factor), E_p * factor, decimal=7)

    @ignore_numpy_errors
    def test_nan_eotf_inverse_BT2020(self):
        """
        Tests :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_inverse_BT2020` definition nan support.
        """

        eotf_inverse_BT2020(
            np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]))

class TestEotf_BT2020(unittest.TestCase):
    """
    Defines :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_BT2020` definition unit tests methods.
    """

    def test_eotf_BT2020(self):
        """
        Tests :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_BT2020` definition.
        """

        self.assertAlmostEqual(eotf_BT2020(0.0), 0.0, places=7)

        self.assertAlmostEqual(eotf_BT2020(0.409007728864150), 0.18, places=7)

        self.assertAlmostEqual(eotf_BT2020(1.0), 1.0, places=7)

    def test_n_dimensional_eotf_BT2020(self):
        """
        Tests :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_BT2020` definition n-dimensional arrays support.
        """

        E_p = 0.409007728864150
        E = eotf_BT2020(E_p)

        E_p = np.tile(E_p, 6)
        E = np.tile(E, 6)
        np.testing.assert_almost_equal(eotf_BT2020(E_p), E, decimal=7)

        E_p = np.reshape(E_p, (2, 3))
        E = np.reshape(E, (2, 3))
        np.testing.assert_almost_equal(eotf_BT2020(E_p), E, decimal=7)

        E_p = np.reshape(E_p, (2, 3, 1))
        E = np.reshape(E, (2, 3, 1))
        np.testing.assert_almost_equal(eotf_BT2020(E_p), E, decimal=7)

    def test_domain_range_scale_eotf_BT2020(self):
        """
        Tests :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_BT2020` definition domain and range scale support.
        """

        E_p = 0.409007728864150
        E = eotf_BT2020(E_p)

        d_r = (('reference', 1), (1, 1), (100, 100))
        for scale, factor in d_r:
            with domain_range_scale(scale):
                np.testing.assert_almost_equal(
                    eotf_BT2020(E_p * factor), E * factor, decimal=7)

    @ignore_numpy_errors
    def test_nan_eotf_BT2020(self):
        """
        Tests :func:`colour.models.rgb.transfer_functions.itur_bt_2020.\
eotf_BT2020` definition nan support.
        """

        eotf_BT2020(np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]))

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

max_stars_repo_name: rift-labs-developer/colour

max_issues_repo_name: rift-labs-developer/colour

max_forks_repo_name: rift-labs-developer/colour


text:
from pathlib import Path
import h5py
import numpy
from easistrain.EDD.strainStressd0cstEDD import strainStressTensor

def generate_config(tmp_path: Path, test_data_path: Path) -> dict:
    with h5py.File(test_data_path, "r") as test_file:
        n_peaks = test_file["infos/nPeaks"][()]
        XEC = test_file["infos/XEC"][()]

    return {
        "fileRead": str(tmp_path / "input_file.h5"),
        "fileSave": str(tmp_path / "output_file.h5"),
        "numberOfPeaks": n_peaks,
        "XEC": XEC,
    }

def generate_input_files(
    tmp_path: Path,
    test_data_path: Path,
    pre_strain_data_path: Path,
) -> dict:
    cfg = generate_config(tmp_path, test_data_path)
    n_peaks = cfg["numberOfPeaks"]

    with h5py.File(cfg["fileRead"], "w") as h5file:
        with h5py.File(pre_strain_data_path, "r") as data_file:
            point_name = "point_00000"
            for i in range(n_peaks):
                peak_grp = h5file.create_group(f"STRAIN_with_d0/peak_{str(i).zfill(4)}")

                peak_grp[f"{point_name}"] = data_file[
                    f"peak_{i}/ref_{point_name}/value"
                ][()]
                peak_grp[f"uncertainty_{point_name}"] = data_file[
                    f"peak_{i}/ref_{point_name}/errors"
                ][()]

    return cfg

def test_strain(tmp_path: Path):
    # Arrange
    data_folder = Path(__file__).parent.parent.resolve() / "data"
    test_data_path = data_folder / "BAIII_strain.hdf5"
    pre_strain_data_path = data_folder / "BAIII_pre_strain.hdf5"

    config = generate_input_files(
        tmp_path,
        test_data_path,
        pre_strain_data_path,
    )

    # Act
    strainStressTensor(**config)

    # Assert
    for tensor_type in ["strain", "stress"]:
        for i in range(config["numberOfPeaks"]):
            peak_grp_name = f"peak_{str(i).zfill(4)}"
            point_name = "point_00000"  # Take only first point
            with h5py.File(config["fileSave"], "r") as h5file:
                point_data = h5file[
                    f"/{peak_grp_name}/{point_name}/{tensor_type}_tensor_fit"
                ][()]

            with h5py.File(test_data_path, "r") as h5file:
                ref_data = h5file[f"{tensor_type}/peak_{i}/ref_{point_name}/value"][
                    0, 3:
                ]
                ref_errors = h5file[f"{tensor_type}/peak_{i}/ref_{point_name}/errors"][
                    0, 3:
                ]

            assert numpy.all(numpy.abs(point_data - ref_data) <= numpy.abs(ref_errors))

max_stars_repo_name: EASI-STRESS/easistrain

max_issues_repo_name: EASI-STRESS/easistrain

max_forks_repo_name: EASI-STRESS/easistrain


text:
import cv2
import numpy as np
import os
from os.path import isfile, join

face_classifier = cv2.CascadeClassifier('Cascade/haarcascade_frontalface_default.xml')

def face_detector(img, size=0.5):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray, 1.3, 5)

    if faces is ():
        return img,[]

    for (x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,255), 2)
        #region of interest
        roi = img[y:y+h, x:x+w]
        roi = cv2.resize(roi, (200,200))

    return img,roi

def startDetecting():
    try:
        model = cv2.face.LBPHFaceRecognizer_create()
        model.read('userData/trainer.yml')
    except:
        print('Please Add your face')
        return None

    flag = False
    cap = cv2.VideoCapture(0)

    while True:
        ret, frame = cap.read()
        image, face = face_detector(frame)

        try:
            face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
            result = model.predict(face)

            if result[1] < 500:
                confindence = int((1-(result[1])/300) * 100) #percentange
                display_string = str(confindence) + '%'
            cv2.putText(image, display_string, (100, 120), cv2.FONT_HERSHEY_COMPLEX, 1, (250, 120, 255), 2)

            if confindence > 80:
                cv2.putText(image, "Unlocked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
                cv2.imshow('Face Cropper', image)
                flag = True
                break
            else:
                cv2.putText(image, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
                cv2.imshow('Face Cropper', image)

        except Exception as e:
            cv2.putText(image, "Face Not Found", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2)
            cv2.imshow('Face Cropper', image)
            pass

        if cv2.waitKey(1) == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
    return flag

imageName = ''
def clickPhoto():
    global imageName
    if os.path.exists('Camera')==False:
        os.mkdir('Camera')

    from time import sleep
    import playsound
    from datetime import datetime

    cam = cv2.VideoCapture(0)
    _, frame = cam.read()
    playsound.playsound('extrafiles/audios/photoclick.mp3')
    imageName = 'Camera/Camera_'+str(datetime.now())[:19].replace(':', '_')+'.png'
    cv2.imwrite(imageName, frame)
    cam.release()
    cv2.destroyAllWindows()

def viewPhoto():
    from PIL import Image
    img = Image.open(imageName)
    img.show()