text:
import os
from kaggle_runner import logger
from kaggle_runner.kernels.fastai_kernel import FastAIKernel
from kaggle_runner.metrics.metrics import matthews_correlation
from kaggle_runner.datasets.transfomers import *
from kaggle_runner.datasets.bert import DatasetRetriever
from kaggle_runner.utils.kernel_utils import get_obj_or_dump
from kaggle_runner.modules.ToxicSimpleNNModel import ToxicSimpleNNModel
from fastai.basic_data import DataBunch
import transformers
from transformers import *
import albumentations

ROOT_PATH = f'/kaggle' # for colab
BACKBONE_PATH = 'xlm-roberta-large'

from kaggle_runner.defaults import SEED
import random
import numpy as np

def seed_everything(seed):
    """seed_everything.

    Args:
      seed: number to generate pseudo random states

    Returns:

    """
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = True

def get_train_transforms():
    """get_train_transforms."""

    return albumentations.Compose([
        ExcludeUsersMentionedTransform(p=0.95),
        ExcludeUrlsTransform(p=0.95),
        ExcludeNumbersTransform(p=0.95),
        ExcludeHashtagsTransform(p=0.95),
        ExcludeDuplicateSentencesTransform(p=0.95),
    ], p=1.0)

def get_synthesic_transforms(supliment_toxic, p=0.5, mix=False):
    """get_synthesic_transforms.

    Args:
      supliment_toxic: extra toxic data which will be used in synthesizing data
      p: probability (Default value = 0.5)
      mix: flag for mixing more data with toxic (it does not work with balanced sampler, too many toxic data) (Default value = False)

    Returns:

    """

    return SynthesicOpenSubtitlesTransform(p=p, supliment_toxic=supliment_toxic, mix=mix)

ROOT_PATH = '/kaggle'
def get_pickled_data(file_path):
    """get_pickled_data from current folder or kaggle data input folder.

    Args:
      file_path:

    Returns:

    """
    obj = get_obj_or_dump(file_path)

    if obj is None:
        #may_debug(True)

        return get_obj_or_dump(f"{ROOT_PATH}/input/clean-pickle-for-jigsaw-toxicity/{file_path}")

    return obj

vocab = get_pickled_data("vocab.pkl")
#if vocab is None: # vocab file read~~
#   vocab = [tokenizer.convert_ids_to_tokens(i) for i in range(tokenizer.vocab_size)]
#   get_obj_or_dump("vocab.pkl", default=vocab)

class Shonenkov(FastAIKernel):
    """Shonenkov kernel, use TPU to train; for data, down sampler and synthesizing."""

    def __init__(self, device, config, **kargs):
        """__init__.

        Args:
          device: param config:
          kargs:
          config:
          **kargs:

        Returns:

        """
        super(Shonenkov, self).__init__(**kargs)
        self.data = None
        self.transformers = None
        self.setup_transformers()
        self.device = device
        self.config = config
        self.learner = None

    def build_and_set_model(self):
        """build_and_set_model."""
        self.model = ToxicSimpleNNModel()
        self.model = self.model.to(self.device)

    def set_random_seed(self):
        """set_random_seed."""
        seed_everything(SEED)

    def setup_transformers(self):
        """setup_transformers."""

        if self.transformers is None:
            supliment_toxic = None # avoid overfit
            train_transforms = get_train_transforms();
            synthesic_transforms_often = get_synthesic_transforms(supliment_toxic, p=0.5)
            synthesic_transforms_low = None
            tokenizer = XLMRobertaTokenizer.from_pretrained(BACKBONE_PATH)
            shuffle_transforms = ShuffleSentencesTransform(always_apply=True)

            self.transformers = {'train_transforms': train_transforms,
                                 'synthesic_transforms_often': synthesic_transforms_often,
                                 'synthesic_transforms_low': synthesic_transforms_low,
                                 'tokenizer': tokenizer, 'shuffle_transforms':
                                 shuffle_transforms}

    def prepare_train_dev_data(self):
        """prepare_train_dev_data."""
        df_train = get_pickled_data("train.pkl")

        if df_train is None:
            df_train = pd.read_csv(f'{ROOT_PATH}/input/jigsaw-toxicity-train-data-with-aux/train_data.csv')
            df_train['comment_text'] = df_train.parallel_apply(lambda x: clean_text(x['comment_text'], x['lang']), axis=1)
            get_obj_or_dump("train.pkl", default=df_train)

        #supliment_toxic = get_toxic_comments(df_train)
        self.train_dataset = DatasetRetriever(
            labels_or_ids=df_train['toxic'].values,
            comment_texts=df_train['comment_text'].values,
            langs=df_train['lang'].values,
            severe_toxic=df_train['severe_toxic'].values,
            obscene=df_train['obscene'].values,
            threat=df_train['threat'].values,
            insult=df_train['insult'].values,
            identity_hate=df_train['identity_hate'].values,
            use_train_transforms=True,
            transformers=self.transformers
        )
        df_val = get_pickled_data("val.pkl")

        if df_val is None:
            df_val = pd.read_csv(f'{ROOT_PATH}/input/jigsaw-multilingual-toxic-comment-classification/validation.csv', index_col='id')
            df_val['comment_text'] = df_val.parallel_apply(lambda x: clean_text(x['comment_text'], x['lang']), axis=1)
            get_obj_or_dump("val.pkl", default=df_val)

        self.validation_tune_dataset = DatasetRetriever(
            labels_or_ids=df_val['toxic'].values,
            comment_texts=df_val['comment_text'].values,
            langs=df_val['lang'].values,
            use_train_transforms=True,
            transformers=self.transformers
        )
        self.validation_dataset = DatasetRetriever(
            labels_or_ids=df_val['toxic'].values,
            comment_texts=df_val['comment_text'].values,
            langs=df_val['lang'].values,
            use_train_transforms=False,
            transformers=self.transformers
        )

        del df_val
        gc.collect();

        del df_train
        gc.collect();

    def prepare_test_data(self):
        """prepare_test_data."""

        if os.path.exists('/content'): # colab
            df_test = get_pickled_data("test.pkl")
        else:
            df_test = None

        if df_test is None:
            df_test = pd.read_csv(f'{ROOT_PATH}/input/jigsaw-multilingual-toxic-comment-classification/test.csv', index_col='id')
            df_test['comment_text'] = df_test.parallel_apply(lambda x: clean_text(x['content'], x['lang']), axis=1)
            get_obj_or_dump("test.pkl", default=df_test)

        self.test_dataset = DatasetRetriever(
            labels_or_ids=df_test.index.values, ## here different!!!
            comment_texts=df_test['comment_text'].values,
            langs=df_test['lang'].values,
            use_train_transforms=False,
            test=True,
            transformers=self.transformers
        )

        del df_test
        gc.collect();
    def after_prepare_data_hook(self):
        """Put to databunch here"""
        logger.debug("kernel use device %s", self.device)
        self.data = DataBunch.create(self.train_dataset,
                                     self.validation_dataset,
                                     bs=self.config.batch_size,
                                     device=self.device,
                                     num_workers=self.config.num_workers)

    def peek_data(self):
        """peek_data."""

        if self.data is not None:
            may_debug()
            o = self.data.one_batch()
            print(o)

            return o
        else:
            if self.logger is not None:
                self.logger.error("peek_data failed, DataBunch is None.")

from kaggle_runner import may_debug

import torch.nn as nn

class ToxicSimpleNNModelChangeInner(nn.Module):
    """ToxicSimpleNNModelChangeInner."""

    def __init__(self, use_aux=True):
        """__init__.

        Args:
          use_aux:  (Default value = True)

        Returns:

        """
        super(ToxicSimpleNNModelChangeInner, self).__init__()

        self.backbone = XLMModel.from_pretrained('xlm-mlm-tlm-xnli15-1024')
        self.dropout = nn.Dropout(0.3)
        aux_len = 0

        if use_aux:
            aux_len = 5

        #in_features = self.backbone.layer[11].ff.layer_2.out_features*2
        #in_features = self.backbone.pooler.dense.out_features*2 # bert - xmlrobert
        in_features = self.backbone.ffns[11].lin2.out_features*2 # xlm
        self.linear = nn.Linear(
            in_features=in_features,
            out_features=2+aux_len,
        )

    def forward(self, input_ids, attention_masks):
        """forward.

        Args:
          input_ids: param attention_masks:
          attention_masks:

        Returns:

        """
        bs, seq_length = input_ids.shape
        seq_x, _ = self.backbone(
            input_ids=input_ids, attention_mask=attention_masks)
        apool = torch.mean(seq_x, 1)
        mpool, _ = torch.max(seq_x, 1)
        x = torch.cat((apool, mpool), 1)
        x = self.dropout(x)

        return self.linear(x)

class ShonenkovChangeInner(Shonenkov):
    """ShonenkovChangeInner."""

    def __init__(self, device, config, **kargs):
        """__init__.

        Args:
          device: param config:
          kargs:
          config:
          **kargs:

        Returns:

        """
        super(ShonenkovChangeInner, self).__init__(device, config, **kargs)
        assert self.transformers is not None

    def build_and_set_model(self):
        """build_and_set_model."""
        self.model = ToxicSimpleNNModelChangeInner()
        self.model = self.model.to(self.device)

    def prepare_train_dev_data(self):
        """prepare_train_dev_data."""
        df_train = get_pickled_data("train_XLM.pkl")

        if df_train is None:
            df_train = pd.read_csv(f'{ROOT_PATH}/input/jigsaw-toxicity-train-data-with-aux/train_data.csv')
            df_train['comment_text'] = df_train.parallel_apply(lambda x: clean_text(x['comment_text'], x['lang']), axis=1)
            get_obj_or_dump("train_XLM.pkl", default=df_train)

        #supliment_toxic = get_toxic_comments(df_train)
        self.train_dataset = DatasetRetriever(
            labels_or_ids=df_train['toxic'].values,
            comment_texts=df_train['comment_text'].values,
            langs=df_train['lang'].values,
            severe_toxic=df_train['severe_toxic'].values,
            obscene=df_train['obscene'].values,
            threat=df_train['threat'].values,
            insult=df_train['insult'].values,
            identity_hate=df_train['identity_hate'].values,
            use_train_transforms=True,
            transformers=self.transformers
        )
        df_val = get_pickled_data("val_XLM.pkl")

        if df_val is None:
            df_val = pd.read_csv(f'{ROOT_PATH}/input/jigsaw-multilingual-toxic-comment-classification/validation.csv', index_col='id')
            df_val['comment_text'] = df_val.parallel_apply(lambda x: clean_text(x['comment_text'], x['lang']), axis=1)
            get_obj_or_dump("val_XLM.pkl", default=df_val)

        self.validation_tune_dataset = DatasetRetriever(
            labels_or_ids=df_val['toxic'].values,
            comment_texts=df_val['comment_text'].values,
            langs=df_val['lang'].values,
            use_train_transforms=True,
            transformers=self.transformers
        )
        self.validation_dataset = DatasetRetriever(
            labels_or_ids=df_val['toxic'].values,
            comment_texts=df_val['comment_text'].values,
            langs=df_val['lang'].values,
            use_train_transforms=False,
            transformers=self.transformers
        )

        del df_val
        gc.collect();

        del df_train
        gc.collect();

    def prepare_test_data(self):
        """prepare_test_data."""

        if os.path.exists('/content'): # colab
            df_test = get_pickled_data("test_XLM.pkl")
        else:
            df_test = None

        if df_test is None:
            df_test = pd.read_csv(f'{ROOT_PATH}/input/jigsaw-multilingual-toxic-comment-classification/test.csv', index_col='id')
            df_test['comment_text'] = df_test.parallel_apply(lambda x: clean_text(x['content'], x['lang']), axis=1)
            get_obj_or_dump("test_XLM.pkl", default=df_test)

        self.test_dataset = DatasetRetriever(
            labels_or_ids=df_test.index.values, ## here different!!!
            comment_texts=df_test['comment_text'].values,
            langs=df_test['lang'].values,
            use_train_transforms=False,
            test=True,
            transformers=self.transformers
        )

        del df_test
        gc.collect();

    def setup_transformers(self):
        """setup_transformers."""

        if not hasattr(self, 'transformers') or self.transformers is None:
            supliment_toxic = None # avoid overfit
            train_transforms = get_train_transforms();
            synthesic_transforms_often = get_synthesic_transforms(supliment_toxic, p=0.5)
            synthesic_transforms_low = None
            shuffle_transforms = ShuffleSentencesTransform(always_apply=True)

            #from tokenizers import BertWordPieceTokenizer
            #tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased')
            tokenizer = XLMTokenizer.from_pretrained('xlm-mlm-tlm-xnli15-1024')

            self.transformers = {'train_transforms': train_transforms,
                                 'synthesic_transforms_often': synthesic_transforms_often,
                                 'synthesic_transforms_low': synthesic_transforms_low,
                                 'tokenizer': tokenizer,
                                 'shuffle_transforms': shuffle_transforms}

import torch
class DummyTrainGlobalConfig:
    """Global Config for this notebook"""
    num_workers = 0  # количество воркеров для loaders
    batch_size = 16  # bs , 8 for GPU, 16 for TPU
    n_epochs = 2  # количество эпох для обучения
    lr = 0.3 * 1e-5 # стартовый learning rate (внутри логика работы с мульти TPU домножает на кол-во процессов)
    fold_number = 0  # номер фолда для обучения

    # -------------------
    verbose = True  # выводить принты
    verbose_step = 25  # количество шагов для вывода принта
    # -------------------

    # --------------------
    step_scheduler = False  # выполнять scheduler.step после вызова optimizer.step
    validation_scheduler = True  # выполнять scheduler.step после валидации loss (например для плато)
    SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau
    scheduler_params = dict(
        mode='max',
        factor=0.7,
        patience=0,
        verbose=False,
        threshold=0.0001,
        threshold_mode='abs',
        cooldown=0,
        min_lr=1e-8,
        eps=1e-08
    )
    # --------------------

    # -------------------
    criterion = 'L'
    # -------------------

def test_change_XLM_module():
    """test_change_XLM_module."""
    from kaggle_runner.losses import LabelSmoothing
    k = ShonenkovChangeInner(torch.device("cpu"), DummyTrainGlobalConfig,
                             metrics=None, loss_func=LabelSmoothing(),
                             opt_func=None)
    assert k is not None
    k.run(dump_flag=False)


text:
""" Color evolution, luminosity evolution phase space """

import numpy as np
import matplotlib.pyplot as plt
plt.rc("font", family="serif")
plt.rc("text", usetex=True)
from astropy.io import ascii
from astropy.cosmology import Planck15
from astropy.time import Time

def at2018gep(ax):
    distmod = Planck15.distmod(z=0.03154).value
    DATA_DIR = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/phot"
    t0 = 2458370.6473

    # For this, only use the P48 g & r forced photometry
    f = DATA_DIR + "/precursor.csv"
    prog = ascii.read(f)
    mjd = prog['mjd']
    jd = mjd + 2400000.5
    dt = jd - t0
    filt = prog['filter']
    mag = prog['mag']
    emag = prog['magerr']
    code = prog['instrument']
    det = np.logical_and.reduce((dt > 0, ~np.isnan(mag), code=='ZTF Camera'))

    # Interpolate everything onto the r-band light curve
    band = filt == 'ztfr'
    choose = np.logical_and(det, band)
    order = np.argsort(dt[choose])
    dt_r = dt[choose][order]
    mag_r = mag[choose][order]

    band = filt=='ztfg'
    choose = np.logical_and(det, band)
    order = np.argsort(dt[choose])
    dt_g = dt[choose][order]
    mag_g = mag[choose][order]

    tgrid = np.copy(dt_g)
    # keep all the points below 1 days, and then only choose 1 pt per day
    tgrid = np.hstack((tgrid[tgrid<1], np.arange(1,30,1)))
    #tgrid = np.logspace(np.log10(tgrid[0]), np.log10(tgrid[-1]), 20)

    r = np.interp(tgrid, dt_r, mag_r)
    g = np.interp(tgrid, dt_g, mag_g)

    gr = g - r
    xdata = gr
    ydata = g-distmod
    ax.plot(xdata, ydata, c='k', zorder=5, lw=2)

    markt = np.array([1/24, 1, 10, 20])
    labs = np.array(["1 hour", "1 day", "10 days", "20 days"])
    cols = np.array(['#f6d746', '#e55c30', '#84206b', '#140b34'])
    for ii,t in enumerate(markt):
        prevptx = np.interp(t-0.1, tgrid, xdata)#xdata[tgrid<t][-1]
        prevpty = np.interp(t-0.1, tgrid, ydata)#ydata[tgrid<t][-1]
        newptx = np.interp(t,tgrid,xdata)
        newpty = np.interp(t,tgrid,ydata)
        ax.annotate('',
            xytext=(prevptx, prevpty),
            xy=(newptx, newpty),
            arrowprops=dict(color=cols[ii], width=1, headlength=10),
            label=labs[ii], zorder=10)
        ax.scatter(0,0,marker='^',c=cols[ii],s=100, label=labs[ii])
    ax.legend(loc='upper right')
        # ax.text(newptx, newpty, "$\Delta t$= %s" %labs[ii], fontsize=12,
        #         horizontalalignment='left', verticalalignment='top')
    ax.text(
            -0.3, -16, "SN2018gep", fontsize=14,
            horizontalalignment='right')

def at2018cow(ax):
    distmod = Planck15.distmod(z=0.014).value
    DATA_DIR = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/lc"
    f = DATA_DIR + "/18cow.txt"
    dat = np.loadtxt(f, dtype=str)
    jd = dat[:,0].astype(float)
    filt = dat[:,2]
    mag = dat[:,4].astype(float)
    emag = dat[:,5].astype(float)

    zp = 58285
    dt = jd-zp
    print(dt)

    tgrid = np.arange(dt[0],30,1)

    band = filt=='g'
    choose = band
    order = np.argsort(dt[choose])
    g = np.interp(tgrid, dt[choose][order], mag[choose][order])

    band = filt=='r'
    choose = band
    order = np.argsort(dt[choose])
    r = np.interp(tgrid, dt[choose][order], mag[choose][order])

    gr = g - r
    xdata = gr
    ydata = g-distmod
    ax.plot(
            xdata, ydata, c='k', ls='--', lw=2, zorder=2)
    markt = np.array([1.8, 10, 20])
    labs = np.array(["1 day", "10 days", "20 days"])
    cols = np.array(['#e55c30', '#84206b', '#140b34'])
    for ii,t in enumerate(markt):
        prevptx = np.interp(t-0.1, tgrid, xdata)#xdata[tgrid<t][-1]
        prevpty = np.interp(t-0.1, tgrid, ydata)#ydata[tgrid<t][-1]
        newptx = np.interp(t,tgrid,xdata)
        newpty = np.interp(t,tgrid,ydata)
        ax.annotate('',
            xytext=(prevptx, prevpty),
            xy=(newptx, newpty),
            arrowprops=dict(color=cols[ii], width=1, headlength=10))
        # ax.text(newptx, newpty, "$\Delta t$= %s" %labs[ii], fontsize=12,
        #         horizontalalignment='left', verticalalignment='top')
    ax.text(-0.37, -20.4, "AT2018cow", fontsize=14)

def drout_all(ax):
    ddir = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/lc"
    z = {'10ah':0.074, '10bjp':0.113, '11qr':0.324, '12bb':0.101, '12bv':0.405,
         '12brf':0.275, '11bbq': 0.646,
         '13duy':0.270, '13dwm':0.245, '13ess':0.296}
    inputf = ddir + "/drout14.txt"
    dat = np.loadtxt(inputf,delimiter=';',dtype=str)
    names = np.array([val.strip() for val in dat[:,0]])
    unames = np.unique(names)
    for ii,name in enumerate(unames):
        choose = names == name
        redshift = z[name]
        # only do this if there is a known redshift
        filt = np.array(dat[:,1][choose]).astype(str)
        dt = dat[:,2][choose].astype(float)
        islim = dat[:,3][choose]
        mag = dat[:,4][choose].astype(float)
        emag = dat[:,5][choose].astype(str)
        distmod = Planck15.distmod(z=redshift).value
        isdet = np.array([val == " " for val in islim])
        isfilt = np.array(['g' in val for val in filt])
        pts = np.logical_and(isdet, isfilt)
        gdt = dt[pts]
        g = mag[pts]
        eg = emag[pts].astype(float)
        isfilt = np.array(['r' in val for val in filt])
        pts = np.logical_and(isdet, isfilt)
        rdt = dt[pts]
        r = mag[pts]
        er = emag[pts].astype(float)

        dt_grid = np.sort(np.intersect1d(rdt,gdt))

        if len(dt_grid) > 1:
            # only do this if npts > 1
            keep = np.array([np.where(rdt==val)[0][0] for val in dt_grid])
            user = r[keep]
            useer = er[keep]
            keep = np.array([np.where(gdt==val)[0][0] for val in dt_grid])
            useg = g[keep]
            useeg = eg[keep]
            gr = useg-user
            egr = np.sqrt(useeg**2+useer**2)

            i = 0
            ax.errorbar(
                    gr[i], useg[i]-distmod,
                    xerr=egr[i], yerr=useeg[i], c='grey', marker='+',
                    alpha=0.3)
            i = -1
            ax.errorbar(
                    gr[i], useg[i]-distmod,
                    xerr=egr[i], yerr=useeg[i], c='grey', marker='+',
                    alpha=0.3)
            ax.text(gr[0], useg[0]-distmod, name)
            if ii == 0:
                ax.plot(gr, useg-distmod, ls='-', c='grey', zorder=0, lw=3,
                        alpha=0.5, label="PS-1 gold (Drout+14)")
                # ax.fill_between(gr, useg-distmod-useeg,
                #     useg-distmod+useeg, color='grey', alpha=0.3, zorder=0,
                #     label="PS-1 gold (Drout+14)")
                #ax.fill_betweenx(useg-distmod, gr-egr, gr+egr,
                #    color='grey', alpha=0.3, zorder=0,
                #    label="PS-1 gold (Drout+14)")
            else:
                ax.plot(gr, useg-distmod, ls='-', c='grey', zorder=0, lw=3,
                        alpha=0.5)

def ps1_10ah(ax):
    """ the only Drout gold transient with z < 0.1 """
    gr = np.array([0, -0.2, 0.05])
    phase = np.array([-3, 0, 14])
    g = np.array([22.18, 19.95, 21.64])
    distmod = Planck15.distmod(z=0.074).value
    G = g-distmod
    ax.plot(gr, G, ls='-', c='grey', zorder=0, lw=3, alpha=0.5)
    ax.text(gr[0], G[0], 'PS1-10ah', fontsize=12)

def ksn2015k(ax):
    diff = Planck15.distmod(z=0.09).value
    gr = -0.17
    G = 20.01-diff
    ax.errorbar(
            gr, G, xerr=0.20, yerr=0.12,
            fmt='v', c='#84206b', mfc='#84206b', ms=12, label=None)
    ax.text(gr, G, "KSN2015K", fontsize=14,
            horizontalalignment='right', verticalalignment='bottom')

def arcavi(ax):
    ddir = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/lc"
    inputf = ddir + "/arcavi_info.txt"
    info = np.loadtxt(inputf,delimiter=';',dtype=str)
    unames = np.array([val.strip() for val in info[:,0]])
    z_key = info[:,3].astype(float)

    inputf = ddir + "/arcavi_lc.txt"
    dat = np.loadtxt(inputf,delimiter=';',dtype=str)
    names = np.array([val.strip() for val in dat[:,0]])
    for ii,name in enumerate(unames):
        choose = names == name
        redshift = z_key[unames==name]
        filt = np.array(dat[:,2][choose]).astype(str)
        mjd = dat[:,3][choose].astype(float)
        dt = mjd-mjd[0]
        islim = dat[:,4][choose]
        mag = dat[:,5][choose].astype(float)
        emag = dat[:,6][choose].astype(str)
        distmod = Planck15.distmod(z=redshift).value
        isdet = np.array([val == " " for val in islim])
        isfilt = np.array(['g' in val for val in filt])
        pts = np.logical_and(isdet, isfilt)
        gdt = dt[pts]
        g = mag[pts]
        eg = emag[pts].astype(float)
        isfilt = np.array(['r' in val for val in filt])
        pts = np.logical_and(isdet, isfilt)
        rdt = dt[pts]
        r = mag[pts]
        er = emag[pts].astype(float)

        dt_grid = np.sort(np.intersect1d(rdt.astype(int),gdt.astype(int)))

        if len(dt_grid) > 1:
            print(dt_grid[-1])
            # only do this if npts > 1
            keep = np.array(
                    [np.where(rdt.astype(int)==val)[0][0] for val in dt_grid])
            user = r[keep]
            useer = er[keep]
            keep = np.array(
                    [np.where(gdt.astype(int)==val)[0][0] for val in dt_grid])
            useg = g[keep]
            useeg = eg[keep]
            gr = useg-user
            egr = np.sqrt(useeg**2+useer**2)

            col = 'orange'
            i = 0
            ax.errorbar(
                    gr[i], useg[i]-distmod,
                    xerr=egr[i], yerr=useeg[i], c=col, marker='+',
                    alpha=0.3)
            i = -1
            ax.errorbar(
                    gr[i], useg[i]-distmod,
                    xerr=egr[i], yerr=useeg[i], c=col, marker='+',
                    alpha=0.3)
            ax.text(gr[0], useg[0]-distmod, name)
            if ii == 2:
                ax.plot(gr, useg-distmod, ls='-', c=col, zorder=0, lw=3,
                        alpha=0.3, label="SNLS (Arcavi+16)")
                # ax.fill_between(gr, useg-distmod-useeg,
                #     useg-distmod+useeg, color='grey', alpha=0.3, zorder=0,
                #     label="PS-1 gold (Drout+14)")
                #ax.fill_betweenx(useg-distmod, gr-egr, gr+egr,
                #    color='grey', alpha=0.3, zorder=0,
                #    label="PS-1 gold (Drout+14)")
            else:
                ax.plot(gr, useg-distmod, ls='-', c=col, zorder=0, lw=3,
                        alpha=0.3)

def sn2002bj(ax):
    distmod = Planck15.distmod(z=0.012108).value
    dt = np.array([1, 3, 4, 5, 9, 11, 15])
    B = np.array([14.79, 14.97, 15.03, 15.15, 15.75, 16.27, 17.92])
    V = np.array([14.91, 15.03, 15.10, 15.20, 15.71, 16.04, 17.93])
    R = np.array([14.97, 15.06, 15.08, 15.21, 15.65, 16.06, 17.32])
    ax.plot(B-R, B-distmod, ls='-', c='purple', zorder=0, lw=3,
            alpha=0.3, label="SN2002bj")

def fbot():
    fig,ax = plt.subplots(1,1,figsize=(7,5))
    at2018gep(ax)
    at2018cow(ax)
    drout(ax)
    arcavi(ax)
    #sn2002bj(ax) # it's a spec. classified SN, not an FBOT
    ax.tick_params(axis='both', labelsize=14)
    cbar.ax.set_ylabel("Days from some $t_0$", fontsize=12)
    cbar.ax.tick_params(labelsize=12)
    ax.set_xlabel("$g-r$, observer frame", fontsize=16)
    ax.set_ylabel("Absolute $g$-band mag, observer frame", fontsize=16)
    plt.xlim(-1, 1.5)
    plt.ylim(-12.5, -21)
    plt.legend(prop={'size':12})

    plt.tight_layout()
    #plt.show()
    plt.savefig("fbot_g_gr.png")

def asu(ax):
    ddir = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/lc"
    dat = np.loadtxt(ddir + "/lc_16asu.txt", delimiter='&', dtype='str')
    t = dat[:,0].astype(float)
    dt = t-t[0]
    band = np.array([val.strip() for val in dat[:,2]])
    mag_raw = dat[:,3]
    mag = np.zeros(len(mag_raw))
    emag = np.zeros(len(mag))
    for ii,val in enumerate(mag_raw):
        if '>' not in val:
            mag[ii] = float(val.split('$pm$')[0])
            emag[ii] = float(val.split('$pm$')[1])
    choose = np.logical_and(mag > 0, band == 'g')
    g = mag[choose]
    gdt = dt[choose]
    choose = np.logical_and(mag > 0, band == 'r')
    r = mag[choose]
    rdt = dt[choose]
    dt_grid = np.arange(20, 36, 1)
    gplt = np.interp(dt_grid, gdt, g)
    rplt = np.interp(dt_grid, rdt, r)
    gr = gplt-rplt
    ax.plot(
            gr, gplt-39.862, c='black',
            lw=3, alpha=0.3, label="No radio, no GRB")
    ax.text(gr[0], gplt[0]-39.862, "iPTF16asu")

def sn2009bb(ax):
    """ got this from the open SN catalog """
    ddir = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/lc"
    dat = np.loadtxt(ddir + "/sn2009bb.txt", delimiter=',', dtype='str')
    mjd = dat[:,1].astype(float)
    mag = dat[:,2].astype(float)
    #emag = dat[:,3].astype(float)
    band = dat[:,5]
    dt = mjd-mjd[0]
    choose = band == 'g'
    g = mag[choose]
    gdt = dt[choose]

    choose = np.logical_or(band == 'r', band == 'R')
    r = mag[choose]
    rdt = dt[choose]

    distmod = Planck15.distmod(z=0.0104).value
    dt_grid = np.arange(0, 50, 1)
    gplt = np.interp(dt_grid, gdt, g)
    rplt = np.interp(dt_grid, rdt, r)
    gr = gplt-rplt
    ax.plot(
            gr, gplt-distmod, c='orange',
            lw=3, alpha=0.3, label="Radio, no GRB")
    ax.text(gr[0], gplt[0]-distmod, "SN2009bb")

def sn2012ap(ax):
    """ got this from the open SN catalog """
    ddir = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/lc"
    dat = np.loadtxt(ddir + "/sn2012ap.txt", delimiter=',', dtype='str')
    mjd = dat[:,1].astype(float)
    mag = dat[:,2].astype(float)
    band = dat[:,5]
    dt = mjd-mjd[0]

    choose = band == 'B'
    g = mag[choose]
    gdt = dt[choose]

    choose = band == 'R'
    r = mag[choose]
    rdt = dt[choose]

    distmod = Planck15.distmod(z=0.01224).value
    dt_grid = np.arange(0, 18, 1)
    gplt = np.interp(dt_grid, gdt, g)
    rplt = np.interp(dt_grid, rdt, r)
    gr = gplt-rplt
    ax.plot(
            gr, gplt-distmod, c='orange',
            lw=3, alpha=0.3)
    ax.text(gr[0], gplt[0]-distmod, "SN2012ap")

def iptf17cw(ax):
    # R-band
    g = np.array([19.041, 19.235, 19.294, 20.93])
    r = np.array([18.703, 18.721, 18.793, 19.998])
    distmod = Planck15.distmod(z=0.093).value
    gr = g-r
    ax.plot(
            gr, g-distmod, c='orange',
            lw=3, alpha=0.3)
    ax.text(gr[0], g[0]-distmod, "iPTF17cw")

def sn2006aj(ax):
    t0 = Time('2006-02-18').mjd + 0.149
    t = np.array(
            [Time('2006-02-20').mjd + 0.162,
             Time('2006-02-21').mjd + 0.196,
             Time('2006-02-23').mjd + 0.196,
             Time('2006-02-25').mjd + 0.112,
             Time('2006-02-27').mjd + 0.167,
             Time('2006-03-04').mjd + 0.097])
    dt = t-t0
    V = np.array([17.84, 17.73, 17.30, 17.10, 17.0, 17.09])
    R = np.array([17.76, 17.22, 17.22, 16.96, 16.84, 16.88])
    gr = V-R
    distmod = Planck15.distmod(z=0.033023).value
    plt.scatter(
            gr, V-distmod, marker='o', edgecolor='orange',
            facecolor='white', label='Ic-BL')
    plt.plot(gr, V-distmod, c='orange')

def sn1998bw(ax):
    """ got this from the open SN catalog """
    ddir = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/lc"
    dat = np.loadtxt(ddir + "/sn1998bw.txt", delimiter=',', dtype='str')
    mjd = dat[:,1].astype(float)
    mag = dat[:,2].astype(float)
    band = dat[:,5]
    dt = mjd-mjd[0]

    choose = band == 'V'
    g = mag[choose]
    gdt = dt[choose]

    choose = np.logical_or(band == 'R', band == "r'")
    r = mag[choose]
    rdt = dt[choose]

    distmod = Planck15.distmod(z=0.0085).value
    dt_grid = np.arange(0, 50, 1)
    gplt = np.interp(dt_grid, gdt, g)
    rplt = np.interp(dt_grid, rdt, r)
    gr = gplt-rplt
    ax.plot(
            gr, gplt-distmod, c='purple',
            lw=3, alpha=0.3)
    ax.text(gr[1], gplt[1]-distmod, "SN1998bw",
            horizontalalignment='right')

def sn2010bh(ax):
    """ got this from the open SN catalog """
    ddir = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/lc"
    dat = np.loadtxt(ddir + "/sn2010bh.txt", delimiter=',', dtype='str')
    mjd = dat[:,1].astype(float)
    mag = dat[:,2].astype(float)
    band = dat[:,5]
    dt = mjd-mjd[0]

    choose = band == 'V'
    g = mag[choose]
    gdt = dt[choose]

    choose = np.logical_or(band == 'R_c', band == "r'")
    r = mag[choose]
    rdt = dt[choose]

    distmod = Planck15.distmod(z=0.0593).value
    dt_grid = np.arange(5, 34, 1)
    gplt = np.interp(dt_grid, gdt, g)
    rplt = np.interp(dt_grid, rdt, r)
    gr = gplt-rplt
    ax.plot(
            gr, gplt-distmod, c='purple',
            lw=3, alpha=0.3)
    ax.text(gr[1], gplt[1]-distmod, "SN2010bh",
            horizontalalignment='right')

def sn2007ru(ax):
    """ got this from the open SN catalog """
    ddir = "/Users/annaho/Dropbox/Projects/Research/ZTF18abukavn/data/lc"
    dat = np.loadtxt(ddir + "/sn2007ru.txt", delimiter=',', dtype='str')
    mjd = dat[:,1].astype(float)
    mag = dat[:,2].astype(float)
    band = dat[:,5]
    dt = mjd-mjd[0]

    choose = band == 'V'
    g = mag[choose]
    gdt = dt[choose]

    choose = np.logical_or(band == 'R_c', band == "r'")
    r = mag[choose]
    rdt = dt[choose]

    distmod = Planck15.distmod(z=0.0593).value
    dt_grid = np.arange(5, 34, 1)
    gplt = np.interp(dt_grid, gdt, g)
    rplt = np.interp(dt_grid, rdt, r)
    gr = gplt-rplt
    ax.plot(
            gr, gplt-distmod, c='purple',
            lw=3, alpha=0.3)
    ax.text(gr[1], gplt[1]-distmod, "SN2010bh",
            horizontalalignment='right')

if __name__=="__main__":
    fig,ax = plt.subplots(1,1,figsize=(7,5))
    cb = at2018gep(ax)
    at2018cow(ax)
    ksn2015k(ax)

    # Formatting
    ax.tick_params(axis='both', labelsize=14)
    ax.set_xlabel("$g-r$, observer frame", fontsize=16)
    ax.set_ylabel("Absolute $g$-band mag, observer frame", fontsize=16)
    plt.xlim(-0.7, 1.0)
    plt.ylim(-15.8, -20.7)
    plt.legend(prop={'size':14})

    plt.tight_layout()

    #plt.show()
    plt.savefig("g_gr.eps", format="eps", dpi=1000)

ext: py

reason: import numpy,from astropy


text:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on March 10 2020

@author: Marianne Bakken

Translated from C++ code by Richard Moore

---
Camera models for pixel-to-vector transformation

"""
import os
import numpy as np
import xmltodict
import math

class OcamCalibCameraModel:
    '''
    OcamCalib camera model for fisheye cameras
    '''
    def __init__(self,calib_file):
        param_dict = self.read_opencv_storage_from_file(calib_file)
        self.set_params(param_dict)
        return None

    def set_params(self,opencv_storage_dict):
        #Etract parameters from opencv storage dictionary object
        d = opencv_storage_dict
        self.fx = np.zeros(5)
        self.fx[0] = float(d['ss4'])
        self.fx[1] = float(d['ss3'])
        self.fx[2] = float(d['ss2'])
        self.fx[3] = float(d['ss1'])
        self.fx[4] = float(d['ss0'])
        self.M = np.zeros((2,2))
        self.M[0,0] = float(d['c'])
        self.M[0,1] = float(d['d'])
        self.M[1,0] = float(d['e'])
        self.M[1,1] = 1.0
        self.xc = float(d['xc'])
        self.yc = float(d['yc'])
        self.width = int(d['width'])
        self.height = int(d['height'])
        self.image_circle_FOV = float(d['imageCircleFOV'])

        #Derived parameters
        self.dfdx = np.zeros(4)
        self.dfdx[0] = 4 * self.fx[0]
        self.dfdx[1] = 3 * self.fx[1]
        self.dfdx[2] = 2 * self.fx[2]
        self.dfdx[3] = self.fx[3]
        self.inv_M = np.linalg.inv(self.M)
        self.initial_x = self.width/4 #starting point for polynomial solver

    def read_opencv_storage_from_file(self,calib_file):
        print(calib_file)
        with open(calib_file) as fd:
            dict_ = xmltodict.parse(fd.read())
            model_dict = dict_['opencv_storage']['cam_model']
        return model_dict

    def vector_to_pixel(self,point):
        '''
        Go from vector (in camera coordinates) to pixel (image coordinates)
        input: point - x,y,z in camera frame
        '''
        forward = np.array([0,0,1])
        r = vec3_normalise(point)
        alpha = np.arccos(np.dot(r,forward))
        R = self.alpha_to_R(alpha)
        if R <0 :
            x = -1.0
            y = -1.0
            return False
        #Scale to get ideal fisheye pixel coordinates:
        mag = np.sqrt(r[0]**2 + r[1]**2)
        if (mag != 0):
            mag = R / mag
        # NOTE: model (x,y) is (height,width) so we swap
        px = r[1] * mag
        py = r[0] * mag
        # Account for non ideal fisheye effects (shear and translation):
        y = self.M[0,0]*px + self.M[0,1]*py + self.xc
        x = self.M[1,0]*px + self.M[1,1]*py + self.yc

        x = np.round(x).astype(int)
        y = np.round(y).astype(int)
        return x, y, R**2

    def pixel_to_vector(self,x,y):
        #NOTE: model (x,y) is (height,width) so we swap
        dx = y - self.xc
        dy = x - self.yc;
        px = self.inv_M[0,0]*dx +self.inv_M[0,1]*dy;
        py = self.inv_M[1,0]*dx + self.inv_M[1,1]*dy;
        R2 = px*px + py*py;

        direction = np.array([0,0,0])
        direction[0] = py;
        direction[1] = px;
        direction[2] = -eval_poly4(self.fx, np.sqrt(R2));

        return direction

    def alpha_to_R(self,alpha):
        '''
        Solves polynomial to go from alpha (angle between rays) to R (distance from center point on sensor)
        '''
        #Newton-Raphson search for the solution

        newFx3 = self.fx[3] - np.tan(alpha - np.pi/2);
        fx = np.array([self.fx[0], self.fx[1], self.fx[2], newFx3, self.fx[4]])
        dfdx = np.array([self.dfdx[0], self.dfdx[1], self.dfdx[2], newFx3])

        x = self.initial_x;
        while True:
            px = x
            x -= eval_poly4(fx,x) / eval_poly3(dfdx,x)
            if (abs(x - px) > 1e-3):
                break
        R = x
        return R

class RectilinearCameraModel:
    '''
    Rectilinear camera model(compatible with Realsense)
    '''
    def __init__(self,calib_file):
        '''
        Load xml file with intrinsic calibration parameters
        '''
        param_dict = self.read_opencv_storage_from_file(calib_file)
        self.set_params(param_dict)

        #Derived parameters
        self.focalLengthPixels = (self.height * 0.5) / math.tan(self.verticalFOV * 0.5)
        R = self.focalLengthPixels * math.tan(self.imageCircleFOV * 0.5)
        if (self.imageCircleFOV <= 0):
            R = self.width + self.height; # allows everything
        self.imageCircleR2 = R * R

    def set_params(self,opencv_storage_dict):
        '''
        Extract parameters from opencv storage dictionary object
        '''
        d = opencv_storage_dict
        self.xc = float(d['centreX'])
        self.yc = float(d['centreY'])
        self.imageCircleFOV = float(d['imageCircleFOV'])
        self.verticalFOV = float(d['verticalFOV'])
        self.width = int(d['width'])
        self.height = int(d['height'])

    def read_opencv_storage_from_file(self,calib_file):
        with open(calib_file) as fd:
            dict_ = xmltodict.parse(fd.read())
            model_dict = dict_['opencv_storage']['cam_model']
        return model_dict

    def vector_to_pixel(self, point):
        '''
        Go from vector (in camera coordinates) to pixel (image coordinates)
        input: point (list) - x,y,z in camera frame
        '''
        s = self.focalLengthPixels / point[2]
        dx = point[0] * s
        dy = point[1] * s
        x = dx + self.xc
        y = dy + self.yc
        x = np.round(x).astype(int)
        y = np.round(y).astype(int)

        R_squared = dx**2 + dy**2
        return x, y, R_squared

    def pixel_to_vector(self, x, y):
        '''
        Go from pixel in image coordinates to vector in camera coordinates
        '''

        #NOTE: model (x,y) is (height,width) so we swap
        dx = x - self.xc
        dy = y - self.yc
        direction = np.array([0,0,0])
        direction[0] = dx
        direction[1] = dy
        direction[2] = self.focalLengthPixels

        return direction

# Utility functions
def vec3_normalise(point):
    norm = np.linalg.norm(point)
    if norm == 0:
       return point
    return point / norm

#Evaluation of polynomials
#cubic
def eval_poly3(poly,x):
    return ((poly[0]*x + poly[1])*x + poly[2])*x + poly[3]

#quartic
def eval_poly4(poly, x):
    return (((poly[0]*x + poly[1])*x + poly[2])*x + poly[3])*x + poly[4]

max_stars_repo_name: SINTEF-Computer-Vision/autolabel

max_issues_repo_name: SINTEF-Computer-Vision/autolabel

max_forks_repo_name: SINTEF-Computer-Vision/autolabel

reason: import numpy


text:
'''
Created on May 23, 2015

@author: hsorby
'''
import numpy as np

from PySide2 import QtCore, QtWidgets

from mapclientplugins.hearttransformstep.view.ui_hearttransformwidget import Ui_HeartTransformWidget
from mapclientplugins.hearttransformstep.scene.master import HeartTransformScene
from mapclientplugins.hearttransformstep.definitions import BASE_PART, APEX_PART, \
    RV_PART

class HeartTransformWidget(QtWidgets.QWidget):
    '''
    classdocs
    '''

    def __init__(self, model, parent=None):
        '''
        Constructor
        '''
        super(HeartTransformWidget, self).__init__(parent)
        self._ui = Ui_HeartTransformWidget()
        self._ui.setupUi(self)
        self._ui.widgetZinc.setContext(model.getContext())
        self._ui.widgetZinc.setModel(model)
        self._callback = None
        self._master_model = model
        self._master_scene = HeartTransformScene(model)

        self._makeConnections()

    def _makeConnections(self):
        self._ui.pushButtonDone.clicked.connect(self._doneButtonClicked)
        self._ui.pushButtonViewAll.clicked.connect(self._viewAllButtonClicked)
        self._ui.pushButtonHideAll.clicked.connect(self._hideAllButtonClicked)
        self._ui.listWidget.itemChanged.connect(self._itemChanged)
        self._ui.comboBoxMode.currentIndexChanged.connect(self._modeChanged)
        self._ui.spinBoxPointSize.valueChanged.connect(self._pointSizeChanged)
        self._ui.pushButtonLoad.clicked.connect(self._loadButtonClicked)
        self._ui.pushButtonSave.clicked.connect(self._saveButtonClicked)

        self._master_model.registerActiveModeListener(self._activeModeChanged)
        self._ui.widgetZinc.graphicsInitialized.connect(self._graphicsInitialized)

    def _graphicsInitialized(self):
        sceneviewer = self._ui.widgetZinc.getSceneviewer()
        scenepicker = self._ui.widgetZinc.getScenepicker()
        if sceneviewer is not None and scenepicker is not None:
            scene = self._master_scene.getScene()
            sceneviewer.setScene(scene)
            scenepicker.setScene(scene)
            sceneviewer.viewAll()

    def _loadButtonClicked(self):
        self._master_model.load()

    def _saveButtonClicked(self):
        self._master_model.save()

    def _pointSizeChanged(self, value):
        self._master_scene.setNodeGraphicsSize(value)

    def _modeChanged(self, value):
        ct = self._ui.comboBoxMode.currentText()
        mode = BASE_PART
        if ct == 'Base':
            mode = BASE_PART
        elif ct == 'Apex':
            mode = APEX_PART
        elif ct == 'RV':
            mode = RV_PART
        self._master_model.setBlockSignals(True)
        self._master_model.setActiveMode(mode)
        self._master_model.setBlockSignals(False)

    def _activeModeChanged(self, mode):
        self._ui.comboBoxMode.blockSignals(True)
        if mode == APEX_PART:
            self._ui.comboBoxMode.setCurrentIndex(0)
        elif mode == BASE_PART:
            self._ui.comboBoxMode.setCurrentIndex(1)
        elif mode == RV_PART:
            self._ui.comboBoxMode.setCurrentIndex(2)
        self._ui.comboBoxMode.blockSignals(False)

    def _itemChanged(self, item):
        region = item.text()
        self._master_model.setImageRegionVisibility(region, item.checkState() == QtCore.Qt.Checked)

    def _viewAllButtonClicked(self):
        self._master_model.beginHierarchicalChange()
        self._ui.widgetZinc.viewAll()
        for index in range(self._ui.listWidget.count()):
            item = self._ui.listWidget.item(index)
            item.setCheckState(QtCore.Qt.Checked)
        self._master_model.endHierarchicalChange()

    def _hideAllButtonClicked(self):
        self._master_model.beginHierarchicalChange()
        for index in range(self._ui.listWidget.count()):
            item = self._ui.listWidget.item(index)
            item.setCheckState(QtCore.Qt.Unchecked)
        self._master_model.endHierarchicalChange()

    def _setupUi(self):
        self._ui.listWidget.clear()
        region_names = self._master_model.getImageRegionNames()

        for region_name in region_names:
            item = QtWidgets.QListWidgetItem(self._ui.listWidget)
            item.setText(region_name)
            item.setFlags(QtCore.Qt.ItemIsUserCheckable | item.flags())
            item.setCheckState(QtCore.Qt.Checked)

    def clear(self):
        self._master_model.clear()
        self._master_scene.clear()

    def initialise(self):
        self._master_model.initialise()
        self._master_scene.initialise()
        self._setupUi()
        self._graphicsInitialized()

    def setImageData(self, axis, image_data):
        self._master_model.setImageData(axis, image_data)

    def registerDoneExecution(self, callback):
        self._callback = callback

    def _doneButtonClicked(self):
        self._callback()

    def getAffineTransformation(self):
        t = self._master_model.getOrigin()
        R = self._master_model.getTransformationMatrix()
        print(t)
        print(R)
        return AffineTransformation(R, t)

class AffineTransformation(object):
    transformType = 'rigid'

    def __init__(self, R, t):
        self._R = R
        self._t = t

    def getRotationMatrix(self):
        return self._R

    def getTranslationVector(self):
        return self._t

    def getT(self):
        return np.array(self._t)

    def getP(self):
        return np.array(self._R)

    def __str__(self):
        r = str(self._R)
        t = str(self._t)

        string = 'mx: {0}\nt: {1}'.format(r, t)
        return string

max_stars_repo_name: tsalemink/hearttransform

max_issues_repo_name: tsalemink/hearttransform

max_forks_repo_name: tsalemink/hearttransform


text:
# NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the software in any medium, provided that you keep intact this entire notice. You may improve, modify and create derivative works of the software or any portion of the software, and you may copy and distribute such modifications or works. Modified works should carry a notice stating that you changed the software and should note the date and nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the source of the software.

# NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE.

# You are solely responsible for determining the appropriateness of using and distributing the software and you assume all risks associated with its use, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of operation. This software is not intended to be used in any situation where a failure could cause risk of injury or damage to property. The software developed by NIST employees is not subject to copyright protection within the United States.

import sys
if sys.version_info[0] < 3:
    print('Python3 required')
    sys.exit(1)

import argparse
import os
import tensorflow as tf
tf_version = tf.__version__.split('.')
if int(tf_version[0]) != 2:
    print('Tensorflow 2.x.x required')
    sys.exit(1)

#trying to fix the cuDNN issue

config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.compat.v1.Session(config=config)

gpu_devices = tf.config.experimental.list_physical_devices("GPU")
for device in gpu_devices:
        tf.config.experimental.set_memory_growth(device, True)

##end of fixing

import unet_model
import numpy as np
import skimage.io
import warnings

# gpu_devices = tf.config.experimental.list_physical_devices('GPU')
# for device in gpu_devices:
#     tf.config.experimental.set_memory_growth(device, True)

def zscore_normalize(image_data):
    image_data = image_data.astype(np.float32)

    if len(image_data.shape) == 3:
        # input is CHW
        for c in range(image_data.shape[0]):
            std = np.std(image_data[c, :, :])
            mv = np.mean(image_data[c, :, :])
            if std <= 1.0:
                # normalize (but dont divide by zero)
                image_data[c, :, :] = (image_data[c, :, :] - mv)
            else:
                # z-score normalize
                image_data[c, :, :] = (image_data[c, :, :] - mv) / std
    elif len(image_data.shape) == 2:
        # input is HW
        std = np.std(image_data)
        mv = np.mean(image_data)
        if std <= 1.0:
            # normalize (but dont divide by zero)
            image_data = (image_data - mv)
        else:
            # z-score normalize
            image_data = (image_data - mv) / std
    else:
        raise IOError("Input to Z-Score normalization needs to be either a 2D or 3D image [HW, or CHW]")

    return image_data

def _inference_tiling(img, model, tile_size):

    # Pad the input image in CPU memory to ensure its dimensions are multiples of the U-Net Size Factor
    pad_x = 0
    pad_y = 0
    if img.shape[0] % unet_model.UNet.SIZE_FACTOR != 0:
        pad_y = (unet_model.UNet.SIZE_FACTOR - img.shape[0] % unet_model.UNet.SIZE_FACTOR)
        print('image height needs to be a multiple of {}, padding with reflect'.format(unet_model.UNet.SIZE_FACTOR))
    if img.shape[1] % unet_model.UNet.SIZE_FACTOR != 0:
        pad_x = (unet_model.UNet.SIZE_FACTOR - img.shape[1] % unet_model.UNet.SIZE_FACTOR)
        print('image width needs to be a multiple of {}, padding with reflect'.format(unet_model.UNet.SIZE_FACTOR))

    if len(img.shape) != 2 and len(img.shape) != 3:
        raise IOError('Invalid number of dimensions for input image. Expecting HW or HWC dimension ordering.')

    if len(img.shape) == 2:
        # add a channel dimension
        img = img.reshape((img.shape[0], img.shape[1], 1))
    if pad_x > 0 or pad_y > 0:
        img = np.pad(img, pad_width=((0, pad_y), (0, pad_x), (0, 0)), mode='reflect')
        print('Padded Image Size: {}'.format(img.shape))

    height = img.shape[0]
    width = img.shape[1]
    mask = np.zeros((height, width), dtype=np.int32)

    radius = unet_model.UNet.RADIUS
    assert tile_size % unet_model.UNet.SIZE_FACTOR == 0
    assert radius % unet_model.UNet.SIZE_FACTOR == 0
    zone_of_responsibility_size = tile_size - 2 * radius

    for i in range(0, height, zone_of_responsibility_size):
        for j in range(0, width, zone_of_responsibility_size):

            x_st_z = j
            y_st_z = i
            x_end_z = x_st_z + zone_of_responsibility_size
            y_end_z = y_st_z + zone_of_responsibility_size

            # pad zone of responsibility by radius
            x_st = x_st_z - radius
            y_st = y_st_z - radius
            x_end = x_end_z + radius
            y_end = y_end_z + radius

            radius_pre_x = radius
            if x_st < 0:
                x_st = 0
                radius_pre_x = 0

            radius_pre_y = radius
            if y_st < 0:
                radius_pre_y = 0
                y_st = 0

            radius_post_x = radius
            if x_end > width:
                radius_post_x = 0
                x_end = width
                x_end_z = width

            radius_post_y = radius
            if y_end > height:
                radius_post_y = 0
                y_end = height
                y_end_z = height

            # crop out the tile
            tile = img[y_st:y_end, x_st:x_end]

            # convert HWC to CHW
            batch_data = tile.transpose((2, 0, 1))
            # convert CHW to NCHW
            batch_data = batch_data.reshape((1, batch_data.shape[0], batch_data.shape[1], batch_data.shape[2]))

            sm = model(batch_data)  # model output defined in unet_model is softmax
            sm = np.squeeze(sm)
            pred = np.squeeze(np.argmax(sm, axis=-1).astype(np.int32))

            # radius_pre_x
            if radius_pre_x > 0:
                pred = pred[:, radius_pre_x:]
                sm = sm[:, radius_pre_x:]

            # radius_pre_y
            if radius_pre_y > 0:
                pred = pred[radius_pre_y:, :]
                sm = sm[radius_pre_y:, :]

            # radius_post_x
            if radius_post_x > 0:
                pred = pred[:, :-radius_post_x]
                sm = sm[:, :-radius_post_x]

            # radius_post_y
            if radius_post_y > 0:
                pred = pred[:-radius_post_y, :]
                sm = sm[:-radius_post_y, :]

            mask[y_st_z:y_end_z, x_st_z:x_end_z] = pred

    # undo and CPU side image padding to make the image a multiple of U-Net Size Factor
    if pad_x > 0:
        mask = mask[:, 0:-pad_x]
    if pad_y > 0:
        mask = mask[0:-pad_y, :]
    return mask

def _inference(img, model):
    pad_x = 0
    pad_y = 0

    if img.shape[0] % unet_model.UNet.SIZE_FACTOR != 0:
        pad_y = (unet_model.UNet.SIZE_FACTOR - img.shape[0] % unet_model.UNet.SIZE_FACTOR)
        print('image height needs to be a multiple of {}, padding with reflect'.format(unet_model.UNet.SIZE_FACTOR))
    if img.shape[1] % unet_model.UNet.SIZE_FACTOR != 0:
        pad_x = (unet_model.UNet.SIZE_FACTOR - img.shape[1] % unet_model.UNet.SIZE_FACTOR)
        print('image width needs to be a multiple of {}, padding with reflect'.format(unet_model.UNet.SIZE_FACTOR))

    if len(img.shape) != 2 and len(img.shape) != 3:
        raise IOError('Invalid number of dimensions for input image. Expecting HW or HWC dimension ordering.')

    if len(img.shape) == 2:
        # add a channel dimension
        img = img.reshape((img.shape[0], img.shape[1], 1))
    img = np.pad(img, pad_width=((0, pad_y), (0, pad_x), (0, 0)), mode='reflect')

    # convert HWC to CHW
    batch_data = img.transpose((2, 0, 1))
    # convert CHW to NCHW
    batch_data = batch_data.reshape((1, batch_data.shape[0], batch_data.shape[1], batch_data.shape[2]))

    softmax = model(batch_data) # model output defined in unet_model is softmax
    softmax = np.squeeze(softmax)
    pred = np.squeeze(np.argmax(softmax, axis=-1).astype(np.int32))

    if pad_x > 0:
        pred = pred[:, 0:-pad_x]
    if pad_y > 0:
        pred = pred[0:-pad_y, :]

    return pred

def inference(saved_model_filepath, image_folder, output_folder, image_format, use_intensity_scaling):
    # create output filepath
    if not os.path.exists(output_folder):
        os.mkdir(output_folder)

    prob_folder = output_folder.replace('_mask','_prob')
    if not os.path.exists(prob_folder):
        os.mkdir(prob_folder)

    img_filepath_list = [os.path.join(image_folder, fn) for fn in os.listdir(image_folder) if fn.endswith('.{}'.format(image_format))]

    print('Loading saved model')
    print('  found files:')
    for fn in os.listdir(saved_model_filepath):
        print(fn)
    model = tf.saved_model.load(saved_model_filepath)

    print('Starting inference of file list')
    for i in range(len(img_filepath_list)):
        img_filepath = img_filepath_list[i]
        _, slide_name = os.path.split(img_filepath)
        print('{}/{} : {}'.format(i, len(img_filepath_list), slide_name))

        # print('Loading image: {}'.format(img_filepath))
        img = skimage.io.imread(img_filepath) # HW or HWC format
        img = img.astype(np.float32)

        # enable or disable intensity scaling - added to support concrete project
        if use_intensity_scaling:
            img = zscore_normalize(img) # normalize with whole image stats
        print('  img.shape={}'.format(img.shape))

        if img.shape[0] > 1024 or img.shape[1] > 1024:
            tile_size = 1024 # in theory UNet takes about 420x the amount of memory of the input image
            # to a tile size of 1024 should require 1.7 GB of GPU memory
            segmented_mask = _inference_tiling(img, model, tile_size)
        else:
            segmented_mask = _inference(img, model)

        if 0 <= np.max(segmented_mask) <= 255:
            segmented_mask = segmented_mask.astype(np.uint8)
        if 255 < np.max(segmented_mask) < 65536:
            segmented_mask = segmented_mask.astype(np.uint16)
        if np.max(segmented_mask) > 65536:
            segmented_mask = segmented_mask.astype(np.int32)

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            if 'tif' in image_format:
                skimage.io.imsave(os.path.join(output_folder, slide_name), segmented_mask, compress=6, bigtiff=True, tile=(1024,1024))
            else:
                skimage.io.imsave(os.path.join(output_folder, slide_name), segmented_mask, compress=6)

def main():
    # Setup the Argument parsing
    parser = argparse.ArgumentParser(prog='inference', description='Script which inferences a folder of images using a unet model')

    parser.add_argument('--model', dest='saved_model_filepath', type=str,
                        help='SavedModel filepath to the  model to use', required=True)
    parser.add_argument('--imageDir', dest='image_dir', type=str, help='filepath to the directory containing the images', required=True)
    parser.add_argument('--outputDir', dest='output_dir', type=str, help='Folder where outputs will be saved (Required)', required=True)
    # added for the concrete project
    parser.add_argument('--useIntensityScaling', dest='use_intensity_scaling', type=str,
                        help='whether to use intensity scaling when inferring [YES, NO]', default="YES")

    print('Arguments:')
    args = parser.parse_args()

    saved_model_filepath = args.saved_model_filepath
    output_dir = args.output_dir
    image_dir = args.image_dir
    use_intensity_scaling = args.use_intensity_scaling
    use_intensity_scaling = use_intensity_scaling.upper() == "YES"

    print('model = {}'.format(saved_model_filepath))
    print('imageDir = {}'.format(image_dir))
    print('outputDir = {}'.format(output_dir))
    print('use_intensity_scaling = {}'.format(use_intensity_scaling))

    image_format = 'tif'

    inference(saved_model_filepath, image_dir, output_dir, image_format, use_intensity_scaling)

if __name__ == "__main__":
    main()

max_stars_repo_name: usnistgov/WIPP-fpn-inference-plugin

max_issues_repo_name: usnistgov/WIPP-fpn-inference-plugin

max_forks_repo_name: usnistgov/WIPP-fpn-inference-plugin


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

import numpy as np
import pylab

from ltv_mpc import Problem
from ltv_mpc import solve_mpc

if __name__ == "__main__":
    # Double integrator LTV system
    horizon_duration = 1.0
    nb_timesteps = 16
    T = horizon_duration / nb_timesteps
    A = np.array([[1.0, T, T ** 2 / 2.0], [0.0, 1.0, T], [0.0, 0.0, 1.0]])
    B = np.array([T ** 3 / 6.0, T ** 2 / 2.0, T]).reshape((3, 1))

    # Acceleration limits
    accel_from_state = np.array([0.0, 0.0, 1.0])
    max_accel = 3.0  # [m] / [s] / [s]
    ineq_matrix = np.vstack([+accel_from_state, -accel_from_state])
    ineq_vector = np.array([+max_accel, +max_accel])

    # Complete LTV problem
    initial_pos = 0.0
    goal_pos = 1.0
    problem = Problem(
        transition_state_matrix=A,
        transition_input_matrix=B,
        ineq_state_matrix=ineq_matrix,
        ineq_input_matrix=None,
        ineq_vector=ineq_vector,
        initial_state=np.array([initial_pos, 0.0, 0.0]),
        goal_state=np.array([goal_pos, 0.0, 0.0]),
        nb_timesteps=nb_timesteps,
        terminal_cost_weight=1.0,
        stage_state_cost_weight=None,
        stage_input_cost_weight=1e-6,
    )

    # Solve our LTV problem
    solution = solve_mpc(problem, solver="osqp")

    # Plot solution
    t = np.linspace(0.0, horizon_duration, nb_timesteps + 1)
    X = solution.stacked_states
    positions, velocities, accelerations = X[:, 0], X[:, 1], X[:, 2]
    pylab.ion()
    pylab.clf()
    pylab.plot(t, positions)
    pylab.plot(t, velocities)
    pylab.plot(t, accelerations)
    pylab.grid(True)
    pylab.legend(("position", "velocity", "acceleration"))

max_stars_repo_name: tasts-robots/ltv-mpc

max_issues_repo_name: tasts-robots/ltv-mpc

max_forks_repo_name: tasts-robots/ltv-mpc


text:
import random
import numpy as np
import cv2
import types

from .blurrender import BlurRender
from .aumentation import ObjectTransform
from . import functional as F

class ToTransform(object):
    """Abstrat class of Generic transform
    """

    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        raise NotImplementedError()

    def __str__(self):
        return self.__class__.__name__

class ToRandomTransform(ToTransform):
    """Random transform:
    """

    def __init__(self, tran, prob):
        """Initialization
        Args:
            @tran: class tranform
            @prob: probability
        """
        self.tran = tran
        self.prob=prob

    def __call__(self,obj):
        if random.random() < self.prob:
            obj = self.tran( obj )
        return obj

class ToRandomChoiceTransform(ToTransform):
    """Random choice transform:
    """

    def __init__(self, transforms):
        """Initialization
        Args:
            @transforms: list of the transforms
        """
        assert(len(transforms))
        self.transforms=transforms

    def __call__(self,obj):
        tran = random.choice( self.transforms )
        obj = tran( obj )
        return obj

class ToRandomOrderTransform(ToTransform):
    """Random order transform:
    """

    def __init__(self, transforms):
        """Initialization
        Args:
            @transforms: list of the transforms
        """
        assert(len(transforms))
        self.transforms=transforms

    def __call__(self,obj):
        order = list(range(len(self.transforms)))
        random.shuffle(order)
        for i in order:
            obj = self.transforms[i](obj)
        return obj

class ToLambdaTransform(ToTransform):
    """Random lambda transform:
    """

    def __init__(self, lambd):
        """Initialization
        Args:
            @lambd (function): Lambda/function to be used for transform.
        """
        assert isinstance(lambd, types.LambdaType)
        self.lambd = lambds

    def __call__(self,obj):
        return self.lambd(obj)

class ToTensor(object):
    """Convert ndarrays in sample to Tensors."""

    def __call__(self, obj):
        obj.to_tensor()
        return obj

# Blur transformation

class ToLinealMotionBlur(ToTransform):
    """Lineal Blur randomly.
    """

    def __init__(self, lmax=100  ):
        """Initialization
        Args:
            @lmax: maximun lineal blur
        """
        self.gen = BlurRender(lmax)

    def __call__(self, obj):
        obj.lineal_blur(self.gen)
        return obj

class ToMotionBlur(ToTransform):
    """Motion Blur randomly.
    """

    def __init__(self,
        pSFsize=64,
        maxTotalLength=64,
        anxiety=0.005,
        numT=2000,
        texp=0.75,
        ):
        """Initialization
        Args:
            @pSFsize: kernel size (psf)
            @maxTotalLength: length trayectory
            @anxiety:
            @numT:
            @texp:
        """
        self.gen = BlurRender(pSFsize, maxTotalLength, anxiety, numT, texp)

    def __call__(self, obj):
        obj.motion_blur(self.gen)
        return obj

class ToGaussianBlur(ToTransform):
    """Gaussian Blur randomly.
    """

    def __init__(self, sigma=0.2 ):
        """Initialization
        Args:
            @lmax: maximun lineal blur
        """
        self.sigma = sigma

    def __call__(self, obj):

        # add gaussian noise
        H,W = obj.size()[:2]
        noise = np.array([random.gauss(0,self.sigma) for i in range(H*W)])
        noise = noise.reshape(H,W)
        obj.add_noise( noise )
        wnd = random.randint(1,3) * 2 + 1
        obj.gaussian_blur(wnd)
        return obj

class ToGaussianNoise(ToTransform):
    """Gaussian Noise randomly.
    """
    def __init__(self, sigma=0.2 ):
        """Initialization
        Args:
            @lmax: maximun lineal blur
        """
        self.sigma = sigma

    def __call__(self, obj):

        # add gaussian noise
        H,W = obj.size()[:2]
        noise = np.array([random.gauss(0,self.sigma) for i in range(H*W)])
        noise = noise.reshape(H,W)
        obj.add_noise( noise )
        return obj

# Color tranformations

class RandomBrightness(ToTransform):
    """Random Brightness.
    """
    def __init__(self, factor=0.5 ):
        """Initialization
        Args:
            @factor: factor
        """
        self.factor = factor

    def __call__(self, obj):
        alpha = 1.0 + self.factor*random.uniform(-1, 1)
        obj.brightness(alpha)
        return obj

class RandomBrightnessShift(ToTransform):
    """Random Brightness Shift.
    """
    def __init__(self, factor=0.125, scale_value=100 ):
        """Initialization
        Args:
            @factor: factor
        """
        self.factor = factor
        self.scale_value = scale_value

    def __call__(self, obj):
        alpha = 1.0 + (self.factor)*random.uniform(-1, 1)
        obj.brightness_shift(alpha, self.scale_value)
        return obj

class RandomContrast(ToTransform):
    """Random Contrast.
    """
    def __init__(self, factor=0.3 ):
        """Initialization
        Args:
            @factor: factor
        """
        self.factor = factor

    def __call__(self, obj):
        alpha = 1.0 + self.factor*random.uniform(-1, 1)
        obj.contrast(alpha)
        return obj

class RandomSaturation(ToTransform):
    """Random Saturation.
    """
    def __init__(self, factor=0.75 ):
        """Initialization
        Args:
            @factor: factor
        """
        self.factor = factor

    def __call__(self, obj):
        alpha = 1.0 + self.factor*random.uniform(-1, 1)
        obj.saturation(alpha)
        return obj

class RandomHueSaturationShift(ToTransform):
    """Random Hue Saturation Shift.
    """
    def __init__(self, factor=0.3 ):
        """Initialization
        Args:
            @factor: factor
        """
        self.factor = factor

    def __call__(self, obj):
        alpha = 1.0 + random.uniform(-self.factor, self.factor)
        obj.hue_saturation_shift(alpha)
        return obj

class RandomHueSaturation(ToTransform):
    """Random Hue Saturation.
    """
    def __init__(self, hue_shift_limit=(-5, 5), sat_shift_limit=(-11, 11), val_shift_limit=(-11, 11)):
        """Initialization
        Args:
            @hue_shift_limit: hue_shift_limit
            @sat_shift_limit: sat_shift_limit
            @val_shift_limit: val_shift_limit
        """
        self.hue_shift_limit = hue_shift_limit
        self.sat_shift_limit = sat_shift_limit
        self.val_shift_limit = val_shift_limit

    def __call__(self, obj):
        hue_shift = random.uniform(self.hue_shift_limit[0], self.hue_shift_limit[1])
        sat_shift = random.uniform(self.sat_shift_limit[0], self.sat_shift_limit[1])
        val_shift = random.uniform(self.val_shift_limit[0], self.val_shift_limit[1])
        obj.hue_saturation(hue_shift, sat_shift, val_shift)
        return obj

class RandomRGBShift(ToTransform):
    """Random RGB Shift.
    """
    def __init__(self, r_shift_limit=(-128, 128), g_shift_limit=(-128, 128), b_shift_limit=(-128, 128)):
        """Initialization
        Args:
            @r_shift_limit: r_shift_limit
            @g_shift_limit: g_shift_limit
            @b_shift_limit: b_shift_limit
        """
        self.r_shift_limit = r_shift_limit
        self.g_shift_limit = g_shift_limit
        self.b_shift_limit = b_shift_limit

    def __call__(self, obj):
        r_shift = random.uniform(self.r_shift_limit[0], self.r_shift_limit[1])
        g_shift = random.uniform(self.g_shift_limit[0], self.g_shift_limit[1])
        b_shift = random.uniform(self.b_shift_limit[0], self.b_shift_limit[1])
        obj.rgbshift(r_shift, g_shift, b_shift)
        return obj

class RandomGamma(ToTransform):
    """Random Gamma.
    """
    def __init__(self, factor=0.5 ):
        """Initialization
        Args:
            @factor: factor
        """
        self.factor = factor

    def __call__(self, obj):
        alpha = 1.0 + self.factor*random.uniform(-1, 1)
        obj.gamma_correction(alpha)
        return obj

class ToGrayscale(ToTransform):
    """To gray image
    """

    def __init__(self):
        """Initialization
        """
        pass
    def __call__(self, obj):
        obj.to_gray()
        return obj

class ToNegative(ToTransform):
    """To negative
    """

    def __init__(self):
        """Initialization
        """
        pass
    def __call__(self, obj):
        obj.to_negative()
        return obj

class RandomRGBPermutation(ToTransform):
    """RGB permutation
    """
    def __init__(self):
        """Initialization
        """
        self.indexs = [0,1,2]
    def __call__(self, obj):
        random.shuffle(self.indexs)
        obj.rgbpermutation( self.indexs )
        return obj

class CLAHE(ToTransform):
    """CLAHE ecualization.
    """
    def __init__(self, clipfactor=2.0, tileGridSize=(8, 8) ):
        """Initialization
        Args:
            @factor: factor
        """
        self.clipfactor = clipfactor
        self.tileGridSize = tileGridSize

    def __call__(self, obj):
        obj.clahe( self.clipfactor,  self.tileGridSize )
        return obj

class ToMeanNormalization(ToTransform):
    """To mean normalization
    """

    def __init__(self, mean, std):
        """Initialization
        Args:
            @mean: mean
            @std: estandar desviation
        """
        self.mean = mean
        self.std = std

    def __call__(self, obj):
        obj.mean_normalization(self.mean, self.std)
        return obj

class ToWhiteNormalization(ToTransform):
    """To white normalization
    """
    def __init__(self ):
        """Initialization
        """
        pass
    def __call__(self, obj):
        obj.white_normalization()
        return obj

class ToNormalization(ToTransform):
    """To white normalization
    """
    def __init__(self ):
        """Initialization
        """
        pass
    def __call__(self, obj):
        obj.normalization()
        return obj

class ToEqNormalization(ToTransform):
    """To equalization normalization
    """
    def __init__(self, imsize ):
        """Initialization
        """
        self.A, self.A_pinv = F.compute_norm_mat( imsize[0], imsize[1] )

    def __call__(self, obj):
        obj.eq_normalization(self.A, self.A_pinv)
        return obj

# geometrical transforms

class ToResize(ToTransform):
    """Resize
    """

    def __init__(self, imsize, resize_mode=None, padding_mode=cv2.BORDER_CONSTANT ):
        """Initialization
        Args:
            @imsize: size input layer resize (w,h)
            @resize_mode: resize mode
        """
        self.imsize = imsize
        self.resize_mode = resize_mode
        self.padding_mode = padding_mode

    def __call__(self, obj):
        obj.resize( self.imsize, self.resize_mode, self.padding_mode )
        return obj

class ToPad(ToTransform):
    r"""Pad
    Args:
        h_pad: height padding
        w_pad: width  padding
        pad_mode: resize mode
    """

    def __init__(self, h_pad, w_pad, padding_mode=cv2.BORDER_CONSTANT ):
        self.h_pad = h_pad
        self.w_pad = w_pad
        self.padding_mode = padding_mode

    def __call__(self, obj):
        obj.pad( self.h_pad, self.w_pad, self.padding_mode )
        return obj

class ToResizeUNetFoV(ToTransform):
    """Resize to unet fov
    """

    def __init__(self, fov=388, padding_mode=cv2.BORDER_CONSTANT):
        """Initialization
        Args:
            @fov: size input layer for unet model
        """
        self.fov=fov
        self.padding_mode = padding_mode

    def __call__(self, obj):
        obj.resize_unet_input( self.fov, self.padding_mode )
        return obj

class CenterCrop(ToTransform):
    """Center Crop
    """

    def __init__(self, cropsize, padding_mode=cv2.BORDER_CONSTANT ):
        """Initialization
        Args:
            @cropsize [w,h]
        """
        self.cropsize = cropsize
        self.padding_mode = padding_mode

    def __call__(self, obj):
        h, w = obj.size()[:2]
        x = (w - self.cropsize[0]) // 2
        y = (h - self.cropsize[1]) // 2
        obj.crop( [ x, y, self.cropsize[0], self.cropsize[1] ], self.padding_mode )
        return obj

class RandomCrop(ToTransform):
    """Random Crop
    """

    def __init__(self, cropsize, limit=10, padding_mode=cv2.BORDER_CONSTANT ):
        """Initialization
        Args:
            @cropsize [w,h]
            @limit
        """
        self.cropsize = cropsize
        self.limit = limit
        self.padding_mode = padding_mode
        self.centecrop = CenterCrop(cropsize, padding_mode)

    def __call__(self, obj):
        h, w = obj.size()[:2]
        newW, newH = self.cropsize

        assert(w - newW + self.limit > 0)
        assert(h - newH + self.limit > 0)

        for _ in range(10):
            x = random.randint( -self.limit, (w - newW) + self.limit )
            y = random.randint( -self.limit, (h - newH) + self.limit )
            box = [ x, y, self.cropsize[0], self.cropsize[1] ]
            if obj.crop( box, self.padding_mode ):
                return obj

        return self.centecrop(obj)

class RandomScale(ToTransform):
    """ SRandom Scale.
    """
    def __init__(self, factor=0.1, padding_mode=cv2.BORDER_CONSTANT,
        ):
        """Initialization
        Args:
            @factor: factor of scale
            @padding_mode
        """
        self.factor = factor
        self.padding_mode = padding_mode

    def __call__(self, obj):
        height, width = obj.size()[:2]
        factor =  1.0 + self.factor*random.uniform(-1.0, 1.0)
        obj.scale( factor, self.padding_mode )
        return obj

class HFlip(ToTransform):
    """ Horizontal Flip.
    """
    def __init__(self):
        """Initialization
        """
        pass

    def __call__(self, obj):
        obj.hflip()
        return obj

class VFlip(ToTransform):
    """ Vertical Flip.
    """
    def __init__(self):
        """Initialization
        """
        pass

    def __call__(self, obj):
        obj.vflip()
        return obj

class Rotate90(ToTransform):
    """ Rotate 90.
    """
    def __init__(self):
        """Initialization
        """
        pass

    def __call__(self, obj):
        obj.rotate90()
        return obj

class Rotate180(ToTransform):
    """ Rotate 180 .
    """
    def __init__(self):
        """Initialization
        """
        pass

    def __call__(self, obj):
        obj.rotate180()
        return obj

class Rotate270(ToTransform):
    """ Rotate 270 .
    """
    def __init__(self):
        """Initialization
        """
        pass

    def __call__(self, obj):
        obj.rotate270()
        return obj

class RandomGeometricalTransform(ToTransform):
    """ Random Geometrical Transform
    """
    def __init__(self, angle=360, translation=0.2, warp=0.0, padding_mode=cv2.BORDER_CONSTANT ):
        """Initialization
        Args:
            @angle: ratate angle
            @translate
            @warp
        """
        self.angle = angle
        self.translation = translation
        self.warp = warp
        self.padding_mode = padding_mode

    def __call__(self, obj):

        imsize = obj.size()[:2]
        for _ in range(10):
            mat_r, mat_t, mat_w = F.get_geometric_random_transform( imsize, self.angle, self.translation, self.warp )
            if obj.applay_geometrical_transform( mat_r, mat_t, mat_w, self.padding_mode ):
                return obj
        return obj

class RandomElasticDistort(ToTransform):
    """ Random Elastic Distort
    """

    def __init__(self, size_grid=50, deform=15, padding_mode=cv2.BORDER_CONSTANT  ):
        """Initialization
        Args:
            @size_grid: ratate angle
            @deform
        """
        self.size_grid = size_grid
        self.deform = deform
        self.padding_mode = padding_mode

    def __call__(self, obj):
        imsize = obj.size()[:2]
        if self.deform is None:
            deform = np.random.randint(10, 16)
        else:
            deform = self.deform

        mapx, mapy = F.get_elastic_transform(imsize, self.size_grid, deform )
        obj.applay_elastic_transform( mapx, mapy, self.padding_mode )
        return obj

class RandomElasticTensorDistort(object):
    """ Random Elastic Tensor Distort
    """

    def __init__(self, size_grid=10, deform=0.05  ):
        """Initialization
        Args:
            @size_grid: ratate angle
            @deform
        """
        self.size_grid = size_grid
        self.deform = deform

    def __call__(self, obj):
        width, height = obj.image.size(1), obj.image.size(2)
        grid = F.get_tensor_elastic_transform( (height, width), self.size_grid, self.deform )
        obj.applay_elastic_tensor_transform( grid )
        return obj