text:
#
# Copyright 2022- IBM Inc. All rights reserved
# SPDX-License-Identifier: Apache2.0
#
import torch
from PIL import Image
import os
import os.path
import numpy as np
import pickle

import torchvision.transforms as transforms

from torchvision.datasets.vision import VisionDataset
from torchvision.datasets.utils import check_integrity , download_and_extract_archive

class CIFAR10(VisionDataset):
    """`CIFAR10 <https://www.cs.toronto.edu/~kriz/cifar.html>`_ Dataset.
    Args:
        root (string): Root directory of dataset where directory
            ``cifar-10-batches-py`` exists or will be saved to if download is set to True.
        train (bool, optional): If True, creates dataset from training set, otherwise
            creates from test set.
        transform (callable, optional): A function/transform that takes in an PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        download (bool, optional): If true, downloads the dataset from the internet and
            puts it in root directory. If dataset is already downloaded, it is not
            downloaded again.
    """
    base_folder = 'cifar-10-batches-py'
    url = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
    filename = "cifar-10-python.tar.gz"
    tgz_md5 = 'c58f30108f718f92721af3b95e74349a'
    train_list = [
        ['data_batch_1', 'c99cafc152244af753f735de768cd75f'],
        ['data_batch_2', 'd4bba439e000b95fd0a9bffe97cbabec'],
        ['data_batch_3', '54ebc095f3ab1f0389bbae665268c751'],
        ['data_batch_4', '634d18415352ddfa80567beed471001a'],
        ['data_batch_5', '482c414d41f54cd18b22e5b47cb7c3cb'],
    ]

    test_list = [
        ['test_batch', '40351d587109b95175f43aff81a1287e'],
    ]
    meta = {
        'filename': 'batches.meta',
        'key': 'label_names',
        'md5': '5ff9c542aee3614f3951f8cda6e48888',
    }

    def __init__(self, root, train=True, transform=None, target_transform=None,
                 download=False, index=None, base_sess=None):

        super(CIFAR10, self).__init__(root, transform=transform,
                                      target_transform=target_transform)
        self.root = os.path.expanduser(root)
        self.train = train  # training set or test set

        if download:
            self.download()

        if not self._check_integrity():
            raise RuntimeError('Dataset not found or corrupted.' +
                               ' You can use download=True to download it')

        # if self.train:
        #     downloaded_list = self.train_list
        # else:
        #     downloaded_list = self.test_list

        if self.train:
            downloaded_list = self.train_list
            self.transform = transforms.Compose([
                transforms.RandomCrop(32, padding=4),
                transforms.RandomHorizontalFlip(),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.507, 0.487, 0.441], std=[0.267, 0.256, 0.276])
            ])
        else:
            downloaded_list = self.test_list
            self.transform = transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.507, 0.487, 0.441], std=[0.267, 0.256, 0.276])
            ])

        self.data = []
        self.targets = []

        # now load the picked numpy arrays
        for file_name, checksum in downloaded_list:
            file_path = os.path.join(self.root, self.base_folder, file_name)
            with open(file_path, 'rb') as f:
                entry = pickle.load(f, encoding='latin1')
                self.data.append(entry['data'])
                if 'labels' in entry:
                    self.targets.extend(entry['labels'])
                else:
                    self.targets.extend(entry['fine_labels'])

        self.data = np.vstack(self.data).reshape(-1, 3, 32, 32)
        self.data = self.data.transpose((0, 2, 3, 1))  # convert to HWC

        self.targets = np.asarray(self.targets)

        if base_sess:
            # label_sel=[]
            # for i in index:
            #     label_sel.append(labels[int(i)])
            # cls_ind = list(set(label_sel))
            self.data, self.targets = self.SelectfromDefault(self.data, self.targets, index)
        else:  # new Class session
            if train:
                self.data, self.targets = self.NewClassSelector(self.data, self.targets, index)
            else:
                self.data, self.targets = self.SelectfromDefault(self.data, self.targets, index)

        self._load_meta()

    def SelectfromDefault(self, data, targets, index):
        data_tmp = []
        targets_tmp = []
        for i in index:
            ind_cl = np.where(i == targets)[0]
            if data_tmp == []:
                data_tmp = data[ind_cl]
                targets_tmp = targets[ind_cl]
            else:
                data_tmp = np.vstack((data_tmp, data[ind_cl]))
                targets_tmp = np.hstack((targets_tmp, targets[ind_cl]))

        return data_tmp, targets_tmp

    def NewClassSelector(self, data, targets, index):
        data_tmp = []
        targets_tmp = []
        ind_list = [int(i) for i in index]
        ind_np = np.array(ind_list)
        index = ind_np.reshape((5,5))
        for i in index:
            ind_cl = i
            if data_tmp == []:
                data_tmp = data[ind_cl]
                targets_tmp = targets[ind_cl]
            else:
                data_tmp = np.vstack((data_tmp, data[ind_cl]))
                targets_tmp = np.hstack((targets_tmp, targets[ind_cl]))

        return data_tmp, targets_tmp

    def _load_meta(self):
        path = os.path.join(self.root, self.base_folder, self.meta['filename'])
        if not check_integrity(path, self.meta['md5']):
            raise RuntimeError('Dataset metadata file not found or corrupted.' +
                               ' You can use download=True to download it')
        with open(path, 'rb') as infile:
            data = pickle.load(infile, encoding='latin1')
            self.classes = data[self.meta['key']]
        self.class_to_idx = {_class: i for i, _class in enumerate(self.classes)}

    def __getitem__(self, index):
        """
        Args:
            index (int): Index
        Returns:
            tuple: (image, target) where target is index of the target class.
        """
        img, target = self.data[index], self.targets[index]

        # doing this so that it is consistent with all other datasets
        # to return a PIL Image
        img = Image.fromarray(img)

        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target

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

    def _check_integrity(self):
        root = self.root
        for fentry in (self.train_list + self.test_list):
            filename, md5 = fentry[0], fentry[1]
            fpath = os.path.join(root, self.base_folder, filename)
            if not check_integrity(fpath, md5):
                return False
        return True

    def download(self):
        if self._check_integrity():
            print('Files already downloaded and verified')
            return
        download_and_extract_archive(self.url, self.root, filename=self.filename, md5=self.tgz_md5)

    def extra_repr(self):
        return "Split: {}".format("Train" if self.train is True else "Test")

class CIFAR100(CIFAR10):
    """`CIFAR100 <https://www.cs.toronto.edu/~kriz/cifar.html>`_ Dataset.
    This is a subclass of the `CIFAR10` Dataset.
    """
    base_folder = 'cifar-100-python'
    url = "https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz"
    filename = "cifar-100-python.tar.gz"
    tgz_md5 = 'eb9058c3a382ffc7106e4002c42a8d85'
    train_list = [
        ['train', '16019d7e3df5f24257cddd939b257f8d'],
    ]

    test_list = [
        ['test', 'f0ef6b0ae62326f3e7ffdfab6717acfc'],
    ]
    meta = {
        'filename': 'meta',
        'key': 'fine_label_names',
        'md5': '7973b15100ade9c7d40fb424638fde48',
    }

if __name__ == "__main__":
    dataroot = '~/dataloader/data/'
    batch_size_base = 128
    txt_path = "../../data/index_list/cifar100/session_2.txt"
    # class_index = open(txt_path).read().splitlines()
    class_index = np.arange(60)
    trainset = CIFAR100(root=dataroot, train=True, download=True, transform=None, index=class_index,
                        base_sess=True)
    testset = CIFAR100(root=dataroot, train=False, download=False,index=class_index, base_sess=True)
    cls = np.unique(trainset.targets)
    trainloader = torch.utils.data.DataLoader(dataset=trainset, batch_size=batch_size_base, shuffle=True, num_workers=8,
                                              pin_memory=True)
    testloader = torch.utils.data.DataLoader(
        dataset=testset, batch_size=100, shuffle=False, num_workers=8, pin_memory=True)
    print(testloader.dataset.data.shape)

ext: py

max_stars_repo_name: IBM/constrained-FSCIL

max_issues_repo_name: IBM/constrained-FSCIL

max_forks_repo_name: IBM/constrained-FSCIL

reason: import numpy


text:
from __future__ import annotations

__authors__: list[str] = ['Rahul Sawhney', 'Purvit Vashishtha', 'Arun Kothari', 'Vibhu Gupta']

__doc__: str = r'''
    Patent Title: Advanced Nuclei Image Segmentation from State-of-the-Art Computer Vision Models
    Patent Abstract: ...
    patent Results: ...
    Patent Conclusion: ...

'''

import warnings, os, copy, time
from typing import Optional
warnings.filterwarnings('ignore')

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from PIL import Image

import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import transforms, utils
from torchvision.models import resnext50_32x4d

#@: Custom Dataset class
class NucleiDatset(torch.utils.data.Dataset):
    def __init__(self, path: 'dir_path', transform: torchvision.transforms) -> None:
        self.path = path
        self.folders = os.listdir(self.path)
        self.transform = transform

    def __len__(self) -> int:
        return len(self.folders)

    def __getitem__(self, index: int) -> tuple[torch.Tensor, torch.tensor]:
        image_folder = os.path.join(self.path, self.folder[index], 'images/')
        mask_folder = os.path.join(self.path, self.folders[index], 'masks/')
        image_path = os.path.join(image_folder, os.listdir(image_folder)[0])
        mask_path = os.path.join(mask_folder, os.listdir(mask_folder)[0])

        if self.transform is not None:
            image, mask = Image.open(image_path), Image.open(mask_path)
            image, mask = self.transform(image), self.transform(mask)

        return image, masks

#@: -- UNet Architecture --
class Double_Conv(nn.Module):
    def __init__(self, in_channels: int, out_channels: int) -> None:
        super(Double_Conv, self).__init__()
        layers: dict[str, object] = {
            'conv_one': nn.Conv2d(in_channels, out_channels, 3, padding= 1),
            'relu_one': nn.ReLU(inplace= True),

            'conv_two': nn.Conv2d(out_channels, out_channels, 3, padding= 1),
            'relu_two': nn.ReLU(inplace= True)
        }
        self.block = nn.Sequential(*layers.values())

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.block(x)

class UNet(nn.Module):
    def __init__(self, n_classes: int) -> None:
        super(UNet, self).__init__()
        self.conv_down1 = Double_Conv(3, 64)
        self.conv_down2 = Double_Conv(64, 128)
        self.conv_down3 = Double_Conv(128, 256)
        self.conv_down4 = Double_Conv(256, 512)

        self.pool = nn.MaxPool2d(2)
        self.upsample = nn.Upsample(scale_factor= 2, mode= 'bilinear', align_corners= True)

        self.conv_up3 = Double_Conv(256 + 512, 256)
        self.conv_up2 = Double_Conv(128 + 256, 128)
        self.conv_up1 = Double_Conv(128 + 64, 64)

        self.last_conv = nn.Conv2d(64, n_classes, kernel_size= 1)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        conv1 = self.conv_down1(x)
        x = self.pool(conv1)
        conv2 = self.conv_down2(x)
        x = self.pool(conv2)
        conv3 = self.conv_down3(x)
        x = self.pool(conv3)
        x = self.conv_down4(x)
        x = self.upsample(x)

        x = torch.cat([x, conv3], dim= 1)

        x = self.conv_up3(x)
        x = self.upsample(x)
        x = torch.cat([x, conv2], dim= 1)

        x = self.conv_up2(x)
        x = self.upsample(x)
        x = torch.cat([x, conv1], dim= 1)

        x = self.conv_up1(x)

        out = self.last_conv(x)
        out = torch.sigmoid(out)

        return out

#@: -- Feature Pyramid Network --
class ConvRelu_Upsample(nn.Module):
    def __init__(self, in_channels: int, out_channels: int, upsample: Optional[bool] = False) -> None:
        super(ConvRelu_Upsample, self).__init__()
        self.upsample = upsample
        self.upsample_layer = nn.Upsample(scale_factor= 2, mode= 'bilinear', align_corners= True)

        layers: dict[str, object] = {
            'conv_one': nn.Conv2d(in_channels, out_channels, kernel_size= 3, stride= 1, padding= 1, bias= False),
            'grp_norm': nn.GroupNorm(32, out_channels),
            'relu_one': nn.ReLU(inplace= True)
        }

        self.block = nn.Sequential(*layers.values())

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.block(x)
        if self.upsample:
            x = self.upsample_layer(x)
        return x

class Segment_Block(nn.Module):
    def __init__(self, in_channels: int, out_channels: int, n_Upsamples: Optional[int] = 0) -> None:
        super(Segment_Block, self).__init__()
        layers: list[object] = [
            ConvRelu_Upsample(in_channels, out_channels, upsample= bool(n_Upsamples))
        ]
        if n_Upsamples > 1:
            for _ in range(1, n_Upsamples):
                layers.append(ConvRelu_Upsample(out_channels, out_channels, upsample= True))

        self.block = nn.Sequential(*layers)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.block(x)

class Feature_Pyramid_Network(nn.Module):
    def __init__(self, n_classes: Optional[int] = 1, pyramid_channels: Optional[int] = 256,
                                                     segmentation_channels: Optional[int] = 256) -> None:
        super(Feature_Pyramid_Network, self).__init__()

        self.conv_down1 = Double_Conv(3, 63)
        self.conv_down2 = Double_Conv(64, 128)
        self.conv_down3 = Double_Conv(128, 256)
        self.conv_down4 = Double_Conv(256, 512)
        self.conv_down5 = Double_Conv(512, 1024)
        self.pool = nn.MaxPool2d(2)

        self.top_layer = nn.Conv2d(1024, 256, kernel_size= 1, stride= 1, padding= 0)

        self.smooth1 = nn.Conv2d(256, 256, kernel_size= 3, stride= 1, padding= 1)
        self.smooth2 = nn.Conv2d(256, 256, kernel_size= 3, stride= 1, padding= 1)
        self.smooth3 = nn.Conv2d(256, 256, kernel_size= 3, stride= 1, padding= 1)

        self.lateral_1 = nn.Conv2d(512, 256, kernel_size= 1, stride= 1, padding= 0)
        self.lateral_2 = nn.Conv2d(256, 256, kernel_size= 1, stride= 1, padding= 0)
        self.lateral_3 = nn.Conv2d(128, 256, kernel_size= 1, stride= 1, padding= 0)

        self.segment_blocks = nn.ModuleList(
            [Segment_Block(pyramid_channels, segmentation_channels, n_Upsamples= n_Upsamples) for n_Upsamples in [0, 1, 2, 3]]
        )

        self.last_conv = nn.Conv2d(256, n_classes, kernel_size= 1, stride= 1, padding= 0)

    def upsample_add(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
        _, _, H, W = y.size()
        upsample = nn.Upsample(size= (H, W), mode= 'bilinear', align_corners= True)
        return upsample(x) + y

    def upsample(self, x: torch.Tensor, H: int, W: int) -> torch.Tensor:
        sample = nn.Upsample(size= (H, W), mode= 'bilinear', align_corners= True)
        return sample(x)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        c1 = self.pool(self.conv_down1(x))
        c2 = self.pool(self.conv_down1(c1))
        c3 = self.pool(self.conv_down1(c2))
        c4 = self.pool(self.conv_down1(c3))
        c5 = self.pool(self.conv_down1(c4))

        p5 = self.top_layer(c5)
        p4 = self.upsample_add(p5, self.lateral_1(c4))
        p3 = self.upsample_add(p4, self.lateral_2(c3))
        p2 = self.upsample_add(p3, self.lateral_1(c2))

        p4 = self.smooth1(p4)
        p3 = self.smooth2(p3)
        p2 = self.smooth3(p2)

        _, _, H, W = p2.size()
        feature_pyramid: list[object] = [seg_block(p) for seg_block, p in zip(self.segment_blocks, [p2, p3, p4, p5])]

        out = self.upsample(self.last_conv(sum(feature_pyramid)), H= 4 * H, W= 4 * W)
        out = torch.sigmoid(out)
        return out

#@: -- Encoder - Decoder NET --
class ConvRelu(nn.Module):
    def __init__(self, in_channels: int, out_channels: int, kernel: int, padding: int) -> None:
        super(ConvRelu, self).__init__()
        layers: dict[str, object] = {
            'conv_one': nn.Conv2d(in_channels, out_channels, kernel_size= kernel, padding= padding),
            'relu_one': nn.ReLU(inplace= True)
        }
        self.block = nn.Sequential(*layers.values())

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.block(x)

class Decoder_Block(nn.Module):
    def __init__(self, in_channels: int, out_channels: int) -> None:
        super(Decoder_Block, self).__init__()
        layers: dict[str, object] = {
            'conv_Relu_One': ConvRelu(in_channels, in_channels// 4, 1, 0),
            'de_conv': nn.ConvTranspose2d(in_channels// 4, in_channels// 4, kernel_size= 3, stride= 2, padding= 1, output_padding= 0),
            'conv_Relu_Two': ConvRelu(in_channels// 4, out_channels, 1, 0)
        }
        self.block = nn.Sequential(*layers.values())

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.block(x)

class ResNextUNet(nn.Module):
    def __init__(self, n_classes: int) -> None:
        super(ResNextUNet, self).__init__()
        self.base_model: object = resnext50_32x4d(pretrained= True)
        self.base_layers: list[int] = list(self.base_model.children())
        filters: list[int] = [4*64, 4*128, 4*256, 4*512]

        self.encoder_0 = nn.Sequential(*self.base_layers[:3])
        self.encoder_1 = nn.Sequential(*self.base_layers[4])
        self.encoder_2 = nn.Sequential(*self.base_layers[5])
        self.encoder_3 = nn.Sequential(*self.base_layers[6])
        self.encoder_4 = nn.Sequential(*self.base_layers[7])

        self.decoder_4 = Decoder_Block(filters[3], filters[2])
        self.decoder_3 = Decoder_Block(filters[2], filters[1])
        self.decoder_2 = Decoder_Block(filters[1], filters[0])
        self.decoder_1 = Decoder_Block(filters[0], filters[0])

        self.last_conv_0 = ConvRelu(256, 128, 3, 1)
        self.last_conv_1 = nn.Conv2d(128, n_classes, 3, padding= 1)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.encoder_0(x)
        e1 = self.encoder_1(x)
        e2 = self.encoder_2(e1)
        e3 = self.encoder_3(e2)
        e4 = self.encoder_4(e3)

        d4 = self.decoder_4(e4) + e3
        d3 = self.decoder_3(d4) + e2
        d2 = self.decoder_2(d3) + e1
        d1 = self.decoder_1(d2)

        out = self.last_conv_0(d1)
        out = self.last_conv_1(out)
        out = torch.sigmoid(out)

        return out

#@: Segmentation Quality Metrics
class dice_coef_metric(nn.Module):
    def __init__(self) -> None:
        super(dice_coef_metric, self).__init__()

    def forward(self, inputs: np.ndarray, target: np.ndarray) -> float:
        intersection = 2.0 * (target * inputs).sum()
        union = target.sum() + inputs.sum()
        if target.sum() == 0 and inputs.sum() == 0:
            return 1.0

        return intersection/ union

#@: Segmentation Loss
__loss_doc__: str = r'''
    LOSS = BCE - Log(DICE)
    where,
        1) BCE = Binary Cross Entropy Loss Function
        2) DICE = Custom Dice Coeffient Loss

'''

class Custom_Loss(nn.Module):
    def __init__(self) -> None:
        super(Custom_Loss, self).__init__()

    def forward(self, inputs: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
        return self._bce_dice_loss(inputs, target)

    def _dice_coeff_loss(self, inputs: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
        smooth: float = 1.0
        intersection = 2.0 * ((target * inputs).sum()) + smooth
        union = target.sum() + inputs.sum() + smooth
        return 1 - (intersection / union)

    def _bce_dice_loss(self, inputs: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
        dice_score = self._dice_coeff_loss(inputs, target)
        bce_score = nn.BCELoss()
        bce_loss = bce_score(inputs, target)
        return bce_loss + dice_score

#@: Model Adaptor Class
class Model():
    def __init__(self, model_name: str, net: object, train_loader: object,
                                                     test_loader: object,
                                                     criterion: object,
                                                     optimizer: object,
                                                     lr_schedular: bool,
                                                     num_epochs: int,
                                                     device: torch.device) -> None:
        super(Model, self).__init__()
        self.model_name = model_name
        self.net = net
        self.train_loader = train_loader
        self.test_loader = test_loader
        self.criterion = criterion
        self.optimizer = optimizer
        self.scheduler = lr_schedular
        self.num_epochs = num_epochs
        self.device = device

    def _compute_IOU(self) -> float:
        threshold: float = 0.3
        test_loss: int| float = 0

        with torch.no_grad():
            for i_step, (data, target) in enumerate(self.test_loader):
                data = data.to(self.device)
                target = target.to(self.device)

                outputs = self.net(data)

                out_cut = np.copy(outputs.data.cpu().numpy())
                out_cut[np.nonzero( out_cut < threshold )] = 0.0
                out_cut[np.nonzero( out_cut >= threshold )] = 1.0

                pic_loss = dice_coef_metric.forward(out_cut, target.data.cpu().numpy())
                test_loss += pic_loss

        return test_loss / i_step

    def _warmup_lr_schedular(self, warmup_iters: int, warmup_factor: float) -> object:
        def f(x: any) -> float:
            if x >= warmup_iters:
                return 1
            alpha = float(x) / warmup_iters
            return warmup_factor * (1 - alpha) + alpha

        return torch.optim.lr_scheduler.LambdaLR(self.optimizer, f)

    def train_validate(self) -> _text:
        print(self.model_name)
        loss_history, self.train_history, self.test_history = [], [], []

        for epoch in range(self.num_epochs):
            self.net.train()
            losses: list = []
            train_iou: list = []

            if self.scheduler:
                warmup_factor = 1.0/ 100
                warmup_iters = min(100, len(self.test_loader) - 1)
                self.scheduler = self._warmup_lr_schedular(warmup_iters, warmup_factor)

            for i_step, (data, target) in enumerate(self.train_loader):
                data = data.to(self.device)
                target = target.to(self.device)
                outputs = self.net(data)

                out_cut = np.copy(outputs.data.cpu().numpy())
                out_cut[np.nonzero(out_cut < 0.5)] = 0.0
                out_cut[np.nonzero(out_cut >= 0.5)] = 1.0

                train_dice = dice_coef_metric(out_cut, target.data.cpu().numpy())
                loss = self.criterion(outputs, target)

                losses.append(loss.item())
                train_iou.append(train_dice)

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

                if not isinstance(self.scheduler, bool):
                    self.scheduler.step()

                val_mean_iou = self._compute_IOU()

                loss_history.append(np.array(losses).mean())
                self.train_history.append(np.array(train_iou).mean())
                self.test_history.append(val_mean_iou)

                print(f'Epoch : {epoch}')
                print(f'Mean Loss on Train : {np.array(losses).mean()},
                        Mean DICE on Train : {np.array(train_iou).mean()},
                        Mean DICE on Test : {val_mean_iou}'
                )

        return self.train_history, self.test_history, loss_history

    def model_history_plot(self) -> 'plot':
        x = np.arange(self.num_epochs)
        fig = plt.figure(figsize= (10, 6))

        plt.plot(x, self.train_history, label= 'Train Dice')
        plt.plot(x, self.test_history, label= 'Test Dice')

        plt.title(f'{self.model_name}', fontsize= 15)
        plt.legend(fontsize= 12)
        plt.xlabel('Epoch', fontsize= 15)
        plt.ylabel('DICE', fontsize= 15)
        plt.show()

#@: driver Code
if __name__.__contains__('__main__'):
    print('Code Still left...')
    ...


text:
import numpy as np

n1 = 5
n2 = 3
n3 = 2

ar1 = np.linspace(start=0, stop=n1 - 1, num=n1)
ar2 = np.linspace(start=0, stop=n2 - 1, num=n2)
ar3 = np.linspace(start=0, stop=n3 - 1, num=n3)

idx1 = 0
idx2 = 0
idx3 = 0

if idx1 < n1 and idx2 < n2 and idx3 < n3:

    stop_req = False

    while True:
        # Perform operation
        print('p1 = {} ================'.format(ar1[idx1]))

        while True:
            # Perform operation
            print('p2 = {} -------'.format(ar2[idx2]))

            while True:
                # Perform operation
                print('p3 = {}'.format(ar3[idx3]))

                if stop_req:
                    print('Loop 3 stopped')
                    break
                else:
                    idx3 += 1
                    if idx3 == n3:
                        idx3 = 0
                        break

            if stop_req:
                print('Loop 2 stopped')
                break
            else:
                idx2 += 1
                if idx2 == n2:
                    idx2 = 0
                    break

        if stop_req:
            print('Loop 1 stopped')
            break
        else:
            idx1 += 1
            if idx1 == n1:
                idx1 = 0
                break

    if stop_req:
        print('Stop hardware')

# Level template
# while True:
#     # Operation
#     # ========
#
#     # Index incrementation
#     if stop_req:
#         break
#     else:
#         idx += 1
#         if idx == n:
#             idx = 0
#             break


text:
import numpy as np
import random
import pyglet

class Tree():

    def __init__(self):
        self.W = np.array([[[0., 0.], [0., 0.5]],
                          [[0.42, -0.42], [0.42, 0.42]],
                          [[0.42, 0.42], [-0.42, 0.42]]])
        self.B = np.array([[0., 0.],
                          [0., 0.2],
                          [0., 0.2]])
        self.X = np.array([2, 2])

    def update(self):
        i = random.choices(population=[0, 1, 2],
                           weights=[0.05, 0.4, 0.4])[0]
        self.X = np.dot(self.W[i], self.X) + self.B[i]

    def draw(self):
        point = self.X*800
        point = tuple(point.astype(int))
        print(point)
        pyglet.graphics.draw(1, pyglet.gl.GL_POINTS, ('v2i', point),
                             ('c3B', (40, 200, 40)))

class Window(pyglet.window.Window):

    def __init__(self):
        # pyglet window initialization
        super().__init__()
        self.set_size(400, 400)
        pyglet.clock.schedule_interval(self.update, 0.001)
        # initialization
        self.tree = Tree()

    def on_draw(self):
        self.tree.draw()

    def update(self, dt):
        self.tree.update()
        pass

if __name__ == '__main__':
    window = Window()
    pyglet.app.run()


text:
"""
cclib (http://cclib.sf.net) is (c) 2006, the cclib development team
and licensed under the LGPL (http://www.gnu.org/copyleft/lgpl.html).
"""

__revision__ = "$Revision: 892 $"

import re

import numpy

import logfileparser
import utils

class GAMESS(logfileparser.Logfile):
    """A GAMESS log file."""
    SCFRMS, SCFMAX, SCFENERGY = range(3) # Used to index self.scftargets[]
    def __init__(self, *args, **kwargs):

        # Call the __init__ method of the superclass
        super(GAMESS, self).__init__(logname="GAMESS", *args, **kwargs)

    def __str__(self):
        """Return a string representation of the object."""
        return "GAMESS log file %s" % (self.filename)

    def __repr__(self):
        """Return a representation of the object."""
        return 'GAMESS("%s")' % (self.filename)

    def normalisesym(self, label):
        """Normalise the symmetries used by GAMESS.

        To normalise, two rules need to be applied:
        (1) Occurences of U/G in the 2/3 position of the label
            must be lower-cased
        (2) Two single quotation marks must be replaced by a double

        >>> t = GAMESS("dummyfile").normalisesym
        >>> labels = ['A', 'A1', 'A1G', "A'", "A''", "AG"]
        >>> answers = map(t, labels)
        >>> print answers
        ['A', 'A1', 'A1g', "A'", 'A"', 'Ag']
        """
        if label[1:] == "''":
            end = '"'
        else:
            end = label[1:].replace("U", "u").replace("G", "g")
        return label[0] + end

    def before_parsing(self):

        self.firststdorient = True # Used to decide whether to wipe the atomcoords clean
        self.geooptfinished = False # Used to avoid extracting the final geometry twice
        self.cihamtyp = "none" # Type of CI Hamiltonian: saps or dets.
        self.scftype = "none" # Type of SCF calculation: BLYP, RHF, ROHF, etc.

    def extract(self, inputfile, line):
        """Extract information from the file object inputfile."""

        if line [1:12] == "INPUT CARD>":
            return

        # We are looking for this line:
        #           PARAMETERS CONTROLLING GEOMETRY SEARCH ARE
        #           ...
        #           OPTTOL = 1.000E-04          RMIN   = 1.500E-03
        if line[10:18] == "OPTTOL =":
            if not hasattr(self, "geotargets"):
                opttol = float(line.split()[2])
                self.geotargets = numpy.array([opttol, 3. / opttol], "d")

        if line.find("FINAL") == 1:
            if not hasattr(self, "scfenergies"):
                self.scfenergies = []
        # Has to deal with such lines as:
        #  FINAL R-B3LYP ENERGY IS     -382.0507446475 AFTER  10 ITERATIONS
        #  FINAL ENERGY IS     -379.7594673378 AFTER   9 ITERATIONS
        # ...so take the number after the "IS"
            temp = line.split()
            self.scfenergies.append(utils.convertor(float(temp[temp.index("IS") + 1]), "hartree", "eV"))

        # Total energies after Moller-Plesset corrections
        if (line.find("RESULTS OF MOLLER-PLESSET") >= 0 or
            line[6:37] == "SCHWARZ INEQUALITY TEST SKIPPED"):
            # Output looks something like this:
            # RESULTS OF MOLLER-PLESSET 2ND ORDER CORRECTION ARE
            #         E(0)=      -285.7568061536
            #         E(1)=         0.0
            #         E(2)=        -0.9679419329
            #       E(MP2)=      -286.7247480864
            # where E(MP2) = E(0) + E(2)
            #
            # with GAMESS-US 12 Jan 2009 (R3) the preceding text is different:
            ##      DIRECT 4-INDEX TRANSFORMATION
            ##      SCHWARZ INEQUALITY TEST SKIPPED          0 INTEGRAL BLOCKS
            ##                     E(SCF)=       -76.0088477471
            ##                       E(2)=        -0.1403745370
            ##                     E(MP2)=       -76.1492222841
            if not hasattr(self, "mpenergies"):
                self.mpenergies = []
            # Each iteration has a new print-out
            self.mpenergies.append([])
            # GAMESS-US presently supports only second order corrections (MP2)
            # PC GAMESS also has higher levels (3rd and 4th), with different output
            # Only the highest level MP4 energy is gathered (SDQ or SDTQ)
            while re.search("DONE WITH MP(\d) ENERGY", line) is None:
                line = inputfile.next()
                if len(line.split()) > 0:
                    # Only up to MP2 correction
                    if line.split()[0] == "E(MP2)=":
                        mp2energy = float(line.split()[1])
                        self.mpenergies[-1].append(utils.convertor(mp2energy, "hartree", "eV"))
                    # MP2 before higher order calculations
                    if line.split()[0] == "E(MP2)":
                        mp2energy = float(line.split()[2])
                        self.mpenergies[-1].append(utils.convertor(mp2energy, "hartree", "eV"))
                    if line.split()[0] == "E(MP3)":
                        mp3energy = float(line.split()[2])
                        self.mpenergies[-1].append(utils.convertor(mp3energy, "hartree", "eV"))
                    if line.split()[0] in ["E(MP4-SDQ)", "E(MP4-SDTQ)"]:
                        mp4energy = float(line.split()[2])
                        self.mpenergies[-1].append(utils.convertor(mp4energy, "hartree", "eV"))

        # Total energies after Coupled Cluster calculations
        # Only the highest Coupled Cluster level result is gathered
        if line[12:23] == "CCD ENERGY:":
            if not hasattr(self, "ccenergies"):
                self.ccenergies = []
            ccenergy = float(line.split()[2])
            self.ccenergies.append(utils.convertor(ccenergy, "hartree", "eV"))
        if line.find("CCSD") >= 0 and line.split()[0:2] == ["CCSD", "ENERGY:"]:
            if not hasattr(self, "ccenergies"):
                self.ccenergies = []
            ccenergy = float(line.split()[2])
            line = inputfile.next()
            if line[8:23] == "CCSD[T] ENERGY:":
                ccenergy = float(line.split()[2])
                line = inputfile.next()
                if line[8:23] == "CCSD(T) ENERGY:":
                    ccenergy = float(line.split()[2])
            self.ccenergies.append(utils.convertor(ccenergy, "hartree", "eV"))
        # Also collect MP2 energies, which are always calculated before CC
        if line [8:23] == "MBPT(2) ENERGY:":
            if not hasattr(self, "mpenergies"):
                self.mpenergies = []
            self.mpenergies.append([])
            mp2energy = float(line.split()[2])
            self.mpenergies[-1].append(utils.convertor(mp2energy, "hartree", "eV"))

        # Extract charge and multiplicity
        if line[1:19] == "CHARGE OF MOLECULE":
            self.charge = int(line.split()[-1])
            self.mult = int(inputfile.next().split()[-1])

        # etenergies (used only for CIS runs now)
        if "EXCITATION ENERGIES" in line and line.find("DONE WITH") < 0:
            if not hasattr(self, "etenergies"):
                self.etenergies = []
            header = inputfile.next().rstrip()
            get_etosc = False
            if header.endswith("OSC. STR."):
                # water_cis_dets.out does not have the oscillator strength
                # in this table...it is extracted from a different section below
                get_etosc = True
                self.etoscs = []
            dashes = inputfile.next()
            line = inputfile.next()
            broken = line.split()
            while len(broken) > 0:
                # Take hartree value with more numbers, and convert.
                # Note that the values listed after this are also less exact!
                etenergy = float(broken[1])
                self.etenergies.append(utils.convertor(etenergy, "hartree", "cm-1"))
                if get_etosc:
                    etosc = float(broken[-1])
                    self.etoscs.append(etosc)
                broken = inputfile.next().split()

        # Detect the CI hamiltonian type, if applicable.
        # Should always be detected if CIS is done.
        if line[8:64] == "RESULTS FROM SPIN-ADAPTED ANTISYMMETRIZED PRODUCT (SAPS)":
            self.cihamtyp = "saps"
        if line[8:64] == "RESULTS FROM DETERMINANT BASED ATOMIC ORBITAL CI-SINGLES":
            self.cihamtyp = "dets"

        # etsecs (used only for CIS runs for now)
        if line[1:14] == "EXCITED STATE":
            if not hasattr(self, 'etsecs'):
                self.etsecs = []
            if not hasattr(self, 'etsyms'):
                self.etsyms = []
            statenumber = int(line.split()[2])
            spin = int(float(line.split()[7]))
            if spin == 0:
                sym = "Singlet"
            if spin == 1:
                sym = "Triplet"
            sym += '-' + line.split()[-1]
            self.etsyms.append(sym)
            # skip 5 lines
            for i in range(5):
                line = inputfile.next()
            line = inputfile.next()
            CIScontribs = []
            while line.strip()[0] != "-":
                MOtype = 0
                # alpha/beta are specified for hamtyp=dets
                if self.cihamtyp == "dets":
                    if line.split()[0] == "BETA":
                        MOtype = 1
                fromMO = int(line.split()[-3])-1
                toMO = int(line.split()[-2])-1
                coeff = float(line.split()[-1])
                # With the SAPS hamiltonian, the coefficients are multiplied
                #   by sqrt(2) so that they normalize to 1.
                # With DETS, both alpha and beta excitations are printed.
                # if self.cihamtyp == "saps":
                #    coeff /= numpy.sqrt(2.0)
                CIScontribs.append([(fromMO,MOtype),(toMO,MOtype),coeff])
                line = inputfile.next()
            self.etsecs.append(CIScontribs)

        # etoscs (used only for CIS runs now)
        if line[1:50] == "TRANSITION FROM THE GROUND STATE TO EXCITED STATE":
            if not hasattr(self, "etoscs"):
                self.etoscs = []
            statenumber = int(line.split()[-1])
            # skip 7 lines
            for i in range(8):
                line = inputfile.next()
            strength = float(line.split()[3])
            self.etoscs.append(strength)

        # TD-DFT for GAMESS-US
        if line[14:29] == "LET EXCITATIONS": # TRIPLET and SINGLET
            self.etenergies = []
            self.etoscs = []
            self.etsecs = []
            etsyms = []
            minus = inputfile.next()
            blank = inputfile.next()
            line = inputfile.next()
            # Loop starts on the STATE line
            while line.find("STATE") >= 0:
                broken = line.split()
                self.etenergies.append(utils.convertor(float(broken[-2]), "eV", "cm-1"))
                broken = inputfile.next().split()
                self.etoscs.append(float(broken[-1]))
                sym = inputfile.next() # Not always present
                if sym.find("SYMMETRY")>=0:
                    etsyms.append(sym.split()[-1])
                    header = inputfile.next()
                minus = inputfile.next()
                CIScontribs = []
                line = inputfile.next()
                while line.strip():
                    broken = line.split()
                    fromMO, toMO = [int(broken[x]) - 1 for x in [2, 4]]
                    CIScontribs.append([(fromMO, 0), (toMO, 0), float(broken[1])])
                    line = inputfile.next()
                self.etsecs.append(CIScontribs)
                line = inputfile.next()
            if etsyms: # Not always present
                self.etsyms = etsyms

        # Maximum and RMS gradients.
        if "MAXIMUM GRADIENT" in line or "RMS GRADIENT" in line:

            if not hasattr(self, "geovalues"):
                self.geovalues = []

            parts = line.split()

            # Newer versions (around 2006) have both maximum and RMS on one line:
            #       MAXIMUM GRADIENT =  0.0531540    RMS GRADIENT = 0.0189223
            if len(parts) == 8:
                maximum = float(parts[3])
                rms = float(parts[7])

            # In older versions of GAMESS, this spanned two lines, like this:
            #       MAXIMUM GRADIENT =    0.057578167
            #           RMS GRADIENT =    0.027589766
            if len(parts) == 4:
                maximum = float(parts[3])
                line = inputfile.next()
                parts = line.split()
                rms = float(parts[3])

            # FMO also prints two final one- and two-body gradients (see exam37):
            #   (1) MAXIMUM GRADIENT =  0.0531540    RMS GRADIENT = 0.0189223
            if len(parts) == 9:
                maximum = float(parts[4])
                rms = float(parts[8])

            self.geovalues.append([maximum, rms])

        if line[11:50] == "ATOMIC                      COORDINATES":
            # This is the input orientation, which is the only data available for
            # SP calcs, but which should be overwritten by the standard orientation
            # values, which is the only information available for all geoopt cycles.
            if not hasattr(self, "atomcoords"):
                self.atomcoords = []
                self.atomnos = []
            line = inputfile.next()
            atomcoords = []
            atomnos = []
            line = inputfile.next()
            while line.strip():
                temp = line.strip().split()
                atomcoords.append([utils.convertor(float(x), "bohr", "Angstrom") for x in temp[2:5]])
                atomnos.append(int(round(float(temp[1])))) # Don't use the atom name as this is arbitary
                line = inputfile.next()
            self.atomnos = numpy.array(atomnos, "i")
            self.atomcoords.append(atomcoords)

        if line[12:40] == "EQUILIBRIUM GEOMETRY LOCATED":
            # Prevent extraction of the final geometry twice
            self.geooptfinished = True

        if line[1:29] == "COORDINATES OF ALL ATOMS ARE" and not self.geooptfinished:
            # This is the standard orientation, which is the only coordinate
            # information available for all geometry optimisation cycles.
            # The input orientation will be overwritten if this is a geometry optimisation
            # We assume that a previous Input Orientation has been found and
            # used to extract the atomnos
            if self.firststdorient:
                self.firststdorient = False
                # Wipes out the single input coordinate at the start of the file
                self.atomcoords = []

            line = inputfile.next()
            hyphens = inputfile.next()

            atomcoords = []
            line = inputfile.next()

            for i in range(self.natom):
                temp = line.strip().split()
                atomcoords.append(map(float, temp[2:5]))
                line = inputfile.next()
            self.atomcoords.append(atomcoords)

        # Section with SCF information.
        #
        # The space at the start of the search string is to differentiate from MCSCF.
        # Everything before the search string is stored as the type of SCF.
        # SCF types may include: BLYP, RHF, ROHF, UHF, etc.
        #
        # For example, in exam17 the section looks like this (note that this is GVB):
        #          ------------------------
        #          ROHF-GVB SCF CALCULATION
        #          ------------------------
        # GVB STEP WILL USE    119875 WORDS OF MEMORY.
        #
        #     MAXIT=  30   NPUNCH= 2   SQCDF TOL=1.0000E-05
        #     NUCLEAR ENERGY=        6.1597411978
        #     EXTRAP=T   DAMP=F   SHIFT=F   RSTRCT=F   DIIS=F  SOSCF=F
        #
        # ITER EX     TOTAL ENERGY       E CHANGE        SQCDF       DIIS ERROR
        #   0  0      -38.298939963   -38.298939963   0.131784454   0.000000000
        #   1  1      -38.332044339    -0.033104376   0.026019716   0.000000000
        # ... and will be terminated by a blank line.
        if line.rstrip()[-16:] == " SCF CALCULATION":

            # Remember the type of SCF.
            self.scftype = line.strip()[:-16]

            dashes = inputfile.next()

            while line [:5] != " ITER":

                # GVB uses SQCDF for checking convergence (for example in exam17).
                if "GVB" in self.scftype and "SQCDF TOL=" in line:
                    scftarget = float(line.split("=")[-1])

                # Normally however the density is used as the convergence criterium.
                # Deal with various versions:
                #   (GAMESS VERSION = 12 DEC 2003)
                #     DENSITY MATRIX CONV=  2.00E-05  DFT GRID SWITCH THRESHOLD=  3.00E-04
                #   (GAMESS VERSION = 22 FEB 2006)
                #     DENSITY MATRIX CONV=  1.00E-05
                #   (PC GAMESS version 6.2, Not DFT?)
                #     DENSITY CONV=  1.00E-05
                elif "DENSITY CONV" in line or "DENSITY MATRIX CONV" in line:
                    scftarget = float(line.split()[-1])

                line = inputfile.next()

            if not hasattr(self, "scftargets"):
                self.scftargets = []

            self.scftargets.append([scftarget])

            if not hasattr(self,"scfvalues"):
                self.scfvalues = []

            line = inputfile.next()

            # Normally the iteration print in 6 columns.
            # For ROHF, however, it is 5 columns, thus this extra parameter.
            if "ROHF" in self.scftype:
                valcol = 4
            else:
                valcol = 5

            # SCF iterations are terminated by a blank line.
            # The first four characters usually contains the step number.
            # However, lines can also contain messages, including:
            #   * * *   INITIATING DIIS PROCEDURE   * * *
            #   CONVERGED TO SWOFF, SO DFT CALCULATION IS NOW SWITCHED ON
            #   DFT CODE IS SWITCHING BACK TO THE FINER GRID
            values = []
            while line.strip():
                try:
                    temp = int(line[0:4])
                except ValueError:
                    pass
                else:
                    values.append([float(line.split()[valcol])])
                line = inputfile.next()
            self.scfvalues.append(values)

        if line.find("NORMAL COORDINATE ANALYSIS IN THE HARMONIC APPROXIMATION") >= 0:
        # GAMESS has...
        # MODES 1 TO 6 ARE TAKEN AS ROTATIONS AND TRANSLATIONS.
        #
        #     FREQUENCIES IN CM**-1, IR INTENSITIES IN DEBYE**2/AMU-ANGSTROM**2,
        #     REDUCED MASSES IN AMU.
        #
        #                          1           2           3           4           5
        #       FREQUENCY:        52.49       41.45       17.61        9.23       10.61
        #    REDUCED MASS:      3.92418     3.77048     5.43419     6.44636     5.50693
        #    IR INTENSITY:      0.00013     0.00001     0.00004     0.00000     0.00003

        # ...or in the case of a numerical Hessian job...

        # MODES 1 TO 5 ARE TAKEN AS ROTATIONS AND TRANSLATIONS.
        #
        #     FREQUENCIES IN CM**-1, IR INTENSITIES IN DEBYE**2/AMU-ANGSTROM**2,
        #     REDUCED MASSES IN AMU.
        #
        #                          1           2           3           4           5
        #       FREQUENCY:         0.05        0.03        0.03       30.89       30.94
        #    REDUCED MASS:      8.50125     8.50137     8.50136     1.06709     1.06709

        # whereas PC-GAMESS has...
        # MODES 1 TO 6 ARE TAKEN AS ROTATIONS AND TRANSLATIONS.
        #
        #     FREQUENCIES IN CM**-1, IR INTENSITIES IN DEBYE**2/AMU-ANGSTROM**2
        #
        #                          1           2           3           4           5
        #       FREQUENCY:         5.89        1.46        0.01        0.01        0.01
        #    IR INTENSITY:      0.00000     0.00000     0.00000     0.00000     0.00000

        # If Raman is present we have (for PC-GAMESS)...
        # MODES 1 TO 6 ARE TAKEN AS ROTATIONS AND TRANSLATIONS.
        #
        #     FREQUENCIES IN CM**-1, IR INTENSITIES IN DEBYE**2/AMU-ANGSTROM**2
        #     RAMAN INTENSITIES IN ANGSTROM**4/AMU, DEPOLARIZATIONS ARE DIMENSIONLESS
        #
        #                          1           2           3           4           5
        #       FREQUENCY:         5.89        1.46        0.04        0.03        0.01
        #    IR INTENSITY:      0.00000     0.00000     0.00000     0.00000     0.00000
        # RAMAN INTENSITY:       12.675       1.828       0.000       0.000       0.000
        #  DEPOLARIZATION:        0.750       0.750       0.124       0.009       0.750

        # If PC-GAMESS has not reached the stationary point we have
        # MODES 1 TO 5 ARE TAKEN AS ROTATIONS AND TRANSLATIONS.
        #
        #     FREQUENCIES IN CM**-1, IR INTENSITIES IN DEBYE**2/AMU-ANGSTROM**2
        #
        #     *******************************************************
        #     * THIS IS NOT A STATIONARY POINT ON THE MOLECULAR PES *
        #     *     THE VIBRATIONAL ANALYSIS IS NOT VALID !!!       *
        #     *******************************************************
        #
        #                          1           2           3           4           5

        # MODES 2 TO 7 ARE TAKEN AS ROTATIONS AND TRANSLATIONS.

            self.vibfreqs = []
            self.vibirs = []
            self.vibdisps = []

            # Need to get to the modes line
            warning = False
            while line.find("MODES") == -1:
                line = inputfile.next()
                if line.find("THIS IS NOT A STATIONARY POINT")>=0:
                    warning = True
            startrot = int(line.split()[1])
            endrot = int(line.split()[3])
            blank = inputfile.next()

            line = inputfile.next() # FREQUENCIES, etc.
            while line != blank:
                line = inputfile.next()
            if warning: # Get past the second warning
                line = inputfile.next()
                while line!= blank:
                    line = inputfile.next()
                self.logger.warning("This is not a stationary point on the molecular"
                                    "PES. The vibrational analysis is not valid.")

            freqNo = inputfile.next()
            while freqNo.find("SAYVETZ") == -1:
                freq = inputfile.next().strip().split()[1:]
            # May include imaginary frequencies
            #       FREQUENCY:       825.18 I    111.53       12.62       10.70        0.89
                newfreq = []
                for i, x in enumerate(freq):
                    if x!="I":
                        newfreq.append(float(x))
                    else:
                        newfreq[-1] = -newfreq[-1]
                self.vibfreqs.extend(newfreq)
                line = inputfile.next()
                if line.find("REDUCED") >= 0: # skip the reduced mass (not always present)
                    line = inputfile.next()
                if line.find("IR INTENSITY") >= 0:
                    # Not present if a numerical Hessian calculation
                    irIntensity = map(float, line.strip().split()[2:])
                    self.vibirs.extend([utils.convertor(x, "Debye^2/amu-Angstrom^2", "km/mol") for x in irIntensity])
                    line = inputfile.next()
                if line.find("RAMAN") >= 0:
                    if not hasattr(self,"vibramans"):
                        self.vibramans = []
                    ramanIntensity = line.strip().split()
                    self.vibramans.extend(map(float, ramanIntensity[2:]))
                    depolar = inputfile.next()
                    line = inputfile.next()
                assert line == blank

                # Extract the Cartesian displacement vectors
                p = [ [], [], [], [], [] ]
                for j in range(len(self.atomnos)):
                    q = [ [], [], [], [], [] ]
                    for k in range(3): # x, y, z
                        line = inputfile.next()[21:]
                        broken = map(float, line.split())
                        for l in range(len(broken)):
                            q[l].append(broken[l])
                    for k in range(len(broken)):
                        p[k].append(q[k])
                self.vibdisps.extend(p[:len(broken)])

                # Skip the Sayvetz stuff at the end
                for j in range(10):
                    line = inputfile.next()
                blank = inputfile.next()
                freqNo = inputfile.next()
            # Exclude rotations and translations
            self.vibfreqs = numpy.array(self.vibfreqs[:startrot-1]+self.vibfreqs[endrot:], "d")
            self.vibirs = numpy.array(self.vibirs[:startrot-1]+self.vibirs[endrot:], "d")
            self.vibdisps = numpy.array(self.vibdisps[:startrot-1]+self.vibdisps[endrot:], "d")
            if hasattr(self, "vibramans"):
                self.vibramans = numpy.array(self.vibramans[:startrot-1]+self.vibramans[endrot:], "d")

        if line[5:21] == "ATOMIC BASIS SET":
            self.gbasis = []
            line = inputfile.next()
            while line.find("SHELL")<0:
                line = inputfile.next()
            blank = inputfile.next()
            atomname = inputfile.next()
            # shellcounter stores the shell no of the last shell
            # in the previous set of primitives
            shellcounter = 1
            while line.find("TOTAL NUMBER")<0:
                blank = inputfile.next()
                line = inputfile.next()
                shellno = int(line.split()[0])
                shellgap = shellno - shellcounter
                gbasis = [] # Stores basis sets on one atom
                shellsize = 0
                while len(line.split())!=1 and line.find("TOTAL NUMBER")<0:
                    shellsize += 1
                    coeff = {}
                    # coefficients and symmetries for a block of rows
                    while line.strip():
                        temp = line.strip().split()
                        sym = temp[1]
                        assert sym in ['S', 'P', 'D', 'F', 'G', 'L']
                        if sym == "L": # L refers to SP
                            if len(temp)==6: # GAMESS US
                                coeff.setdefault("S", []).append( (float(temp[3]), float(temp[4])) )
                                coeff.setdefault("P", []).append( (float(temp[3]), float(temp[5])) )
                            else: # PC GAMESS
                                assert temp[6][-1] == temp[9][-1] == ')'
                                coeff.setdefault("S", []).append( (float(temp[3]), float(temp[6][:-1])) )
                                coeff.setdefault("P", []).append( (float(temp[3]), float(temp[9][:-1])) )
                        else:
                            if len(temp)==5: # GAMESS US
                                coeff.setdefault(sym, []).append( (float(temp[3]), float(temp[4])) )
                            else: # PC GAMESS
                                assert temp[6][-1] == ')'
                                coeff.setdefault(sym, []).append( (float(temp[3]), float(temp[6][:-1])) )
                        line = inputfile.next()
                    # either a blank or a continuation of the block
                    if sym == "L":
                        gbasis.append( ('S', coeff['S']))
                        gbasis.append( ('P', coeff['P']))
                    else:
                        gbasis.append( (sym, coeff[sym]))
                    line = inputfile.next()
                # either the start of the next block or the start of a new atom or
                # the end of the basis function section

                numtoadd = 1 + (shellgap / shellsize)
                shellcounter = shellno + shellsize
                for x in range(numtoadd):
                    self.gbasis.append(gbasis)

        if line.find("EIGENVECTORS") == 10 or line.find("MOLECULAR OBRITALS") == 10:
            # The details returned come from the *final* report of evalues and
            #   the last list of symmetries in the log file.
            # Should be followed by lines like this:
            #           ------------
            #           EIGENVECTORS
            #           ------------
            #
            #                       1          2          3          4          5
            #                   -10.0162   -10.0161   -10.0039   -10.0039   -10.0029
            #                      BU         AG         BU         AG         AG
            #     1  C  1  S    0.699293   0.699290  -0.027566   0.027799   0.002412
            #     2  C  1  S    0.031569   0.031361   0.004097  -0.004054  -0.000605
            #     3  C  1  X    0.000908   0.000632  -0.004163   0.004132   0.000619
            #     4  C  1  Y   -0.000019   0.000033   0.000668  -0.000651   0.005256
            #     5  C  1  Z    0.000000   0.000000   0.000000   0.000000   0.000000
            #     6  C  2  S   -0.699293   0.699290   0.027566   0.027799   0.002412
            #     7  C  2  S   -0.031569   0.031361  -0.004097  -0.004054  -0.000605
            #     8  C  2  X    0.000908  -0.000632  -0.004163  -0.004132  -0.000619
            #     9  C  2  Y   -0.000019  -0.000033   0.000668   0.000651  -0.005256
            #    10  C  2  Z    0.000000   0.000000   0.000000   0.000000   0.000000
            #    11  C  3  S   -0.018967  -0.019439   0.011799  -0.014884  -0.452328
            #    12  C  3  S   -0.007748  -0.006932   0.000680  -0.000695  -0.024917
            #    13  C  3  X    0.002628   0.002997   0.000018   0.000061  -0.003608
            # and so forth... with blanks lines between blocks of 5 orbitals each.
            # Warning! There are subtle differences between GAMESS-US and PC-GAMES
            #   in the formatting of the first four columns.
            #
            # Watch out for F orbitals...
            # PC GAMESS
            #   19  C   1 YZ   0.000000   0.000000   0.000000   0.000000   0.000000
            #   20  C    XXX   0.000000   0.000000   0.000000   0.000000   0.002249
            #   21  C    YYY   0.000000   0.000000  -0.025555   0.000000   0.000000
            #   22  C    ZZZ   0.000000   0.000000   0.000000   0.002249   0.000000
            #   23  C    XXY   0.000000   0.000000   0.001343   0.000000   0.000000
            # GAMESS US
            #   55  C  1 XYZ   0.000000   0.000000   0.000000   0.000000   0.000000
            #   56  C  1XXXX  -0.000014  -0.000067   0.000000   0.000000   0.000000
            #
            # This is fine for GeoOpt and SP, but may be weird for TD and Freq.

            # This is the stuff that we can read from these blocks.
            self.moenergies = [[]]
            self.mosyms = [[]]
            if not hasattr(self, "nmo"):
                self.nmo = self.nbasis
            self.mocoeffs = [numpy.zeros((self.nmo, self.nbasis), "d")]
            readatombasis = False
            if not hasattr(self, "atombasis"):
                self.atombasis = []
                self.aonames = []
                for i in range(self.natom):
                    self.atombasis.append([])
                self.aonames = []
                readatombasis = True

            dashes = inputfile.next()
            for base in range(0, self.nmo, 5):

                line = inputfile.next()
                # Make sure that this section does not end prematurely - checked by regression test 2CO.ccsd.aug-cc-pVDZ.out.
                if line.strip() != "":
                    break;

                numbers = inputfile.next() # Eigenvector numbers.

                # Sometimes there are some blank lines here.
                while not line.strip():
                    line = inputfile.next()

                # Eigenvalues for these orbitals (in hartrees).
                try:
                    self.moenergies[0].extend([utils.convertor(float(x), "hartree", "eV") for x in line.split()])
                except:
                    self.logger.warning('MO section found but could not be parsed!')
                    break;

                # Orbital symmetries.
                line = inputfile.next()
                if line.strip():
                    self.mosyms[0].extend(map(self.normalisesym, line.split()))

                # Now we have nbasis lines.
                # Going to use the same method as for normalise_aonames()
                # to extract basis set information.
                p = re.compile("(\d+)\s*([A-Z][A-Z]?)\s*(\d+)\s*([A-Z]+)")
                oldatom ='0'
                for i in range(self.nbasis):
                    line = inputfile.next()

                    # If line is empty, break (ex. for FMO in exam37).
                    if not line.strip(): break

                    # Fill atombasis and aonames only first time around
                    if readatombasis and base == 0:
                        aonames = []
                        start = line[:17].strip()
                        m = p.search(start)
                        if m:
                            g = m.groups()
                            aoname = "%s%s_%s" % (g[1].capitalize(), g[2], g[3])
                            oldatom = g[2]
                            atomno = int(g[2])-1
                            orbno = int(g[0])-1
                        else: # For F orbitals, as shown above
                            g = [x.strip() for x in line.split()]
                            aoname = "%s%s_%s" % (g[1].capitalize(), oldatom, g[2])
                            atomno = int(oldatom)-1
                            orbno = int(g[0])-1
                        self.atombasis[atomno].append(orbno)
                        self.aonames.append(aoname)
                    coeffs = line[15:] # Strip off the crud at the start.
                    j = 0
                    while j*11+4 < len(coeffs):
                        self.mocoeffs[0][base+j, i] = float(coeffs[j * 11:(j + 1) * 11])
                        j += 1

            line = inputfile.next()
            # If it's restricted and no more properties:
            #  ...... END OF RHF/DFT CALCULATION ......
            # If there are more properties (DENSITY MATRIX):
            #               --------------
            #
            # If it's unrestricted we have:
            #
            #  ----- BETA SET -----
            #
            #          ------------
            #          EIGENVECTORS
            #          ------------
            #
            #                      1          2          3          4          5
            # ... and so forth.
            line = inputfile.next()
            if line[2:22] == "----- BETA SET -----":
                self.mocoeffs.append(numpy.zeros((self.nmo, self.nbasis), "d"))
                self.moenergies.append([])
                self.mosyms.append([])
                for i in range(4):
                    line = inputfile.next()
                for base in range(0, self.nmo, 5):
                    blank = inputfile.next()
                    line = inputfile.next() # Eigenvector no
                    line = inputfile.next()
                    self.moenergies[1].extend([utils.convertor(float(x), "hartree", "eV") for x in line.split()])
                    line = inputfile.next()
                    self.mosyms[1].extend(map(self.normalisesym, line.split()))
                    for i in range(self.nbasis):
                        line = inputfile.next()
                        temp = line[15:] # Strip off the crud at the start
                        j = 0
                        while j * 11 + 4 < len(temp):
                            self.mocoeffs[1][base+j, i] = float(temp[j * 11:(j + 1) * 11])
                            j += 1
                line = inputfile.next()
            self.moenergies = [numpy.array(x, "d") for x in self.moenergies]

        # Natural orbitals - presently support only CIS.
        # Looks basically the same as eigenvectors, without symmetry labels.
        if line[10:30] == "CIS NATURAL ORBITALS":

            self.nocoeffs = numpy.zeros((self.nmo, self.nbasis), "d")

            dashes = inputfile.next()
            for base in range(0, self.nmo, 5):

                blank = inputfile.next()
                numbers = inputfile.next() # Eigenvector numbers.

                # Eigenvalues for these natural orbitals (not in hartrees!).
                # Sometimes there are some blank lines before it.
                line = inputfile.next()
                while not line.strip():
                    line = inputfile.next()
                eigenvalues = line

                # Orbital symemtry labels are normally here for MO coefficients.
                line = inputfile.next()

                # Now we have nbasis lines with the coefficients.
                for i in range(self.nbasis):

                    line = inputfile.next()
                    coeffs = line[15:]
                    j = 0
                    while j*11+4 < len(coeffs):
                        self.nocoeffs[base+j, i] = float(coeffs[j * 11:(j + 1) * 11])
                        j += 1

        # We cannot trust this self.homos until we come to the phrase:
        #   SYMMETRIES FOR INITAL GUESS ORBITALS FOLLOW
        # which either is followed by "ALPHA" or "BOTH" at which point we can say
        # for certain that it is an un/restricted calculations.
        # Note that MCSCF calcs also print this search string, so make sure
        #   that self.homos does not exist yet.
        if line[1:28] == "NUMBER OF OCCUPIED ORBITALS" and not hasattr(self,'homos'):
            homos = [int(line.split()[-1])-1]
            line = inputfile.next()
            homos.append(int(line.split()[-1])-1)
            self.homos = numpy.array(homos, "i")

        if line.find("SYMMETRIES FOR INITIAL GUESS ORBITALS FOLLOW") >= 0:
            # Not unrestricted, so lop off the second index.
            # In case the search string above was not used (ex. FMO in exam38),
            #   we can try to use the next line which should also contain the
            #   number of occupied orbitals.
            if line.find("BOTH SET(S)") >= 0:
                nextline = inputfile.next()
                if "ORBITALS ARE OCCUPIED" in nextline:
                    homos = int(nextline.split()[0])-1
                    if hasattr(self,"homos"):
                        try:
                            assert self.homos[0] == homos
                        except AssertionError:
                            self.logger.warning("Number of occupied orbitals not consistent. This is normal for ECP and FMO jobs.")
                    else:
                        self.homos = [homos]
                self.homos = numpy.resize(self.homos, [1])

        # Set the total number of atoms, only once.
        # Normally GAMESS print TOTAL NUMBER OF ATOMS, however in some cases
        #   this is slightly different (ex. lower case for FMO in exam37).
        if not hasattr(self,"natom") and "NUMBER OF ATOMS" in line.upper():
            self.natom = int(line.split()[-1])

        if line.find("NUMBER OF CARTESIAN GAUSSIAN BASIS") == 1 or line.find("TOTAL NUMBER OF BASIS FUNCTIONS") == 1:
            # The first is from Julien's Example and the second is from Alexander's
            # I think it happens if you use a polar basis function instead of a cartesian one
            self.nbasis = int(line.strip().split()[-1])

        elif line.find("SPHERICAL HARMONICS KEPT IN THE VARIATION SPACE") >= 0:
            # Note that this line is present if ISPHER=1, e.g. for C_bigbasis
            self.nmo = int(line.strip().split()[-1])

        elif line.find("TOTAL NUMBER OF MOS IN VARIATION SPACE") == 1:
            # Note that this line is not always present, so by default
            # NBsUse is set equal to NBasis (see below).
            self.nmo = int(line.split()[-1])

        elif line.find("OVERLAP MATRIX") == 0 or line.find("OVERLAP MATRIX") == 1:
            # The first is for PC-GAMESS, the second for GAMESS
            # Read 1-electron overlap matrix
            if not hasattr(self, "aooverlaps"):
                self.aooverlaps = numpy.zeros((self.nbasis, self.nbasis), "d")
            else:
                self.logger.info("Reading additional aooverlaps...")
            base = 0
            while base < self.nbasis:
                blank = inputfile.next()
                line = inputfile.next() # Basis fn number
                blank = inputfile.next()
                for i in range(self.nbasis - base): # Fewer lines each time
                    line = inputfile.next()
                    temp = line.split()
                    for j in range(4, len(temp)):
                        self.aooverlaps[base+j-4, i+base] = float(temp[j])
                        self.aooverlaps[i+base, base+j-4] = float(temp[j])
                base += 5

        # ECP Pseudopotential information
        if "ECP POTENTIALS" in line:
            if not hasattr(self, "coreelectrons"):
                self.coreelectrons = [0]*self.natom
            dashes = inputfile.next()
            blank = inputfile.next()
            header = inputfile.next()
            while header.split()[0] == "PARAMETERS":
                name = header[17:25]
                atomnum = int(header[34:40])
                # The pseudopotnetial is given explicitely
                if header[40:50] == "WITH ZCORE":
                  zcore = int(header[50:55])
                  lmax = int(header[63:67])
                  self.coreelectrons[atomnum-1] = zcore
                # The pseudopotnetial is copied from another atom
                if header[40:55] == "ARE THE SAME AS":
                  atomcopy = int(header[60:])
                  self.coreelectrons[atomnum-1] = self.coreelectrons[atomcopy-1]
                line = inputfile.next()
                while line.split() <> []:
                    line = inputfile.next()
                header = inputfile.next()

        # This was used before refactoring the parser, geotargets was set here after parsing.
        #if not hasattr(self, "geotargets"):
        #    opttol = 1e-4
        #    self.geotargets = numpy.array([opttol, 3. / opttol], "d")
        #if hasattr(self,"geovalues"): self.geovalues = numpy.array(self.geovalues, "d")

if __name__ == "__main__":
    import doctest, gamessparser
    doctest.testmod(gamessparser, verbose=False)

max_stars_repo_name: vrlambert/RMG-Py

max_issues_repo_name: vrlambert/RMG-Py

max_forks_repo_name: vrlambert/RMG-Py


text:
import os
import sys
import cv2 as cv
import numpy as np
from PIL import Image

from model import model
from visualise import class_heatmap, segment_image, combine_image

os.environ["KERAS_BACKEND"] = "theano"
os.environ["THEANO_FLAGS"] = "mode=FAST_RUN, device=cuda, floatX=float32, optimizer=fast_compile"

def segment(prediction):
    """assign class labels to pixels according to class probabilities.

    for each pixel, max(class_prob).

    returns an array. len = num pixels.
    """
    pixel_class = np.apply_along_axis(np.argmax, 1, prediction)
    return pixel_class.astype("uint8")

def load_image(img_src):
    """just load an image.

    returns numpy array.
    """
    return cv.imread(img_src)

def prep_image(img):
    """prepare image for input into network."""
    # move channel column to front. (RGB, height, width)
    img = np.rollaxis(img, 2)
    # normalise pixels
    img = img/255
    return img

def load_model(weight_src="weights.hdf5"):
    """load pre-trained model"""
    m = model()
    m.load_weights(weight_src)
    return m

def write_class_heatmaps(prediction, output_dir="/tmp", height=256, width=256):
    """write class heatmaps.

    one image per class (for debugging)
    """
    magenta = np.array([
        [255,255,255],
        [255,247,243],
        [253,224,221],
        [252,197,192],
        [250,159,181],
        [247,104,161],
        [221,52,151],
        [174,1,126],
        [122,1,119],
        [73,0,106]])

    classes = prediction.shape[1]
    for i in range(0, classes):
        img = class_heatmap(magenta, prediction[:, i], 256, 256, True).resize((height, width))
        f = "{}/{:02d}_class_heatmap.png".format(output_dir, i)
        print("{} ...".format(f))
        img.save(f)

def write_segments(input_image, pixel_classes, output_dir="/tmp", height=256, width=256):
    """write segmented image

    output image is original with overlayed translucent segments."""
    class_colours = np.array([
        [255,255,255], # white: background/unlabeled
        [152,78,163],  # purple: residential
        [55,126,184],  # blue: commercial
        [255,255,51],  # yellow: industrial
        [77,175,74],   # green: vegetation
        [228,26,28],   # red: building
        [166,86,40]])  # brown: brownfield
    img = segment_image(class_colours, pixel_classes, height, width, bg_alpha=0, fg_alpha=192)
    combined_img = combine_image(img, input_image)
    f = "{}/segments.png".format(output_dir)

    combined_img.save(f)

if __name__ == "__main__":

    if len(sys.argv) != 4:
        print(sys.argv[0] + " <hdf5 weights> <img location> <output dir>")
        sys.exit(0)

    weight_src, img_src, output_dir = sys.argv[1:]

    # load input image
    input_image = load_image(img_src)
    img_arr = prep_image(input_image)
    assert img_arr.shape == (3, 256, 256)

    # load model
    m = load_model(weight_src)

    # get prediction
    input_image = Image.fromarray(input_image)
    img_arr = np.reshape(img_arr, (1, 3, 256, 256))
    prediction = m.predict(img_arr)
    prediction = prediction[0]

    # class heatmaps
    write_class_heatmaps(prediction, output_dir)

    # segmented image
    pixel_classes = segment(prediction)
    write_segments(input_image, pixel_classes, output_dir)

    # summarise pixel assignments
    class_names = ["background", "residential", "commercial", "industrial", "vegetation", "building", "brownfield"]
    counts = np.asarray(np.unique(np.array(pixel_classes), return_counts=True)).T
    for class_n, count in counts:
        print("{} = {}".format(class_names[class_n], count))

max_stars_repo_name: Pandinosaurus/laika

max_issues_repo_name: Pandinosaurus/laika

max_forks_repo_name: Pandinosaurus/laika


text:
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
from scipy.ndimage.interpolation import zoom
from collections import OrderedDict
import utils
import torch
import torch.nn as nn
import torch.nn.functional as F
import copy, numbers, numpy as np

def get_parameters(model, bias):
  for m in model.modules():
    if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
      if bias:
        yield m.bias
      else:
        yield m.weight
    elif isinstance(m, nn.BatchNorm2d):
      if bias:
        yield m.bias
      else:
        yield m.weight

def remove_module_dict(state_dict, is_print=False):
  new_state_dict = OrderedDict()
  for k, v in state_dict.items():
    if k[:7] == 'module.':
      name = k[7:] # remove `module.`
    else:
      name = k
    new_state_dict[name] = v
  if is_print: print(new_state_dict.keys())
  return new_state_dict

reason: from scipy


text:
import numpy as np
import pytest
from open3d.ml.contrib import subsample, subsample_batch

def test_subsample():
    points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1],
                       [5, 0, 0], [5, 1, 0]],
                      dtype=np.float32)
    features = np.array(range(21), dtype=np.float32).reshape(-1, 3)
    labels = np.array(range(7), dtype=np.int32)

    # Passing only points.
    sub_points_ref = np.array([[5, 0.5, 0], [0.4, 0.4, 0.4]], dtype=np.float32)
    sub_points = subsample(points, sampleDl=1.1)

    np.testing.assert_equal(sub_points, sub_points_ref)

    # Passing points and features.
    sub_features_ref = np.array([[16.5, 17.5, 18.5], [6, 7, 8]],
                                dtype=np.float32)
    sub_points_feat = subsample(points, features=features, sampleDl=1.1)

    assert len(sub_points_feat) == 2

    sub_points, sub_features = sub_points_feat

    np.testing.assert_equal(sub_features.shape, sub_features_ref.shape)
    np.testing.assert_equal(sub_features, sub_features_ref)
    np.testing.assert_equal(sub_points, sub_points_ref)

    # Passing points, features and labels.
    sub_labels_ref = np.array([6, 4], dtype=np.int32)
    sub_points_feat_lab = subsample(points,
                                    features=features,
                                    classes=labels,
                                    sampleDl=1.1)

    assert len(sub_points_feat_lab) == 3

    sub_points, sub_features, sub_labels = sub_points_feat_lab

    np.testing.assert_equal(sub_labels, sub_labels_ref)
    np.testing.assert_equal(sub_features.shape, sub_features_ref.shape)
    np.testing.assert_equal(sub_points, sub_points_ref)

    # Test wrong dtype.
    with pytest.raises(RuntimeError):
        sub_points = subsample(np.array(points, dtype=np.int32), sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample(np.array(points, dtype=np.float64), sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample(points,
                               features=np.array(features, dtype=np.int32),
                               sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample(points,
                               features=features,
                               classes=np.array(labels, dtype=np.float32),
                               sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample(points,
                               features=np.array(features, np.int32),
                               classes=np.array(labels, dtype=np.float32),
                               sampleDl=1.1)

    # Test shape mismatch
    with pytest.raises(RuntimeError):
        sub_points = subsample(points[0], sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample(np.ones((10, 4), dtype=np.float32), sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample(points, features=features[0], sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample(points[0], sampleDl=1.1)

    with pytest.raises(TypeError):
        sub_points = subsample(None, sampleDl=1.1)

def test_subsample_batch():
    points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1],
                       [5, 0, 0], [5, 1, 0]],
                      dtype=np.float32)
    features = np.array(range(28), dtype=np.float32).reshape(-1, 4)
    labels = np.array(range(7), dtype=np.int32)
    batches = np.array([3, 2, 2], dtype=np.int32)

    # Passing only points.
    sub_points_ref = np.array(
        [[0.3333333, 0.3333333, 0], [0.5, 0.5, 1], [5, 0.5, 0]],
        dtype=np.float32)
    sub_batch_ref = np.array([1, 1, 1], dtype=np.int32)

    (sub_points, sub_batch) = subsample_batch(points, batches, sampleDl=1.1)

    np.testing.assert_almost_equal(sub_points, sub_points_ref)
    np.testing.assert_almost_equal(sub_batch, sub_batch_ref)

    # Passing points and features.
    sub_features_ref = np.array(
        [[4, 5, 6, 7], [14, 15, 16, 17], [22, 23, 24, 25]], dtype=np.float32)
    sub_points_feat = subsample_batch(points,
                                      batches,
                                      features=features,
                                      sampleDl=1.1)

    assert len(sub_points_feat) == 3

    sub_points, sub_batch, sub_features = sub_points_feat

    np.testing.assert_equal(sub_features.shape, sub_features_ref.shape)
    np.testing.assert_almost_equal(sub_features, sub_features_ref)
    np.testing.assert_almost_equal(sub_points, sub_points_ref)
    np.testing.assert_almost_equal(sub_batch, sub_batch_ref)

    # Passing points, features and labels.
    sub_labels_ref = np.array([2, 4, 6], dtype=np.int32)
    sub_points_feat_lab = subsample_batch(points,
                                          batches,
                                          features=features,
                                          classes=labels,
                                          sampleDl=1.1)

    assert len(sub_points_feat_lab) == 4

    sub_points, sub_batch, sub_features, sub_labels = sub_points_feat_lab

    np.testing.assert_equal(sub_labels, sub_labels_ref)
    np.testing.assert_equal(sub_features.shape, sub_features_ref.shape)
    np.testing.assert_almost_equal(sub_points, sub_points_ref)
    np.testing.assert_almost_equal(sub_batch, sub_batch_ref)

    # Test wrong dtype.
    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(np.array(points, dtype=np.int32),
                                     batches,
                                     sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points,
                                     np.array(batches, dtype=np.float32),
                                     sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(np.array(points, dtype=np.float64),
                                     batches,
                                     sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points,
                                     batches,
                                     features=np.array(features,
                                                       dtype=np.int32),
                                     sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points,
                                     batches,
                                     features=features,
                                     classes=np.array(labels, dtype=np.float32),
                                     sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points,
                                     batches,
                                     features=np.array(features, np.int32),
                                     classes=np.array(labels, dtype=np.float32),
                                     sampleDl=1.1)

    # Test shape mismatch
    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points[0], batches, sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(np.ones((10, 4), dtype=np.float32),
                                     batches,
                                     sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points,
                                     batches,
                                     features=features[0],
                                     sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points[0], batches, sampleDl=1.1)

    with pytest.raises(TypeError):
        sub_points = subsample_batch(None, None, sampleDl=1.1)

    # Test sum(batch) != num_points
    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points,
                                     np.array([3, 3, 2], dtype=np.int32),
                                     sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points,
                                     np.array([3, 3, 2], dtype=np.int32),
                                     features=features,
                                     classes=labels,
                                     sampleDl=1.1)

    with pytest.raises(RuntimeError):
        sub_points = subsample_batch(points,
                                     np.array([1], dtype=np.int32),
                                     sampleDl=1.1)


text:
# pylint: disable=missing-function-docstring, missing-module-docstring/
# pylint: disable=wildcard-import
from pyccel.decorators import types

@types(int)
def set_num_threads(n):
    from pyccel.stdlib.internal.openmp import omp_set_num_threads
    omp_set_num_threads(n)

@types()
def get_num_threads():
    from pyccel.stdlib.internal.openmp import omp_get_num_threads
    #$ omp parallel
    n = omp_get_num_threads()
    #$ omp end parallel
    return n

def get_max_threads():
    from pyccel.stdlib.internal.openmp import omp_get_max_threads
    max_threads = omp_get_max_threads()

    return max_threads

@types('int')
def f1(i):
    from pyccel.stdlib.internal.openmp import omp_get_thread_num
    out = -1
    #$ omp parallel private(idx)
    idx = omp_get_thread_num()

    if idx == i:
        out = idx

    #$ omp end parallel
    return out

def directive_in_else(x : int):
    result = 0
    if x < 30:
        return x
    else:
        #$ omp parallel
        #$ omp for reduction(+:result)
        for i in range(x):
            result = result + i
        #$ omp end parallel

    return result

def test_omp_number_of_procs():
    from pyccel.stdlib.internal.openmp import omp_get_num_procs
    procs_num = omp_get_num_procs()
    return procs_num

def test_omp_in_parallel1():
    from pyccel.stdlib.internal.openmp import omp_in_parallel
    in_parallel = omp_in_parallel()
    return in_parallel

def test_omp_in_parallel2():
    from pyccel.stdlib.internal.openmp import omp_in_parallel
    #$ omp parallel
    in_parallel = omp_in_parallel()
    #$ omp end parallel
    return in_parallel

@types ('bool')
def test_omp_set_get_dynamic(dynamic_theads):
    from pyccel.stdlib.internal.openmp import omp_set_dynamic, omp_get_dynamic
    omp_set_dynamic(dynamic_theads)
    return omp_get_dynamic()

@types ('bool')
def test_omp_set_get_nested(nested):
    from pyccel.stdlib.internal.openmp import omp_set_nested, omp_get_nested
    omp_set_nested(nested)
    return omp_get_nested()

def test_omp_get_cancellation():
    from pyccel.stdlib.internal.openmp import omp_get_cancellation
    cancel_var = omp_get_cancellation()
    return cancel_var

def test_omp_get_thread_limit():
    from pyccel.stdlib.internal.openmp import omp_get_thread_limit
    #$ omp parallel
    maximum_threads_available = omp_get_thread_limit()
    #$ omp end parallel
    return maximum_threads_available

@types ('int')
def test_omp_get_set_max_active_levels(max_active_levels):
    from pyccel.stdlib.internal.openmp import omp_get_max_active_levels, omp_set_max_active_levels
    omp_set_max_active_levels(max_active_levels)
    max_active_levels_var = omp_get_max_active_levels()
    return max_active_levels_var

def test_omp_get_level():
    from pyccel.stdlib.internal.openmp import omp_get_level
    #$ omp parallel
    #$ omp parallel
    nested_parallel_regions = omp_get_level()
    #$ omp end parallel
    #$ omp end parallel
    return nested_parallel_regions

def test_omp_get_active_level():
    from pyccel.stdlib.internal.openmp import omp_get_active_level
    #$ omp parallel
    #$ omp parallel
    active_level_vars = omp_get_active_level()
    #$ omp end parallel
    #$ omp end parallel
    return active_level_vars

def test_omp_get_ancestor_thread_num():
    from pyccel.stdlib.internal.openmp import omp_get_ancestor_thread_num, omp_get_active_level
    #$ omp parallel
    active_level = omp_get_active_level()
    ancestor_thread = omp_get_ancestor_thread_num(active_level)
    #$ omp end parallel
    return ancestor_thread

def test_omp_get_team_size():
    from pyccel.stdlib.internal.openmp import omp_get_team_size, omp_get_active_level
    #$ omp parallel
    active_level = omp_get_active_level()
    team_size = omp_get_team_size(active_level)
    #$ omp end parallel
    return team_size

def test_omp_in_final():
    from pyccel.stdlib.internal.openmp import omp_in_final
    x = 20
    z = 0
    result = 0

    #$ omp parallel
    #$ omp single
    #$ omp task final(i >= 10)
    for i in range(x):
        z = z + i
        if omp_in_final() == 1:
            result = 1
    #$ omp end task
    #$ omp end single
    #$ omp end parallel
    return result

def test_omp_get_proc_bind():
    from pyccel.stdlib.internal.openmp import omp_get_proc_bind

    bind_var = omp_get_proc_bind()
    return bind_var

#The function give som errors
# def test_omp_places():
#     from pyccel.stdlib.internal.openmp import omp_get_partition_num_places
#     from pyccel.stdlib.internal.openmp import omp_get_partition_place_nums
#     from pyccel.stdlib.internal.openmp import omp_get_place_num
#     from pyccel.stdlib.internal.openmp import omp_get_place_proc_ids
#     from pyccel.stdlib.internal.openmp import omp_get_place_num_procs
#     from pyccel.stdlib.internal.openmp import omp_get_num_places
#
#     partition_num_places = omp_get_partition_num_places()
#     #partition_places_num =    omp_get_partition_place_nums(0)
#     place_num = omp_get_place_num()
#     if place_num < 0:
#         return -1
#     #place_num, ids = omp_get_place_proc_ids(place_num, ids)
#     procs = omp_get_place_num_procs(place_num)
#     num_places = omp_get_num_places()
#     return place_num

@types ('int')
def test_omp_set_get_default_device(device_num):
    from pyccel.stdlib.internal.openmp import omp_get_default_device
    from pyccel.stdlib.internal.openmp import omp_set_default_device
    omp_set_default_device(device_num)
    default_device = omp_get_default_device()
    return default_device

def test_omp_get_num_devices():
    from pyccel.stdlib.internal.openmp import omp_get_num_devices
    num_devices = omp_get_num_devices()
    return num_devices

def test_omp_get_num_teams():
    from pyccel.stdlib.internal.openmp import omp_get_num_teams
    #$ omp teams num_teams(2)
    num_teams = omp_get_num_teams()
    #$ omp end teams
    return num_teams

@types('int')
def test_omp_get_team_num(i):
    from pyccel.stdlib.internal.openmp import omp_get_team_num
    out = -1
    #$ omp teams num_teams(2)
    team_num = omp_get_team_num()
    if team_num == i:
        out = team_num
    #$ omp end teams
    return out

def test_omp_is_initial_device():
    from pyccel.stdlib.internal.openmp import omp_is_initial_device
    is_task_in_init_device = omp_is_initial_device()
    return is_task_in_init_device

def test_omp_get_initial_device():
    from pyccel.stdlib.internal.openmp import omp_get_initial_device
    #$ omp target
    host_device = omp_get_initial_device()
    #$ omp end target
    return host_device

def test_omp_get_set_schedule():
    from pyccel.stdlib.internal.openmp import omp_get_schedule, omp_set_schedule
    result = 0
    #$ omp parallel private(i)
    #$ omp for schedule(runtime) reduction (+:sum)
    omp_set_schedule(2, 2)
    schedule_kind = 0
    chunk_size = 0
    omp_get_schedule(schedule_kind, chunk_size)
    for i in range(16):
        result = result + i
    #$ omp end for nowait
    return True

def test_omp_get_max_task_priority():
    from pyccel.stdlib.internal.openmp import omp_get_max_task_priority
    result = 0
    max_task_priority_var = 0
    #$ omp parallel
    #$ omp single
    #$ omp task
    max_task_priority_var = omp_get_max_task_priority()
    #$ omp end task
    #$ omp end single
    #$ omp end parallel
    return max_task_priority_var

@types('real[:,:], real[:,:], real[:,:]')
def omp_matmul(A, x, out):
    #$ omp parallel shared(A,x,out) private(i,j,k)
    #$ omp for
    for i in range(len(A)):# pylint: disable=C0200
        for j in range(len(x[0])):# pylint: disable=C0200
            for k in range(len(x)):# pylint: disable=C0200
                out[i][j] += A[i][k] * x[k][j]
    #$ omp end parallel
    #to let the function compile using epyccel issue #468
    "bypass issue #468" # pylint: disable=W0105

@types('real[:,:], real[:,:], real[:,:]')
def omp_matmul_single(A, x, out):
    from numpy import matmul
    #$ omp parallel
    #$ omp single
    out[:] = matmul(A, x)
    #$ omp end single
    #$ omp end parallel
    #to let the function compile using epyccel issue #468
    "bypass issue #468" # pylint: disable=W0105

@types('int[:]', 'int[:]', 'real[:]')
def omp_nowait(x, y, z):
    #$ omp parallel
    #$ omp for nowait
    for i in range(0, 1000):
        y[i] = x[i] * 2
    #$ omp for nowait
    for j in range(0, 1000):
        z[j] = x[j] / 2
    #$ omp end parallel
    "bypass issue #468" # pylint: disable=W0105

@types('int[:]')
def omp_arraysum(x):
    result = 0
    #$ omp parallel private(i)
    #$ omp for reduction (+:result)
    for i in range(0, 5):
        result += x[i]
    #$ omp end parallel
    return result

@types('int[:]')
def omp_arraysum_combined(x):
    result = 0
    #$ omp parallel for reduction (+:result)
    for i in range(0, 5):
        result += x[i]
    return result

@types('int')
def omp_range_sum_critical(x):
    result = 0
    #$ omp parallel for num_threads(4) shared(result)
    for i in range(0, x):
        #$ omp critical
        result += i
        #$ omp end critical
    return result

@types('int[:]')
def omp_arraysum_single(x):
    result = 0
    #$ omp parallel
    #$ omp single
    for i in range(0, 10):
        result += x[i]
    #$ omp end single
    #$ omp end parallel
    return result

def omp_master():
    result = 30
    #$omp parallel num_threads(3) reduction(+:result)
    #$omp master
    result += 1
    #$omp end master
    #$omp end parallel
    return result

@types('int')
def omp_taskloop(n):
    result = 0
    #$omp parallel num_threads(n)
    #$omp taskloop
    for i in range(0, 10): # pylint: disable=unused-variable
        #$omp atomic
        result = result + 1
    #$omp end parallel
    return result

@types('int')
def omp_tasks(x):
    @types('int', results='int')
    def fib(n):
        if n < 2:
            return n
        #$ omp task shared(i) firstprivate(n)
        i = fib(n-1)
        #$ omp end task
        #$ omp task shared(j) firstprivate(n)
        j = fib(n-2)
        #$ omp end task
        #$ omp taskwait
        return i + j
    #$ omp parallel
    #$ omp single
    m = fib(x)
    #$ omp end single
    #$ omp end parallel
    return m

@types('int')
def omp_simd(n):
    from numpy import zeros
    result = 0
    arr = zeros(n, dtype=int)
    #$ omp parallel num_threads(4)
    #$ omp simd
    for i in range(0, n):
        arr[i] = i
    #$ omp end parallel
    for i in range(0, n):
        result = result + arr[i]
    return result

def omp_flush():
    from pyccel.stdlib.internal.openmp import omp_get_thread_num
    flag = 0
    #$ omp parallel num_threads(2)
    if omp_get_thread_num() == 0:
        #$ omp atomic update
        flag = flag + 1
    elif omp_get_thread_num() == 1:
        #$ omp flush(flag)
        while flag < 1:
            pass
            #$ omp flush(flag)
        #$ omp atomic update
        flag = flag + 1
    #$ omp end parallel
    return flag

def omp_barrier():
    from numpy import zeros
    arr = zeros(1000, dtype=int)
    result = 0
    #$ omp parallel num_threads(3)
    #$ omp for
    for i in range(0, 1000):
        arr[i] = i * 2

    #$ omp barrier
    #$ omp for reduction(+:result)
    for i in range(0, 1000):
        result = result + arr[i]
    #$ omp end parallel
    return result

def combined_for_simd():
    import numpy as np
    x = np.array([1,2,1,2,1,2,1,2])
    y = np.array([2,1,2,1,2,1,2,1])
    z = np.zeros(8, dtype = int)
    result = 0
    #$ omp parallel for simd
    for i in range(0, 8):
        z[i] = x[i] + y[i]

    for i in range(0, 8):
        result = result + z[i]
    return result

def omp_sections():
    n = 8
    sum1 = 0
    sum2 = 0
    sum3 = 0
    #$ omp parallel num_threads(2)
    #$ omp sections

    #$ omp section
    for i in range(0, int(n/3)):
        sum1 = sum1 + i
    #$ omp end section

    #$ omp section
    for i in range(0, int(n/2)):
        sum2 = sum2 + i
    #$ omp end section

    #$ omp section
    for i in range(0, n):
        sum3 = sum3 + i
    #$ omp end section
    #$ omp end sections

    #$ omp end parallel

    return (sum1 + sum2 + sum3)

max_stars_repo_name: dina-fouad/pyccel

max_issues_repo_name: dina-fouad/pyccel

max_forks_repo_name: dina-fouad/pyccel

reason: import numpy,from numpy


text:
#!/usr/bin/env python
import os
import yaml
import numpy as np
from collections import defaultdict

FOLDS = 10
# The amount of kernel computation time
# corresponding to one outer training fold
FACTOR = (float(FOLDS - 1)/FOLDS)**2

BIRDS = ('BRCR', 'CBCH', 'DEJU', 'HAFL', 'HETH', 'HEWA', 'OSFL',
         'PSFL', 'RBNU', 'SWTH', 'VATH', 'WETA', 'WIWR',)

def compute_runtimes(emptimes, hardmargintimes, outputfile):
    if os.path.exists(outputfile):
        with open(outputfile, 'r+') as f:
            existing_lines = [line.strip() for line in f]
    else:
        existing_lines = []

    ktimes = dict()
    with open(hardmargintimes, 'r+') as f:
        for line in f:
            dset, ktype, eps, delta, seed, time = line.strip().split(',')
            eps, delta, time = map(float, (eps, delta, time))
            seed = int(seed)
            ktimes[dset, ktype, eps, delta, seed] = FACTOR*time
            if dset in BIRDS:
                for d in BIRDS:
                    ktimes[d, ktype, eps, delta, seed] = FACTOR*time

    with open(emptimes, 'r+') as f:
        for line in f:
            dset, alg, k, ktype, eps, delta, seed, p, trans, time = line.strip().split(',')
            eps, delta, time = map(float, (eps, delta, time))
            seed, p, trans = map(int, (seed, p, trans))

            line = ('%s,%s,%s,%s,%f,%f,%d,%d,%d' %
                (dset, alg, k, ktype, eps, delta, seed, p, trans))
            if any(l.startswith(line) for l in existing_lines):
                continue

            time += ktimes[dset, ktype, eps, delta, seed]
            line += (',%f\n' % time)
            print line,
            with open(outputfile, 'a+') as f:
                f.write(line)

if __name__ == '__main__':
    from optparse import OptionParser, OptionGroup
    parser = OptionParser(usage="Usage: %prog emptimes hardmargintimes outputfile")
    options, args = parser.parse_args()
    options = dict(options.__dict__)
    if len(args) != 3:
        parser.print_help()
        exit()
    compute_runtimes(*args, **options)

max_stars_repo_name: garydoranjr/mikernels

max_issues_repo_name: garydoranjr/mikernels

max_forks_repo_name: garydoranjr/mikernels