text:
import os
import numpy as np
import itertools
import scipy.io as sio
import torch
import torch.optim as optim
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from torch.utils.data import TensorDataset
from modalities.Modality import Modality
from brain_image_text.networks.VAEtrimodal import VAEtrimodal,VAEbimodal
from brain_image_text.networks.QNET import QNet
from brain_image_text.networks.MLP_Brain import EncoderBrain, DecoderBrain
from brain_image_text.networks.MLP_Image import EncoderImage, DecoderImage
from brain_image_text.networks.MLP_Text import EncoderText, DecoderText
from utils.BaseExperiment import BaseExperiment

class BrainImageText(BaseExperiment):
    def __init__(self, flags, alphabet):
        super().__init__(flags)

        self.modalities = self.set_modalities()
        self.num_modalities = len(self.modalities.keys())
        self.subsets = self.set_subsets()
        self.dataset_train = None
        self.dataset_test = None

        self.set_dataset()
        self.mm_vae = self.set_model()
        self.optimizer = None
        self.rec_weights = self.set_rec_weights()
        self.style_weights = self.set_style_weights()
        self.Q1,self.Q2,self.Q3 = self.set_Qmodel()
        self.eval_metric = accuracy_score

        self.labels = ['digit']

    def set_model(self):
        model = VAEtrimodal(self.flags, self.modalities, self.subsets)
        model = model.to(self.flags.device)
        return model

    def set_modalities(self):
        mod1 = Modality('brain', EncoderBrain(self.flags), DecoderBrain(self.flags),
                    self.flags.class_dim, self.flags.style_m1_dim, 'normal')
        mod2 = Modality('image', EncoderImage(self.flags), DecoderImage(self.flags),
                    self.flags.class_dim, self.flags.style_m2_dim, 'normal')
        mod3 = Modality('text', EncoderText(self.flags), DecoderText(self.flags),
                    self.flags.class_dim, self.flags.style_m3_dim, 'normal')
        mods = {mod1.name: mod1, mod2.name: mod2, mod3.name: mod3}
        return mods

    def set_dataset(self):
        # load data
        data_dir_root = self.flags.data_dir_root
        sbj = self.flags.sbj
        image_model = self.flags.image_model
        text_model = self.flags.text_model
        roi = self.flags.roi
        brain_dir = os.path.join(data_dir_root, 'brain_feature', roi, sbj)
        image_dir_train = os.path.join(data_dir_root, 'visual_feature/ImageNetTraining', image_model + '-PCA', sbj)
        image_dir_test = os.path.join(data_dir_root, 'visual_feature/ImageNetTest', image_model + '-PCA', sbj)
        text_dir_train = os.path.join(data_dir_root, 'textual_feature/ImageNetTraining/text', text_model, sbj)
        text_dir_test = os.path.join(data_dir_root, 'textual_feature/ImageNetTest/text', text_model, sbj)

        train_brain = sio.loadmat(os.path.join(brain_dir, 'fmri_train_data.mat'))['data'].astype('double')
        train_image = sio.loadmat(os.path.join(image_dir_train, 'feat_pca_train.mat'))['data'].astype('double')
        train_text = sio.loadmat(os.path.join(text_dir_train, 'text_feat_train.mat'))['data'].astype('double')
        train_label = sio.loadmat(os.path.join(brain_dir, 'fmri_train_data.mat'))['class_idx'].T.astype('int')

        # test_brain = sio.loadmat(os.path.join(brain_dir, 'fmri_test_data_unique.mat'))['data'].astype('double')
        # test_image = sio.loadmat(os.path.join(image_dir_test, 'feat_pca_test_unique.mat'))['data'].astype('double')
        # test_text = sio.loadmat(os.path.join(text_dir_test, 'text_feat_test_unique.mat'))['data'].astype('double')
        # test_label = sio.loadmat(os.path.join(brain_dir, 'fmri_test_data_unique.mat'))['class_idx'].T.astype('int')

        test_brain = sio.loadmat(os.path.join(brain_dir, 'fmri_test_data.mat'))['data'].astype('double')
        test_image = sio.loadmat(os.path.join(image_dir_test, 'feat_pca_test.mat'))['data'].astype('double')
        test_text = sio.loadmat(os.path.join(text_dir_test, 'text_feat_test.mat'))['data'].astype('double')
        test_label = sio.loadmat(os.path.join(brain_dir, 'fmri_test_data.mat'))['class_idx'].T.astype('int')

        if self.flags.aug_type == 'image_text':
            image_dir_aug = os.path.join(data_dir_root, 'visual_feature/Aug_1000', image_model + '-PCA', sbj)
            text_dir_aug = os.path.join(data_dir_root, 'textual_feature/Aug_1000/text', text_model, sbj)
            aug_image = sio.loadmat(os.path.join(image_dir_aug, 'feat_pca_aug.mat'))['data'].astype('double')
            aug_text = sio.loadmat(os.path.join(text_dir_aug, 'text_feat_aug.mat'))['data'].astype('double')
            aug_image = torch.from_numpy(aug_image)
            aug_text = torch.from_numpy(aug_text)
            print('aug_image=', aug_image.shape)
            print('aug_text=', aug_text.shape)
        elif self.flags.aug_type == 'text_only':
            text_dir_aug = os.path.join(data_dir_root, 'textual_feature/Aug_1000/text', text_model, sbj)
            aug_text = sio.loadmat(os.path.join(text_dir_aug, 'text_feat_aug.mat'))['data'].astype('double')
            aug_text = aug_text
            aug_text = torch.from_numpy(aug_text)
            print('aug_text=', aug_text.shape)

        elif self.flags.aug_type == 'image_only':
            image_dir_aug = os.path.join(data_dir_root, 'visual_feature/Aug_1000', image_model + '-PCA', sbj)
            aug_image = sio.loadmat(os.path.join(image_dir_aug, 'feat_pca_aug.mat'))['data'].astype('double')
            aug_image = torch.from_numpy(aug_image)
            print('aug_image=', aug_image.shape)
        elif self.flags.aug_type == 'no_aug':
            print('no augmentation')

        if self.flags.test_type=='normal':
            train_label_stratify = train_label
            train_brain, val_brain, train_label, val_label = train_test_split(train_brain, train_label_stratify, test_size=0.2, stratify=train_label_stratify)
            train_image, val_image, train_label, val_label = train_test_split(train_image, train_label_stratify, test_size=0.2, stratify=train_label_stratify)
            train_text, val_text, train_label, val_label = train_test_split(train_text, train_label_stratify, test_size=0.2, stratify=train_label_stratify)

            val_brain = torch.from_numpy(val_brain)
            val_image = torch.from_numpy(val_image)
            val_text = torch.from_numpy(val_text)
            val_label = torch.from_numpy(val_label)
            print('val_brain=', val_brain.shape)
            print('val_image=', val_image.shape)
            print('val_text=', val_text.shape)

        train_brain = torch.from_numpy(train_brain)
        test_brain = torch.from_numpy(test_brain)
        train_image = torch.from_numpy(train_image)
        test_image = torch.from_numpy(test_image)
        train_text = torch.from_numpy(train_text)
        test_text = torch.from_numpy(test_text)
        train_label = torch.from_numpy(train_label)
        test_label = torch.from_numpy(test_label)

        print('train_brain=', train_brain.shape)
        print('train_image=', train_image.shape)
        print('train_text=', train_text.shape)
        print('test_brain=', test_brain.shape)
        print('test_image=', test_image.shape)
        print('test_text=', test_text.shape)

        self.m1_dim = train_brain.shape[1]
        self.m2_dim = train_image.shape[1]
        self.m3_dim = train_text.shape[1]

        train_dataset = torch.utils.data.TensorDataset(train_brain, train_image, train_text, train_label)
        test_dataset = torch.utils.data.TensorDataset(test_brain, test_image, test_text,test_label)

        self.dataset_train = train_dataset
        self.dataset_test = test_dataset

        if self.flags.test_type == 'normal':
            val_dataset = torch.utils.data.TensorDataset(val_brain, val_image, val_text, val_label)
            self.dataset_val = val_dataset

        if self.flags.aug_type == 'image_text':
            aug_dataset = torch.utils.data.TensorDataset(aug_image, aug_text)
            self.dataset_aug = aug_dataset
        elif self.flags.aug_type == 'text_only':
            aug_dataset = torch.utils.data.TensorDataset(aug_text)
            self.dataset_aug = aug_dataset
        elif self.flags.aug_type == 'image_only':
            aug_image = torch.utils.data.TensorDataset(aug_image)
            self.dataset_aug = aug_image
        elif self.flags.aug_type == 'no_aug':
            print('no augmentation')

    def set_optimizer(self):
        optimizer = optim.Adam(
            itertools.chain(self.mm_vae.parameters(),self.Q1.parameters(),self.Q2.parameters(),self.Q3.parameters()),
            lr=self.flags.initial_learning_rate,
            betas=(self.flags.beta_1, self.flags.beta_2))
        optimizer_mvae = optim.Adam(
            list(self.mm_vae.parameters()),
            lr=self.flags.initial_learning_rate,
            betas=(self.flags.beta_1, self.flags.beta_2))
        optimizer_Qnet = optim.Adam(
            itertools.chain(self.Q1.parameters(),self.Q2.parameters(),self.Q3.parameters()),
            lr=self.flags.initial_learning_rate,
            betas=(self.flags.beta_1, self.flags.beta_2))
        self.optimizer = {'mvae':optimizer_mvae,'Qnet':optimizer_Qnet,'all':optimizer}
        scheduler_mvae = optim.lr_scheduler.StepLR(optimizer_mvae, step_size=20, gamma=1.0)
        scheduler_Qnet = optim.lr_scheduler.StepLR(optimizer_Qnet, step_size=20, gamma=1.0)
        scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=1.0)
        self.scheduler = {'mvae': scheduler_mvae, 'Qnet': scheduler_Qnet, 'all': scheduler}

    def set_Qmodel(self):
        Q1 = QNet(input_dim=self.flags.m1_dim, latent_dim=self.flags.class_dim).cuda()
        Q2 = QNet(input_dim=self.flags.m2_dim, latent_dim=self.flags.class_dim).cuda()
        Q3 = QNet(input_dim=self.flags.m3_dim, latent_dim=self.flags.class_dim).cuda()
        return Q1, Q2 ,Q3

    def set_rec_weights(self):
        weights = dict()
        weights['brain'] = self.flags.beta_m1_rec
        weights['image'] = self.flags.beta_m2_rec
        weights['text'] = self.flags.beta_m3_rec
        return weights

    def set_style_weights(self):
        weights = dict()
        weights['brain'] = self.flags.beta_m1_style
        weights['image'] = self.flags.beta_m2_style
        weights['text'] = self.flags.beta_m3_style
        return weights

max_stars_repo_name: cvsubmittemp/BraVL

max_issues_repo_name: cvsubmittemp/BraVL

max_forks_repo_name: cvsubmittemp/BraVL

reason: import numpy,import scipy


text:
import numpy as np
import tensorflow as tf
import dynastes.layers as dynl
from dynastes.layers.convolutional_layers import DynastesConv1DTranspose, DynastesDepthwiseConv1D, \
    DynastesConv2DTranspose, DynastesConv2D, DynastesDepthwiseConv2D, DynastesConv1D, Upsampling1D, Upsampling2D
from dynastes.util.test_utils import layer_test

to_tensor = tf.convert_to_tensor
normal = np.random.normal

class DynastesConv1DTest(tf.test.TestCase):

    def test_masking(self):
        layer = DynastesConv1D(32, kernel_size=3, strides=2, padding='same', conv_mask=True)

        ts = to_tensor(normal(size=(8, 16, 32))
                       .astype(np.float32))

        mask_len = 16 // 6
        mask = to_tensor(([True] * (16 - mask_len)) + ([False] * (mask_len)))
        mask = tf.expand_dims(mask, axis=0)
        mask = tf.tile(mask, [8, 1])
        layer(ts, mask=mask)
        layer.compute_mask(ts, mask=mask)

        @tf.function
        def graph_test_fn(x, mask):
            layer(x, mask=mask)
            layer.compute_mask(x, mask=mask)

        graph_test_fn(x=ts, mask=mask)

    def test_simple(self):
        layer_test(
            DynastesConv1D, kwargs={'filters': 3, 'kernel_size': 3, 'activation': 'mish'}, input_shape=(5, 32, 3))

    def test_specnorm(self):
        layer_test(
            DynastesConv1D, kwargs={'filters': 3, 'kernel_size': 3,
                                    'kernel_normalizer': 'spectral',
                                    'use_wscale': True,
                                    'kernel_regularizer': 'orthogonal'}, input_shape=(5, 32, 3))

class DynastesConv2DTest(tf.test.TestCase):
    def test_simple(self):
        layer_test(
            DynastesConv2D, kwargs={'filters': 3, 'kernel_size': (3, 3), 'activation': 'swish'},
            input_shape=(4, 16, 16, 3))

    def test_masking(self):
        layer = DynastesConv2D(32, kernel_size=3, strides=2, padding='same', conv_mask=True)

        ts = to_tensor(normal(size=(8, 16, 16, 32))
                       .astype(np.float32))

        mask_len = 16 // 8
        mask = to_tensor(([True] * (16 - mask_len)) + ([False] * (mask_len)))
        mask = tf.expand_dims(mask, axis=0)
        mask = tf.tile(mask, [8, 1])
        mask = tf.expand_dims(mask, axis=1)
        mask = tf.tile(mask, [1, 16, 1])
        layer(ts, mask=mask)
        out_mask = layer.compute_mask(ts, mask=mask)

        @tf.function
        def graph_test_fn(x, mask):
            layer(x, mask=mask)
            layer.compute_mask(x, mask=mask)

        graph_test_fn(x=ts, mask=mask)

    def test_specnorm(self):
        layer_test(
            DynastesConv2D, kwargs={'filters': 7, 'kernel_size': (3, 3),
                                    'kernel_normalizer': 'spectral',
                                    'kernel_regularizer': 'orthogonal'}, input_shape=(4, 16, 16, 5))

class DynastesConv1DTransposeTest(tf.test.TestCase):
    def test_simple(self):
        layer_test(
            DynastesConv1DTranspose,
            kwargs={'filters': 3, 'kernel_size': 3, 'strides': 2, 'padding': 'same'},
            input_shape=(None, 16, 3),
            expected_output_shape=(None, 32, 3)
        )

    def test_masking(self):
        layer = DynastesConv1DTranspose(32, kernel_size=3, strides=2, padding='same', conv_mask=True)

        ts = to_tensor(normal(size=(8, 16, 32))
                       .astype(np.float32))

        mask_len = 16 // 6
        mask = to_tensor(([True] * (16 - mask_len)) + ([False] * (mask_len)))
        mask = tf.expand_dims(mask, axis=0)
        mask = tf.tile(mask, [8, 1])
        layer(ts, mask=mask)
        layer.compute_mask(ts, mask=mask)

        @tf.function
        def graph_test_fn(x, mask):
            layer(x, mask=mask)
            layer.compute_mask(x, mask=mask)

        graph_test_fn(x=ts, mask=mask)

    def test_specnorm(self):
        layer_test(
            DynastesConv1DTranspose,
            kwargs={'filters': 7, 'kernel_size': 3, 'strides': 2, 'padding': 'same',
                    'kernel_normalizer': 'spectral',
                    'kernel_regularizer': 'orthogonal'},
            input_shape=(None, 16, 5),
            expected_output_shape=(None, 32, 7)
        )

class DynastesConv2DTransposeTest(tf.test.TestCase):
    def test_simple(self):
        layer_test(
            DynastesConv2DTranspose,
            kwargs={'filters': 3, 'kernel_size': (3, 3), 'strides': (2, 2), 'padding': 'same'},
            input_shape=(None, 16, 16, 3),
            expected_output_shape=(None, 32, 32, 3)
        )

    def test_masking(self):
        layer = DynastesConv2DTranspose(32, kernel_size=3, strides=2, padding='same', wnorm=True, conv_mask=True)

        ts = to_tensor(normal(size=(8, 16, 16, 32))
                       .astype(np.float32))

        mask_len = 16 // 8
        mask = to_tensor(([True] * (16 - mask_len)) + ([False] * (mask_len)))
        mask = tf.expand_dims(mask, axis=0)
        mask = tf.tile(mask, [8, 1])
        mask = tf.expand_dims(mask, axis=1)
        mask = tf.tile(mask, [1, 16, 1])

        @tf.function
        def graph_test_fn(x, mask):
            layer(x, mask=mask)
            layer.compute_mask(x, mask=mask)

        graph_test_fn(x=ts, mask=mask)
        print(layer.variables[-1])

        layer(ts, mask=mask)
        layer.compute_mask(ts, mask=mask)

        print(layer.variables)

    def test_specnorm(self):
        layer_test(
            DynastesConv2DTranspose,
            kwargs={'filters': 7, 'kernel_size': (3, 3), 'strides': (2, 2), 'padding': 'same',
                    'kernel_normalizer': 'spectral',
                    'kernel_regularizer': 'orthogonal'},
            input_shape=(None, 16, 16, 5),
            expected_output_shape=(None, 32, 32, 7)
        )

class DynastesDepthwiseConv1DTest(tf.test.TestCase):
    def test_simple(self):
        layer_test(
            DynastesDepthwiseConv1D, kwargs={'kernel_size': 3}, input_shape=(5, 32, 3))

    def test_masking(self):
        layer = DynastesDepthwiseConv1D(kernel_size=3, strides=2, padding='same', conv_mask=True)

        ts = to_tensor(normal(size=(8, 16, 32))
                       .astype(np.float32))

        mask_len = 16 // 6
        mask = to_tensor(([True] * (16 - mask_len)) + ([False] * (mask_len)))
        mask = tf.expand_dims(mask, axis=0)
        mask = tf.tile(mask, [8, 1])
        layer(ts, mask=mask)
        layer.compute_mask(ts, mask=mask)

        @tf.function
        def graph_test_fn(x, mask):
            layer(x, mask=mask)
            layer.compute_mask(x, mask=mask)

        graph_test_fn(x=ts, mask=mask)

    def test_specnorm(self):
        layer_test(
            DynastesDepthwiseConv1D, kwargs={'kernel_size': 3,
                                             'kernel_normalizer': 'spectral',
                                             'kernel_regularizer': 'orthogonal'}, input_shape=(5, 32, 3))

class Upsampling2DTest(tf.test.TestCase):
    def test_simple(self):
        layer_test(
            Upsampling2D,
            kwargs={'strides': (2, 2)},
            input_shape=(None, 16, 16, 3),
            expected_output_shape=(None, 32, 32, 3)
        )

    def test_masking(self):
        layer = Upsampling2D(strides=2)

        ts = to_tensor(normal(size=(8, 16, 16, 32))
                       .astype(np.float32))

        mask_len = 16 // 8
        mask = to_tensor(([True] * (16 - mask_len)) + ([False] * (mask_len)))
        mask = tf.expand_dims(mask, axis=0)
        mask = tf.tile(mask, [8, 1])
        mask = tf.expand_dims(mask, axis=1)
        mask = tf.tile(mask, [1, 16, 1])
        layer(ts, mask=mask)
        layer.compute_mask(ts, mask=mask)

        @tf.function
        def graph_test_fn(x, mask):
            layer(x, mask=mask)
            layer.compute_mask(x, mask=mask)

        graph_test_fn(x=ts, mask=mask)

class Upsampling1DTest(tf.test.TestCase):
    def test_simple(self):
        layer_test(
            Upsampling1D, kwargs={'strides': 2}, input_shape=(None, 32, 3), expected_output_shape=(None, 64, 3))

    def test_masking(self):
        layer = Upsampling1D(strides=2)

        ts = to_tensor(normal(size=(8, 16, 32))
                       .astype(np.float32))

        mask_len = 16 // 6
        mask = to_tensor(([True] * (16 - mask_len)) + ([False] * (mask_len)))
        mask = tf.expand_dims(mask, axis=0)
        mask = tf.tile(mask, [8, 1])
        layer(ts, mask=mask)
        layer.compute_mask(ts, mask=mask)

        @tf.function
        def graph_test_fn(x, mask):
            layer(x, mask=mask)
            layer.compute_mask(x, mask=mask)

        graph_test_fn(x=ts, mask=mask)

class DynastesDepthwiseConv2DTest(tf.test.TestCase):
    def test_simple(self):
        layer_test(
            DynastesDepthwiseConv2D, kwargs={'kernel_size': (3, 3)}, input_shape=(4, 16, 16, 3))

    def test_masking(self):
        layer = DynastesDepthwiseConv2D(kernel_size=3, strides=2, padding='same', conv_mask=True)

        ts = to_tensor(normal(size=(8, 16, 16, 32))
                       .astype(np.float32))

        mask_len = 16 // 6
        mask = to_tensor(([True] * (16 - mask_len)) + ([False] * (mask_len)))
        mask = tf.expand_dims(mask, axis=0)
        mask = tf.tile(mask, [8, 1])
        mask = tf.expand_dims(mask, axis=1)
        mask = tf.tile(mask, [1, 16, 1])
        layer(ts, mask=mask)
        layer.compute_mask(ts, mask=mask)

        @tf.function
        def graph_test_fn(x, mask):
            layer(x, mask=mask)
            layer.compute_mask(x, mask=mask)

        graph_test_fn(x=ts, mask=mask)

    def test_specnorm(self):
        layer_test(
            DynastesDepthwiseConv2D, kwargs={'kernel_size': (3, 3),
                                             'kernel_normalizer': 'spectral'}, input_shape=(4, 16, 16, 5))

ext: py

max_stars_repo_name: dynastes-team/dynastes

max_issues_repo_name: veqtor/dynastes

max_forks_repo_name: veqtor/dynastes

reason: import numpy


text:
import random
import numpy as np

class Obstacle:

    def __init__(self, x,y, std_x,std_y,radius=6):
        self.x = x
        self.y = y
        self.std_x = std_x
        self.std_y = std_y
        self.radius = radius
        self.__update_state()

    def is_in_obstacle(self,x,y,buff = 0):
        return ((x-self.x_c)**2+(y-self.y_c)**2 < (self.radius+buff)**2)

    def __repr__(self):
        return "{},{}".format(self.x,self.y)

    def __update_state(self):
        X = []
        Y = []
        for i in range(100):
            X.append(random.gauss(self.x, self.std_x))
            Y.append(random.gauss(self.y, self.std_y))
        self.x_c = np.mean(np.array(X))
        self.y_c = np.mean(np.array(Y))

    def set_std(self, std_x, std_y):
        self.std_x = std_x
        self.std_y = std_y

    def set_coordinates(self, x, y):
        self.x = x
        self.y = y
        self.__update_state()

    def get_x(self):
        return self.x_c

    def get_y(self):
        return self.y_c

    def get_std(self):
        return self.std_x, self.std_y
    def get_radius(self):
        return self.radius


text:
import glob
import os
import random
import tarfile
import shutil
import hashlib
from PIL import Image, ImageOps
import numpy as np

import requests

from config import WINDOW_SIZE
from modules.integral_image import to_integral

def gamma(
    values: np.ndarray, coeff: float = 2.2
) -> np.ndarray:
    return values ** (1.0 / coeff)

def gleam(values: np.ndarray) -> np.ndarray:
    return (
        np.sum(gamma(values), axis=2)
        / values.shape[2]
    )

def to_float_array(
    img: Image.Image,
) -> np.ndarray:
    return (
        np.array(img).astype(np.float32) / 255.0
    )

def to_image(values: np.ndarray) -> Image.Image:
    return Image.fromarray(
        np.uint8(values * 255.0)
    )

def download_file(url: str, path: str):
    print("Downloading file ...")
    with requests.get(url, stream=True) as r:
        r.raise_for_status()
        with open(path, "wb") as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
    print("Download completed.")

def md5(
    path: str, chunk_size: int = 65536
) -> str:
    hash_md5 = hashlib.md5()
    with open(path, "rb") as f:
        for chunk in iter(
            lambda: f.read(chunk_size), b""
        ):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()

def untar(file_path: str, dest_path: str):
    print("Extracting file.")
    with tarfile.open(file_path, "r:gz") as f:
        f.extractall(dest_path)
    print("Extraction completed.")

def open_face(
    path: str, resize: bool = True
) -> Image.Image:
    CROP_TOP = 50

    img = Image.open(path)
    img = to_image(
        gamma(to_float_array(img)[CROP_TOP:, :])
    )
    min_size = np.min(img.size)
    img = ImageOps.fit(
        img, (min_size, min_size), Image.ANTIALIAS
    )
    if resize:
        img = img.resize(
            (WINDOW_SIZE, WINDOW_SIZE),
            Image.ANTIALIAS,
        )
    return img.convert("L")

def random_crop(img: Image.Image,) -> Image.Image:
    max_allowed_size = np.min(img.size)
    size = random.randint(
        WINDOW_SIZE, max_allowed_size
    )
    max_width = img.size[0] - size - 1
    max_height = img.size[1] - size - 1
    left = (
        0
        if (max_width <= 1)
        else random.randint(0, max_width)
    )
    top = (
        0
        if (max_height <= 1)
        else random.randint(0, max_height)
    )
    return img.crop(
        (left, top, left + size, top + size,)
    )

def open_background(
    path: str, resize: bool = True
) -> Image.Image:
    img = Image.open(path)
    img = to_image(gleam(to_float_array(img)))
    img = random_crop(img)
    if resize:
        img = img.resize(
            (WINDOW_SIZE, WINDOW_SIZE),
            Image.ANTIALIAS,
        )
    return img.convert("L")

class Dataloader:
    def __init__(
        self, dataset_path: str = "datasets/set0",
    ) -> None:
        self.dataset_path = dataset_path
        self.faces_dir = os.path.join(
            dataset_path,
            "faces_aligned_small_mirrored_co_aligned_cropped_cleaned",
        )
        self.backgrounds_dir = os.path.join(
            dataset_path, "iccv09Data"
        )

    def load(self, p: int, n: int):
        """返回样本积分图数组及其对应标签列表

        Args:
            p (int): 正样本数
            n (int): 负样本数

        """
        if not os.path.exists(self.dataset_path):
            os.mkdir(self.dataset_path)
        face_image_files = self.load_faces()
        background_image_files = (
            self.load_background()
        )
        xs = []
        xs.extend(
            [
                to_integral(to_float_array(open_face(f)))
                for f in random.sample(
                    face_image_files, p
                )
            ]
        )
        xs.extend(
            [
                to_integral(to_float_array(open_background(f)))
                for f in np.random.choice(
                    background_image_files,
                    n,
                    replace=True,
                )
            ]
        )

        ys = np.hstack(
            [np.ones((p,)), np.zeros((n,))]
        )
        return np.array(xs), ys

    def load_faces(self) -> list:
        dataset_path = self.dataset_path
        faces_url = "https://www.dropbox.com/s/ubjjoo0b2wz4vgz/faces_aligned_small_mirrored_co_aligned_cropped_cleaned.tar.gz?dl=1"
        faces_md5 = (
            "ab853c17ca6630c191457ff1fb16c1a4"
        )

        faces_archive = os.path.join(
            dataset_path,
            "faces_aligned_small_mirrored_co_aligned_cropped_cleaned.tar.gz",
        )
        faces_dir = self.faces_dir

        if (
            not os.path.exists(faces_archive)
            or md5(faces_archive) != faces_md5
        ):
            download_file(
                faces_url, faces_archive
            )

        if not os.path.exists(faces_dir):
            untar(faces_archive, dataset_path)

        return glob.glob(
            os.path.join(
                faces_dir, "**", "*.png"
            ),
            recursive=True,
        )

    def load_background(self) -> list:
        dataset_path = self.dataset_path
        backgrounds_url = "http://dags.stanford.edu/data/iccv09Data.tar.gz"
        backgrounds_md5 = (
            "f469cf0ab459d94990edcf756694f4d5"
        )

        backgrounds_archive = os.path.join(
            dataset_path, "iccv09Data.tar.gz"
        )
        backgrounds_dir = self.backgrounds_dir

        if (
            not os.path.exists(
                backgrounds_archive
            )
            or md5(backgrounds_archive)
            != backgrounds_md5
        ):
            download_file(
                backgrounds_url,
                backgrounds_archive,
            )

        if not os.path.exists(backgrounds_dir):
            untar(
                backgrounds_archive, dataset_path
            )

        return glob.glob(
            os.path.join(
                backgrounds_dir, "**", "*.jpg"
            ),
            recursive=True,
        )

max_stars_repo_name: lprintf/Viola-Jones

max_issues_repo_name: lprintf/Viola-Jones

max_forks_repo_name: lprintf/Viola-Jones


text:
import numpy as np

from PyEngine3D.Utilities import *
from PyEngine3D.Common import logger, log_level, COMMAND

from GameClient.Constants import *

EXPLOSION_00 = "explosion_00"
DAMAGE_00 = "damage_00"

class GameEffectManager:
    def __init__(self):
        self.scene_manager = None
        self.resource_manager = None
        self.game_client = None
        self.effect_instances = []

    def initialize(self, game_client):
        self.scene_manager = game_client.scene_manager
        self.resource_manager = game_client.resource_manager
        self.game_client = game_client

    def create_explosion_particle(self, pos):
        effect_info = self.scene_manager.add_effect(name=EXPLOSION_00, effect_info=EXPLOSION_00, pos=pos, scale=(10.0, 10.0, 10.0))
        self.effect_instances.append(effect_info)

    def create_damage_particle(self, pos):
        effect_info = self.scene_manager.add_effect(name=DAMAGE_00, effect_info=DAMAGE_00, pos=pos, scale=(1.0, 1.0, 1.0))
        self.effect_instances.append(effect_info)

    def update(self):
        index = 0
        effect_count = len(self.effect_instances)
        for i in range(effect_count):
            effct_instance = self.effect_instances[index]
            if effct_instance.alive:
                index += 1
            else:
                self.scene_manager.delete_object(effct_instance.name)
                self.effect_instances.pop(index)

max_stars_repo_name: ubuntunux/Landseair

max_issues_repo_name: ubuntunux/Landseair

max_forks_repo_name: ubuntunux/Landseair


text:
"""UI container module."""

__all__ = ["Panel2D", "TabPanel2D", "TabUI", "ImageContainer2D",
           "GridUI"]

import numpy as np
import vtk

from fury.io import load_image
from fury.ui.core import UI, Rectangle2D, TextBlock2D
from fury.utils import set_input, rotate
from fury.actor import grid

class Panel2D(UI):
    """A 2D UI Panel.

    Can contain one or more UI elements.

    Attributes
    ----------
    alignment : [left, right]
        Alignment of the panel with respect to the overall screen.
    """

    def __init__(self, size, position=(0, 0), color=(0.1, 0.1, 0.1),
                 opacity=0.7, align="left"):
        """Init class instance.

        Parameters
        ----------
        size : (int, int)
            Size (width, height) in pixels of the panel.
        position : (float, float)
            Absolute coordinates (x, y) of the lower-left corner of the panel.
        color : (float, float, float)
            Must take values in [0, 1].
        opacity : float
            Must take values in [0, 1].
        align : [left, right]
            Alignment of the panel with respect to the overall screen.
        """
        super(Panel2D, self).__init__(position)
        self.resize(size)
        self.alignment = align
        self.color = color
        self.opacity = opacity
        self.position = position
        self._drag_offset = None

    def _setup(self):
        """Setup this UI component.

        Create the background (Rectangle2D) of the panel.

        """
        self._elements = []
        self.element_offsets = []
        self.background = Rectangle2D()
        self.add_element(self.background, (0, 0))

        # Add default events listener for this UI component.
        self.background.on_left_mouse_button_pressed = self.left_button_pressed
        self.background.on_left_mouse_button_dragged = self.left_button_dragged

    def _get_actors(self):
        """Get the actors composing this UI component."""
        actors = []
        for element in self._elements:
            actors += element.actors

        return actors

    def _add_to_scene(self, scene):
        """Add all subcomponents or VTK props that compose this UI component.

        Parameters
        ----------
        scene : scene

        """
        for element in self._elements:
            element.add_to_scene(scene)

    def _get_size(self):
        return self.background.size

    def resize(self, size):
        """Set the panel size.

        Parameters
        ----------
        size : (float, float)
            Panel size (width, height) in pixels.

        """
        self.background.resize(size)

    def _set_position(self, coords):
        """Set the lower-left corner position of this UI component.

        Parameters
        ----------
        coords: (float, float)
            Absolute pixel coordinates (x, y).

        """
        coords = np.array(coords)
        for element, offset in self.element_offsets:
            element.position = coords + offset

    @property
    def color(self):
        return self.background.color

    @color.setter
    def color(self, color):
        self.background.color = color

    @property
    def opacity(self):
        return self.background.opacity

    @opacity.setter
    def opacity(self, opacity):
        self.background.opacity = opacity

    def add_element(self, element, coords, anchor="position"):
        """Add a UI component to the panel.

        The coordinates represent an offset from the lower left corner of the
        panel.

        Parameters
        ----------
        element : UI
            The UI item to be added.
        coords : (float, float) or (int, int)
            If float, normalized coordinates are assumed and they must be
            between [0,1].
            If int, pixels coordinates are assumed and it must fit within the
            panel's size.

        """
        coords = np.array(coords)

        if np.issubdtype(coords.dtype, np.floating):
            if np.any(coords < 0) or np.any(coords > 1):
                raise ValueError("Normalized coordinates must be in [0,1].")

            coords = coords * self.size

        if anchor == "center":
            element.center = self.position + coords
        elif anchor == "position":
            element.position = self.position + coords
        else:
            msg = ("Unknown anchor {}. Supported anchors are 'position'"
                   " and 'center'.")
            raise ValueError(msg)

        self._elements.append(element)
        offset = element.position - self.position
        self.element_offsets.append((element, offset))

    def remove_element(self, element):
        """Remove a UI component from the panel.

        Parameters
        ----------
        element : UI
            The UI item to be removed.
        """
        idx = self._elements.index(element)
        del self._elements[idx]
        del self.element_offsets[idx]

    def update_element(self, element, coords, anchor="position"):
        """Update the position of a UI component in the panel.

        Parameters
        ----------
        element : UI
            The UI item to be updated.
        coords : (float, float) or (int, int)
            New coordinates.
            If float, normalized coordinates are assumed and they must be
            between [0,1].
            If int, pixels coordinates are assumed and it must fit within the
            panel's size.
        """
        self.remove_element(element)
        self.add_element(element, coords, anchor)

    def left_button_pressed(self, i_ren, _obj, panel2d_object):
        click_pos = np.array(i_ren.event.position)
        self._drag_offset = click_pos - panel2d_object.position
        i_ren.event.abort()  # Stop propagating the event.

    def left_button_dragged(self, i_ren, _obj, _panel2d_object):
        if self._drag_offset is not None:
            click_position = np.array(i_ren.event.position)
            new_position = click_position - self._drag_offset
            self.position = new_position
        i_ren.force_render()

    def re_align(self, window_size_change):
        """Re-organise the elements in case the window size is changed.

        Parameters
        ----------
        window_size_change : (int, int)
            New window size (width, height) in pixels.
        """
        if self.alignment == "left":
            pass
        elif self.alignment == "right":
            self.position += np.array(window_size_change)
        else:
            msg = "You can only left-align or right-align objects in a panel."
            raise ValueError(msg)

class TabPanel2D(UI):
    """Render content within a Tab.

    Attributes
    ----------
    content_panel: :class: 'Panel2D'
        Hold all the content UI components.
    text_block: :class: 'TextBlock2D'
        Renders the title of the tab.
    """

    def __init__(self, position=(0, 0), size=(100, 100),
                 title="New Tab", color=(0.5, 0.5, 0.5), content_panel=None):
        """Init class instance.

        Parameters
        ----------
        position : (float, float)
            Absolute coordinates (x, y) of the lower-left corner of the
            UI component
        size : (int, int)
            Width and height of the pixels of this UI component.
        title : str
            Renders the title for Tab panel.
        color : list of 3 floats
            Background color of tab panel.
        content_panel : Panel2D
            Panel consisting of the content UI elements.
        """
        self.content_panel = content_panel
        self.panel_size = size
        self._text_size = (int(1.0 * size[0]), size[1])

        super(TabPanel2D, self).__init__()
        self.title = title
        self.panel.position = position
        self.color = color

    def _setup(self):
        """Setup this UI component.

        Create parent panel.
        Create Text to hold tab information.
        Create Button to close tab.

        """
        self.panel = Panel2D(size=self.panel_size)
        self.text_block = TextBlock2D(size=self._text_size,
                                      color=(0, 0, 0))
        self.panel.add_element(self.text_block, (0, 0))

    def _get_actors(self):
        """Get the actors composing this UI component."""
        return self.panel.actors + self.content_panel.actors

    def _add_to_scene(self, _scene):
        """Add all subcomponents or VTK props that compose this UI component.

        Parameters
        ----------
        scene : scene

        """
        self.panel.add_to_scene(_scene)
        self.content_panel.add_to_scene(_scene)

    def _set_position(self, _coords):
        """Set the lower-left corner position of this UI component.

        Parameters
        ----------
        coords: (float, float)
            Absolute pixel coordinates (x, y).

        """
        self.panel.position = _coords

    def _get_size(self):
        self.panel.size

    def resize(self, size):
        """Resize Tab panel.

        Parameters
        ----------
        size : (int, int)
            New width and height in pixels.

        """
        self._text_size = (int(0.7 * size[0]), size[1])
        self._button_size = (int(0.3 * size[0]), size[1])
        self.panel.resize(size)
        self.text_block.resize(self._text_size)

    @property
    def color(self):
        """Return the background color of tab panel."""
        return self.panel.color

    @color.setter
    def color(self, color):
        """Set background color of tab panel.

        Parameters
        ----------
        color : list of 3 floats.
        """
        self.panel.color = color

    @property
    def title(self):
        """Return the title of tab panel."""
        return self.text_block.message

    @title.setter
    def title(self, text):
        """Set the title of tab panel.

        Parameters
        ----------
        text : str
            New title for tab panel.
        """
        self.text_block.message = text

    def add_element(self, element, coords, anchor="position"):
        """Add a UI component to the content panel.

        The coordinates represent an offset from the lower left corner of the
        panel.

        Parameters
        ----------
        element : UI
            The UI item to be added.
        coords : (float, float) or (int, int)
            If float, normalized coordinates are assumed and they must be
            between [0,1].
            If int, pixels coordinates are assumed and it must fit within the
            panel's size.
        """
        element.set_visibility(False)
        self.content_panel.add_element(element, coords, anchor)

    def remove_element(self, element):
        """Remove a UI component from the content panel.

        Parameters
        ----------
        element : UI
            The UI item to be removed.
        """
        self.content_panel.remove_element(element)

    def update_element(self, element, coords, anchor="position"):
        """Update the position of a UI component in the content panel.

        Parameters
        ----------
        element : UI
            The UI item to be updated.
        coords : (float, float) or (int, int)
            New coordinates.
            If float, normalized coordinates are assumed and they must be
            between [0,1].
            If int, pixels coordinates are assumed and it must fit within the
            panel's size.
        """
        self.content_panel.update_element(element, coords, anchor="position")

class TabUI(UI):
    """UI element to add multiple panels within a single window.

    Attributes
    ----------
    tabs: :class: List of 'TabPanel2D'
        Stores all the instances of 'TabPanel2D' that renderes the contents.
    """

    def __init__(self, position=(0, 0), size=(100, 100), nb_tabs=1,
                 active_color=(1, 1, 1), inactive_color=(0.5, 0.5, 0.5),
                 draggable=False):
        """Init class instance.

        Parameters
        ----------
        position : (float, float)
            Absolute coordinates (x, y) of the lower-left corner of this
            UI component.
        size : (int, int)
            Width and height in pixels of this UI component.
        nb_tabs : int
            Number of tabs to be renders.
        active_color : tuple of 3 floats.
            Background color of active tab panel.
        inactive_color : tuple of 3 floats.
            Background color of inactive tab panels.
        draggable : bool
            Whether the UI element is draggable or not.
        """
        self.tabs = []
        self.nb_tabs = nb_tabs
        self.parent_size = size
        self.content_size = (size[0], int(0.9 * size[1]))
        self.draggable = draggable
        self.active_color = active_color
        self.inactive_color = inactive_color
        self.active_tab_idx = None
        self.collapsed = True

        super(TabUI, self).__init__()
        self.position = position

    def _setup(self):
        """Setup this UI component.

        Create parent panel.
        Create tab panels.
        """
        self.parent_panel = Panel2D(self.parent_size, opacity=0.0)

        # Offer some standard hooks to the user.
        self.on_change = lambda ui: None
        self.on_collapse = lambda ui: None

        for _ in range(self.nb_tabs):
            content_panel = Panel2D(size=self.content_size)
            content_panel.set_visibility(False)
            tab_panel = TabPanel2D(content_panel=content_panel)
            self.tabs.append(tab_panel)
        self.update_tabs()

    def _get_actors(self):
        """Get the actors composing this UI component."""
        actors = []
        actors += self.parent_panel.actors
        for tab_panel in self.tabs:
            actors += tab_panel.actors

        return actors

    def _add_to_scene(self, _scene):
        """Add all subcomponents or VTK props that compose this UI component.

        Parameters
        ----------
        scene : scene

        """
        self.parent_panel.add_to_scene(_scene)
        for tab_panel in self.tabs:
            tab_panel.add_to_scene(_scene)

    def _set_position(self, _coords):
        """Set the lower-left corner position of this UI component.

        Parameters
        ----------
        coords: (float, float)
            Absolute pixel coordinates (x, y).

        """
        self.parent_panel.position = _coords

    def _get_size(self):
        return self.parent_panel.size

    def update_tabs(self):
        """Update position, size and callbacks for tab panels."""
        self.tab_panel_size =\
            (self.size[0] // self.nb_tabs, int(0.1*self.size[1]))

        tab_panel_pos = [0.0, 0.9]
        for tab_panel in self.tabs:
            tab_panel.resize(self.tab_panel_size)
            tab_panel.content_panel.position = self.position

            content_panel = tab_panel.content_panel
            if self.draggable:
                tab_panel.panel.background.on_left_mouse_button_pressed =\
                    self.left_button_pressed
                content_panel.background.on_left_mouse_button_pressed =\
                    self.left_button_pressed
                tab_panel.text_block.on_left_mouse_button_pressed =\
                    self.left_button_pressed

                tab_panel.panel.background.on_left_mouse_button_dragged =\
                    self.left_button_dragged
                content_panel.background.on_left_mouse_button_dragged =\
                    self.left_button_dragged
                tab_panel.text_block.on_left_mouse_button_dragged =\
                    self.left_button_dragged
            else:
                tab_panel.panel.background.on_left_mouse_button_dragged =\
                    lambda i_ren, _obj, _comp: i_ren.force_render
                content_panel.background.on_left_mouse_button_dragged =\
                    lambda i_ren, _obj, _comp: i_ren.force_render

            tab_panel.text_block.on_left_mouse_button_clicked =\
                self.select_tab_callback
            tab_panel.panel.background.on_left_mouse_button_clicked =\
                self.select_tab_callback

            tab_panel.text_block.on_right_mouse_button_clicked =\
                self.collapse_tab_ui
            tab_panel.panel.background.on_right_mouse_button_clicked =\
                self.collapse_tab_ui

            tab_panel.content_panel.resize(self.content_size)
            self.parent_panel.add_element(tab_panel, tab_panel_pos)
            self.parent_panel.add_element(tab_panel.content_panel, (0.0, 0.0))
            tab_panel_pos[0] += 1/self.nb_tabs

    def select_tab_callback(self, iren, _obj, _tab_comp):
        """Handle events when a tab is selected."""
        for idx, tab_panel in enumerate(self.tabs):
            if tab_panel.text_block is not _tab_comp and\
               tab_panel.panel.background is not _tab_comp:
                tab_panel.color = self.inactive_color
                tab_panel.content_panel.set_visibility(False)
            else:
                tab_panel.color = self.active_color
                tab_panel.content_panel.set_visibility(True)
                self.active_tab_idx = idx

        self.collapsed = False
        self.on_change(self)
        iren.force_render()
        iren.event.abort()

    def collapse_tab_ui(self, iren, _obj, _tab_comp):
        """Handle events when Tab UI is collapsed."""
        if self.active_tab_idx is not None:
            active_tab_panel = self.tabs[self.active_tab_idx]
            active_tab_panel.color = self.inactive_color
            active_tab_panel.content_panel.set_visibility(False)
        self.active_tab_idx = None
        self.collapsed = True
        self.on_collapse(self)
        iren.force_render()
        iren.event.abort()

    def add_element(self, tab_idx, element, coords, anchor="position"):
        """Add element to content panel after checking its existence."""
        if tab_idx < self.nb_tabs and tab_idx >= 0:
            self.tabs[tab_idx].add_element(element, coords, anchor)
        else:
            raise IndexError("Tab with index "
                             "{} does not exist".format(tab_idx))

    def remove_element(self, tab_idx, element):
        """Remove element from content panel after checking its existence."""
        if tab_idx < self.nb_tabs and tab_idx >= 0:
            self.tabs[tab_idx].remove_element(element)
        else:
            raise IndexError("Tab with index "
                             "{} does not exist".format(tab_idx))

    def update_element(self, tab_idx, element, coords, anchor="position"):
        """Update element on content panel after checking its existence."""
        if tab_idx < self.nb_tabs and tab_idx >= 0:
            self.tabs[tab_idx].update_element(element, coords, anchor)
        else:
            raise IndexError("Tab with index "
                             "{} does not exist".format(tab_idx))

    def left_button_pressed(self, i_ren, _obj, _sub_component):
        click_pos = np.array(i_ren.event.position)
        self._click_position = click_pos
        i_ren.event.abort()  # Stop propagating the event.

    def left_button_dragged(self, i_ren, _obj, _sub_component):
        click_position = np.array(i_ren.event.position)
        change = click_position - self._click_position
        self.parent_panel.position += change
        self._click_position = click_position
        i_ren.force_render()

class ImageContainer2D(UI):
    """A 2D container to hold an image.

    Currently Supports:
    - png and jpg/jpeg images

    Attributes
    ----------
    size: (float, float)
        Image size (width, height) in pixels.
    img : vtkImageDataGeometryFilters
        The image loaded from the specified path.

    """

    def __init__(self, img_path, position=(0, 0), size=(100, 100)):
        """Init class instance.

        Parameters
        ----------
        img_path : string
            URL or local path of the image
        position : (float, float), optional
            Absolute coordinates (x, y) of the lower-left corner of the image.
        size : (int, int), optional
            Width and height in pixels of the image.
        """
        super(ImageContainer2D, self).__init__(position)
        self.img = load_image(img_path, as_vtktype=True)
        self.set_img(self.img)
        self.resize(size)

    def _get_size(self):
        lower_left_corner = self.texture_points.GetPoint(0)
        upper_right_corner = self.texture_points.GetPoint(2)
        size = np.array(upper_right_corner) - np.array(lower_left_corner)
        return abs(size[:2])

    def _setup(self):
        """Setup this UI Component.

        Return an image as a 2D actor with a specific position.

        Returns
        -------
        :class:`vtkTexturedActor2D`
        """
        self.texture_polydata = vtk.vtkPolyData()
        self.texture_points = vtk.vtkPoints()
        self.texture_points.SetNumberOfPoints(4)

        polys = vtk.vtkCellArray()
        polys.InsertNextCell(4)
        polys.InsertCellPoint(0)
        polys.InsertCellPoint(1)
        polys.InsertCellPoint(2)
        polys.InsertCellPoint(3)
        self.texture_polydata.SetPolys(polys)

        tc = vtk.vtkFloatArray()
        tc.SetNumberOfComponents(2)
        tc.SetNumberOfTuples(4)
        tc.InsertComponent(0, 0, 0.0)
        tc.InsertComponent(0, 1, 0.0)
        tc.InsertComponent(1, 0, 1.0)
        tc.InsertComponent(1, 1, 0.0)
        tc.InsertComponent(2, 0, 1.0)
        tc.InsertComponent(2, 1, 1.0)
        tc.InsertComponent(3, 0, 0.0)
        tc.InsertComponent(3, 1, 1.0)
        self.texture_polydata.GetPointData().SetTCoords(tc)

        texture_mapper = vtk.vtkPolyDataMapper2D()
        texture_mapper = set_input(texture_mapper, self.texture_polydata)

        image = vtk.vtkTexturedActor2D()
        image.SetMapper(texture_mapper)

        self.texture = vtk.vtkTexture()
        image.SetTexture(self.texture)

        image_property = vtk.vtkProperty2D()
        image_property.SetOpacity(1.0)
        image.SetProperty(image_property)
        self.actor = image

        # Add default events listener to the VTK actor.
        self.handle_events(self.actor)

    def _get_actors(self):
        """Return the actors that compose this UI component."""
        return [self.actor]

    def _add_to_scene(self, scene):
        """Add all subcomponents or VTK props that compose this UI component.

        Parameters
        ----------
        scene : scene
        """
        scene.add(self.actor)

    def resize(self, size):
        """Resize the image.

        Parameters
        ----------
        size : (float, float)
            image size (width, height) in pixels.
        """
        # Update actor.
        self.texture_points.SetPoint(0, 0, 0, 0.0)
        self.texture_points.SetPoint(1, size[0], 0, 0.0)
        self.texture_points.SetPoint(2, size[0], size[1], 0.0)
        self.texture_points.SetPoint(3, 0, size[1], 0.0)
        self.texture_polydata.SetPoints(self.texture_points)

    def _set_position(self, coords):
        """Set the lower-left corner position of this UI component.

        Parameters
        ----------
        coords: (float, float)
            Absolute pixel coordinates (x, y).
        """
        self.actor.SetPosition(*coords)

    def scale(self, factor):
        """Scale the image.

        Parameters
        ----------
        factor : (float, float)
            Scaling factor (width, height) in pixels.
        """
        self.resize(self.size * factor)

    def set_img(self, img):
        """Modify the image used by the vtkTexturedActor2D.

        Parameters
        ----------
        img : imageDataGeometryFilter

        """
        self.texture = set_input(self.texture, img)

class GridUI(UI):
    """Add actors in a grid and interact with them individually."""

    def __init__(self,
                 actors, captions=None, caption_offset=(0, -100, 0),
                 cell_padding=0,
                 cell_shape="rect", aspect_ratio=16/9., dim=None,
                 rotation_speed=1, rotation_axis=(0, 1, 0)):

        # TODO: add rotation axis None by default

        self.container = grid(actors, captions=captions,
                              caption_offset=caption_offset,
                              cell_padding=cell_padding,
                              cell_shape=cell_shape,
                              aspect_ratio=aspect_ratio, dim=dim)
        self._actors = []
        self._actors_dict = {}
        self.rotation_speed = rotation_speed
        self.rotation_axis = rotation_axis

        for item in self.container._items:
            self._actors.append(item._items[0])
            self._actors_dict[item._items[0]] = {'x': -np.inf, 'y': -np.inf}

        super(GridUI, self).__init__(position=(0, 0, 0))

    def _get_size(self):
        return

    @staticmethod
    def left_click_callback(istyle, _obj, _what):
        istyle.trackball_actor.OnLeftButtonDown()
        istyle.force_render()
        istyle.event.abort()

    @staticmethod
    def left_release_callback(istyle, _obj, _what):

        istyle.trackball_actor.OnLeftButtonUp()
        istyle.force_render()
        istyle.event.abort()

    @staticmethod
    def mouse_move_callback(istyle, _obj, _what):
        istyle.trackball_actor.OnMouseMove()
        istyle.force_render()
        istyle.event.abort()

    @staticmethod
    def left_click_callback2(istyle, obj, self):

        rx, ry, rz = self.rotation_axis
        clockwise_rotation = np.array([self.rotation_speed,
                                       rx, ry, rz])
        rotate(obj, clockwise_rotation)

        istyle.force_render()
        istyle.event.abort()

    @staticmethod
    def left_release_callback2(istyle, _obj, _what):

        istyle.force_render()
        istyle.event.abort()

    @staticmethod
    def mouse_move_callback2(istyle, obj, self):

        if self._actors_dict[obj]['y'] == - np.inf:

            iren = istyle.GetInteractor()
            event_pos = iren.GetEventPosition()
            self._actors_dict[obj]['y'] = event_pos[1]

        else:

            iren = istyle.GetInteractor()
            event_pos = iren.GetEventPosition()
            rx, ry, rz = self.rotation_axis

            if event_pos[1] >= self._actors_dict[obj]['y']:
                clockwise_rotation = np.array([-self.rotation_speed,
                                               rx, ry, rz])
                rotate(obj, clockwise_rotation)
            else:
                anti_clockwise_rotation = np.array(
                    [self.rotation_speed, rx, ry, rz])
                rotate(obj, anti_clockwise_rotation)

            self._actors_dict[obj]['y'] = event_pos[1]

            istyle.force_render()
            istyle.event.abort()

    ANTICLOCKWISE_ROTATION_Y = np.array([-10, 0, 1, 0])
    CLOCKWISE_ROTATION_Y = np.array([10, 0, 1, 0])
    ANTICLOCKWISE_ROTATION_X = np.array([-10, 1, 0, 0])
    CLOCKWISE_ROTATION_X = np.array([10, 1, 0, 0])

    def key_press_callback(self, istyle, obj, _what):
        has_changed = False
        if istyle.event.key == "Left":
            has_changed = True
            for a in self._actors:
                rotate(a, self.ANTICLOCKWISE_ROTATION_Y)
        elif istyle.event.key == "Right":
            has_changed = True
            for a in self._actors:
                rotate(a, self.CLOCKWISE_ROTATION_Y)
        elif istyle.event.key == "Up":
            has_changed = True
            for a in self._actors:
                rotate(a, self.ANTICLOCKWISE_ROTATION_X)
        elif istyle.event.key == "Down":
            has_changed = True
            for a in self._actors:
                rotate(a, self.CLOCKWISE_ROTATION_X)

        if has_changed:
            istyle.force_render()

    def _setup(self):
        """Set up this UI component and the events of its actor."""
        # Add default events listener to the VTK actor.
        for actor in self._actors:
            # self.handle_events(actor)

            if self.rotation_axis is None:
                self.add_callback(actor, "LeftButtonPressEvent",
                                  self.left_click_callback)
                self.add_callback(actor, "LeftButtonReleaseEvent",
                                  self.left_release_callback)
                self.add_callback(actor, "MouseMoveEvent",
                                  self.mouse_move_callback)
            else:
                self.add_callback(actor, "LeftButtonPressEvent",
                                  self.left_click_callback2)
                # TODO: possibly add this too
                self.add_callback(actor, "LeftButtonReleaseEvent",
                                  self.left_release_callback2)
                self.add_callback(actor, "MouseMoveEvent",
                                  self.mouse_move_callback2)

            # TODO: this is currently not running
            self.add_callback(actor, "KeyPressEvent",
                              self.key_press_callback)
        # self.on_key_press = self.key_press_callback2

    def _get_actors(self):
        """Get the actors composing this UI component."""
        return self._actors

    def _add_to_scene(self, scene):
        """Add all subcomponents or VTK props that compose this UI component.

        Parameters
        ----------
        scene : scene

        """
        self.container.add_to_scene(scene)

    def resize(self, size):
        """Resize the button.

        Parameters
        ----------
        size : (float, float)
            Button size (width, height) in pixels.

        """
        # Update actor.
        pass

    def _set_position(self, coords):
        """Set the lower-left corner position of this UI component.

        Parameters
        ----------
        coords: (float, float)
            Absolute pixel coordinates (x, y).
        """
        # coords = (0, 0, 0)
        pass
        # self.actor.SetPosition(*coords)
        # self.container.SetPosition(*coords)

max_stars_repo_name: mlraglin/fury

max_issues_repo_name: mlraglin/fury

max_forks_repo_name: mlraglin/fury


text:
'''
Specifies examples of the full set of parameters required to solve various
consumption-saving models.
'''
from copy import copy
import numpy as np

# -----------------------------------------------------------------------------
# --- Define all of the parameters for the perfect foresight model ------------
# -----------------------------------------------------------------------------

CRRA = 4.0                          # Coefficient of relative risk aversion
Rfree = 1.03                        # Interest factor on assets
DiscFac = [0.96]                    # Intertemporal discount factor
LivPrb = [0.98]                     # Survival probability
PermGroFac = [1.01]                 # Permanent income growth factor
Nagents = 10000                     # Number of agents of this type (only matters for simulation)

# Make a dictionary to specify a perfect foresight consumer type
init_perfect_foresight = { 'CRRA': CRRA,
                           'Rfree': Rfree,
                           'DiscFac': DiscFac,
                           'LivPrb': LivPrb,
                           'PermGroFac': PermGroFac,
                           'Nagents': Nagents
                          }

# -----------------------------------------------------------------------------
# --- Define additional parameters for the idiosyncratic shocks model ---------
# -----------------------------------------------------------------------------

# Parameters for constructing the "assets above minimum" grid
exp_nest = 3                        # Number of times to "exponentially nest" when constructing a_grid
aXtraMin = 0.001                    # Minimum end-of-period "assets above minimum" value
aXtraMax = 20                       # Minimum end-of-period "assets above minimum" value
aXtraHuge = None                    # A very large value of assets to add to the grid, not used
aXtraExtra = None                   # Some other value of assets to add to the grid, not used
aXtraCount = 12                     # Number of points in the grid of "assets above minimum"

# Parameters describing the income process
PermShkCount = 7                    # Number of points in discrete approximation to permanent income shocks
TranShkCount = 7                    # Number of points in discrete approximation to transitory income shocks
PermShkStd = [0.1]                  # Standard deviation of log permanent income shocks
TranShkStd = [0.1]                  # Standard deviation of log transitory income shocks
UnempPrb = 0.05                     # Probability of unemployment while working
UnempPrbRet = 0.005                 # Probability of "unemployment" while retired
IncUnemp = 0.3                      # Unemployment benefits replacement rate
IncUnempRet = 0.0                   # "Unemployment" benefits when retired

# A few other parameters
BoroCnstArt = 0.0                   # Artificial borrowing constraint; imposed minimum level of end-of period assets
tax_rate = 0.0                      # Flat income tax rate
CubicBool = True                    # Use cubic spline interpolation when True, linear interpolation when False
vFuncBool = False                   # Whether to calculate the value function during solution
T_total = 1                         # Total number of periods in cycle for this agent
T_retire = 0                        # Period of retirement (0 --> no retirement)

# Make a dictionary to specify an idiosyncratic income shocks consumer
init_idiosyncratic_shocks = { 'CRRA': CRRA,
                              'Rfree': Rfree,
                              'DiscFac': DiscFac,
                              'LivPrb': LivPrb,
                              'PermGroFac': PermGroFac,
                              'Nagents': Nagents,
                              'exp_nest': exp_nest,
                              'aXtraMin': aXtraMin,
                              'aXtraMax': aXtraMax,
                              'aXtraCount': aXtraCount,
                              'aXtraExtra': [aXtraExtra,aXtraHuge],
                              'PermShkStd': PermShkStd,
                              'PermShkCount': PermShkCount,
                              'TranShkStd': TranShkStd,
                              'TranShkCount': TranShkCount,
                              'UnempPrb': UnempPrb,
                              'UnempPrbRet': UnempPrbRet,
                              'IncUnemp': IncUnemp,
                              'IncUnempRet': IncUnempRet,
                              'BoroCnstArt': BoroCnstArt,
                              'tax_rate':0.0,
                              'vFuncBool':vFuncBool,
                              'CubicBool':CubicBool,
                              'T_retire':T_retire,
                              'T_total':T_total
                             }

# Make a dictionary to specify a lifecycle consumer with a finite horizon
init_lifecycle = copy(init_idiosyncratic_shocks)
init_lifecycle['DiscFac'] = 10*DiscFac
init_lifecycle['PermGroFac'] = [1.01,1.01,1.01,1.01,1.01,1.02,1.02,1.02,1.02,1.02]
init_lifecycle['PermShkStd'] = [0.1,0.2,0.1,0.2,0.1,0.2,0.1,0,0,0]
init_lifecycle['TranShkStd'] = [0.3,0.2,0.1,0.3,0.2,0.1,0.3,0,0,0]
init_lifecycle['LivPrb']     = [0.99,0.98,0.97,0.96,0.95,0.94,0.93,0.92,0.91,0.90]
init_lifecycle['T_total']    = 10
init_lifecycle['T_retire']   = 7

# Make a dictionary to specify an infinite consumer with a four period cycle
init_cyclical = copy(init_idiosyncratic_shocks)
init_cyclical['DiscFac'] = 4*DiscFac
init_cyclical['PermGroFac'] = [1.082251, 2.8, 0.3, 1.1]
init_cyclical['PermShkStd'] = [0.1,0.1,0.1,0.1]
init_cyclical['TranShkStd'] = [0.1,0.1,0.1,0.1]
init_cyclical['LivPrb']     = 4*[0.98]
init_cyclical['T_total']    = 4

# -----------------------------------------------------------------------------
# -------- Define additional parameters for the "kinked R" model --------------
# -----------------------------------------------------------------------------

Rboro = 1.20           # Interest factor on assets when borrowing, a < 0
Rsave = 1.01           # Interest factor on assets when saving, a > 0

# Make a dictionary to specify a "kinked R" idiosyncratic shock consumer
init_kinked_R = copy(init_idiosyncratic_shocks)
del init_kinked_R['Rfree'] # get rid of constant interest factor
init_kinked_R['Rboro'] = Rboro
init_kinked_R['Rsave'] = Rsave
init_kinked_R['BoroCnstArt'] = None # kinked R is a bit silly if borrowing not allowed
init_kinked_R['aXtraCount'] = 48
init_kinked_R['CubicBool'] = False # kinked R currently only compatible with linear cFunc

# -----------------------------------------------------------------------------
# ----- Define additional parameters for the preference shock model -----------
# -----------------------------------------------------------------------------

PrefShkCount = 12        # Number of points in discrete approximation to preference shock dist
PrefShk_tail_N = 4       # Number of "tail points" on each end of pref shock dist
PrefShkStd = [0.30]      # Standard deviation of utility shocks

# Make a dictionary to specify a preference shock consumer
init_preference_shocks = copy(init_kinked_R)
init_preference_shocks['PrefShkCount'] = PrefShkCount
init_preference_shocks['PrefShk_tail_N'] = PrefShk_tail_N
init_preference_shocks['PrefShkStd'] = PrefShkStd

# -----------------------------------------------------------------------------
# ----- Define additional parameters for the aggregate shocks model -----------
# -----------------------------------------------------------------------------
kGrid = np.array([0.01,0.1,0.3,0.6,0.8,0.9,0.98,1.0,1.02,1.1,1.2,1.6,2.0])  # Grid of capital-to-labor-ratios
kNextFunc = lambda k : 0.1 + 0.9*k    # Beliefs about next period's capital ratio as a function of this period's
Rfunc = lambda k : 1.0/k              # Interest factor on assets as a function of capital-to-labor ratio
wFunc = lambda k : k                  # Wage rate as a function of capital-to-labor ratio

# Make a dictionary to specify an aggregate shocks consumer


text:
import mujoco_py
import numpy as np
from rlkit.envs.mujoco_env import MujocoEnv
import mujoco_py

class Mani2dEnv(MujocoEnv):
    def __init__(self):
        self.init_serialization(locals())
        xml_path = "2dmanipulator.xml"

        super().__init__(
            xml_path,
            frame_skip=5,
            automatically_set_obs_and_action_space=True,
        )
    def step(self, a):
        cur_obs = self._get_obs()
        dist = np.linalg.norm(cur_obs[7:9] - cur_obs[-2:])

        reward = - dist
        # reward = 1.0 if dist < 0.01 else 0.0

        self.do_simulation(a, self.frame_skip)

        return self._get_obs(), reward, False, {}

    def _get_obs(self):
        qpos = self.sim.data.qpos.copy()
        qvel = self.sim.data.qvel.copy()

        robot_obs = np.hstack([qpos[:3], qvel[:3]])
        obj_obs = np.hstack([qpos[4:], qvel[4:]])
        target_obs = self.sim.data.site_xpos[0, :2]
        return np.concatenate([
            robot_obs,
            obj_obs,
            # target_obs,
        ])

    def reset_model(self):
        target_xy = list(self.np_random.uniform(low=np.array([-0.2, -0.2]), high=np.array([0.2, 0.2])))
        site_pos = self.model.body_pos
        site_pos = np.array(site_pos)
        site_pos[-1, :] = np.array([target_xy + [0.01]])
        self.model.body_pos[:] = site_pos

        high = np.array([np.pi, np.pi/2.0, np.pi/2.0, np.pi/2.0, 0.2, 0.2])
        qpos = self.init_qpos + self.np_random.uniform(low=-high, high=high)
        qvel = self.init_qvel #+ self.np_random.randn(self.model.nv) * .1

        self.set_state(qpos, qvel)
        return self._get_obs()

if __name__=="__main__":
    env = Mani2dEnv()
    print(env.observation_space.shape)
    # env.reset()
    while True:
        env.render()
        print(env.step([1., 0.0] + [np.random.randn()])[1])

max_stars_repo_name: DonghyunSung-MS/RlkitExtension

max_issues_repo_name: DonghyunSung-MS/RlkitExtension

max_forks_repo_name: DonghyunSung-MS/RlkitExtension


text:
"""
2021 Simon Bing, ETHZ, MPI IS
"""
import numpy as np
from sklearn.preprocessing import StandardScaler
from healthgen.data_access.preprocessing.base_processor import BaseProcessor

class Standardize(BaseProcessor):
    def __init__(self):
        super().__init__()

    def transform(self, x):
        """
        Standardize input features.

        Args:
            x: dict with X_train, X_val, X_test arrays of dim.: [N, features, time_len]

        Returns:
            X_dict_tf: dict of transformed inputs.
        """
        # Check if missing values mask exists and replace zeros with nans
        if 'm_train' in x.keys():
            X_train = np.where(x['m_train'], x['X_train'], np.nan)
            X_val = np.where(x['m_val'], x['X_val'], np.nan)
            X_test = np.where(x['m_test'], x['X_test'], np.nan)
        else:
            X_train = x['X_train']
            X_val = x['X_val']
            X_test = x['X_test']

        n_features = X_train.shape[1]
        time_len = X_train.shape[2]

        # Flatten time dimension
        X_train_reshape = np.reshape(X_train.transpose(0,2,1), (-1, n_features))
        X_val_reshape = np.reshape(X_val.transpose(0, 2, 1), (-1, n_features))
        X_test_reshape = np.reshape(X_test.transpose(0, 2, 1), (-1, n_features))

        # Fit scaler
        scaler = StandardScaler()
        scaler.fit(X_train_reshape)

        # Transform data
        X_train_reshape_tf = scaler.transform(X_train_reshape)
        X_val_reshape_tf = scaler.transform(X_val_reshape)
        X_test_reshape_tf = scaler.transform(X_test_reshape)

        # Recover original dimensions
        X_train_tf = np.reshape(X_train_reshape_tf, X_train.transpose(0,2,1).shape).transpose(0,2,1)
        X_val_tf = np.reshape(X_val_reshape_tf, X_val.transpose(0, 2, 1).shape).transpose(0,2,1)
        X_test_tf = np.reshape(X_test_reshape_tf, X_test.transpose(0, 2, 1).shape).transpose(0,2,1)

        X_dict_tf = x.copy()
        X_dict_tf['X_train'] = np.nan_to_num(X_train_tf)
        X_dict_tf['X_val'] = np.nan_to_num(X_val_tf)
        X_dict_tf['X_test'] = np.nan_to_num(X_test_tf)

        return X_dict_tf

max_stars_repo_name: simonbing/HealthGen

max_issues_repo_name: simonbing/HealthGen

max_forks_repo_name: simonbing/HealthGen


text:
from sys import maxsize
import torch, torchvision
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
import numpy as np
import os, json, cv2, random
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
import time, pickle
from multiprocessing import Pool
from pycocotools import mask as mask_utils

start_time = time.time()

#parameters, source_dir1 should contain files from coco dataset, source_dir2 can be any images
source_dir2 = '/home/PeterYuan/Coding/dataset/coco_train2017'
destination_dir = '/home/PeterYuan/Coding/dataset/ALIS_dataset/ALIS_sample'
seg_results = '/home/PeterYuan/Coding/detectron/seg_result_coco_test2017.pkl'
data_usage = "sample"    #"train" or "val"
existing_img_count = 0
target_object_index = 0    #person = 0, car = 2, cat = 15, dog = 16, chair = 56
available_images = []   #a list of tuple that has structure: : (img_file_name, prediction_box, prediction_mask)
max_result_img = 0  #an int, since looping through dir1 is a time-consuming tasks, 0 = loop through all images in dir1, 500 = loop through the first 500 images in dir1, etc.
max_dir2_img = 500
confidence_requirement = 0.98
minimum_x, minimum_y = 100, 200 #minumum pixel width and height to be considered
object_class_name = {
    0: "person",
    2: "car",
    15: "cat",
    16: "dog",
    56: "chair"
}

#load model
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
predictor = DefaultPredictor(cfg)

#setup output directory
generated_dir = os.path.join(destination_dir, "ALIS_" + object_class_name[target_object_index] + "_" + data_usage + "2021")
mask_dir = os.path.join(destination_dir, "annotations")
if not os.path.exists(generated_dir):
    os.makedirs(generated_dir)
if not os.path.exists(mask_dir):
    os.makedirs(mask_dir)

def instance_seg(img):
    outputs = predictor(img)
    pred_classes = outputs["instances"].pred_classes
    pred_boxes = outputs["instances"].pred_boxes.to("cpu").tensor.numpy()
    pred_masks = outputs["instances"].pred_masks
    scores = outputs["instances"].scores
    return (pred_classes, pred_boxes, pred_masks, scores)

def generate_spliced_image(input):
    #read parameters
    id, path1, path2, box, mask = input
    #read the two images
    img1 = cv2.imread(path1)
    img2 = cv2.imread(path2)
    #crop the obejct out of img1
    #print(img1.shape, mask.shape)
    cropped = cv2.bitwise_and(img1, img1, mask=mask)
    x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
    cropped = cropped[y1:y2, x1:x2]
    #set scale, flip, offset
    max_scale = min((img2.shape[0] / cropped.shape[0]), img2.shape[1] / cropped.shape[1])
    scale = random.uniform(max_scale * 0.3, max_scale * 0.8)
    flip = random.randint(-1, 1)
    #print(img2.shape)
    offset_x = random.randint(0, int(img2.shape[1] - cropped.shape[1] * scale))
    offset_y = random.randint(0, int(img2.shape[0] - cropped.shape[0] * scale))
    #apply cropped area to img2
    mask_inv = np.logical_not(mask).astype(np.uint8)[y1:y2, x1:x2]
    #resize
    mask_inv = cv2.resize(mask_inv, (int((x2 - x1) * scale), int((y2 - y1) * scale)))
    cropped = cv2.resize(cropped, (mask_inv.shape[1], mask_inv.shape[0]))
    #flip
    if random.uniform(0, 1) >= 0.5:
        mask_inv = cv2.flip(mask_inv, flip)
        cropped = cv2.flip(cropped, flip)
    #locate roi
    roi = img2[offset_y:cropped.shape[0] + offset_y, offset_x:cropped.shape[1] + offset_x]
    roi = cv2.bitwise_and(roi, roi, mask = mask_inv)
    roi = cv2.add(cropped, roi)
    img2[offset_y:cropped.shape[0] + offset_y, offset_x:cropped.shape[1] + offset_x] = roi
    truth = np.zeros((img2.shape[0], img2.shape[1]))
    truth[offset_y:cropped.shape[0] + offset_y, offset_x:cropped.shape[1] + offset_x] = (cv2.bitwise_not(mask_inv).astype(np.uint8) - 254) * 255
    mask_truth = np.zeros([img2.shape[0], img2.shape[1], 3])
    for x in range(mask_truth.shape[1]):
        for y in range(mask_truth.shape[0]):
            mask_truth[y, x] = np.array([truth[y, x], truth[y, x], truth[y, x]])
    #save image
    cv2.imwrite(os.path.join(generated_dir, (format(id, "09d") + ".jpg")), img2)
    cv2.imwrite(os.path.join(mask_dir, (format(id, "09d") + "_forged_0" + ".png")), mask_truth)
    #return img2, mask_truth

#search for images with desired objects in dir1
img_looped = 0
start_load_result = time.time()
seg_results = pickle.load(open(seg_results, "rb"))
print(f"Loading seg_result takes {round(time.time() - start_load_result, 2)} seconds")
for result in seg_results:
    if max_result_img != 0 and img_looped >= max_result_img:
        break
    #unpack result
    image_path, pred_classes, pred_boxes, pred_masks, scores = result
    #print(type(image_path), image_path)
    #check if the desired object exists. If so, find the one with highest confidence
    object_index = -1
    for index in range(pred_classes.shape[0]):
        pred_box = pred_boxes[index].astype(np.int_)
        #print(pred_box)
        size_satifise = (pred_box[2] - pred_box[0] >= minimum_x) and (pred_box[3] - pred_box[1] >= minimum_y)
        if pred_classes[index].item() == target_object_index and scores[index].item() >= confidence_requirement and size_satifise:
            object_index = index
            break
    #keep image if it has desires object
    if object_index != -1:
        #print(type(mask_utils.decode(pred_masks[object_index])))
        available_images.append((image_path, pred_boxes[object_index].astype(np.int_), mask_utils.decode(pred_masks[object_index])))
        #cv2.imwrite(os.path.join("/home/PeterYuan/Coding/dataset/selected", image), cv2.imread(os.path.join(source_dir1, image)))
    img_looped += 1

# for image in os.listdir(source_dir1):
#     if max_dir1_img != 0 and img_looped >= max_dir1_img:
#         break
#     #get image output
#     pred_classes, pred_boxes, pred_masks, scores = instance_seg(cv2.imread(os.path.join(source_dir1, image)))
#     #check if the desired object exists. If so, find the one with highest confidence
#     object_index = -1
#     for index in range(pred_classes.shape[0]):
#         pred_box = pred_boxes[index].astype(np.int_)
#         #print(pred_box)
#         size_satifise = (pred_box[2] - pred_box[0] >= minimum_x) and (pred_box[3] - pred_box[1] >= minimum_y)
#         if pred_classes[index].item() == target_object_index and scores[index].item() >= confidence_requirement and size_satifise:
#             object_index = index
#             break
#     #decide whether to keep the image
#     if object_index != -1:
#         available_images.append((image, pred_boxes[object_index].astype(np.int_), pred_masks[object_index].type(torch.int).to("cpu").numpy().astype(np.uint8)))
#         #cv2.imwrite(os.path.join("/home/PeterYuan/Coding/dataset/selected", image), cv2.imread(os.path.join(source_dir1, image)))
#     img_looped += 1

print(f"Available images with '{object_class_name[target_object_index]}' class: {len(available_images)}")

start_generate_iter = time.time()
#create generate_iter
image_itered = 0
generate_iter = []
for image2 in os.listdir(source_dir2):
    if max_dir2_img != 0 and image_itered >= max_dir2_img:
        break
    #execute if the file is an image
    if image2.lower().endswith(".jpg") or image2.lower().endswith(".jpeg") or image2.lower().endswith(".png") or image2.lower().endswith(".bmp") or image2.lower().endswith(".tif"):
        #select random image from dir1
        image1 = random.choice(available_images)
        #generate parameters
        file_id = image_itered + existing_img_count
        image1_path, box, mask = image1
        image2_path = os.path.join(source_dir2, image2)
        generate_iter.append((file_id, image1_path, image2_path, box, mask))

        image_itered += 1
print(f"Generate_iter takes {round(time.time() - start_generate_iter, 2)} seconds")

#multiprocess pool
start_generate_time = time.time()
p = Pool(78)
result = p.map(generate_spliced_image, generate_iter)
p.close()
p.join()
print(f"Generate {len(generate_iter)} images takes {round(time.time() - start_generate_time, 2)} seconds, {round((time.time() - start_generate_time) / len(generate_iter), 4)}s/image")

# #generate image
# image_generated = 0

# for image2 in os.listdir(source_dir2):
#     if max_dir2_img != 0 and image_generated >= max_dir2_img:
#         break
#     if image2.lower().endswith(".jpg") or image2.lower().endswith(".jpeg") or image2.lower().endswith(".png") or image2.lower().endswith(".bmp") or image2.lower().endswith(".tif"):
#         image1 = random.choice(available_images)
#         output, mask = generate_spliced_image(os.path.join(source_dir1, image1[0]), os.path.join(source_dir2, image2), image1[1], image1[2])
#         #print(type((os.path.splitext(image2))))
#         cv2.imwrite(os.path.join(generated_dir, (format(image_generated + existing_img_count, "09d") + ".jpg")), output)
#         cv2.imwrite(os.path.join(mask_dir, (format(image_generated+  existing_img_count, "09d") + "_forged_0" + ".png")), mask)
#         image_generated += 1

print(f"Finished in {time.time() - start_time} seconds")


text:
""" module that contains sematic model for the predictions

-  Train on history of the user days with locations and labels
-  Should be able to predict labels probabilities for each time slot for each weekday
-  Should be able to give scores/metrics for validation

Copyright (c) 2021 Idiap Research Institute, https://www.idiap.ch/
Written by William Droz <william.droz@idiap.ch>,

"""

from collections import defaultdict
from pprint import pprint
from typing import Dict, List

import numpy as np
from regions_builder.algorithms import estimate_stay_points  # type: ignore
from regions_builder.algorithms import estimate_stay_regions, labelize_stay_region
from regions_builder.data_loading import BaseSourceLabels  # type: ignore
from regions_builder.data_loading import BaseSourceLocations
from regions_builder.models import LabelledStayRegion  # type: ignore
from regions_builder.models import LocationPoint, StayRegion

from personal_context_builder import config
from personal_context_builder.wenet_analysis import BagOfWordsVectorizer, _loads_regions
from personal_context_builder.wenet_exceptions import SemanticRoutinesComputationError
from personal_context_builder.wenet_logger import create_logger
from personal_context_builder.wenet_profile_manager import Label

_LOGGER = create_logger(__name__)

class SemanticModel(object):
    def __init__(
        self,
        locations_source: BaseSourceLocations,
        labels_source: BaseSourceLabels,
        name: str = "unknown_semantic_model",
        regions_mapping_file: str = config.PCB_REGION_MAPPING_FILE,
    ):
        """Constructor
        Args:
            locations_source: source of the locations
            labels_sources: source of the labels
            name: name of the model
        """
        self._locations_source = locations_source
        self._labels_source = labels_source
        self._name = name
        self._regions_mapping = _loads_regions(regions_mapping_file)

    def _compute_indexed_weekday_locations(self, user_id: str):
        locations = self._locations_source.get_locations(user_id)
        if len(locations) == 0:
            raise SemanticRoutinesComputationError(f"no locations for user {user_id}")
        stay_points = estimate_stay_points(locations)
        if len(stay_points) == 0:
            raise SemanticRoutinesComputationError(f"no stay_points for user {user_id}")
        stay_regions = estimate_stay_regions(stay_points)
        if len(stay_regions) == 0:
            raise SemanticRoutinesComputationError(
                f"no stay_regions for user {user_id}"
            )
        user_places = self._labels_source.get_labels(user_id)
        if len(user_places) == 0 and not config.PCB_FILL_UNKNOWN:
            raise SemanticRoutinesComputationError(f"no user_places for user {user_id}")
        labelled_stay_regions = labelize_stay_region(
            stay_regions, user_places, fill_unknown=config.PCB_FILL_UNKNOWN
        )
        if len(labelled_stay_regions) == 0:
            raise SemanticRoutinesComputationError(
                f"no labelled_stay_regions for user {user_id}"
            )
        stay_regions = list(set(stay_regions) - set(labelled_stay_regions))
        all_days_locations = BagOfWordsVectorizer.group_by_days(locations, user_id)
        indexed_weekday_locations = self.index_per_weekday(all_days_locations)
        return indexed_weekday_locations, labelled_stay_regions, stay_regions

    def compute_labels_for_user(self, user_id: str, labelled_stay_regions: List):
        """Compute the labels for a given user
        Args:
            user_id: for which user

        TODO user specific location for labels

        Returns: dict of semantic labels with Label
        """
        region_to_lat_lng = defaultdict(lambda: (0, 0))
        for labelled_stay_region in labelled_stay_regions:
            if labelled_stay_region._topleft_lat != 0:
                region_to_lat_lng[labelled_stay_region._label] = (
                    (
                        labelled_stay_region._topleft_lat
                        + labelled_stay_region._bottomright_lat
                    )
                    / 2,
                    (
                        labelled_stay_region._topleft_lng
                        + labelled_stay_region._bottomright_lng
                    )
                    / 2,
                )
        res = dict(
            [
                (
                    semantic_id,
                    Label(label_name, semantic_id, *region_to_lat_lng[label_name]),
                )
                for label_name, semantic_id in self._regions_mapping.items()
            ]
        )
        return res

    def compute_weekdays(self, user_id: str):
        """Compute the all weekday distributions for the given user
        Args:
            user_id: for which user the weekday are computed

        Return: Distribution of the labels for each day for each time slots
        """
        raise NotImplementedError("not implemented")

    def evaluate_user(self, user_id: str):
        """Score the models for user_id"""

    def evaluate_all(self):
        """Score the models for all users"""

    @classmethod
    def index_per_weekday(
        cls, all_days_locations: List[LocationPoint]
    ) -> Dict[int, List[LocationPoint]]:
        """group all days by weekday and use weekday as index
        Args:
            all_days_locations: list of list of locations, sublist are full day

        Return: indexed days of locations
        """
        res = defaultdict(list)
        for locations_day in all_days_locations:
            middle_location = locations_day[len(locations_day) // 2]
            weekday = middle_location._pts_t.weekday()
            res[weekday].append(locations_day)
        return res

class SemanticModelHist(SemanticModel):
    def compute_weekdays(self, user_id: str):
        (
            indexed_weekday_locations,
            labelled_stay_regions,
            stay_regions,
        ) = self._compute_indexed_weekday_locations(user_id)
        labels_count: Dict[int, Dict[str, Dict[str, float]]] = defaultdict(
            lambda: defaultdict(lambda: defaultdict(lambda: 0))
        )
        for weekday, days_locations in indexed_weekday_locations.items():
            for day_locations in days_locations:
                for location in day_locations:
                    self._fill_with_labels_count(
                        location,
                        labels_count,
                        weekday,
                        labelled_stay_regions,
                        stay_regions,
                    )
        labels_dist = self._compute_labels_dist(labels_count)

        return labels_dist, labelled_stay_regions

    def _compute_labels_dist(
        self, labels_count: Dict[int, Dict[str, Dict[str, float]]]
    ):
        """compute the distribution of the labels for each timeslots grouped per weekday
        Args:
            labels_count: hierarchical labels count (dict of dict of dict)
        """
        labels_dist: Dict[int, Dict[str, Dict[str, float]]] = defaultdict(
            lambda: defaultdict(lambda: dict())
        )
        for weekday, time_labels_freq in labels_count.items():
            for time_slot, labels_freq in time_labels_freq.items():
                nb_items = sum(labels_freq.values())
                for label, nb in labels_freq.items():
                    labels_dist[weekday][time_slot][label] = nb / nb_items
        return labels_dist

    def _fill_with_labels_count(
        self,
        location: LocationPoint,
        labels_count: Dict[int, Dict[str, Dict[str, float]]],
        weekday: int,
        labelled_stay_regions: List[LabelledStayRegion],
        stay_regions: List[StayRegion],
    ):
        """determine the labels and fill hierarchically labels_count with
            the number of each labels per timeslot per weekday
        Args:
            location: location point to check
            labels_count: hierarchical labels count (dict of dict of dict)
            weekday: day of the week (number)
            labelled_stay_regions: the labelled stay regions
            stay_regions: the unlabelled stay regions
        """
        if location is None or np.isnan(location._lat):
            labels_count[weekday][location._pts_t.strftime("%H:%M:%S")][
                self._regions_mapping["no_data"]
            ] += 1
        else:
            is_in_region = False
            for region in labelled_stay_regions:
                if location in region:
                    if region._label in self._regions_mapping:
                        label = self._regions_mapping[region._label]
                    else:
                        label = self._regions_mapping["unknown_labelled_region"]
                    labels_count[weekday][location._pts_t.strftime("%H:%M:%S")][
                        label
                    ] += 1
                    is_in_region = True
                    break
            for region in stay_regions:
                if location in region:
                    labels_count[weekday][location._pts_t.strftime("%H:%M:%S")][
                        self._regions_mapping["unknown_region"]
                    ] += 1
                    is_in_region = True
                    break
            if not is_in_region:
                labels_count[weekday][location._pts_t.strftime("%H:%M:%S")][
                    self._regions_mapping["unknown"]
                ] += 1

max_stars_repo_name: InternetOfUs/personal-context-builder

max_issues_repo_name: InternetOfUs/personal-context-builder

max_forks_repo_name: InternetOfUs/personal-context-builder


text:
#!/usr/bin/env python

import pickle

import cv2
import gym_super_mario_bros
import neat
import numpy as np
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
from nes_py.wrappers import JoypadSpace

def nnout_to_action(nnout):
    return nnout.index(max(nnout))

def eval_genomes(genomes, config):
    for genome_id, genome in genomes:
        genome.fitness = eval_genome(genome, config, genome_id)

def eval_genome(genome, config, genome_id=None):
    env = gym_super_mario_bros.make("SuperMarioBros-v0")
    env = JoypadSpace(env, SIMPLE_MOVEMENT)
    state = env.reset()

    iny, inx, inc = env.observation_space.shape
    inx = int(inx / 8)
    iny = int(iny / 8)

    net = neat.nn.FeedForwardNetwork.create(genome, config)

    done = False
    fitness_current = 0.0
    frames = 0
    old_x = 0
    lives_remaining = 2
    total_coins = 0
    current_status = "small"

    while not done:
        state = cv2.resize(state, (inx, iny))
        state = cv2.cvtColor(state, cv2.COLOR_BGR2GRAY)
        state = np.reshape(state, (inx, iny))
        # env.render()

        imgarray = np.ndarray.flatten(state)
        nnout = net.activate(imgarray)
        action = nnout_to_action(nnout)
        state, rew, done, info = env.step(action)
        fitness_current += rew

        # if mario gets to the flag give a very high reward meet fitness_threshold
        if info["flag_get"]:
            fitness_current += 500000

        # extra penalty for dying
        if info["life"] < lives_remaining:
            lives_remaining = info["life"]
            fitness_current -= 250

        # bonus for managing to change status
        if current_status != info["status"]:
            current_status = info["status"]
            if info["status"] != "small":
                fitness_current += 100

        total_coins = info["coins"]
        frames += 1
        if frames % 50 == 0:
            if old_x == info["x_pos"]:
                done = True
            else:
                old_x = info["x_pos"]

    # bonus for collecting coins
    fitness_current += total_coins * 10

    if genome_id:
        print(f"GenomeID: {genome_id}, Fitness: {fitness_current}")
    else:
        print(f"Fitness: {fitness_current}")

    env.close()
    return fitness_current

config = neat.Config(
    neat.DefaultGenome,
    neat.DefaultReproduction,
    neat.DefaultSpeciesSet,
    neat.DefaultStagnation,
    "config-feedforward",
)
p = neat.Population(config)
# p = neat.Checkpointer.restore_checkpoint("neat-checkpoint-144")

p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()

p.add_reporter(stats)

# Save the process after each 10 frames
p.add_reporter(neat.Checkpointer(1))

pe = neat.ParallelEvaluator(10, eval_genome)
winner = p.run(pe.evaluate)

# winner = p.run(eval_genomes)

with open("winner.pkl", "wb") as output:
    pickle.dump(winner, output, 1)

max_stars_repo_name: chrisvaughn/marioAI

max_issues_repo_name: chrisvaughn/marioAI

max_forks_repo_name: chrisvaughn/marioAI


text:
from climt import (
    mass_to_volume_mixing_ratio,
    get_interface_values,
    calculate_q_sat, numpy_version_of,
    RRTMGShortwave, get_default_state,
    set_constants_from_dict,
    list_available_constants
)

from sympl import DataArray, get_constant
import numpy as np
import pytest

sample_constants = dict(
    constant_one=dict(
        value=10.0,
        units='J kg^-1 m^-2'),
    constant_two=dict(
        value=1.002,
        units='J kg^-1'),
    constant_three=dict(
        value=1.024e6,
        units='kg^-1'))

def test_mol_weight_not_passed():

    with pytest.raises(ValueError) as excinfo:
        mass_to_volume_mixing_ratio(None)

    assert 'molecular weight' in str(excinfo.value)

def test_for_365ppm():

    mass_mixing_ratio_co2 = 605e-6
    molecular_weight_co2 = 48

    expected_vol_mixing_ratio = 365.0670833333333e-6

    co2 = DataArray(
        mass_mixing_ratio_co2*np.ones((1, 1, 1)),
        dims=['longitude', 'latitude', 'mid_levels'],
        attrs=dict(units='g/g'))

    vol_mixing_ratio = mass_to_volume_mixing_ratio(
        co2, molecular_weight=molecular_weight_co2)

    assert np.all(np.isclose(
        vol_mixing_ratio, expected_vol_mixing_ratio))

def test_for_g_per_kg():

    mass_mixing_ratio_co2 = 605e-3
    molecular_weight_co2 = 48

    expected_vol_mixing_ratio = [[[365.0670833333333e-6]]]

    co2 = DataArray(
        mass_mixing_ratio_co2*np.ones((1, 1, 1)),
        dims=['longitude', 'latitude', 'mid_levels'],
        attrs=dict(units='g/kg'))

    vol_mixing_ratio = mass_to_volume_mixing_ratio(
        co2.to_units('g/g').values, molecular_weight=molecular_weight_co2)

    print(vol_mixing_ratio, expected_vol_mixing_ratio)
    assert np.all(np.isclose(
        vol_mixing_ratio, expected_vol_mixing_ratio))

def test_interface_levels():

    mid_level_values = np.ones((10, 1))
    surface_value = np.ones((1, 1))

    pressure_mid_level = np.linspace(0.995, 0.001, 10)[:, None]
    surface_pressure = 1.

    pressure_interface_level = np.zeros((11, 1))

    pressure_interface_level[1:-1, :] = (
        pressure_mid_level[1:, :] + pressure_mid_level[:-1, :])/2.

    pressure_interface_level[0, :] = surface_pressure
    pressure_interface_level[-1, :] = 0.0005

    interface_values = get_interface_values(
        mid_level_values, surface_value,
        pressure_mid_level, pressure_interface_level)

    assert np.all(interface_values == np.ones((11, 1)))

def test_qsat():

    surf_temp = 290*np.random.randn(10, 10)
    surf_temp[surf_temp < 260] = 260
    surf_temp[surf_temp > 280] = 280

    qsat_at_280 = 0.0062856560708380816
    qsat_at_260 = 0.0012408979354134003

    surf_press = 1e5*np.ones(surf_temp.shape)

    Rd = 287.058  # J/kg/K
    Rv = 461.5  # J/kg/K

    qsat = calculate_q_sat(surf_temp, surf_press, Rd, Rv)

    assert np.all(qsat[surf_temp == 280] == qsat_at_280)
    assert np.all(qsat[surf_temp == 260] == qsat_at_260)

def test_get_numpy_version_with_numpy_array_in_state():

    component = RRTMGShortwave()
    state = get_default_state([component])

    state['test_values'] = np.arange(100)

    raw_array = numpy_version_of(state)

    assert np.all(raw_array['test_values'] == np.arange(100))

def test_adding_constant():

    set_constants_from_dict(sample_constants)

    for constant in sample_constants.keys():

        constant_value = get_constant(constant,
                                      sample_constants[constant]['units'])

        assert constant_value == sample_constants[constant]['value']

def test_print_constant():

    list_available_constants()

if __name__ == '__main__':
    pytest.main([__file__])