text:
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
import math
from convlab.modules.word_policy.multiwoz.hdsa.transformer.Beam import Beam
from convlab.modules.word_policy.multiwoz.hdsa.transformer import Constants
from torch.autograd import Variable

class Sclstm(nn.Module):
    def __init__(self, hidden_size, emb_size, d_size, dropout=0.5):
        super(Sclstm, self).__init__()
        self.hidden_size = hidden_size
        self.dropout = dropout

        self.dc = nn.Linear(d_size, hidden_size, bias=False)

        self.w2h = nn.Linear(2 * emb_size, hidden_size*4)
        self.h2h = nn.Linear(hidden_size, hidden_size*4)

        self.w2h_r= nn.Linear(2 * emb_size, d_size)
        self.h2h_r= nn.Linear(hidden_size, d_size)

        self.src_proj = nn.Linear(emb_size, emb_size)
        self.att_proj = nn.Linear(emb_size, 1)

        self.softmax = nn.Softmax(1)

    def _step(self, input_t, last_hidden, last_cell, last_dt, src_enc, src_mask):
        '''
        * Do feedforward for one step *
        Args:
            input_t: (batch_size, 1, hidden_size)
            last_hidden: (batch_size, hidden_size)
            last_cell: (batch_size, hidden_size)
        Return:
            cell, hidden at this time step
        '''
        # Attend to the source side
        ctx, prob = self.attention(last_hidden, src_enc, src_mask)
        input_t = torch.cat([input_t, ctx], -1)
        # get all gates
        w2h = self.w2h(input_t) # (batch_size, hidden_size*5)
        w2h = torch.split(w2h, self.hidden_size, dim=1) # (batch_size, hidden_size) * 4
        h2h = self.h2h(last_hidden)
        h2h = torch.split(h2h, self.hidden_size, dim=1)

        gate_i = torch.sigmoid(w2h[0] + h2h[0]) # (batch_size, hidden_size)
        gate_f = torch.sigmoid(w2h[1] + h2h[1])
        gate_o = torch.sigmoid(w2h[2] + h2h[2])

        # updata dt
        alpha = 0.5
        gate_r = torch.sigmoid(torch.relu(self.w2h_r(input_t) + alpha * self.h2h_r(last_hidden)))
        dt = gate_r * last_dt

        cell_hat = torch.tanh(w2h[3] + h2h[3])
        cell = gate_f * last_cell + gate_i * cell_hat + self.dc(dt)
        hidden = gate_o * torch.tanh(cell)

        return hidden, cell, dt

    def attention(self, hidden, src_enc, src_mask):
        energy_pre = hidden.unsqueeze(1) + self.src_proj(src_enc)
        energy = self.att_proj(torch.tanh(energy_pre)).squeeze()
        prob = self.softmax(energy)
        prob = prob * src_mask / torch.sum(prob * src_mask, 1, keepdim=True)
        ctx = torch.sum(prob[:, :, None] * src_enc, 1)
        return ctx, prob

    def forward(self, input_seq, last_dt, src_enc, src_mask):
        '''
        Args:
            input_seq: (batch_size, max_len, emb_size)
            dt: (batch_size, feat_size)
        Return:
            output_all: (batch_size, max_len, vocab_size)
        '''
        batch_size = input_seq.size(0)
        max_len = input_seq.size(1)
        # prepare init h and c
        output_all = []
        dt_all = []
        last_hidden = Variable(torch.zeros(batch_size, self.hidden_size))
        last_cell = Variable(torch.zeros(batch_size, self.hidden_size))
        last_hidden = last_hidden.cuda()
        last_cell = last_cell.cuda()

        for t in range(max_len):
            hidden, cell, dt = self._step(input_seq[:, t, :], last_hidden, last_cell, last_dt, src_enc, src_mask)
            last_hidden, last_cell, last_dt = hidden, cell, dt
            output_all.append(hidden.unsqueeze(1))
            dt_all.append(dt.unsqueeze(1))

        output_all = torch.cat(output_all, 1)
        dt_all = torch.cat(dt_all, 1)

        return output_all, dt_all

class PositionalEmbedding(nn.Module):

    def __init__(self, d_model, max_len=512):
        super(PositionalEmbedding, self).__init__()

        # Compute the positional encodings once in log space.
        pe = torch.zeros(max_len, d_model).float()
        pe.require_grad = False

        position = torch.arange(0, max_len).float().unsqueeze(1)
        div_term = (torch.arange(0, d_model, 2).float() * -(math.log(10000.0) / d_model)).exp()

        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)

        pe = pe.unsqueeze(0)
        self.register_buffer('pe', pe)

    def forward(self, x):
        return self.pe[:, :x.size(1)]

class EncoderLayer(nn.Module):
    ''' Compose with two layers '''

    def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1):
        super(EncoderLayer, self).__init__()
        self.slf_attn = MultiHeadAttention(
            n_head, d_model, d_k, d_v, dropout=dropout)
        self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout)

    def forward(self, enc_input, non_pad_mask=None, slf_attn_mask=None):
        enc_output, enc_slf_attn = self.slf_attn(enc_input, enc_input, enc_input, mask=slf_attn_mask)
        enc_output *= non_pad_mask

        enc_output = self.pos_ffn(enc_output)
        enc_output *= non_pad_mask

        return enc_output, enc_slf_attn

class AverageHeadAttention(nn.Module):
    def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
        super(AverageHeadAttention, self).__init__()

        self.n_head = n_head
        self.d_k = d_k
        self.d_v = d_v

        self.w_qs = nn.Linear(d_model, n_head * d_k)
        self.w_ks = nn.Linear(d_model, n_head * d_k)
        self.w_vs = nn.Linear(d_model, n_head * d_v)
        nn.init.normal_(self.w_qs.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_k)))
        nn.init.normal_(self.w_ks.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_k)))
        nn.init.normal_(self.w_vs.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_v)))

        self.attention = ScaledDotProductAttention(temperature=np.power(d_k, 0.5))
        self.layer_norm = nn.LayerNorm(d_model)

        self.fc = nn.Linear(d_v, d_model)
        nn.init.xavier_normal_(self.fc.weight)

        self.dropout = nn.Dropout(dropout)

    def forward(self, a, q, k, v, mask=None):

        d_k, d_v, n_head = self.d_k, self.d_v, self.n_head

        sz_b, len_q, _ = q.size()
        sz_b, len_k, _ = k.size()
        sz_b, len_v, _ = v.size()
        residual = q

        q = self.w_qs(q).view(sz_b, len_q, n_head, d_k)
        k = self.w_ks(k).view(sz_b, len_k, n_head, d_k)
        v = self.w_vs(v).view(sz_b, len_v, n_head, d_v)

        q = q.permute(2, 0, 1, 3).contiguous().view(-1, len_q, d_k) # (n*b) x lq x dk
        k = k.permute(2, 0, 1, 3).contiguous().view(-1, len_k, d_k) # (n*b) x lk x dk
        v = v.permute(2, 0, 1, 3).contiguous().view(-1, len_v, d_v) # (n*b) x lv x dv

        mask = mask.repeat(n_head, 1, 1) # (n*b) x .. x ..
        output, attn = self.attention(q, k, v, mask=mask)

        output = output.view(n_head, sz_b, len_q, d_v)
        a = a.permute(1, 0).contiguous()[:, :, None, None]

        #output = output * a
        output = torch.sum(output * a, 0)
        output = output.view(sz_b, len_q, -1)
        #output = output.permute(1, 2, 0, 3).contiguous().view(sz_b, len_q, -1) # b x lq x (n*dv)

        output = self.dropout(self.fc(output))
        output = self.layer_norm(output + residual)

        return output, attn

class MultiHeadAttention(nn.Module):
    ''' Multi-Head Attention module '''

    def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
        super(MultiHeadAttention, self).__init__()

        self.n_head = n_head
        self.d_k = d_k
        self.d_v = d_v

        self.w_qs = nn.Linear(d_model, n_head * d_k)
        self.w_ks = nn.Linear(d_model, n_head * d_k)
        self.w_vs = nn.Linear(d_model, n_head * d_v)
        nn.init.normal_(self.w_qs.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_k)))
        nn.init.normal_(self.w_ks.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_k)))
        nn.init.normal_(self.w_vs.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_v)))

        self.attention = ScaledDotProductAttention(temperature=np.power(d_k, 0.5))
        self.layer_norm = nn.LayerNorm(d_model)

        self.fc = nn.Linear(n_head * d_v, d_model)
        nn.init.xavier_normal_(self.fc.weight)

        self.dropout = nn.Dropout(dropout)

    def forward(self, q, k, v, mask=None):

        d_k, d_v, n_head = self.d_k, self.d_v, self.n_head

        sz_b, len_q, _ = q.size()
        sz_b, len_k, _ = k.size()
        sz_b, len_v, _ = v.size()

        residual = q

        q = self.w_qs(q).view(sz_b, len_q, n_head, d_k)
        k = self.w_ks(k).view(sz_b, len_k, n_head, d_k)
        v = self.w_vs(v).view(sz_b, len_v, n_head, d_v)

        q = q.permute(2, 0, 1, 3).contiguous().view(-1, len_q, d_k) # (n*b) x lq x dk
        k = k.permute(2, 0, 1, 3).contiguous().view(-1, len_k, d_k) # (n*b) x lk x dk
        v = v.permute(2, 0, 1, 3).contiguous().view(-1, len_v, d_v) # (n*b) x lv x dv

        mask = mask.repeat(n_head, 1, 1) # (n*b) x .. x ..
        output, attn = self.attention(q, k, v, mask=mask)

        output = output.view(n_head, sz_b, len_q, d_v)
        output = output.permute(1, 2, 0, 3).contiguous().view(sz_b, len_q, -1) # b x lq x (n*dv)

        output = self.dropout(self.fc(output))
        output = self.layer_norm(output + residual)

        return output, attn

class PositionwiseFeedForward(nn.Module):
    ''' A two-feed-forward-layer module '''

    def __init__(self, d_in, d_hid, dropout=0.1):
        super(PositionwiseFeedForward, self).__init__()
        self.w_1 = nn.Conv1d(d_in, d_hid, 1) # position-wise
        self.w_2 = nn.Conv1d(d_hid, d_in, 1) # position-wise
        self.layer_norm = nn.LayerNorm(d_in)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x):
        residual = x
        output = x.transpose(1, 2)
        output = self.w_2(F.relu(self.w_1(output)))
        output = output.transpose(1, 2)
        output = self.dropout(output)
        output = self.layer_norm(output + residual)
        return output

class ScaledDotProductAttention(nn.Module):
    ''' Scaled Dot-Product Attention '''

    def __init__(self, temperature, attn_dropout=0.1):
        super(ScaledDotProductAttention, self).__init__()
        self.temperature = temperature
        self.dropout = nn.Dropout(attn_dropout)
        self.softmax = nn.Softmax(dim=2)

    def forward(self, q, k, v, mask=None):

        attn = torch.bmm(q, k.transpose(1, 2))
        attn = attn / self.temperature

        if mask is not None:
            attn = attn.masked_fill(mask, -np.inf)

        attn = self.softmax(attn)
        attn = self.dropout(attn)
        output = torch.bmm(attn, v)

        return output, attn

def get_non_pad_mask(seq):
    assert seq.dim() == 2
    return seq.ne(Constants.PAD).type(torch.float).unsqueeze(-1)

def get_sinusoid_encoding_table(n_position, d_hid, padding_idx=None):
    ''' Sinusoid position encoding table '''

    def cal_angle(position, hid_idx):
        return position / np.power(10000, 2 * (hid_idx // 2) / d_hid)

    def get_posi_angle_vec(position):
        return [cal_angle(position, hid_j) for hid_j in range(d_hid)]

    sinusoid_table = np.array([get_posi_angle_vec(pos_i) for pos_i in range(n_position)])

    sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2])  # dim 2i
    sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2])  # dim 2i+1

    if padding_idx is not None:
        # zero vector for padding dimension
        sinusoid_table[padding_idx] = 0.

    return torch.FloatTensor(sinusoid_table)

def get_attn_key_pad_mask(seq_k, seq_q):
    ''' For masking out the padding part of key sequence. '''

    # Expand to fit the shape of key query attention matrix.
    len_q = seq_q.size(1)
    padding_mask = seq_k.eq(Constants.PAD)
    padding_mask = padding_mask.unsqueeze(1).expand(-1, len_q, -1)  # b x lq x lk

    return padding_mask

def get_subsequent_mask(seq):
    ''' For masking out the subsequent info. '''

    sz_b, len_s = seq.size()
    subsequent_mask = torch.triu(
        torch.ones((len_s, len_s), device=seq.device, dtype=torch.uint8), diagonal=1)
    subsequent_mask = subsequent_mask.unsqueeze(0).expand(sz_b, -1, -1)  # b x ls x ls

    return subsequent_mask

class Transformer(nn.Module):
    ''' A encoder model with self attention mechanism. '''

    def __init__(self, n_src_vocab, len_max_seq, d_word_vec,
                n_layers, n_head, d_k, d_v,
                d_model, d_inner, embedding, dropout=0.1):

        super(Transformer, self).__init__()

        n_position = len_max_seq + 1

        self.src_word_emb = nn.Embedding(n_src_vocab, d_word_vec, padding_idx=Constants.PAD)
        #self.src_word_emb = nn.Embedding.from_pretrained(embedding, freeze=False)

        self.position_enc = nn.Embedding.from_pretrained(
            get_sinusoid_encoding_table(n_position, d_word_vec, padding_idx=0),
            freeze=True)

        self.layer_stack = nn.ModuleList([
            EncoderLayer(d_model, d_inner, n_head, d_k, d_v, dropout=dropout)
            for _ in range(n_layers)])

    def forward(self, src_seq, src_pos, act_vocab_id):
        # -- Prepare masks
        slf_attn_mask = get_attn_key_pad_mask(seq_k=src_seq, seq_q=src_seq)
        non_pad_mask = get_non_pad_mask(src_seq)

        # -- Forward Word Embedding
        enc_output = self.src_word_emb(src_seq) + self.position_enc(src_pos)
        # -- Forward Ontology Embedding
        ontology_embedding = self.src_word_emb(act_vocab_id)

        for enc_layer in self.layer_stack:
            enc_output, enc_slf_attn = enc_layer(
                enc_output,
                non_pad_mask=non_pad_mask,
                slf_attn_mask=slf_attn_mask)

        dot_prod = torch.sum(enc_output[:, :, None, :] * ontology_embedding[None, None, :, :], -1)
        #index = length[:, None, None].repeat(1, 1, dot_prod.size(-1))
        #pooled_dot_prod = dot_prod.gather(1, index).squeeze()
        pooled_dot_prod = dot_prod[:, 0, :]
        pooling_likelihood = torch.sigmoid(pooled_dot_prod)
        return pooling_likelihood, enc_output

class AvgDecoderLayer(nn.Module):
    ''' Compose with three layers '''

    def __init__(self, d_model, d_inner, n_head, d_k, d_v, n_head_enc, dropout=0.1):
        super(AvgDecoderLayer, self).__init__()
        self.slf_attn = AverageHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)
        self.enc_attn = MultiHeadAttention(n_head_enc, d_model, d_model // n_head_enc, d_model // n_head_enc, dropout=dropout)
        self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout)

    def forward(self, act_vecs, dec_input, enc_output, non_pad_mask=None, slf_attn_mask=None, dec_enc_attn_mask=None):
        dec_output, dec_slf_attn = self.slf_attn(act_vecs, dec_input, dec_input, dec_input, mask=slf_attn_mask)
        dec_output *= non_pad_mask
        dec_output, dec_enc_attn = self.enc_attn(dec_output, enc_output, enc_output, mask=dec_enc_attn_mask)
        dec_output *= non_pad_mask

        dec_output = self.pos_ffn(dec_output)
        dec_output *= non_pad_mask

        return dec_output, dec_slf_attn, None

class DecoderLayer(nn.Module):
    ''' Compose with three layers '''

    def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1):
        super(DecoderLayer, self).__init__()
        self.slf_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)
        self.enc_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)
        self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout)

    def forward(self, dec_input, enc_output, non_pad_mask=None, slf_attn_mask=None, dec_enc_attn_mask=None):
        dec_output, dec_slf_attn = self.slf_attn(
            dec_input, dec_input, dec_input, mask=slf_attn_mask)
        dec_output *= non_pad_mask

        dec_output, dec_enc_attn = self.enc_attn(
            dec_output, enc_output, enc_output, mask=dec_enc_attn_mask)
        dec_output *= non_pad_mask

        dec_output = self.pos_ffn(dec_output)
        dec_output *= non_pad_mask

        return dec_output, dec_slf_attn, dec_enc_attn

class TransformerDecoder(nn.Module):
    ''' A decoder model with self attention mechanism. '''

    def __init__(self, vocab_size, d_word_vec, n_layers, d_model, n_head, act_dim, dropout=0.1):

        super(TransformerDecoder, self).__init__()
        d_k = d_model // n_head
        d_v = d_model // n_head
        d_inner = d_model * 4

        self.tgt_word_emb = nn.Embedding(vocab_size, d_word_vec, padding_idx=Constants.PAD)
        self.act_word_emb = nn.Linear(act_dim, d_word_vec, bias=False)

        self.post_word_emb = PositionalEmbedding(d_model=d_word_vec)

        self.enc_layer_stack = nn.ModuleList([
            EncoderLayer(d_model, d_inner, n_head, d_k, d_v, dropout=dropout)
            for _ in range(n_layers)])

        self.layer_stack = nn.ModuleList([
            DecoderLayer(d_model, d_inner, n_head, d_k, d_v, dropout=dropout)
            for _ in range(n_layers)])

        self.tgt_word_prj = nn.Linear(d_model, vocab_size, bias=False)

    def forward(self, tgt_seq, src_seq, act_vecs):
        # -- Encode source
        non_pad_mask = get_non_pad_mask(src_seq)
        slf_attn_mask = get_attn_key_pad_mask(seq_k=src_seq, seq_q=src_seq)
        enc_inp = self.tgt_word_emb(src_seq) + self.post_word_emb(src_seq)

        for layer in self.enc_layer_stack:
            enc_inp, _ = layer(enc_inp, non_pad_mask, slf_attn_mask)
        enc_output = enc_inp

        # -- Prepare masks
        non_pad_mask = get_non_pad_mask(tgt_seq)

        slf_attn_mask_subseq = get_subsequent_mask(tgt_seq)
        slf_attn_mask_keypad = get_attn_key_pad_mask(seq_k=tgt_seq, seq_q=tgt_seq)
        slf_attn_mask = (slf_attn_mask_keypad + slf_attn_mask_subseq).gt(0)
        dec_enc_attn_mask = get_attn_key_pad_mask(seq_k=src_seq, seq_q=tgt_seq)

        # -- Forward
        dec_output = self.tgt_word_emb(tgt_seq) + self.post_word_emb(tgt_seq) + self.act_word_emb(act_vecs)[:, None, :]

        for dec_layer in self.layer_stack:
            dec_output, dec_slf_attn, dec_enc_attn = dec_layer(
                dec_output, enc_output,
                non_pad_mask=non_pad_mask,
                slf_attn_mask=slf_attn_mask,
                dec_enc_attn_mask=dec_enc_attn_mask)

        logits = self.tgt_word_prj(dec_output)
        return logits

    def translate_batch(self, act_vecs, src_seq, n_bm, max_token_seq_len=30):
        ''' Translation work in one batch '''
        device = src_seq.device
        def collate_active_info(act_vecs, src_seq, inst_idx_to_position_map, active_inst_idx_list):
            # Sentences which are still active are collected,
            # so the decoder will not run on completed sentences.
            n_prev_active_inst = len(inst_idx_to_position_map)
            active_inst_idx = [inst_idx_to_position_map[k] for k in active_inst_idx_list]
            active_inst_idx = torch.LongTensor(active_inst_idx).to(device)

            active_src_seq = collect_active_part(src_seq, active_inst_idx, n_prev_active_inst, n_bm)
            active_act_vecs = collect_active_part(act_vecs, active_inst_idx, n_prev_active_inst, n_bm)
            #active_template_output = collect_active_part(template_output, active_inst_idx, n_prev_active_inst, n_bm)

            active_inst_idx_to_position_map = get_inst_idx_to_tensor_position_map(active_inst_idx_list)

            return active_act_vecs, active_src_seq, active_inst_idx_to_position_map

        def beam_decode_step(inst_dec_beams, len_dec_seq, active_inst_idx_list, act_vecs, src_seq, \
                             inst_idx_to_position_map, n_bm):
            ''' Decode and update beam status, and then return active beam idx '''
            n_active_inst = len(inst_idx_to_position_map)

            #dec_partial_seq = [b.get_current_state() for b in inst_dec_beams if not b.done]
            dec_partial_seq = [inst_dec_beams[idx].get_current_state()
                               for idx in active_inst_idx_list if not inst_dec_beams[idx].done]
            dec_partial_seq = torch.stack(dec_partial_seq).to(device)
            dec_partial_seq = dec_partial_seq.view(-1, len_dec_seq)

            logits = self.forward(dec_partial_seq, src_seq, act_vecs)[:, -1, :] / Constants.T
            word_prob = F.log_softmax(logits, dim=1)
            word_prob = word_prob.view(n_active_inst, n_bm, -1)

            # Update the beam with predicted word prob information and collect incomplete instances
            active_inst_idx_list = []
            for inst_idx, inst_position in inst_idx_to_position_map.items():
                is_inst_complete = inst_dec_beams[inst_idx].advance(word_prob[inst_position])
                if not is_inst_complete:
                    active_inst_idx_list += [inst_idx]

            return active_inst_idx_list
        with torch.no_grad():
            #-- Repeat data for beam search
            n_inst, len_s = src_seq.size()
            src_seq = src_seq.repeat(1, n_bm).view(n_inst * n_bm, len_s)
            act_vecs = act_vecs.repeat(1, n_bm).view(n_inst * n_bm, -1)

            #-- Prepare beams
            inst_dec_beams = [Beam(n_bm, device=device) for _ in range(n_inst)]

            #-- Bookkeeping for active or not
            active_inst_idx_list = list(range(n_inst))
            inst_idx_to_position_map = get_inst_idx_to_tensor_position_map(active_inst_idx_list)

            #-- Decode
            for len_dec_seq in range(1, max_token_seq_len + 1):
                active_inst_idx_list = beam_decode_step(inst_dec_beams, len_dec_seq, active_inst_idx_list,
                                                        act_vecs, src_seq, inst_idx_to_position_map, n_bm)

                if not active_inst_idx_list:
                    break  # all instances have finished their path to <EOS>

                act_vecs, src_seq, inst_idx_to_position_map = collate_active_info(
                    act_vecs, src_seq, inst_idx_to_position_map, active_inst_idx_list)

        def collect_hypothesis_and_scores(inst_dec_beams, n_best):
            all_hyp, all_scores = [], []
            for beam in inst_dec_beams:
                scores = beam.scores
                hyps = np.array([beam.get_hypothesis(i) for i in range(beam.size)], 'long')
                lengths = (hyps != Constants.PAD).sum(-1)
                normed_scores = [scores[i].item()/lengths[i] for i, hyp in enumerate(hyps)]
                idxs = np.argsort(normed_scores)[::-1]

                all_hyp.append([hyps[idx] for idx in idxs])
                all_scores.append([normed_scores[idx] for idx in idxs])
            """
            for inst_idx in range(len(inst_dec_beams)):
                scores, tail_idxs = inst_dec_beams[inst_idx].sort_scores()
                all_scores += [scores[:n_best]]

                hyps = [inst_dec_beams[inst_idx].get_hypothesis(i) for i in tail_idxs[:n_best]]
                all_hyp += [hyps]
            """
            return all_hyp, all_scores

        batch_hyp, batch_scores = collect_hypothesis_and_scores(inst_dec_beams, n_bm)

        result = []
        for _ in batch_hyp:
            finished = False
            for r in _:
                if len(r) >= 8 and len(r) < 40:
                    result.append(r)
                    finished = True
                    break
            if not finished:
                result.append(_[0])
        return result

class TableSemanticDecoder(nn.Module):

    def __init__(self, vocab_size, d_word_vec, n_layers, d_model, n_head, dropout=0.1):

        #super(TableSemanticDecoder, self).__init__(vocab_size, d_word_vec, n_layers, d_model, n_head, dropout)
        super(TableSemanticDecoder, self).__init__()
        self.take_domain = True

        self.tgt_word_emb = nn.Embedding(vocab_size, d_word_vec, padding_idx=Constants.PAD)
        self.post_word_emb = PositionalEmbedding(d_model=d_word_vec)

        d_inner = d_model * 4
        d_k, d_v = d_model // n_head, d_model // n_head

        self.layer_stack = nn.ModuleList([
            EncoderLayer(d_model, d_inner, n_head, d_k, d_v, dropout=dropout)
            for _ in range(n_layers)])

        d_inner = d_model * 4
        d_k, d_v = d_model // n_head, d_model // n_head
        if self.take_domain:
            self.prior_layer_stack = AvgDecoderLayer(d_model, d_inner, len(Constants.domains),
                                                     d_k, d_v, n_head_enc=n_head, dropout=dropout)
            self.middle_layer_stack = AvgDecoderLayer(d_model, d_inner, len(Constants.functions),
                                                      d_k, d_v, n_head_enc=n_head, dropout=dropout)
            self.post_layer_stack = AvgDecoderLayer(d_model, d_inner, len(Constants.arguments),
                                                      d_k, d_v, n_head_enc=n_head, dropout=dropout)
            self.final_layer_stack = DecoderLayer(d_model, d_inner, n_head, d_k , d_v, dropout=dropout)
        else:
            self.prior_layer_stack = AvgDecoderLayer(d_model, d_inner, len(Constants.functions),
                                                     d_k, d_v, n_head_enc=n_head, dropout=dropout)
            self.middle_layer_stack = AvgDecoderLayer(d_model, d_inner, len(Constants.arguments),
                                                      d_k, d_v, n_head_enc=n_head, dropout=dropout)
            self.post_layer_stack = DecoderLayer(d_model, d_inner, n_head, d_k , d_v, dropout=dropout)

        self.tgt_word_prj = nn.Linear(d_model, vocab_size, bias=False)
        self.softmax = nn.Softmax(-1)

    def forward(self, tgt_seq, src_seq, act_vecs):
        # -- Encode source
        non_pad_mask = get_non_pad_mask(src_seq)
        slf_attn_mask = get_attn_key_pad_mask(seq_k=src_seq, seq_q=src_seq)
        enc_inp = self.tgt_word_emb(src_seq) + self.post_word_emb(src_seq)

        for layer in self.layer_stack:
            enc_inp, _ = layer(enc_inp, non_pad_mask, slf_attn_mask)
        enc_output = enc_inp

        # -- Prepare masks
        non_pad_mask = get_non_pad_mask(tgt_seq)
        slf_attn_mask_subseq = get_subsequent_mask(tgt_seq)
        slf_attn_mask_keypad = get_attn_key_pad_mask(seq_k=tgt_seq, seq_q=tgt_seq)
        slf_attn_mask = (slf_attn_mask_keypad + slf_attn_mask_subseq).gt(0)
        dec_enc_attn_mask = get_attn_key_pad_mask(seq_k=src_seq, seq_q=tgt_seq)

        # -- Forward
        dec_inp = self.tgt_word_emb(tgt_seq) + self.post_word_emb(tgt_seq)
        domain_vecs = act_vecs[:, :len(Constants.domains)]
        function_vecs = act_vecs[:, len(Constants.domains):len(Constants.domains)+len(Constants.functions)]
        argument_vecs = act_vecs[:, len(Constants.domains)+len(Constants.functions):]
        if self.take_domain:
            dec_inp, _, _ = self.prior_layer_stack(domain_vecs, dec_inp, enc_output,
                                                   non_pad_mask=non_pad_mask,
                                                   slf_attn_mask=slf_attn_mask,
                                                   dec_enc_attn_mask=dec_enc_attn_mask)
            dec_inp, _, _ = self.middle_layer_stack(function_vecs, dec_inp, enc_output,
                                                    non_pad_mask=non_pad_mask,
                                                    slf_attn_mask=slf_attn_mask,
                                                    dec_enc_attn_mask=dec_enc_attn_mask)
            dec_inp, _, _ = self.post_layer_stack(argument_vecs, dec_inp, enc_output,
                                                  non_pad_mask=non_pad_mask,
                                                  slf_attn_mask=slf_attn_mask,
                                                  dec_enc_attn_mask=dec_enc_attn_mask)
            dec_inp, _, _ = self.final_layer_stack(dec_inp, enc_output,
                                                  non_pad_mask=non_pad_mask,
                                                  slf_attn_mask=slf_attn_mask,
                                                  dec_enc_attn_mask=dec_enc_attn_mask)
        else:
            dec_inp, _, _ = self.prior_layer_stack(function_vecs, dec_inp, enc_output,
                                                   non_pad_mask=non_pad_mask,
                                                   slf_attn_mask=slf_attn_mask,
                                                   dec_enc_attn_mask=dec_enc_attn_mask)
            dec_inp, _, _ = self.middle_layer_stack(argument_vecs, dec_inp, enc_output,
                                                    non_pad_mask=non_pad_mask,
                                                    slf_attn_mask=slf_attn_mask,
                                                    dec_enc_attn_mask=dec_enc_attn_mask)
            dec_inp, _, _ = self.post_layer_stack(dec_inp, enc_output,
                                                  non_pad_mask=non_pad_mask,
                                                  slf_attn_mask=slf_attn_mask,
                                                  dec_enc_attn_mask=dec_enc_attn_mask)
        logits = self.tgt_word_prj(dec_inp)
        return logits

    def translate_batch(self, act_vecs, src_seq, n_bm, max_token_seq_len=30):
        ''' Translation work in one batch '''
        device = src_seq.device
        def collate_active_info(act_vecs, src_seq, inst_idx_to_position_map, active_inst_idx_list):
            # Sentences which are still active are collected,
            # so the decoder will not run on completed sentences.
            n_prev_active_inst = len(inst_idx_to_position_map)
            active_inst_idx = [inst_idx_to_position_map[k] for k in active_inst_idx_list]
            active_inst_idx = torch.LongTensor(active_inst_idx).to(device)

            active_src_seq = collect_active_part(src_seq, active_inst_idx, n_prev_active_inst, n_bm)
            active_act_vecs = collect_active_part(act_vecs, active_inst_idx, n_prev_active_inst, n_bm)
            #active_template_output = collect_active_part(template_output, active_inst_idx, n_prev_active_inst, n_bm)

            active_inst_idx_to_position_map = get_inst_idx_to_tensor_position_map(active_inst_idx_list)

            return active_act_vecs, active_src_seq, active_inst_idx_to_position_map

        def beam_decode_step(inst_dec_beams, len_dec_seq, active_inst_idx_list, act_vecs, src_seq, \
                             inst_idx_to_position_map, n_bm):
            ''' Decode and update beam status, and then return active beam idx '''
            n_active_inst = len(inst_idx_to_position_map)

            #dec_partial_seq = [b.get_current_state() for b in inst_dec_beams if not b.done]
            dec_partial_seq = [inst_dec_beams[idx].get_current_state()
                               for idx in active_inst_idx_list if not inst_dec_beams[idx].done]
            dec_partial_seq = torch.stack(dec_partial_seq).to(device)
            dec_partial_seq = dec_partial_seq.view(-1, len_dec_seq)

            logits = self.forward(dec_partial_seq, src_seq, act_vecs)[:, -1, :] / Constants.T
            word_prob = F.log_softmax(logits, dim=1)
            word_prob = word_prob.view(n_active_inst, n_bm, -1)

            # Update the beam with predicted word prob information and collect incomplete instances
            active_inst_idx_list = []
            for inst_idx, inst_position in inst_idx_to_position_map.items():
                is_inst_complete = inst_dec_beams[inst_idx].advance(word_prob[inst_position])
                if not is_inst_complete:
                    active_inst_idx_list += [inst_idx]

            return active_inst_idx_list

        with torch.no_grad():
            #-- Repeat data for beam search
            n_inst, len_s = src_seq.size()
            src_seq = src_seq.repeat(1, n_bm).view(n_inst * n_bm, len_s)
            act_vecs = act_vecs.repeat(1, n_bm).view(n_inst * n_bm, -1)

            #-- Prepare beams
            inst_dec_beams = [Beam(n_bm, device=device) for _ in range(n_inst)]

            #-- Bookkeeping for active or not
            active_inst_idx_list = list(range(n_inst))
            inst_idx_to_position_map = get_inst_idx_to_tensor_position_map(active_inst_idx_list)

            #-- Decode
            for len_dec_seq in range(1, max_token_seq_len + 1):
                active_inst_idx_list = beam_decode_step(inst_dec_beams, len_dec_seq, active_inst_idx_list,
                                                        act_vecs, src_seq, inst_idx_to_position_map, n_bm)

                if not active_inst_idx_list:
                    break  # all instances have finished their path to <EOS>

                act_vecs, src_seq, inst_idx_to_position_map = collate_active_info(
                    act_vecs, src_seq, inst_idx_to_position_map, active_inst_idx_list)

        def collect_hypothesis_and_scores(inst_dec_beams, n_best):
            all_hyp, all_scores = [], []
            for beam in inst_dec_beams:
                scores = beam.scores
                hyps = np.array([beam.get_hypothesis(i) for i in range(beam.size)], 'long')
                lengths = (hyps != Constants.PAD).sum(-1)
                normed_scores = [scores[i].item()/lengths[i] for i, hyp in enumerate(hyps)]
                idxs = np.argsort(normed_scores)[::-1]

                all_hyp.append([hyps[idx] for idx in idxs])
                all_scores.append([normed_scores[idx] for idx in idxs])
            return all_hyp, all_scores

        batch_hyp, batch_scores = collect_hypothesis_and_scores(inst_dec_beams, n_bm)

        result = []
        for _ in batch_hyp:
            finished = False
            for r in _:
                if len(r) >= 8 and len(r) < 40:
                    result.append(r)
                    finished = True
                    break
            if not finished:
                result.append(_[0])
        return result

def get_inst_idx_to_tensor_position_map(inst_idx_list):
    ''' Indicate the position of an instance in a tensor. '''
    return {inst_idx: tensor_position for tensor_position, inst_idx in enumerate(inst_idx_list)}

def collect_active_part(beamed_tensor, curr_active_inst_idx, n_prev_active_inst, n_bm):
    ''' Collect tensor parts associated to active instances. '''
    _, *d_hs = beamed_tensor.size()
    n_curr_active_inst = len(curr_active_inst_idx)
    new_shape = (n_curr_active_inst * n_bm, *d_hs)

    beamed_tensor = beamed_tensor.view(n_prev_active_inst, -1)
    beamed_tensor = beamed_tensor.index_select(0, curr_active_inst_idx)
    beamed_tensor = beamed_tensor.view(*new_shape)

    return beamed_tensor

max_stars_repo_name: ngduyanhece/ConvLab

max_issues_repo_name: ngduyanhece/ConvLab

max_forks_repo_name: ngduyanhece/ConvLab


text:
import numpy as np
from scipy import interpolate

import pyHMT2D

from matplotlib import pyplot as plt

plt.rc('text', usetex=False)  #allow the use of Latex for math expressions and equations
plt.rc('font', family='serif') #specify the default font family to be "serif"

def demo_backwater_1d_model_data():
    #load Backwater_1D_Data configuration data
    my_backwater_1D_data = pyHMT2D.Hydraulic_Models_Data.Backwater_1D_Data("calibration.json")

    #create a Backwater_1D_Model object
    my_backwater_1D_model = pyHMT2D.Hydraulic_Models_Data.Backwater_1D_Model()

    #set the simulation case in the Backwater_1D_Model object
    my_backwater_1D_model.set_simulation_case(my_backwater_1D_data)

    #run the Backwater_1D_Model model
    my_backwater_1D_model.run_model()

    #sample some results as manufactured solution
    xmin = np.min(my_backwater_1D_data.gridx)
    xmax = np.max(my_backwater_1D_data.gridx)

    #make some sampling points
    sampling_points = np.linspace(xmin+100,xmax-100,4)

    #build the WSE interpolator
    f_WSE = interpolate.interp1d(my_backwater_1D_data.gridx, my_backwater_1D_data.WSE)

    #bed elevation
    bedElev = my_backwater_1D_data.WSE - my_backwater_1D_data.waterDepth

    #sample the WSE
    sampled_WSE = f_WSE(sampling_points)

    #sampled the x-velocity
    sampled_U = my_backwater_1D_data.specificDischarge/sampled_WSE

    #output the true solution and sampled WSE and velocity
    fid = open("sampled_wse.csv", "w")
    fid.write("Name,x,y,wse\n")
    for pointI in range(len(sampling_points)):
       fid.write("Point%d, %f, 0, %f\n" % (pointI,sampling_points[pointI],sampled_WSE[pointI]))
    fid.close()

    fid = open("sampled_velocity.csv", "w")
    fid.write("Name,x,y,angle,velocity\n")
    for pointI in range(len(sampling_points)):
       fid.write("Point%d, %f, 0, 90, %f\n" % (pointI,sampling_points[pointI],sampled_U[pointI]))
    fid.close()

    #plot result
    fig, ax = plt.subplots()
    ax2 = ax.twinx()

    negX = -(my_backwater_1D_data.gridx - my_backwater_1D_data.startx) / 1000
    #print("negX, WSE = ", negX, my_backwater_1D_data.WSE)

    wse_plot = ax.plot(negX[::50], my_backwater_1D_data.WSE[::50], 'g-', label='WSE')
    bedElev_plot = ax.plot(negX[::50], bedElev[::50], 'k-', label='Bed')

    #plot the sampled WSE
    ax.scatter(-(sampling_points-my_backwater_1D_data.startx)/1000, sampled_WSE, marker='o', facecolor='none',
               edgecolor='blue', s=20, linewidths=1)

    ax.set_xlabel('x (km)', fontsize=16)
    ax.set_ylabel('Elevation (m)', color='g', fontsize=16)

    #plot Manning's n
    n_plot = ax2.plot(negX[::50], my_backwater_1D_data.gridManningN[::50], 'b--', label='Manning\'s $n$')
    ax2.set_ylabel('Manning\'s $n$', color = 'b', fontsize=16)

    # show the ticks on both axes and set the font size
    plt.tick_params(axis='both', which='major', labelsize=12)

    #plt.title('Backwater curve')

    ax.set_xlim(-11, 1)
    ax.set_ylim(-0.3, 3)
    ax2.set_ylim(0.03,0.06)

    # add all plots' label
    lns = wse_plot + bedElev_plot + n_plot
    labs = [l.get_label() for l in lns]
    ax.legend(lns, labs, loc='center left', fontsize=14, frameon=False)

    fig.tight_layout()

    #save figure
    plt.savefig("backwater_1d_calibration_example.png", dpi=300, bbox_inches='tight', pad_inches=0)

    plt.show()

def demo_calibrator():
    """ Demo the Calibrator class

    Returns
    -------

    """

    #my_calibrator = pyHMT2D.Calibration.Calibrator("calibration.json")
    my_calibrator = pyHMT2D.Calibration.Calibrator("calibration_enumerator.json")

    my_calibrator.calibrate()

if __name__ == "__main__":

    #run the Backwater_1D model and sample some data for calibration later (manufactured solution)
    #demo_backwater_1d_model_data()

    #run the calibration
    demo_calibrator()

    print("All done!")

ext: py

reason: import numpy,from scipy


text:
#######################################################################
# Copyright (C)                                                       #
# 2016 - 2018 Shangtong Zhang(zhangshangtong.cpp@gmail.com)           #
# 2016 Jan Hakenberg(jan.hakenberg@gmail.com)                         #
# 2016 Tian Jun(tianjun.cpp@gmail.com)                                #
# 2016 Kenta Shimada(hyperkentakun@gmail.com)                         #
# Permission given to modify the code as long as you keep this        #
# declaration at the top                                              #
#######################################################################

import numpy as np
import pickle

BOARD_ROWS = 3
BOARD_COLS = 3
BOARD_SIZE = BOARD_ROWS * BOARD_COLS

class State:
    def __init__(self):
        # the board is represented by an n * n array,
        # 1 represents a chessman of the player who moves first,
        # -1 represents a chessman of another player
        # 0 represents an empty position
        self.data = np.zeros((BOARD_ROWS, BOARD_COLS))
        self.winner = None
        self.hash_val = None
        self.end = None

    # compute the hash value for one state, it's unique
    def hash(self):
        if self.hash_val is None:
            self.hash_val = 0
            for i in self.data.reshape(BOARD_ROWS * BOARD_COLS):
                if i == -1:
                    i = 2
                self.hash_val = self.hash_val * 3 + i
        return int(self.hash_val)

    # check whether a player has won the game, or it's a tie
    def is_end(self):
        if self.end is not None:
            return self.end
        results = []
        # check row
        for i in range(0, BOARD_ROWS):
            results.append(np.sum(self.data[i, :]))
        # check columns
        for i in range(0, BOARD_COLS):
            results.append(np.sum(self.data[:, i]))

        # check diagonals
        results.append(0)
        for i in range(0, BOARD_ROWS):
            results[-1] += self.data[i, i]
        results.append(0)
        for i in range(0, BOARD_ROWS):
            results[-1] += self.data[i, BOARD_ROWS - 1 - i]

        for result in results:
            if result == 3:
                self.winner = 1
                self.end = True
                return self.end
            if result == -3:
                self.winner = -1
                self.end = True
                return self.end

        # whether it's a tie
        sum = np.sum(np.abs(self.data))
        if sum == BOARD_ROWS * BOARD_COLS:
            self.winner = 0
            self.end = True
            return self.end

        # game is still going on
        self.end = False
        return self.end

    # @symbol: 1 or -1
    # put chessman symbol in position (i, j)
    def next_state(self, i, j, symbol):
        new_state = State()
        new_state.data = np.copy(self.data)
        new_state.data[i, j] = symbol
        return new_state

    # print the board
    def print_state(self):
        for i in range(0, BOARD_ROWS):
            print('-------------')
            out = '| '
            for j in range(0, BOARD_COLS):
                if self.data[i, j] == 1:
                    token = '*'
                if self.data[i, j] == 0:
                    token = '0'
                if self.data[i, j] == -1:
                    token = 'x'
                out += token + ' | '
            print(out)
        print('-------------')

def get_all_states_impl(current_state, current_symbol, all_states):
    for i in range(0, BOARD_ROWS):
        for j in range(0, BOARD_COLS):
            if current_state.data[i][j] == 0:
                newState = current_state.next_state(i, j, current_symbol)
                newHash = newState.hash()
                if newHash not in all_states.keys():
                    isEnd = newState.is_end()
                    all_states[newHash] = (newState, isEnd)
                    if not isEnd:
                        get_all_states_impl(newState, -current_symbol, all_states)

def get_all_states():
    current_symbol = 1
    current_state = State()
    all_states = dict()
    all_states[current_state.hash()] = (current_state, current_state.is_end())
    get_all_states_impl(current_state, current_symbol, all_states)
    return all_states

# all possible board configurations
all_states = get_all_states()

class Judger:
    # @player1: the player who will move first, its chessman will be 1
    # @player2: another player with a chessman -1
    def __init__(self, player1, player2):
        self.p1 = player1
        self.p2 = player2
        self.current_player = None
        self.p1_symbol = 1
        self.p2_symbol = -1
        self.p1.set_symbol(self.p1_symbol)
        self.p2.set_symbol(self.p2_symbol)
        self.current_state = State()

    def reset(self):
        self.p1.reset()
        self.p2.reset()

    def alternate(self):
        while True:
            yield self.p1
            yield self.p2

    # @print_state: if True, print each board during the game
    def play(self, print_state=False):
        alternator = self.alternate()
        self.reset()
        current_state = State()
        self.p1.set_state(current_state)
        self.p2.set_state(current_state)
        while True:
            player = next(alternator)
            if print_state:
                current_state.print_state()
            [i, j, symbol] = player.act()
            next_state_hash = current_state.next_state(i, j, symbol).hash()
            current_state, is_end = all_states[next_state_hash]
            self.p1.set_state(current_state)
            self.p2.set_state(current_state)
            if is_end:
                if print_state:
                    current_state.print_state()
                return current_state.winner

# AI player
class Player:
    # @step_size: the step size to update estimations
    # @epsilon: the probability to explore
    def __init__(self, step_size=0.1, epsilon=0.1):
        self.estimations = dict()
        self.step_size = step_size
        self.epsilon = epsilon
        self.states = []
        self.greedy = []

    def reset(self):
        self.states = []
        self.greedy = []

    def set_state(self, state):
        self.states.append(state)
        self.greedy.append(True)

    def set_symbol(self, symbol):
        self.symbol = symbol
        for hash_val in all_states.keys():
            (state, is_end) = all_states[hash_val]
            if is_end:
                if state.winner == self.symbol:
                    self.estimations[hash_val] = 1.0
                elif state.winner == 0:
                    # we need to distinguish between a tie and a lose
                    self.estimations[hash_val] = 0.5
                else:
                    self.estimations[hash_val] = 0
            else:
                self.estimations[hash_val] = 0.5

    # update value estimation
    def backup(self):
        # for debug
        # print('player trajectory')
        # for state in self.states:
        #     state.print_state()

        self.states = [state.hash() for state in self.states]

        for i in reversed(range(len(self.states) - 1)):
            state = self.states[i]
            td_error = self.greedy[i] * (self.estimations[self.states[i + 1]] - self.estimations[state])
            self.estimations[state] += self.step_size * td_error

    # choose an action based on the state
    def act(self):
        state = self.states[-1]
        next_states = []
        next_positions = []
        for i in range(BOARD_ROWS):
            for j in range(BOARD_COLS):
                if state.data[i, j] == 0:
                    next_positions.append([i, j])
                    next_states.append(state.next_state(i, j, self.symbol).hash())

        if np.random.rand() < self.epsilon:
            action = next_positions[np.random.randint(len(next_positions))]
            action.append(self.symbol)
            self.greedy[-1] = False
            return action

        values = []
        for hash, pos in zip(next_states, next_positions):
            values.append((self.estimations[hash], pos))
        # to select one of the actions of equal value at random
        np.random.shuffle(values)
        values.sort(key=lambda x: x[0], reverse=True)
        action = values[0][1]
        action.append(self.symbol)
        return action

    def save_policy(self):
        with open('policy_%s.bin' % ('first' if self.symbol == 1 else 'second'), 'wb') as f:
            pickle.dump(self.estimations, f)

    def load_policy(self):
        with open('policy_%s.bin' % ('first' if self.symbol == 1 else 'second'), 'rb') as f:
            self.estimations = pickle.load(f)

# human interface
# input a number to put a chessman
# | q | w | e |
# | a | s | d |
# | z | x | c |
class HumanPlayer:
    def __init__(self, **kwargs):
        self.symbol = None
        self.keys = ['q', 'w', 'e', 'a', 's', 'd', 'z', 'x', 'c']
        self.state = None
        return

    def reset(self):
        return

    def set_state(self, state):
        self.state = state

    def set_symbol(self, symbol):
        self.symbol = symbol
        return

    def backup(self, _):
        return

    def act(self):
        self.state.print_state()
        key = input("Input your position:")
        data = self.keys.index(key)
        i = data // int(BOARD_COLS)
        j = data % BOARD_COLS
        return (i, j, self.symbol)

def train(epochs, print_every_n=500):
    player1 = Player(epsilon=0.01)
    player2 = Player(epsilon=0.01)
    judger = Judger(player1, player2)
    player1_win = 0.0
    player2_win = 0.0
    for i in range(1, epochs + 1):
        winner = judger.play(print_state=False)
        if winner == 1:
            player1_win += 1
        if winner == -1:
            player2_win += 1
        if i % print_every_n == 0:
            print('Epoch %d, player 1 winrate: %.02f, player 2 winrate: %.02f' % (i, player1_win / i, player2_win / i))
        player1.backup()
        player2.backup()
        judger.reset()
    player1.save_policy()
    player2.save_policy()

def compete(turns):
    player1 = Player(epsilon=0)
    player2 = Player(epsilon=0)
    judger = Judger(player1, player2)
    player1.load_policy()
    player2.load_policy()
    player1_win = 0.0
    player2_win = 0.0
    for _ in range(0, turns):
        winner = judger.play()
        if winner == 1:
            player1_win += 1
        if winner == -1:
            player2_win += 1
        judger.reset()
    print('%d turns, player 1 win %.02f, player 2 win %.02f' % (turns, player1_win / turns, player2_win / turns))

# The game is a zero sum game. If both players are playing with an optimal strategy, every game will end in a tie.
# So we test whether the AI can guarantee at least a tie if it goes second.
def play():
    while True:
        player1 = HumanPlayer()
        player2 = Player(epsilon=0)
        judger = Judger(player1, player2)
        player2.load_policy()
        winner = judger.play()
        if winner == player2.symbol:
            print("You lose!")
        elif winner == player1.symbol:
            print("You win!")
        else:
            print("It is a tie!")

if __name__ == '__main__':
    train(int(1e5))
    compete(int(1e3))
    play()

reason: import numpy


text:
import fasteners
import math
import niscope
import numpy
import os
import pytest
import tempfile

# There are system tests below that need either a PXI-5124 or a PXI-5142 instead of the PXIe-5164 we use everywhere else
# because of specific capabilities on those models. Due to internal NI bug 969274, opening a simulated session to those models
# sometimes fails. As a workaround, the nimi-bot VMs are configured with one persistently simulated instrument of each kind respectively
# named "5124" and "5142". If you want to run these tests on your own system, you will need to create these two simulated
# instruments using MAX.
# In addition, we need a global lock in order to keep us from opening more than one session to the same simulated instrument
# at the same time. This is because NI-SCOPE (like other MI driver runtimes) disallow two simultaneous sessions to the same
# instrument, even when the instrument is simulated. This will impact the performance at which system tests run because we
# parallelize at the tox level :(.
daqmx_sim_5124_lock_file = os.path.join(tempfile.gettempdir(), 'daqmx_5124.lock')
daqmx_sim_5124_lock = fasteners.InterProcessLock(daqmx_sim_5124_lock_file)
daqmx_sim_5142_lock_file = os.path.join(tempfile.gettempdir(), 'daqmx_5142.lock')
daqmx_sim_5142_lock = fasteners.InterProcessLock(daqmx_sim_5142_lock_file)

@pytest.fixture(scope='function')
def session():
    with niscope.Session('FakeDevice', False, True, 'Simulate=1, DriverSetup=Model:5164; BoardType:PXIe') as simulated_session:
        yield simulated_session

@pytest.fixture(scope='function')
def session_5124():
    with daqmx_sim_5124_lock:
        with niscope.Session('5124') as simulated_session:  # 5124 is needed for video triggering
            yield simulated_session

@pytest.fixture(scope='function')
def session_5142():
    with daqmx_sim_5142_lock:
        with niscope.Session('5142') as simulated_session:  # 5142 is needed for OSP
            yield simulated_session

# Attribute tests
def test_vi_boolean_attribute(session):
    session.allow_more_records_than_memory = False
    default_option = session.allow_more_records_than_memory
    assert default_option is False

def test_vi_string_attribute(session):
    session.acq_arm_source = 'NISCOPE_VAL_IMMEDIATE'
    start_trigger_source = session.acq_arm_source
    assert start_trigger_source == 'NISCOPE_VAL_IMMEDIATE'

# Basic usability tests
def test_read(session):
    test_voltage = 1.0
    test_record_length = 2000
    test_channels = range(2)
    test_num_channels = 2
    test_num_records = 3
    session.configure_vertical(test_voltage, niscope.VerticalCoupling.AC)
    session.configure_horizontal_timing(50000000, test_record_length, 50.0, test_num_records, True)
    waveforms = session.channels[test_channels].read(num_samples=test_record_length, num_records=test_num_records)
    assert len(waveforms) == test_num_channels * test_num_records
    for i in range(len(waveforms)):
        assert len(waveforms[i].samples) == test_record_length

def test_fetch(session):
    test_voltage = 1.0
    test_record_length = 2000
    test_channels = range(2)
    test_num_channels = 2
    test_num_records = 3
    session.configure_vertical(test_voltage, niscope.VerticalCoupling.AC)
    session.configure_horizontal_timing(50000000, test_record_length, 50.0, test_num_records, True)
    with session.initiate():
        waveforms = session.channels[test_channels].fetch(num_samples=test_record_length, num_records=test_num_records)
    assert len(waveforms) == test_num_channels * test_num_records
    for i in range(len(waveforms)):
        assert len(waveforms[i].samples) == test_record_length

def test_fetch_defaults(session):
    test_voltage = 1.0
    test_record_length = 2000
    test_channels = range(2)
    test_num_channels = 2
    session.configure_vertical(test_voltage, niscope.VerticalCoupling.AC)
    session.configure_horizontal_timing(50000000, test_record_length, 50.0, 1, True)
    with session.initiate():
        waveforms = session.channels[test_channels].fetch()
    assert len(waveforms) == test_num_channels
    for i in range(len(waveforms)):
        assert len(waveforms[i].samples) == test_record_length

def test_fetch_binary8_into(session):
    test_voltage = 1.0
    test_record_length = 2000
    test_channels = range(2)
    test_num_channels = 2
    waveform = numpy.ndarray(test_num_channels * test_record_length, dtype=numpy.int8)
    # Initialize with NaN so we can later verify all samples were overwritten by the driver.
    waveform.fill(float('nan'))
    session.configure_vertical(test_voltage, niscope.VerticalCoupling.AC)
    session.configure_horizontal_timing(50000000, test_record_length, 50.0, 1, True)
    with session.initiate():
        waveforms = session.channels[test_channels].fetch_into(waveform=waveform)

    for sample in waveform:
        assert not math.isnan(sample)
    assert len(waveforms) == test_num_channels

    for i in range(len(waveforms)):
        record_wfm = waveforms[i].samples
        assert len(record_wfm) == test_record_length
        for j in range(len(record_wfm)):
            assert record_wfm[j] == waveform[i * test_record_length + j]

def test_fetch_binary16_into(session):
    test_voltage = 1.0
    test_record_length = 2000
    test_channels = range(2)
    test_num_channels = 2
    waveform = numpy.ndarray(test_num_channels * test_record_length, dtype=numpy.int16)
    # Initialize with NaN so we can later verify all samples were overwritten by the driver.
    waveform.fill(float('nan'))
    session.configure_vertical(test_voltage, niscope.VerticalCoupling.AC)
    session.configure_horizontal_timing(50000000, test_record_length, 50.0, 1, True)
    with session.initiate():
        waveforms = session.channels[test_channels].fetch_into(waveform=waveform)

    for sample in waveform:
        assert not math.isnan(sample)
    assert len(waveforms) == test_num_channels

    for i in range(len(waveforms)):
        record_wfm = waveforms[i].samples
        assert len(record_wfm) == test_record_length
        for j in range(len(record_wfm)):
            assert record_wfm[j] == waveform[i * test_record_length + j]

def test_fetch_binary32_into(session):
    test_voltage = 1.0
    test_record_length = 2000
    test_channels = range(2)
    test_num_channels = 2
    waveform = numpy.ndarray(test_num_channels * test_record_length, dtype=numpy.int32)
    # Initialize with NaN so we can later verify all samples were overwritten by the driver.
    waveform.fill(float('nan'))
    session.configure_vertical(test_voltage, niscope.VerticalCoupling.AC)
    session.configure_horizontal_timing(50000000, test_record_length, 50.0, 1, True)
    with session.initiate():
        waveforms = session.channels[test_channels].fetch_into(waveform=waveform)

    for sample in waveform:
        assert not math.isnan(sample)
    assert len(waveforms) == test_num_channels

    for i in range(len(waveforms)):
        record_wfm = waveforms[i].samples
        assert len(record_wfm) == test_record_length
        for j in range(len(record_wfm)):
            assert record_wfm[j] == waveform[i * test_record_length + j]

def test_fetch_double_into(session):
    test_voltage = 1.0
    test_record_length = 2000
    test_channels = range(2)
    test_num_channels = 2
    waveform = numpy.ndarray(test_num_channels * test_record_length, dtype=numpy.float64)
    # Initialize with NaN so we can later verify all samples were overwritten by the driver.
    waveform.fill(float('nan'))
    session.configure_vertical(test_voltage, niscope.VerticalCoupling.AC)
    session.configure_horizontal_timing(50000000, test_record_length, 50.0, 1, True)
    with session.initiate():
        waveforms = session.channels[test_channels].fetch_into(waveform=waveform)

    for sample in waveform:
        assert not math.isnan(sample)
    assert len(waveforms) == test_num_channels

    for i in range(len(waveforms)):
        record_wfm = waveforms[i].samples
        assert len(record_wfm) == test_record_length
        for j in range(len(record_wfm)):
            assert record_wfm[j] == waveform[i * test_record_length + j]

def test_self_test(session):
    # We should not get an assert if self_test passes
    session.self_test()

def test_reset(session):
    deault_fetch_relative_to = session._fetch_relative_to
    assert deault_fetch_relative_to == niscope.FetchRelativeTo.PRETRIGGER
    session._fetch_relative_to = niscope.FetchRelativeTo.READ_POINTER
    non_default_acqusition_type = session._fetch_relative_to
    assert non_default_acqusition_type == niscope.FetchRelativeTo.READ_POINTER
    session.reset()
    assert session._fetch_relative_to == niscope.FetchRelativeTo.PRETRIGGER

def test_reset_device(session):
    deault_meas_time_histogram_high_time = session._meas_time_histogram_high_time
    assert deault_meas_time_histogram_high_time == 0.0005
    session._meas_time_histogram_high_time = 0.0010
    non_default_meas_time_histogram_high_time = session._meas_time_histogram_high_time
    assert non_default_meas_time_histogram_high_time == 0.0010
    session.reset_device()
    assert session._meas_time_histogram_high_time == 0.0005

def test_reset_with_defaults(session):
    deault_meas_time_histogram_high_time = session._meas_time_histogram_high_time
    assert deault_meas_time_histogram_high_time == 0.0005
    session._meas_time_histogram_high_time = 0.0010
    non_default_meas_time_histogram_high_time = session._meas_time_histogram_high_time
    assert non_default_meas_time_histogram_high_time == 0.0010
    session.reset_with_defaults()
    assert session._meas_time_histogram_high_time == 0.0005

def test_error_message():
    try:
        # We pass in an invalid model name to force going to error_message
        with niscope.Session('FakeDevice', False, True, 'Simulate=1, DriverSetup=Model:invalid_model; BoardType:PXIe'):
            assert False
    except niscope.Error as e:
        assert e.code == -1074118609
        assert e.description.find('Simulation does not support the selected model and board type.') != -1

def test_get_error(session):
    try:
        session.instrument_model = ''
        assert False
    except niscope.Error as e:
        assert e.code == -1074135027  # Error : Attribute is read-only.
        assert e.description.find('Attribute is read-only.') != -1

def test_acquisition_status(session):
    assert session.acquisition_status() == niscope.AcquisitionStatus.COMPLETE

def test_self_cal(session):
    session.self_cal(niscope.Option.SELF_CALIBRATE_ALL_CHANNELS)

def test_probe_compensation_signal(session):
    session.probe_compensation_signal_start()
    session.probe_compensation_signal_stop()

def test_configure_horizontal_timing(session):
    session.configure_vertical(5.0, niscope.VerticalCoupling.DC)
    session.auto_setup()
    session.configure_horizontal_timing(10000000, 1000, 50.0, 1, True)
    session.trigger_modifier = niscope.TriggerModifier.AUTO
    session.configure_trigger_immediate()
    session.horz_record_length == 1000
    session.horz_sample_rate == 10000000

def test_configure_chan_characteristics(session):
    session.vertical_range = 4.0
    session.configure_chan_characteristics(50, 0)
    assert 50.0 == session.input_impedance

def test_filter_coefficients(session_5142):
    assert [1.0] + [0.0] * 34 == session_5142.get_equalization_filter_coefficients()  # coefficients list should have 35 items
    try:
        filter_coefficients = [1.0, 0.0, 0.0]
        session_5142.configure_equalization_filter_coefficients(filter_coefficients)
    except niscope.Error as e:
        assert "Incorrect number of filter coefficients." in e.description
        assert e.code == -1074135024
    filter_coefficients = [0.01] * 35
    try:
        # TODO(marcoskirsch): The following should work. It doesn't due to internal NI-SCOPE driver bug 959625.
        #                     The bug is fixed in NI-SCOPE 20.0 which has not shipped at the time of this writing.
        #                     The workaround is to explicitly pass a channel rather than the default "" to the driver runtime
        #                     which should be equivalent to "all channels" (in the PXI-5142 case, that would be "0,1").
        session_5142.configure_equalization_filter_coefficients(filter_coefficients)
        assert False, "Looks like NI-SCOPE bug 959625 has been fixed. You can now remove this try/except clause"
    except niscope.errors.DriverError as e:
        assert e.code == -214202
    session_5142.channels[0].configure_equalization_filter_coefficients(filter_coefficients)
    assert filter_coefficients == session_5142.get_equalization_filter_coefficients()

def test_send_software_trigger_edge(session):
    session.send_software_trigger_edge(niscope.WhichTrigger.ARM_REFERENCE)

def test_disable(session):
    assert session.allow_more_records_than_memory is False
    session.allow_more_records_than_memory = True
    session.disable()
    assert session.allow_more_records_than_memory is False

def test_configure_ref_levels(session):
    session._configure_ref_levels()
    assert 90.0 == session._meas_chan_high_ref_level

def test_configure_trigger_digital(session):
    session.configure_trigger_digital('VAL_RTSI_0')
    session.vertical_range = 5
    assert 'VAL_RTSI_0' == session.trigger_source

def test_configure_trigger_edge(session):
    assert niscope.TriggerSlope.POSITIVE == session.trigger_slope
    session.configure_trigger_edge('0', 0.0, niscope.TriggerCoupling.DC)
    session.commit()
    assert '0' == session.trigger_source
    assert niscope.TriggerCoupling.DC == session.trigger_coupling

def test_configure_trigger_hysteresis(session):
    session.configure_trigger_hysteresis('1', 0.0, 0.05, niscope.TriggerCoupling.DC)
    assert '1' == session.trigger_source
    assert niscope.TriggerCoupling.DC == session.trigger_coupling

def test_import_export_buffer(session):
    test_value_1 = 1
    test_value_2 = 5
    session.vertical_range = test_value_1
    assert session.vertical_range == test_value_1
    buffer = session.export_attribute_configuration_buffer()
    session.vertical_range = test_value_2
    assert session.vertical_range == test_value_2
    session.import_attribute_configuration_buffer(buffer)
    assert session.vertical_range == test_value_1

def test_import_export_file(session):
    test_value_1 = 1
    test_value_2 = 5
    temp_file = tempfile.NamedTemporaryFile(suffix='.txt', delete=False)
    # NamedTemporaryFile() returns the file already opened, so we need to close it before we can use it
    temp_file.close()
    path = temp_file.name
    session.vertical_range = test_value_1
    assert session.vertical_range == test_value_1
    session.export_attribute_configuration_file(path)
    session.vertical_range = test_value_2
    assert session.vertical_range == test_value_2
    session.import_attribute_configuration_file(path)
    assert session.vertical_range == test_value_1
    os.remove(path)

def test_configure_trigger_software(session):
    session.configure_trigger_software()

def test_configure_trigger_video(session_5124):
    session_5124.configure_trigger_video('0', niscope.VideoSignalFormat.PAL, niscope.VideoTriggerEvent.FIELD1, niscope.VideoPolarity.POSITIVE, niscope.TriggerCoupling.DC)
    assert niscope.VideoSignalFormat.PAL == session_5124.tv_trigger_signal_format
    assert niscope.VideoTriggerEvent.FIELD1 == session_5124.tv_trigger_event
    assert niscope.VideoPolarity.POSITIVE == session_5124.tv_trigger_polarity
    assert niscope.TriggerCoupling.DC == session_5124.trigger_coupling

def test_configure_trigger_window(session):
    session.configure_trigger_window('1', 0, 5, niscope.TriggerWindowMode.ENTERING, niscope.TriggerCoupling.DC)
    assert '1' == session.trigger_source
    assert niscope.TriggerWindowMode.ENTERING == session.trigger_window_mode

max_stars_repo_name: AlexHearnNI/nimi-python

max_issues_repo_name: AlexHearnNI/nimi-python

max_forks_repo_name: AlexHearnNI/nimi-python


text:
import networkx;
import os,sys,FVS;
os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open ('network/bladder_tumorogenesis.sif') as f:
	edges=f.readlines()
data=[tuple(line.strip().split()) for line in edges]
data=[(edge[0],edge[2]) for edge in data]

G = networkx.DiGraph()

G.add_edges_from(data)
G_FVS=FVS.FVS(G, T_0=0.6, alpha=0.99, maxMvt_factor=5, maxFail=50, randomseed=1)

with open('results/results.txt', 'w') as writer:
	for item in G_FVS:
        	writer.write("%s\n" % item)

reason: import networkx


text:
import os
from datetime import datetime

import numpy as np
from matplotlib import pyplot as plt

class MlpUtils:
    """MLP Utils"""

    def split_data_set(self, input_number, output_number, ratio, required_samples, training_set):
        """

        :param input_number:
        :param output_number:
        :param ratio:
        :param required_samples:
        :param training_set:
        :return:
        """
        if ratio < 1:
            minimum_required_ratio = 1
            raise ValueError('Ratio must be great or equal to {}'.format(minimum_required_ratio))

        if len(training_set.X[0]) != input_number:
            raise ValueError('Input number={} and data set input number={} must be equal'.format(
                input_number, len(training_set.X.T[0])))

        if len(training_set.Y[0]) != output_number:
            raise ValueError('Output number={} and data set output number={} must be equal'.format(
                output_number, len(training_set.Y.T[0])))

        fitting_set_x = np.empty((0, input_number))
        fitting_set_y = np.empty((0, output_number))
        testing_set_x = np.empty((0, input_number))
        testing_set_y = np.empty((0, output_number))
        for idx in range(required_samples):
            if idx % ratio:
                fitting_set_x = np.append(fitting_set_x, np.array([training_set.X[idx]]), axis=0)
                fitting_set_y = np.append(fitting_set_y, np.array([training_set.Y[idx]]), axis=0)
            else:
                testing_set_x = np.append(testing_set_x, np.array([training_set.X[idx]]), axis=0)
                testing_set_y = np.append(testing_set_y, np.array([training_set.Y[idx]]), axis=0)
        return fitting_set_x, fitting_set_y, testing_set_x, testing_set_y

    def plot_testing_approximation(self, dir_name, plot_name, testing_set_x, testing_set_y,
                                   test_outputs, loss, save_to_file):
        """

        :param test_outputs:
        :param dir_name:
        :param plot_name:
        :param testing_set_x:
        :param testing_set_y:
        :param loss:
        :param save_to_file:
        """

        for idx, test_output in enumerate(test_outputs.T):
            plt.plot(testing_set_x.T[idx], testing_set_y.T[idx], '-', label='F{} Expected'.format(idx))
            plt.plot(testing_set_x.T[idx], test_output, '-',
                     label='F{} Predicted {:2.3}%'.format(idx, loss[idx][0] * 100))
        plt.xlabel('TEST ' + plot_name + ' {:2.3}%'.format(np.mean(loss) * 100))
        plt.ylim(-0.1, 1.1)
        plt.legend()
        if dir_name:
            plt.savefig('{}_TEST_ACC.png'.format(dir_name))
            plt.cla()
        else:
            plt.show()

    def plot_learning_approximation(self, dir_name, fitting_set_x, fitting_set_y, learned_outputs, metrics,
                                    plot_name, save_to_file):
        """

        :param dir_name:
        :param fitting_set_x:
        :param fitting_set_y:
        :param learned_outputs:
        :param metrics:
        :param plot_name:
        :param save_to_file:
        """

        for idx, learn_output in enumerate(learned_outputs.T):
            plt.plot(fitting_set_x.T[idx], fitting_set_y.T[idx], '-', label='F{} Expected'.format(idx))
            plt.plot(fitting_set_x.T[idx], learn_output, '-',
                     label='F{} Predicted {:2.3}%'.format(idx, metrics.MeanSquaredErrors[idx][-1] * 100))

        plt.xlabel('FIT ' + plot_name + ' {:2.3}%'.format(np.mean(metrics.AvgMeanSquaredError) * 100))

        plt.ylim(-0.1, 1.1)
        plt.legend()
        if dir_name:
            plt.savefig('{}_FIT_ACC.png'.format(dir_name))
            plt.cla()
        else:
            plt.show()

    def plot_rmse(self, epoch_number, dir_name, metrics, plot_name, save_to_file):
        """

        :param epoch_number:
        :param dir_name:
        :param metrics:
        :param plot_name:
        :param save_to_file:
        :return:
        """
        for idx, mse in enumerate(metrics.MeanSquaredErrors):
            plt.plot(np.ascontiguousarray(np.arange(epoch_number)), mse, '-', label='F{} RMSE'.format(idx))

        plt.plot(np.ascontiguousarray(np.arange(epoch_number)), metrics.AvgMeanSquaredError, 'r-',
                 label='Avg RMSE')
        plt.xlabel('FIT ' + plot_name)
        plt.ylim(0, np.max(metrics.MeanSquaredErrors) * 1.1)
        plt.legend()
        if dir_name:
            plt.savefig('{}_FIT_MSE.png'.format(dir_name))
            plt.cla()
        else:
            plt.show()

        return plot_name

    def create_date_folder_if_not_exists(self, base_directory='..\\TestResults'):
        """

        :return:
        """
        if base_directory is None:
            base_directory = '.'

        today = datetime.now()
        folder_name = today.strftime('%Y%m%H%M')
        directory = base_directory + '\\' + folder_name + '\\'
        if not os.path.exists(directory):
            os.makedirs(directory)
        return directory

max_stars_repo_name: HalfInner/MLPApproximator

max_issues_repo_name: HalfInner/MLPApproximator

max_forks_repo_name: HalfInner/MLPApproximator


text:
import statistics as st
import matplotlib.pyplot as plt
from numpy import array
import math
import pandas as pd
from statisticalLib import *

with open('./listaDeLicenças.txt') as csvfile:
        # Adiciona os valores a um objeto, onde cada item vai ser uma linha
        temp = csv.reader(csvfile, delimiter=',')
        # Cria lista que vai receber os dados do objeto
        readCSV = []
        # Transforma o objeto em uma lista
        for line in temp:
                readCSV.append(line)
        licenses = []
        for row in readCSV:
                licenses.append(row[0])

        resumo = set(licenses)
        quantidade = []
        for qtd in resumo:
                quantidade.append(licenses.count(qtd))

        licencasResumo = ['mit','apache-2.0','bsd-new','facebook-patent-rights-2','generic-cla','Outras Licenças']
        quantidadeResumo = [78704,52174,44671,10329,9312, 64167]
        print(sum(quantidadeResumo))
        print(quantidade)
        print(licenses)

        fig1, ax1 = plt.subplots()
        ax1.pie(quantidadeResumo, labels = licencasResumo, autopct='%1.1f%%', shadow=False, startangle=90)
        ax1.axis('equal')
        plt.title('Licenças Com Maior Insidencia')
        plt.savefig('./graphs/licencas_comuns.png', bbox_inches='tight')
        plt.show()

reason: from numpy


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

import functools

import numpy as np
import tensorflow.compat.v1 as tf

CIFAR10_CLASSES = [
    'airplane', 'automobile', 'bird', 'cat', 'deer',
    'dog', 'frog', 'horse', 'ship', 'truck',
]

def random_crop(image, image_shape, crop_shape):
  """Data augmentation used in training.

  Args:
    image: 4D tensor of shape (batch_size, image_height, image_width, channels)
    image_shape: A tuple of (image_height, image_width, channels)
    crop_shape: A tuple of (crop_height, crop_width, channels)

  Returns:
    fn: A cropping function, which takes a Tensor for a single image and
      returns a Tensor for a cropped version of the image.
  """
  has_batch = len(image.shape) > len(image_shape)
  image = tf.image.convert_image_dtype(image, tf.float32)
  image = tf.reshape(image, [-1] + list(image_shape))
  image = tf.image.random_flip_left_right(image)
  image = tf.pad(
      image, paddings=[[0, 0], [4, 4], [4, 4], [0, 0]], mode='REFLECT')
  image = tf.random_crop(image, [tf.shape(image)[0]] + list(crop_shape))
  # Remove the batch dimension if it didn't originally have one.
  if not has_batch:
    image = tf.squeeze(image, axis=0)
  return image

def random_crop_preprocess_fn(image_shape, crop_shape):
  """Pre-processing to occur as part of the training dataset.

  Pre-processing for the training set entails random cropping. The test set
  receives no pre-processing: it's just the identity function.

  Args:
    image_shape: A tuple of (image_height, image_width, channels)
    crop_shape: A tuple of (crop_height, crop_width, channels)

  Returns:
    Data batch preprocessing function, accepting and returning a `dict` with
      keys 'image' and 'label'.
  """
  image_fn = functools.partial(
      random_crop, image_shape=image_shape, crop_shape=crop_shape)

  return lambda x: {'image': image_fn(x['image']), 'label': x['label']}

def cifar10_model_preprocess(image):
  """Processing which should be combined with model for adv eval.

  Performs an affine transform on each element of the image tensor to map them
  to a range that straddles zero. A slightly different transform is applied
  to each channel to reflect the statistics of the Cifar-10 dataset.

  Args:
    image: 4D NHWC image tensor with float32 values in the range [0, 1].

  Returns:
    4D NHWC image tensor with float32 values in the approximate range [-1, +1].
  """
  cifar_means = [125.3, 123.0, 113.9]
  cifar_devs = [63.0, 62.1, 66.7]
  rescaled_means = np.array([x / 255. for x in cifar_means])
  rescaled_devs = np.array([x / 255. for x in cifar_devs])
  return (image - rescaled_means) / rescaled_devs

def mnist_model_preprocess(image):
  """Processing which should be combined with model for adv eval."""
  return 2. * image - 1.


text:
import numpy as np

from garageofcode.mip.solver import get_solver

def tsp(points, depot=None):
    """MIP exact solution of TSP
    Returns the edges
    """
    if not points:
        return []
    if depot is None:
        depot = points[0]
    else:
        points = [depot] + points
    # set entrance and exit depot the same
    points.append(depot)
    ids, coords = zip(*points)
    N = len(ids)

    solver = get_solver("CBC")
    D  = np.array([[np.linalg.norm(x - y)
                    for y in coords] for x in coords])
    #  gate variables
    GV = np.array([[solver.IntVar(0, int(i != j))
                    for j in range(N)] for i in range(N)])
    #  time variables
    TV = [solver.NumVar(lb=0) for _ in range(N)]

    #  exactly one entrance to all except entrance depot
    for j in range(1, N):
        solver.Add(solver.Sum(GV[:, j]) == 1)
    #  no entrance to entrance depot
    solver.Add(solver.Sum(GV[:, 0]) == 0)

    #  exactly one exit from all except exit depot
    for i in range(N-1):
        solver.Add(solver.Sum(GV[i, :]) == 1)
    #  no exit from exit depot
    solver.Add(solver.Sum(GV[-1, :]) == 0)

    #  can get tighter big M, by greedy solution
    #  actually, it doesn't make it faster
    M = N * np.max(D) * 2
    #  eliminate subtours with time variable formulation
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            gv = GV[i, j]
            d = D[i, j]
            solver.Add(TV[j] >= TV[i] + d - M * (1 - gv))

    #  minimize finish time
    solver.SetObjective(TV[-1], maximize=False)

    solver.Solve(time_limit=10, verbose=False)

    idx_order = np.argsort([solver.solution_value(tv) for tv in TV])
    return [(ids[idx0], ids[idx1])
                for idx0, idx1 in zip(idx_order, idx_order[1:])]

    '''
    sample = partial(np.random.choice, size=k, replace=False)
    c = Counter(chain.from_iterable(tsp([points[i]
                            for i in sample(n)]) for _ in range(num_iter)))

    for (u, v), num in c.items():
        u = points[u][1]
        v = points[v][1]
        x, y = zip(*[u, v])
        plt.plot(x, y, linewidth=num, color='b')
    plt.show()
    '''

    '''
    #  solution synthesis
    #  this is super stupid
    G = nx.Graph()
    for i in range(n):
        G.add_node(i)
    def unsaturated(u):
        return len(G[u]) <= 1
    for (u, v), _ in c.most_common():
        if unsaturated(u) and unsaturated(v):
            try:
                nx.shortest_path_length(G, u, v)
            except nx.exception.NetworkXNoPath:
                G.add_edge(u, v)

    for u, v in G.edges():
        u = points[u][1]
        v = points[v][1]
        x, y = zip(*[u, v])
        plt.plot(x, y, color='b')
    plt.show()
    '''


text:
"""Plots the ROC curve with ERA5 as a point
"""

import os
from os import path

import joblib
import matplotlib.pyplot as plt
import numpy as np

import compound_poisson
import dataset

RAIN_ARRAY = [0, 1, 2, 3, 4, 5, 10, 15, 20, 25]

def main():

    directory = "figure_roc_compare"
    if not path.isdir(directory):
        os.mkdir(directory)

    era5 = compound_poisson.era5.TimeSeries()
    era5.fit(dataset.Era5Cardiff())

    observed_data = dataset.CardiffTest()

    time_series = joblib.load(path.join("result", "TimeSeriesHyperSlice.gz"))
    time_series.forecaster.load_memmap("r")

    for rain in RAIN_ARRAY:
        positive_array = era5.forecaster.forecast > rain
        alt_array = observed_data.rain > rain
        null_array = np.logical_not(alt_array)

        true_positive_rate = (np.sum(np.logical_and(positive_array, alt_array))
                             / np.sum(alt_array))
        false_positive_rate = (np.sum(
            np.logical_and(positive_array, null_array))
            / np.sum(null_array))

        roc = time_series.forecaster.get_roc_curve(rain, observed_data.rain)
        plt.figure()
        roc.plot()
        plt.scatter(false_positive_rate, true_positive_rate, label="ERA5")
        plt.legend()
        plt.savefig(path.join(directory, str(rain)+".pdf"))
        plt.close()

    time_series.forecaster.del_memmap()

if __name__ == "__main__":
    main()


text:
#!/usr/bin/env python3
"""
 Copyright (C) 2019 Intel Corporation

 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 sys
import os
import numpy as np
import logging as log
from time import time
from openvino.inference_engine import IENetwork, IECore
import codecs
from deepspeech_openvino.speech_features import audio_spectrogram, mfcc
from deepspeech_openvino.ctc_beamsearch_decoder import ctc_beam_search_decoder
from abc import abstractmethod

n_input = 26
n_context = 9
n_steps = 16
numcep = n_input
numcontext = n_context
beamwidth = 10

def preprocess_sound(audio_rev, fs):
    """
    converts audio to features
    """
    audio = np.frombuffer(audio_rev, dtype=np.dtype("<h"))
    # normalize to -1 to 1, int 16 to float32
    audio = audio / np.float32(32768)
    audio = audio.reshape(-1, 1)
    spectrogram = audio_spectrogram(
        audio, (16000 * 32 / 1000), (16000 * 20 / 1000), True
    )
    features = mfcc(spectrogram.reshape(1, spectrogram.shape[0], -1), fs, 26)
    empty_context = np.zeros((numcontext, numcep), dtype=features.dtype)
    features = np.concatenate((empty_context, features, empty_context))
    num_strides = len(features) - (n_context * 2)
    # Create a view into the array with overlapping strides of size
    # numcontext (past) + 1 (present) + numcontext (future)
    window_size = 2 * n_context + 1
    features = np.lib.stride_tricks.as_strided(
        features,
        (num_strides, window_size, n_input),
        (features.strides[0], features.strides[0], features.strides[1]),
        writeable=False,
    )
    return features

class DeepSpeechASR(object):
    def __init__(self, model, device, sample_rate, alphabet_cfg="", SPEECH_LIB_PATH=""):
        # TODO asserts on model= have .xml xtension, sample rate should be a number,
        # alphabet_cfg a file name, protect it additionally, speechlib path apath
        # assert device in supported openvino devices
        self.output_text = ""
        self.cpu_extension = SPEECH_LIB_PATH
        self.sample_rate = ""
        self.model_bin = model.replace(".xml", ".bin")
        self.model_xml = model
        self.device = device
        self.alphabet_cfg = alphabet_cfg
        self.sample_rate = 16000
        self.ie = IECore()
        self.exec_net = None
        # TODO: Enable cpu extensions
        # if self.cpu_extension and 'CPU' in self.device:
        # if os.path.exists(self.cpu_extension) and 'CPU' in self.device:
        #    self.ie.add_extension(self.cpu_extension, "CPU")
        # Read IR
        log.debug(
            "DeepSpeechASR: Loading network files:\n\t{}\n\t{}".format(
                self.model_xml, self.model_bin
            )
        )
        self.net = self.ie.read_network(
            model=self.model_xml, weights=self.model_bin)
        return self._load_model_to_device()

    @abstractmethod
    def _load_model_to_device(self):
        pass

    @abstractmethod
    def _transcribe(self, features):
        pass

    def close(self):
        log.info("DeepSpeechASR: close()")
        if self.exec_net:
            del self.exec_net
        if self.ie:
            del self.ie

    def push_data(self, buffer):
        features = preprocess_sound(buffer, self.sample_rate)
        self.output_text = self._transcribe(features)

    def get_result(self, final, finish_processing):
        # TODO: do something with final
        result = self.output_text
        if finish_processing:
            self.exec_net = ""
            self._load_model_to_device()
        return result

class Alphabet(object):
    def __init__(self, alphabet_cfg):
        self._label_to_str = []
        self._str_to_label = {}
        self._size = 0
        with codecs.open(alphabet_cfg, "r", "utf-8") as fin:
            for line in fin:
                if line[0:2] == "\\#":
                    line = "#\n"
                elif line[0] == "#":
                    continue
                self._label_to_str += line[:-1]  # remove the line ending
                self._str_to_label[line[:-1]] = self._size
                self._size += 1

    def string_from_label(self, label):
        return self._label_to_str[label]

    def label_from_string(self, string):
        return self._str_to_label[string]

    def size(self):
        return self._size

def main():
    # TODO: Write sample app here
    # asr = DeepSpeechASR()
    pass

if __name__ == "__main__":
    main()


text:
# Copyright 2014 Diamond Light Source Ltd.
#
# 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.

"""
.. module:: tomophantom_loader
   :platform: Unix
   :synopsis: A full-field tomography loader that creates a NeXus file in \
   NXtomo format and contains tomographic synthetic data generated hdf5 dataset of a \
   specified size.

.. moduleauthor:: Daniil Kazantsev <scientificsoftware@diamond.ac.uk>

"""

import numpy as np

from savu.plugins.utils import register_plugin
from savu.plugins.loaders.base_tomophantom_loader import BaseTomophantomLoader
from savu.data.data_structures.data_types.data_plus_darks_and_flats \
    import ImageKey

@register_plugin
class TomoPhantomLoader(BaseTomophantomLoader):
    def __init__(self, name='TomoPhantomLoader'):
        super(TomoPhantomLoader, self).__init__(name)

    def setup(self):
        data_obj,data_obj2 = super(TomoPhantomLoader, self).setup()

        image_key = self.__set_image_key(data_obj)
        data_obj.data = ImageKey(data_obj, image_key, 0)
        data_obj.set_shape(data_obj.data.shape)
        self.set_data_reduction_params(data_obj)

        data_obj2.set_shape(data_obj2.data.shape)
        self.set_data_reduction_params(data_obj2)

    def __set_image_key(self, data_obj):
        proj_slice = \
            data_obj.get_data_patterns()['PROJECTION']['slice_dims'][0]
        image_key = np.zeros(data_obj.data.shape[proj_slice], dtype=int)
        return image_key


text:
"""
Runs example tournaments using the Axelrod library available at
https://github.com/marcharper/Axelrod
"""

import argparse
import os

import numpy
import matplotlib.pyplot as plt

import axelrod
axl = axelrod

def ensure_directory(directory):
    """Makes sure that a directory exists and creates it if it does not."""

    head, tail = os.path.split(directory)
    if head:
        ensure_directory(head)

    if not os.path.isdir(directory):
        os.mkdir(directory)

def axelrod_strategies(cheaters=False, meta=False, transformer=None):
    """Obtains the list of strategies from Axelrod library."""

    s = [s for s in axelrod.all_strategies if axelrod.obey_axelrod(s())]
    if cheaters:
        s.extend(axelrod.cheating_strategies)
    if not meta:
        s = [t for t in s if not (
            t.__name__.startswith("Meta") or t.__name__.startswith("NiceMeta")
        or t.__name__.startswith("NMW"))]
    # Instantiate
    if transformer:
        s = [transformer(t)() for t in s]
    else:
        s = [t() for t in s]
    # Sort by name
    s.sort(key=str)
    return s

def deterministic_strategies():
    """Deterministic Strategies"""
    strategies = []
    for s in axelrod_strategies():
        if not s.classifier["stochastic"]:
            strategies.append(s)
    return strategies

def stochastic_strategies():
    """Stochastic Strategies"""
    strategies = []
    for s in axelrod_strategies():
        if s.classifier["stochastic"]:
            strategies.append(s)
    return strategies

def finite_memory_strategies(lower=0, upper=float('inf')):
    """Filter strategies down to those that have finite memory_depth."""
    strategies = []
    for s in axelrod_strategies():
        memory_depth = s.classifier['memory_depth']
        if memory_depth >= lower and memory_depth < upper:
            strategies.append(s)
    return strategies

def memoryone_strategies():
    """Filter strategies down to those that are memoryone, that is having
    memory_depth 0 or 1."""

    return finite_memory_strategies(lower=0, upper=2)

def tscizzle_strategies():
    """The list of strategies used in @tscizzle's Morality Metrics paper."""

    strategies = [
        axelrod.Cooperator(),
        axelrod.Defector(),
        axelrod.Eatherley(),
        axelrod.Champion(),
        axelrod.GTFT(p=0.1),
        axelrod.GTFT(p=0.3),
        axelrod.GoByMajority(soft=True),
        axelrod.GoByMajority(soft=False),
        axelrod.TitFor2Tats(),
        axelrod.Random(0.8),
        axelrod.Random(0.5),
        axelrod.Random(0.2),
        axelrod.WinStayLoseShift(), # Pavlov
        axelrod.TitForTat(),
        axelrod.TwoTitsForTat(),
        axelrod.Grudger(), # Friedman
        axelrod.Tester(),
        axelrod.SuspiciousTitForTat(),
        axelrod.Joss(0.9),
        axelrod.Joss(0.7),

    ]
    return strategies

def sp_strategies():
    """The list of strategies used in Stewart and Plotkin's 2012 tournament."""

    strategies = [
        axelrod.Cooperator(), # ALLC
        axelrod.Defector(), # ALLD
        axelrod.GTFT(),
        axelrod.GoByMajority(soft=False), # HARD_MAJO
        #axelrod.GoByMajority(soft=True), # SOFT_MAJO
        axelrod.TitFor2Tats(), # TFT2
        axelrod.HardTitFor2Tats(), # HARD_TFT2
        axelrod.Random(), # RANDOM
        axelrod.WinStayLoseShift(), # WSLS
        axelrod.TitForTat(),
        axelrod.HardTitForTat(), # HARD_TFT
        axelrod.Grudger(), # GRIM
        axelrod.Joss(), # HARD_JOSS
        axelrod.ZDGTFT2(),
        axelrod.ZDExtort2(),
        axelrod.Prober(),
        axelrod.Prober2(),
        axelrod.Prober3(),
        axelrod.HardProber(),
        axelrod.Calculator(),
    ]
    return strategies

def random_strategies():
    strategies = []
    for value in numpy.arange(0, 1.05, 0.05):
        strategies.append(axelrod.Random(value))
    return strategies

def all_plots(label, results, filename_suffix, file_format, output_directory):
    """Obtain all plots"""
    plot = axl.Plot(results)

    f = plot.boxplot(title="Payoff " + label)
    filename = os.path.join(output_directory,
                            "{}_boxplot.{}".format(filename_suffix, file_format))
    f.savefig(filename)
    plt.close(f)

    f = plot.payoff(title="Payoff " + label)
    filename = os.path.join(output_directory,
                            "{}_payoff.{}".format(filename_suffix, file_format))
    f.savefig(filename)
    plt.close(f)

    f = plot.winplot(title="Wins " + label)
    filename = os.path.join(output_directory,
                            "{}_winplot.{}".format(filename_suffix, file_format))
    f.savefig(filename)
    plt.close(f)

    f = plot.sdvplot(title="Payoff differences " + label)
    filename = os.path.join(output_directory,
                            "{}_sdvplot.{}".format(filename_suffix, file_format))
    f.savefig(filename)
    plt.close(f)

    f = plot.pdplot(title="Payoff differences " + label)
    filename = os.path.join(output_directory,
                            "{}_pdplot.{}".format(filename_suffix, file_format))
    f.savefig(filename)
    plt.close(f)

def run_tournament(name, strategies, repetitions=100, turns=200, noise=0,
                   processes=None):
    if not processes:
        processes = 3

    # Make sure the output directories exist
    root_directory = os.path.join("assets", "tournaments")
    output_directory = os.path.join(root_directory, name)
    ensure_directory(output_directory)

    # Set up a tournament manager
    tournament = axelrod.Tournament(strategies, turns=turns,
                                    repetitions=repetitions,
                                    noise=noise)
    results = tournament.play(processes=processes)
    # Make the plots
    all_plots(label=name, results=results, filename_suffix=name,
              file_format="svg", output_directory=output_directory)
    return results

def parse_args():
    parser = argparse.ArgumentParser(description="Run Sample Axelrod tournaments")

    parser.add_argument(
        '-t',
        '--turns',
        type=int,
        default=200,
        help='turns per pair')

    parser.add_argument(
        '-r', '--repetitions',
        type=int,
        default=10,
        help='round-robin repetitions')

    parser.add_argument(
        '-p', '--processes',
        type=int,
        default=None,
        help='Number of parallel processes to spawn. 0 uses cpu count.')

    parser.add_argument(
        '-n', '--noise',
        type=float,
        default=0,
        help='Noise level')

    parser.add_argument(
        '-a',
        "--all_strategies",
        action='store_true',
        dest="all_strategies",
        help='Run just the all strategies tournament')

    args = parser.parse_args()

    return (args.turns, args.repetitions, args.processes, args.noise,
            args.all_strategies)

if __name__ == "__main__":
    turns, repetitions, processes, noise, all_strategies = parse_args()

    if all_strategies:
        strategies_names = [(axelrod_strategies(cheaters=False), "AllFairStrategies")]
    else:
        strategies_names = [
            (deterministic_strategies(), "Deterministic"),
            (stochastic_strategies(), "Stochastic"),
            (memoryone_strategies(), "Memoryone"),
            (finite_memory_strategies(), "FiniteMemory"),
            (tscizzle_strategies(), "tscizzle"),
            (sp_strategies(), "StewartPlotkin2012")
            ]
    for strategies, name in strategies_names:
        if noise:
            name += "-noise"
        run_tournament(name, strategies, repetitions=repetitions, turns=turns, noise=noise, processes=processes)

max_stars_repo_name: Axelrod-Python/AxelrodExamples

max_issues_repo_name: joekroese/math-of-revolution

max_forks_repo_name: joekroese/math-of-revolution