text:
# -*- coding: utf-8 -*-
"""
@author : Abolfazl Ahmadi
"""

__authors__ = "Ahmad Mehrabi & Abolfazl Ahmadi Rahmat"
__license__ = "MIT"
__version_info__ = ('25','April','2019')
__version__ = '-'.join(__version_info__)
__status__ = "Development"

__check_mce_installed__ = False
try:
    from MCEvidence import MCEvidence
    __check_mce_installed__ = True
except:
    print("Dependency ERROR! 'MCEvidence not found!")
    __check_mce_installed__ = False

import numpy as np
import inspect, os, sys

class BaseFuncs:
    def printErr(self, info):
        print("ERROR!:{}".format(info))
    def printInfo(self, info):
        print("INFO:{}".format(info))
        # Disable
    def blockPrint(self):
        sys.stdout = open(os.devnull, 'w')
    # Restore
    def enablePrint(self):
        sys.stdout = sys.__stdout__

class Rel_ent(BaseFuncs):
    'this is mylass form of the package'
    def __init__(self, chain_path=None, lnprior_path=None, inThinlen=0.0, inBurnlen=0.0):
        self.state = False
        if chain_path != None:
            if lnprior_path != None:
                self.state = True
            else:
                self.printErr('Problem in loading lnprior')
        else:
            self.printErr('Problem in loading chain!')

        if self.state:
            self.Thinlen = inThinlen
            self.Burnlen = inBurnlen
            self.printInfo('Loading chain from {}'.format(chain_path))
            self.chain = np.genfromtxt(chain_path)
            self.printInfo('Loading lnprior from {}'.format(lnprior_path))
            lnprior = np.genfromtxt(lnprior_path)
            self.Blen = int(self.Burnlen*len(lnprior))
            lnp = self.chain[:, 1]
            self.printInfo("All data loaded.")
            self.lnlike = lnp - lnprior
            self.lnlike = self.lnlike[self.Blen:]
    def printRes(self, resVec):
        for k in range(len(resVec)):
            print('D[k={}] = {}'.format(k+1, resVec[k]))
    def myResult(self, avgL, mce):
        result = np.zeros(len(mce))
        result = -1 * mce + avgL
        return result
    def Run(self):
        if self.state:
            self.printInfo("Calculatong MCEvidence...")

            mce = MCEvidence([self.chain], ischain=True, thinlen=self.Thinlen, burnlen=self.Burnlen).evidence(pos_lnp=True)
            avgL = np.mean(self.lnlike)
            Gresult = self.myResult(avgL, mce)

            self.printInfo("Results are :")
            self.printRes(Gresult)
            return Gresult
        else:
            self.printErr("path error!")
            return 0
class Exp_rel_ent(BaseFuncs):
    'this is expecte_relative_entropy form of the package'
    def __init__(self, sample_path=None, function_path=None, lcm=np.zeros(1), n_in=0):
        self.state = False
        if sample_path != None:
            if len(lcm) != 1:
                self.state = True
            else:
                self.printErr('input error, Like Cov Matrix!')
        else:
            self.printErr('import error, sample path dose not exist!')
        if self.state:
            self.sPath = sample_path
            self.Like_Cov_Matrix = lcm
            self.n = n_in
            self.fpath = function_path[:-12]
            sys.path.append(self.fpath)
            print(self.fpath)
            from Functions import model_function
            self.mf = model_function()
    def check_input(self):
        if len(self.Like_Cov_Matrix) > self.n or len(self.Like_Cov_Matrix) != len(self.Like_Cov_Matrix[0]):
            self.printErr('The likelihood_cov matrix should be a square matrix (n*n)!')
            self.state = False
    #This function has been used in the code
    def MAH_Distance(self, x, y):
        return np.transpose(x)@y@x
    def jfun(self, i, l, data, cov_like_inv):
        ss = 0
        fl = float(l)
        for j in range(l):
            ss = ss + np.exp(-0.5*self.MAH_Distance(data[i]-self.mf.fun(self.sample[j]), cov_like_inv))
        term2 = np.log(ss/fl)
        term1 = -0.5 * self.MAH_Distance(data[i]-self.mf.fun(self.sample[i]), cov_like_inv)
        return term1 - term2
    def Run(self, l_in=0):
        self.l = l_in
        if self.l == 0:
            #ERROR
            self.state = False
        if self.state:
            self.printInfo('file importing...')
            self.printInfo('samples file importing '+self.sPath)
            self.sample = np.genfromtxt(self.sPath)
            self.printInfo('checking input files...')
            self.check_input()
            if self.l>len(self.sample):
                self.printErr('The number of sample used to estimate expected relative entropy should be smaller than given sample size')
                self.state = False
            if self.state:
                from numpy.linalg import inv
                self.printInfo('Expected Relative Entropy (ERE):')
                self.printInfo('genertaing data sample...')
                cov_like_inv = inv(self.Like_Cov_Matrix)
                data = np.zeros((self.l,self.n))
                for i in range(self.l):
                    data[i] = np.random.multivariate_normal(self.mf.fun(self.sample[i]), self.Like_Cov_Matrix)
                exp_rel = 0
                for m in range(self.l):
                    exp_rel = exp_rel + self.jfun(m, self.l, data, cov_like_inv)
                result = (exp_rel/self.l)
                self.printInfo(result)
                return result
        else:
            self.printErr('State Error')
    def PRun(self, l_in=0, coreN=1):
        self.l = l_in
        if self.l == 0:
            #ERROR
            self.printErr("data length is zero!")
            self.state = False
        else:
            self.check_input()
        import multiprocessing
        cn = multiprocessing.cpu_count()
        if coreN > cn:
            self.printErr(
                "number of threads for this machine cannot be larger than "+
                str(cn)+
                " (number of cores of this machine)!"
                )
            self.state = False
        if self.state:
            lPath = self.fpath + 'cash_0000_cov.txt'
            np.savetxt(lPath, self.Like_Cov_Matrix)
            my_path = str(__file__)
            bashCommand = "mpiexec -np " + str(coreN) + " python " + my_path[:-3] + "_parallelERE.py"
            bashCommand = bashCommand + " " + str(self.n) + " " + str(self.l) + " " + str(self.sPath) + " " + str(lPath) + " " + str(self.fpath)
            os.system(bashCommand)
            CF = self.fpath + 'cash.txt'
            result = np.genfromtxt(CF)
            os.remove(CF)
            os.remove(lPath)
            return float(result)

max_stars_repo_name: ahmadiphy/MCKLdivergence

max_issues_repo_name: ahmadiphy/MCKLdivergence

max_forks_repo_name: ahmadiphy/MCKLdivergence

reason: import numpy,from numpy


text:
from caffe2.python import core
import caffe2.python.hypothesis_test_util as hu
import caffe2.python.serialized_test.serialized_test_util as serial
from hypothesis import given, settings
import hypothesis.strategies as st
import unittest
import numpy as np

def get_op(input_len, output_len, args):
    input_names = ['in_scores', 'in_boxes', 'in_batch_splits']
    assert input_len <= len(input_names)
    input_names = input_names[:input_len]

    out_names = ['scores', 'boxes', 'classes', 'batch_splits', 'keeps', 'keeps_size']
    assert output_len <= len(out_names)
    out_names = out_names[:output_len]

    op = core.CreateOperator(
        'BoxWithNMSLimit',
        input_names,
        out_names,
        **args)

    return op

HU_CONFIG = {
    'gc': hu.gcs_cpu_only['gc'],
}

def gen_boxes(count, center):
    len = 10
    len_half = len / 2.0
    ret = np.tile(
        np.array(
            [center[0] - len_half, center[1] - len_half,
            center[0] + len_half, center[1] + len_half]
        ).astype(np.float32),
        (count, 1)
    )
    return ret

def gen_multiple_boxes(centers, scores, count, num_classes):
    ret_box = None
    ret_scores = None
    for cc, ss in zip(centers, scores):
        box = gen_boxes(count, cc)
        ret_box = np.vstack((ret_box, box)) if ret_box is not None else box
        cur_sc = np.ones((count, 1), dtype=np.float32) * ss
        ret_scores = np.vstack((ret_scores, cur_sc)) \
            if ret_scores is not None else cur_sc
    ret_box = np.tile(ret_box, (1, num_classes))
    ret_scores = np.tile(ret_scores, (1, num_classes))
    assert ret_box.shape == (len(centers) * count, 4 * num_classes)
    assert ret_scores.shape == (len(centers) * count, num_classes)
    return ret_box, ret_scores

class TestBoxWithNMSLimitOp(serial.SerializedTestCase):
    @given(**HU_CONFIG)
    @settings(deadline=10000)
    def test_simple(self, gc):
        in_centers = [(0, 0), (20, 20), (50, 50)]
        in_scores = [0.9, 0.8, 0.6]
        boxes, scores = gen_multiple_boxes(in_centers, in_scores, 10, 2)

        gt_boxes, gt_scores = gen_multiple_boxes(in_centers, in_scores, 1, 1)
        gt_classes = np.ones(gt_boxes.shape[0], dtype=np.float32)

        op = get_op(2, 3, {"score_thresh": 0.5, "nms": 0.9})

        def ref(*args, **kwargs):
            return (gt_scores.flatten(), gt_boxes, gt_classes)

        self.assertReferenceChecks(gc, op, [scores, boxes], ref)

    @given(**HU_CONFIG)
    @settings(deadline=1000)
    def test_score_thresh(self, gc):
        in_centers = [(0, 0), (20, 20), (50, 50)]
        in_scores = [0.7, 0.85, 0.6]
        boxes, scores = gen_multiple_boxes(in_centers, in_scores, 10, 2)

        gt_centers = [(20, 20)]
        gt_scores = [0.85]
        gt_boxes, gt_scores = gen_multiple_boxes(gt_centers, gt_scores, 1, 1)
        gt_classes = np.ones(gt_boxes.shape[0], dtype=np.float32)

        op = get_op(2, 3, {"score_thresh": 0.8, "nms": 0.9})

        def ref(*args, **kwargs):
            return (gt_scores.flatten(), gt_boxes, gt_classes)

        self.assertReferenceChecks(gc, op, [scores, boxes], ref)

    @given(det_per_im=st.integers(1, 3), **HU_CONFIG)
    @settings(deadline=1000)
    def test_detections_per_im(self, det_per_im, gc):
        in_centers = [(0, 0), (20, 20), (50, 50)]
        in_scores = [0.7, 0.85, 0.6]
        boxes, scores = gen_multiple_boxes(in_centers, in_scores, 10, 2)

        gt_centers = [(20, 20), (0, 0), (50, 50)][:det_per_im]
        gt_scores = [0.85, 0.7, 0.6][:det_per_im]
        gt_boxes, gt_scores = gen_multiple_boxes(gt_centers, gt_scores, 1, 1)
        gt_classes = np.ones(gt_boxes.shape[0], dtype=np.float32)

        op = get_op(
            2, 3,
            {"score_thresh": 0.5, "nms": 0.9, "detections_per_im": det_per_im}
        )

        def ref(*args, **kwargs):
            return (gt_scores.flatten(), gt_boxes, gt_classes)

        self.assertReferenceChecks(gc, op, [scores, boxes], ref)

    @given(
        num_classes=st.integers(2, 10),
        det_per_im=st.integers(1, 4),
        cls_agnostic_bbox_reg=st.booleans(),
        input_boxes_include_bg_cls=st.booleans(),
        output_classes_include_bg_cls=st.booleans(),
        **HU_CONFIG
    )
    @settings(deadline=1000)
    def test_multiclass(
        self,
        num_classes,
        det_per_im,
        cls_agnostic_bbox_reg,
        input_boxes_include_bg_cls,
        output_classes_include_bg_cls,
        gc
    ):
        in_centers = [(0, 0), (20, 20), (50, 50)]
        in_scores = [0.7, 0.85, 0.6]
        boxes, scores = gen_multiple_boxes(in_centers, in_scores, 10, num_classes)

        if not input_boxes_include_bg_cls:
            # remove background class
            boxes = boxes[:, 4:]
        if cls_agnostic_bbox_reg:
            # only leave one class
            boxes = boxes[:, :4]
        # randomize un-used scores for background class
        scores_bg_class_id = 0 if input_boxes_include_bg_cls else -1
        scores[:, scores_bg_class_id] = np.random.rand(scores.shape[0]).astype(np.float32)

        gt_centers = [(20, 20), (0, 0), (50, 50)][:det_per_im]
        gt_scores = [0.85, 0.7, 0.6][:det_per_im]
        gt_boxes, gt_scores = gen_multiple_boxes(gt_centers, gt_scores, 1, 1)
        # [1, 1, 1, 2, 2, 2, 3, 3, 3, ...]
        gt_classes = np.tile(
            np.array(range(1, num_classes), dtype=np.float32),
            (gt_boxes.shape[0], 1)).T.flatten()
        if not output_classes_include_bg_cls:
            # remove background class
            gt_classes -= 1
        gt_boxes = np.tile(gt_boxes, (num_classes - 1, 1))
        gt_scores = np.tile(gt_scores, (num_classes - 1, 1)).flatten()

        op = get_op(
            2, 3,
            {
                "score_thresh": 0.5,
                "nms": 0.9,
                "detections_per_im": (num_classes - 1) * det_per_im,
                "cls_agnostic_bbox_reg": cls_agnostic_bbox_reg,
                "input_boxes_include_bg_cls": input_boxes_include_bg_cls,
                "output_classes_include_bg_cls": output_classes_include_bg_cls
            }
        )

        def ref(*args, **kwargs):
            return (gt_scores, gt_boxes, gt_classes)

        self.assertReferenceChecks(gc, op, [scores, boxes], ref)

    @given(det_per_im=st.integers(1, 3), **HU_CONFIG)
    def test_detections_per_im_same_thresh(self, det_per_im, gc):
        in_centers = [(0, 0), (20, 20), (50, 50)]
        in_scores = [0.7, 0.7, 0.7]
        boxes, scores = gen_multiple_boxes(in_centers, in_scores, 10, 2)

        gt_centers = [(20, 20), (0, 0), (50, 50)][:det_per_im]
        gt_scores = [0.7, 0.7, 0.7][:det_per_im]
        gt_boxes, gt_scores = gen_multiple_boxes(gt_centers, gt_scores, 1, 1)
        gt_classes = np.ones(gt_boxes.shape[0], dtype=np.float32)

        op = get_op(
            2, 3,
            {"score_thresh": 0.5, "nms": 0.9, "detections_per_im": det_per_im}
        )

        # boxes output could be in any order
        def verify(inputs, outputs):
            # check scores
            np.testing.assert_allclose(
                outputs[0], gt_scores.flatten(), atol=1e-4, rtol=1e-4,
            )
            # check classes
            np.testing.assert_allclose(
                outputs[2], gt_classes, atol=1e-4, rtol=1e-4,
            )
            self.assertEqual(outputs[1].shape, gt_boxes.shape)

        self.assertValidationChecks(gc, op, [scores, boxes], verify, as_kwargs=False)

    @given(num_classes=st.integers(2, 10), **HU_CONFIG)
    def test_detections_per_im_same_thresh_multiclass(self, num_classes, gc):
        in_centers = [(0, 0), (20, 20), (50, 50)]
        in_scores = [0.6, 0.7, 0.7]
        boxes, scores = gen_multiple_boxes(in_centers, in_scores, 10, num_classes)

        det_per_im = 1
        gt_centers = [(20, 20), (50, 50)]
        gt_scores = [0.7, 0.7]
        gt_boxes, gt_scores = gen_multiple_boxes(gt_centers, gt_scores, 1, 1)

        op = get_op(
            2, 3,
            {"score_thresh": 0.5, "nms": 0.9, "detections_per_im": det_per_im}
        )

        # boxes output could be in any order
        def verify(inputs, outputs):
            # check scores
            self.assertEqual(outputs[0].shape, (1,))
            self.assertEqual(outputs[0][0], gt_scores[0])

            # check boxes
            self.assertTrue(
                np.allclose(outputs[1], gt_boxes[0, :], atol=1e-4, rtol=1e-4) or
                np.allclose(outputs[1], gt_boxes[1, :], atol=1e-4, rtol=1e-4)
            )

            # check class
            self.assertNotEqual(outputs[2][0], 0)

        self.assertValidationChecks(gc, op, [scores, boxes], verify, as_kwargs=False)

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

ext: py

max_stars_repo_name: deltabravozulu/pytorch

max_issues_repo_name: deltabravozulu/pytorch

max_forks_repo_name: deltabravozulu/pytorch

reason: import numpy


text:
import torch
import os
import torch.nn as nn
import torch.nn.functional as F
import math, copy, time
from torch.autograd import Variable
from typing import Dict, Any
from Utils.utils import MLP, OptionalLayer, LayerNorm1
import numpy as np

def save_npy(data, filename):
    np.save("vis/"+filename, data)

class InputModule(nn.Module):
    def __init__(self, config: Dict[str, Any]):
        super(InputModule, self).__init__()
        self.word_embed = nn.Embedding(num_embeddings=config["vocab_size"],
                                       embedding_dim=config["symbol_size"])
        nn.init.uniform_(self.word_embed.weight, -config["init_limit"], config["init_limit"])
        # positional embeddings
        self.pos_embed = nn.Parameter(torch.ones(config["max_seq"], config["symbol_size"]))
        nn.init.ones_(self.pos_embed.data)
        self.pos_embed.data /= config["max_seq"]

    def forward(self, story: torch.Tensor, query: torch.Tensor):
        # Sentence embedding
        sentence_embed = self.word_embed(story)  # [b, s, w, e]
        sentence_sum = torch.einsum('bswe,we->bse', sentence_embed, self.pos_embed[:sentence_embed.shape[2]])
        # Query embedding
        query_embed = self.word_embed(query)  # [b, w, e]
        query_sum = torch.einsum('bwe,we->be', query_embed, self.pos_embed[:query_embed.shape[1]])
        return sentence_sum, query_sum

class InferenceModule(nn.Module):
    def __init__(self, config: Dict[str, Any]):
        super(InferenceModule, self).__init__()
        self.hidden_size = config["hidden_size"]
        self.ent_size = config["entity_size"]
        self.role_size = config["role_size"]
        self.symbol_size = config["symbol_size"]
        # output embeddings
        self.Z = nn.Parameter(torch.zeros(config["entity_size"], config["vocab_size"]))
        nn.init.xavier_uniform_(self.Z.data)

        # TODO: remove unused entity head?
        self.e = MLP(D_in=config["symbol_size"], hidden_dims=[self.hidden_size, self.hidden_size], D_out=config["vocab_size"])

    def forward(self, query_embed: torch.Tensor, TPR: torch.Tensor):
        e = torch.einsum('bsf->bf', torch.einsum('bsf,bf->bsf', TPR, query_embed))
        e1 = self.e(e)

        return e1 # logits

def clones(module, N):
    """Produce N identical layers. (in the paper N=6)"""
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])

def attention(query, key, value, mask=None, dropout=None):
    "Compute 'Scaled Dot Product Attention'"
    d_k = query.size(-1)
    scores = torch.matmul(query, key.transpose(-2, -1)) \
             / math.sqrt(d_k)
    if mask is not None:
        scores = scores.masked_fill(mask == 0, -1e9)
    p_attn = F.softmax(scores, dim = -1)
    if dropout is not None:
        p_attn = dropout(p_attn)
    return torch.matmul(p_attn, value), p_attn

class MultiHeadedAttention(nn.Module):
    def __init__(self, h, d_model, dropout=0.1):
        "Take in model size and number of heads."
        super(MultiHeadedAttention, self).__init__()
        assert d_model % h == 0
        # We assume d_v always equals d_k
        self.d_k = d_model // h
        self.h = h
        self.linears = clones(nn.Linear(d_model, d_model), 4)
        self.attn = None
        self.dropout = nn.Dropout(p=dropout)

    def forward(self, query, key, value, mask=None):
        "Implements Figure 2"
        if mask is not None:
            # Same mask applied to all h heads.
            mask = mask.unsqueeze(1)
        nbatches = query.size(0)

        # 1) Do all the linear projections in batch from d_model => h x d_k
        query, key, value = \
            [l(x).view(nbatches, -1, self.h, self.d_k).transpose(1, 2)
             for l, x in zip(self.linears, (query, key, value))]

        # 2) Apply attention on all the projected vectors in batch.
        x, self.attn = attention(query, key, value, mask=mask,
                                 dropout=self.dropout)
        if int(os.environ["attention_vis"]):
            print("visualizing attention")
            save_npy( self.attn.cpu().detach().numpy(), "attention_e"+str(os.environ["epoch"])+"_t"+str(os.environ["task"])+".npy")
            os.environ["attention_vis"] = "0"

        # 3) "Concat" using a view and apply a final linear.
        x = x.transpose(1, 2).contiguous() \
             .view(nbatches, -1, self.h * self.d_k)
        return self.linears[-1](x)

class EncoderDecoder(nn.Module):
    """
    A standard Encoder-Decoder architecture. Base for this and many
    other models.
    """
    def __init__(self, encoder, decoder, embedding, position):
        super(EncoderDecoder, self).__init__()
        self.position = position
        self.encoder = encoder
        self.decoder = decoder
        self.embedding = embedding

    def forward(self, src, src_mask, query):
        "Take in and process masked src and target sequences."
        src_embed, query_embed = self.embedding(src, query)
        src_embed = self.position(src_embed)

        out = self.encode(src_embed, None)
        return self.decode(out, query_embed)

    def encode(self, src, src_mask):
        return self.encoder(src, src_mask)

    def decode(self, memory, query):
        return self.decoder(query, memory) # the memory is the context

class Encoder(nn.Module):
    "Core encoder is a stack of N layers"
    def __init__(self, layer, N):
        super(Encoder, self).__init__()
        self.layers = clones(layer, N)
        self.norm = LayerNorm(layer.size)

    def forward(self, x, mask):
        "Pass the input (and mask) through each layer in turn."
        for layer in self.layers:
            x = layer(x, mask)
        return self.norm(x)

class LayerNorm(nn.Module):
    "Construct a layernorm module (See citation for details)."
    def __init__(self, features, eps=1e-6):
        super(LayerNorm, self).__init__()
        self.a_2 = nn.Parameter(torch.ones(features))
        self.b_2 = nn.Parameter(torch.zeros(features))
        self.eps = eps

    def forward(self, x):
        mean = x.mean(-1, keepdim=True)
        std = x.std(-1, keepdim=True)
        return self.a_2 * (x - mean) / (std + self.eps) + self.b_2

class SublayerConnection(nn.Module):
    """
    A residual connection followed by a layer norm.
    Note for code simplicity the norm is first as opposed to last.
    """
    def __init__(self, size, dropout):
        super(SublayerConnection, self).__init__()
        self.norm = LayerNorm(size)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x, sublayer):
        "Apply residual connection to any sublayer with the same size."
        return x + self.dropout(sublayer(self.norm(x)))

class EncoderLayer(nn.Module):
    "Encoder is made up of self-attn and feed forward (defined below)"
    def __init__(self, size, self_attn, relational, dropout):
        super(EncoderLayer, self).__init__()
        self.self_attn = self_attn
        self.relational = relational
        self.sublayer = clones(SublayerConnection(size, dropout), 2)
        self.size = size

    def forward(self, x, mask):
        "Follow Figure 1 (left) for connections."
        x = self.sublayer[0](x, lambda x: self.self_attn(x, x, x, mask))
        return self.sublayer[1](x, self.relational)

class PositionwiseFeedForward(nn.Module):
    "Implements FFN equation."
    def __init__(self, d_model, d_ff, dropout=0.1):
        super(PositionwiseFeedForward, self).__init__()
        self.w_1 = nn.Linear(d_model, d_ff)
        self.w_2 = nn.Linear(d_ff, d_model)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x):
        return self.w_2(self.dropout(F.relu(self.w_1(x))))

class PositionalEncoding(nn.Module):
    "Implement the PE function."
    def __init__(self, d_model, dropout, max_len=5000):
        super(PositionalEncoding, self).__init__()
        self.dropout = nn.Dropout(p=dropout)

        # Compute the positional encodings once in log space.
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2) *
                             -(math.log(10000.0) / d_model))
        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):
        x = x + Variable(self.pe[:, :x.size(1)],
                         requires_grad=False)
        return self.dropout(x)

max_stars_repo_licenses: FTL CNRI-Python

max_issues_repo_licenses: FTL CNRI-Python

max_forks_repo_licenses: FTL CNRI-Python


text:
#!/usr/bin/python3

import numpy as np
import pandas as pd

from matplotlib import pyplot as plt
from matplotlib.patches import Patch

from matplotlib import cm
from matplotlib import colors as mcolors

# LOLH objects

import sys
sys.path.append('../../python')

from instance import Instance
from solver import Solver

import visualizer
import histogram

# import the cell types
file_name = '../../dataset/Imagine/cell_types.csv'
df_cell_types = pd.read_csv(file_name, index_col=0)
df_cell_types.rename(columns={'cellType_final': 'Label'}, inplace=True)

# import the UMAP 2d representation
file_name = '../../dataset/Imagine/umap_coordinates.csv'
umap_coordinates = pd.read_csv(file_name, index_col = 0)

# import the single cell discretized matrix
file_name = '../../dataset/Imagine/discrete_matrix.csv'
df = pd.read_csv(file_name, index_col=0)

# creation of the classification instance
cell_type = 'NK'
instance = Instance.create_cluster_instance(df.copy(deep=False), df_cell_types, cell_type)

positive_cells = instance._pos_samples
negative_cells = instance._neg_samples

score_thresold = 0.55

# compute the body from the score threshold
solver = Solver(instance)
LOLH_rule, _ = solver.select_best_atoms_threshold(score_thresold)

print([instance.get_atom(index) for index in LOLH_rule])

max_stars_repo_name: smbct/LOLH

max_issues_repo_name: smbct/LOLH

max_forks_repo_name: smbct/LOLH


text:
#################################
# Joel Anyanti | 06/20/2018
# Carnegie Mellon University
#################################
#################################
# imports
#################################
import numpy as np
from sklearn.datasets import load_iris
from sklearn.datasets import load_breast_cancer
import matplotlib.pyplot as plt
from drawnow import *
from tensorflow.examples.tutorials.mnist import input_data

np.random.seed(23)
# mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
#
# X_train = np.vstack([img.reshape(-1,) for img in mnist.train.images])
# y_train = mnist.train.labels
# print(y_train.shape)
#
# X_train = X_train.T
# y_train = y_train.T
# print(X_train.shape, y_train.shape)

# with open('data.txt', 'w') as file:
#     file.write('X:' + str(X_train))
#     file.write('y' + str(y_train))
#     file.close()

"""
'model' defines the characteristics of the nueral network
the first number in the list is the number of input neurons
or features in the nueral network. The last number in the list is
the number of desired output neurons. The numbers in the middle
represent the neural networks hideen layers. This model will allow
us to define the paramaters of the neural network"""

#################################
# Model Variables
#################################
# data = load_iris()
data = load_breast_cancer()
# print(data2.data.shape, data2.target.shape)
X = data.data
X = X.T

# test = [[1,0,0],[0,1,0],[0,0,1]]
# test = np.array(test, dtype='float')

# Number of training examples
# m = X_train.shape[1]
# Y = np.reshape(data.target, (1,m))
# number of input/output neurons
# n_x = X_train.shape[0]
# n_y = Y.shape[0]
n_y = 10

data_1 = load_iris()
X_train_1 = data_1.data
X_train_1 = X_train_1.T
m = X_train_1.shape[1]
Y_train_1 = np.reshape(data_1.target.T, (1,m))
n_x = X_train_1.shape[0]
n_y = 3                                 # set value for number of output neurons
model_1 = [n_x, 4, 6, 4, n_y]
# nueral network model
model = [n_x, 6, 8, 5, n_y]

# number of layers
layers = len(model) - 1

#################################
# Activation Functions
#################################

# activation function that returns a value between 0 and 1
def sigmoid(Z):
    return (1/(1+np.exp(-Z)))

# activation function that returns a value between -1 and 1
def tanh(Z):
    return np.tanh(Z)

# activation function that returns the identity of input or 0 if less than 0
def relu(Z):
    return np.maximum(0,Z)

# activation function for multiclass outputs | returns probability measure
def softmax(Z):
    max = np.apply_along_axis(np.max, 0, Z)
    ex = np.exp(Z)
    return ex/np.sum(ex, axis=0)

activations = {
    'sigmoid' :sigmoid,
    'tanh' : tanh,
    'relu' : relu,
    'softmax' : softmax
}

#################################
# Activation Function Derivatives
#################################

# returns derivative of sigmoid function
def sigmoid_prime(Z):
    return sigmoid(Z)*(1 - sigmoid(Z))

# returns derivative of tanh function
def tanh_prime(Z):
    return 1 - np.square(tanh(Z))

# returns derivative of relu function
def relu_prime(Z):
    Z[np.isnan(Z)] = 0
    Z[np.isinf(Z)] = 1
    Z[Z<=0] = 0
    Z[Z>0] = 1
    return Z

activation_derivatives = {
    'sigmoid' :sigmoid_prime,
    'tanh' : tanh_prime,
    'relu' : relu_prime
}

##############################
# Loss Functions
##############################
# reimpliment if results are not good
"""
Y_h denotes the output of the neural network
Y denotes the labels(correct) predictions
"""

# returns the cost of the neural network using L2 Loss
def squared_error(Y_h, Y):
    m = Y.shape[1]
    square_comp = np.square(Y_h, Y)/2
    sum_layers = np.sum(square_comp, axis=0)
    mean = np.mean(sum_layers)
    return np.squeeze(mean)

# returns the cost of the neural network using Log Loss
def log_loss(Y_h, Y):
    # print(Y_h, Y)
    m = Y.shape[1]
    Y_h[Y_h<=0] = 10**-8
    # log_comp = log_comparison(Y_h, Y) + log_comparison(1-Y_h, 1-Y)
    log_comp =  np.multiply(Y, np.log(Y_h)) + np.multiply((1-Y), np.log((1-Y_h)))
    sum_layers = - np.sum(log_comp, axis=0)
    mean = np.mean(sum_layers)
    return np.squeeze(mean)

def log_comparison(Y_h, Y):
    rows, cols = Y.shape[0], Y.shape[1]
    result = np.zeros((rows, cols))
    for row in range(rows):
        for col in range(cols):
            y_h, y = Y_h[row, col], Y[row, col]
            if y == 0:
                continue
            else:
                try:
                    comp = np.multiply(y, np.log(y_h))
                except:
                    # print(y_h)
                    comp = np.multiply(y, np.log(y_h+0.00001))
                result[row, col] = comp
    return result

# returns the cost of the neural network using Cross Entropy
def cross_entropy(Y_h, Y):
    m = Y.shape[1]
    log_comp = np.multiply(Y, np.log(Y_h))
    sum_layers = - np.sum(log_comp, axis=0)
    mean = np.mean(sum_layers)
    return np.squeeze(mean)

losses = {
    'squared_error': squared_error,
    'cross_entropy': cross_entropy,
    'log_loss': log_loss
}

##############################
# Output Derivatives Functions
##############################

def output_error(Y_h, Y, loss_function='log_loss'):
    loss_string = error_command[loss_function]
    command_string = loss_string %('Y_h', 'Y')
    result = eval(command_string)
    return result

error_command = {
    'log_loss': '%s - %s',
    'cross_entropy': '(1 - %s) * %s'
}

# returns a dictionary of parameters (weight, biases) accoridng to model
def define_parameters(model):
    paramaters = dict()
    layers = len(model)-1
    n_temp = n_x # number of neurons in the previous layer
    for i in range (layers):
        # define dictionary keys for each weight/bias matrix accoridng to layer
        key_w, key_b = ('W' + str(i+1)),('b' + str(i+1))
        n_h = model[i+1] # depth of the layer
        weights = np.random.randn(n_h, n_temp)
        biases = np.random.randn(n_h, 1)
        paramaters[key_w] = weights
        paramaters[key_b] = biases
        n_temp = n_h
    return paramaters

def one_hot_encoder(Y, n):
    m = Y.shape[1]
    result = np.zeros((n,m))
    for i in range(m):
        val = Y[0,i]
        result[val,i] = 1
    return result
relu
# performs forward propagtion
def forward_propagate(X, parameters, layers, a_function='relu', o_function='softmax'):
    activate_h = activations[a_function] # activation function for hidden layers
    activate_o = activations[o_function] # activation function for output layer
    A = X # current layer matrix(activation)
    m = X.shape[1]
    neuron_cache = dict()
    neuron_cache['A0'] = X
    neuron_cache['activation'] = a_function
    neuron_cache['output'] = o_function
    for i in range (layers):
        pos = str(i+1)
        # define dictionary keys for each neuron/activation matrix accoridng to layer
        key_z, key_a = ('Z' + pos), ('A' + pos)
        key_w, key_b = ('W' + pos),('b' + pos) # used to access paramaters for network
        W, b = parameters[key_w], parameters[key_b]
        Z = np.dot(W, A) + b
        if not i == (layers - 1):  # conditional on if hidden/output layers is being calculated
            A = activate_h(Z)
        else:
            A = activate_o(Z)
        neuron_cache[key_z], neuron_cache[key_a] = Z, A # save outputs into nueron_cache
    neuron_cache.update(parameters)
    assert(A.shape[1] == m)
    assert(A.shape[0] == n_y)
    return A, neuron_cache # final output and neuron_cache returned

def calculate_cost(Y, data, layers, loss_function='log_loss'):
    Y_h, cache = data
    cost_function = losses[loss_function]
    return cost_function(Y_h, Y)

def backwards_propagate(X, Y, cache, layers,  l_function='log_loss'):
    Y_h, cache = cache
    a_derivative = activation_derivatives[cache['activation']]
    #o_derivative = activation_derivatives[cache['output']]
    gradient_updates = dict()
    for i in range(layers, 0, -1):
        pos = i
        if (i == layers):
            dZ =  output_error(Y_h, Y, l_function)
            E = dZ # donetes error
            gradient = backwards_propagate_helper(X, Y, cache, pos, dZ)
        else:
            key_W, key_Z = 'W' + str(pos+1) , 'Z' + str(pos)
            W, Z = cache[key_W], cache[key_Z]
            g_prime_Z = a_derivative(Z)
            dZ = np.dot(W.T, E) * g_prime_Z
            E = dZ
            gradient = backwards_propagate_helper(X, Y, cache, pos, dZ)
        gradient_updates.update(gradient)
    return gradient_updates

# performs forward propagtion
def backwards_propagate_helper(X, Y, cache, pos, dZ):
    m = X.shape[1]
    key_db, key_dW = 'db' + str(pos), 'dW' + str(pos)
    key_A = 'A' + str(pos-1)
    A = cache[key_A]
    dW = np.dot(dZ, A.T)/m
    db = np.sum(dZ, axis=1, keepdims=True)/m
    result = dict([(key_dW, dW), (key_db, db)])
    return result

def update_params(params, gradients, layers, learning_rate=0.0005):
    alpha = learning_rate
    params_keys = set(params.keys())
    for (key, value) in gradients.items():
        key_params = key[1:]
        if (key_params in params_keys):
            params[key_params] = params[key_params] - alpha * value
    return params

# X = np.random.randn(n_x, m)
# Y = np.random.randn(model[-1],m)

params = define_parameters(model_1)
Y_train_1 = one_hot_encoder(Y_train_1, n_y)
cost_list = []
             #Set y min and max values based on known voltage quantity
plt.grid(True)
plt.xlabel("iterations")
# plt.scatter(cost_list, '-', label='Cost'
plt.axis([0, 50000, 0, 10])
plt.ion()

print(X_train_1)
for i in range (50000):
    cost_list = []
    index = []
    data = forward_propagate(X_train_1, params, layers)
    cost = calculate_cost(Y_train_1, data, layers)
    grads = backwards_propagate(X_train_1, Y_train_1, data, layers)
    params = update_params(params, grads, layers)
    if (i % 1000 == 0) :
        print(cost)
        plt.scatter(i, 2*cost)
        plt.pause(0.002)
    plt.show()
print(cost)
print(params)

#result = output_error(data[0], Y)
#print(result)
#print(cost)

"""
print('Relu:', relu_prime(L1))
print('Sigmoid', sigmoid_prime(L1))
print('Tanh', tanh_prime(L1))"""
#print(data)

max_stars_repo_name: janyanti/OpenML

max_issues_repo_name: janyanti/OpenML

max_forks_repo_name: janyanti/OpenML


text:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
from mayavi import mlab

def cusp_bi(x,r,h):
    return(h - r*x - x**3)
mlab.figure(size=(800,700))
# mlab.axes()
x,r , h = np.mgrid[-20:20:200j, -40:40:200j, -40:40:200j]
cusp = mlab.contour3d(x,r,h, cusp_bi,transparent=True)
boundaries = (-15,15,-40,40,-40,40)
mlab.axes(cusp,
                    extent=boundaries)
mlab.outline(cusp, extent=boundaries)
mlab.pipeline.vector_cut_plane(cusp,
                                                scale_factor=1,
                                                mode='2darrow',
                                                opacity=0.8,
                                                transparent=False,
                                                plane_orientation='y_axes')
plt.show()

max_stars_repo_name: yuchiaol/Non-linear-dynamics-Strogatz


text:
import unittest

import numpy as np

import openmdao.api as om
import dymos as dm

from openmdao.utils.assert_utils import assert_check_partials, assert_near_equal

from dymos.examples.brachistochrone.brachistochrone_ode import BrachistochroneODE

class Simple2StateODE(om.ExplicitComponent):
    """
    A simple ODE from https://math.okstate.edu/people/yqwang/teaching/math4513_fall11/Notes/rungekutta.pdf
    """
    def initialize(self):
        self.options.declare('num_nodes', types=(int,))

    def setup(self):
        nn = self.options['num_nodes']
        self.add_input('x', shape=(nn,), units='s**2')
        self.add_input('y', shape=(nn,), units='s')
        self.add_input('t', shape=(nn,), units='s')
        self.add_input('p', shape=(nn,), units='s**2')

        self.add_output('x_dot', shape=(nn,), units='s')
        self.add_output('y_dot', shape=(nn,), units=None)

        ar = np.arange(nn, dtype=int)
        self.declare_partials(of='x_dot', wrt='x', rows=ar, cols=ar, val=1.0)
        self.declare_partials(of='x_dot', wrt='t', rows=ar, cols=ar)
        self.declare_partials(of='x_dot', wrt='p', rows=ar, cols=ar, val=1.0)

        self.declare_partials(of='y_dot', wrt='y', rows=ar, cols=ar, val=-2.0)
        self.declare_partials(of='y_dot', wrt='t', rows=ar, cols=ar, val=1.0)

    def compute(self, inputs, outputs):
        x = inputs['x']
        y = inputs['y']
        t = inputs['t']
        p = inputs['p']
        outputs['x_dot'] = x - t**2 + p
        outputs['y_dot'] = -2 * y + t**3 * np.exp(-2*t)

    def compute_partials(self, inputs, partials):
        t = inputs['t']
        partials['x_dot', 't'] = -2*t
        partials['y_dot', 't'] = -np.exp(-2*t) * t**2 * (2 * t - 3)

class Simple1StateODE(om.ExplicitComponent):
    """
    A simple ODE from https://math.okstate.edu/people/yqwang/teaching/math4513_fall11/Notes/rungekutta.pdf
    """
    def initialize(self):
        self.options.declare('num_nodes', types=(int,))

    def setup(self):
        nn = self.options['num_nodes']
        self.add_input('y', shape=(nn,), units=None)
        self.add_input('t', shape=(nn,), units='s')

        self.add_output('y_dot', shape=(nn,), units='1/s')

        ar = np.arange(nn, dtype=int)

        self.declare_partials(of='y_dot', wrt='y', rows=ar, cols=ar, val=-2.0)
        self.declare_partials(of='y_dot', wrt='t', rows=ar, cols=ar, val=1.0)

    def compute(self, inputs, outputs):
        y = inputs['y']
        t = inputs['t']
        outputs['y_dot'] = -2 * y + t**3 * np.exp(-2*t)

    def compute_partials(self, inputs, partials):
        t = inputs['t']
        partials['y_dot', 't'] = -np.exp(-2*t) * t**2 * (2 * t - 3)

class TestExplicitShooting(unittest.TestCase):

    def test_1_state_run_model(self):
        prob = om.Problem()

        tx = dm.transcriptions.ExplicitShooting(num_segments=1, grid='gauss-lobatto', method='rk4',
                                                order=3, num_steps_per_segment=10, compressed=True)

        phase = dm.Phase(ode_class=Simple1StateODE, transcription=tx)

        phase.set_time_options(targets=['t'], units='s')

        # automatically discover states
        phase.set_state_options('y', targets=['y'], rate_source='y_dot')

        prob.model.add_subsystem('phase0', phase)

        prob.setup(force_alloc_complex=False)

        prob.set_val('phase0.t_initial', 0.0)
        prob.set_val('phase0.t_duration', 1.0)
        prob.set_val('phase0.states:y', 1.0)

        prob.run_model()

    def test_2_states_run_model(self):

        for method in ['rk4', 'euler', '3/8', 'ralston', 'rkf', 'rkck', 'dopri']:
            with self.subTest(f"test brachistochrone explicit shooting with method '{method}'"):

                prob = om.Problem()

                tx = dm.transcriptions.ExplicitShooting(num_segments=1, grid='gauss-lobatto',
                                                        method='rk4', order=3,
                                                        num_steps_per_segment=100, compressed=True)

                phase = dm.Phase(ode_class=Simple2StateODE, transcription=tx)

                phase.set_time_options(targets=['t'], units='s')

                # automatically discover states
                phase.set_state_options('x', targets=['x'], rate_source='x_dot')
                phase.set_state_options('y', targets=['y'], rate_source='y_dot')

                phase.add_parameter('p', targets=['p'])

                prob.model.add_subsystem('phase0', phase)

                prob.setup(force_alloc_complex=True)

                prob.set_val('phase0.t_initial', 0.0)
                prob.set_val('phase0.t_duration', 1.0)
                prob.set_val('phase0.states:x', 0.5)
                prob.set_val('phase0.states:y', 1.0)
                prob.set_val('phase0.parameters:p', 1)

                prob.run_model()

                t_f = prob.get_val('phase0.integrator.t_final')
                x_f = prob.get_val('phase0.integrator.states_out:x')
                y_f = prob.get_val('phase0.integrator.states_out:y')

                assert_near_equal(t_f, 1.0)
                assert_near_equal(x_f, 2.64085909, tolerance=1.0E-5)
                assert_near_equal(y_f, 0.1691691, tolerance=1.0E-5)

                with np.printoptions(linewidth=1024):
                    cpd = prob.check_partials(compact_print=True, method='cs')
                    assert_check_partials(cpd, atol=1.0E-5, rtol=1.0E-5)

    def test_brachistochrone_explicit_shooting_run_model(self):
        prob = om.Problem()

        prob.driver = om.pyOptSparseDriver(optimizer='SNOPT')
        prob.driver.opt_settings['Verify level'] = 3
        prob.driver.opt_settings['iSumm'] = 6

        tx = dm.transcriptions.ExplicitShooting(num_segments=5, grid='gauss-lobatto',
                                                order=5, num_steps_per_segment=10, compressed=True)

        phase = dm.Phase(ode_class=BrachistochroneODE, transcription=tx)

        phase.set_time_options(units='s', fix_initial=True, duration_bounds=(1.0, 10.0))

        # automatically discover states
        phase.set_state_options('x', fix_initial=True)
        phase.set_state_options('y', fix_initial=True)
        phase.set_state_options('v', fix_initial=True)

        phase.add_parameter('g', val=9.80665, units='m/s**2', opt=False)
        phase.add_control('theta', val=45.0, units='deg', opt=True, lower=1.0E-6, upper=179.9)

        phase.add_boundary_constraint('x', loc='final', equals=10.0)
        phase.add_boundary_constraint('y', loc='final', equals=5.0)

        prob.model.add_subsystem('phase0', phase)

        phase.add_objective('time', loc='final')

        prob.setup(force_alloc_complex=True)

        prob.set_val('phase0.t_initial', 0.0)
        prob.set_val('phase0.t_duration', 2)
        prob.set_val('phase0.states:x', 0.0)
        prob.set_val('phase0.states:y', 10.0)
        prob.set_val('phase0.states:v', 1.0E-6)
        prob.set_val('phase0.parameters:g', 9.80665, units='m/s**2')
        prob.set_val('phase0.controls:theta', phase.interp('theta', ys=[0.01, 90]), units='deg')

        prob.run_model()

        with np.printoptions(linewidth=1024):
            cpd = prob.check_partials(compact_print=True, method='fd')
            assert_check_partials(cpd, atol=1.0E-5, rtol=1.0E-5)

            subprob = prob.model._get_subsystem('phase0.integrator')._prob
            with np.printoptions(linewidth=1024):
                cpd = subprob.check_partials(compact_print=True, method='cs')
            assert_check_partials(cpd, atol=1.0E-5, rtol=1.0E-5)

    def test_brachistochrone_explicit_shooting(self):

        for method in ['rk4', 'ralston']:
            with self.subTest(f"test brachistochrone explicit shooting with method '{method}'"):
                prob = om.Problem()

                prob.driver = om.pyOptSparseDriver(optimizer='SLSQP')

                tx = dm.transcriptions.ExplicitShooting(num_segments=3, grid='gauss-lobatto', method='rk4',
                                                        order=3, num_steps_per_segment=10, compressed=True)

                phase = dm.Phase(ode_class=BrachistochroneODE, transcription=tx)

                phase.set_time_options(units='s', fix_initial=True, duration_bounds=(1.0, 10.0))

                # automatically discover states
                phase.set_state_options('x', fix_initial=True)
                phase.set_state_options('y', fix_initial=True)
                phase.set_state_options('v', fix_initial=True)

                phase.add_parameter('g', val=1.0, units='m/s**2', opt=True, lower=1, upper=9.80665)
                phase.add_control('theta', val=45.0, units='deg', opt=True, lower=1.0E-6, upper=179.9)

                phase.add_boundary_constraint('x', loc='final', equals=10.0)
                phase.add_boundary_constraint('y', loc='final', equals=5.0)

                prob.model.add_subsystem('phase0', phase)

                phase.add_objective('time', loc='final')

                prob.setup(force_alloc_complex=True)

                prob.set_val('phase0.t_initial', 0.0)
                prob.set_val('phase0.t_duration', 2)
                prob.set_val('phase0.states:x', 0.0)
                prob.set_val('phase0.states:y', 10.0)
                prob.set_val('phase0.states:v', 1.0E-6)
                prob.set_val('phase0.parameters:g', 1.0, units='m/s**2')
                prob.set_val('phase0.controls:theta', phase.interp('theta', ys=[0.01, 90]), units='deg')

                prob.run_driver()

                x_f = prob.get_val('phase0.integrator.states_out:x')
                y_f = prob.get_val('phase0.integrator.states_out:y')
                t_f = prob.get_val('phase0.integrator.t_final')

                assert_near_equal(x_f, 10.0, tolerance=1.0E-3)
                assert_near_equal(y_f, 5.0, tolerance=1.0E-3)
                assert_near_equal(t_f, 1.8016, tolerance=1.0E-2)

                with np.printoptions(linewidth=1024):
                    cpd = prob.check_partials(compact_print=True, method='cs', out_stream=None)
                    assert_check_partials(cpd, atol=1.0E-5, rtol=1.0E-5)

    def test_brachistochrone_explicit_shooting_polynomial_control(self):
        prob = om.Problem()

        prob.driver = om.pyOptSparseDriver(optimizer='SLSQP')

        tx = dm.transcriptions.ExplicitShooting(num_segments=3, grid='radau-ps', method='rk4',
                                                order=3, num_steps_per_segment=10, compressed=True)

        phase = dm.Phase(ode_class=BrachistochroneODE, transcription=tx)

        phase.set_time_options(units='s', fix_initial=True, duration_bounds=(1.0, 10.0))

        # automatically discover states
        phase.set_state_options('x', fix_initial=True)
        phase.set_state_options('y', fix_initial=True)
        phase.set_state_options('v', fix_initial=True)

        phase.add_parameter('g', val=1.0, units='m/s**2', opt=True, lower=1, upper=9.80665)
        phase.add_polynomial_control('theta', order=9, val=45.0, units='deg', opt=True, lower=1.0E-6, upper=179.9)

        phase.add_boundary_constraint('x', loc='final', equals=10.0)
        phase.add_boundary_constraint('y', loc='final', equals=5.0)

        prob.model.add_subsystem('phase0', phase)

        phase.add_objective('time', loc='final')

        prob.setup(force_alloc_complex=True)

        prob.set_val('phase0.t_initial', 0.0)
        prob.set_val('phase0.t_duration', 2)
        prob.set_val('phase0.states:x', 0.0)
        prob.set_val('phase0.states:y', 10.0)
        prob.set_val('phase0.states:v', 1.0E-6)
        prob.set_val('phase0.parameters:g', 1.0, units='m/s**2')
        prob.set_val('phase0.polynomial_controls:theta', phase.interp('theta', ys=[0.01, 90]), units='deg')

        prob.run_driver()

        x_f = prob.get_val('phase0.integrator.states_out:x')
        y_f = prob.get_val('phase0.integrator.states_out:y')
        t_f = prob.get_val('phase0.integrator.t_final')

        assert_near_equal(x_f, 10.0, tolerance=1.0E-5)
        assert_near_equal(y_f, 5.0, tolerance=1.0E-5)
        assert_near_equal(t_f, 1.8016, tolerance=5.0E-3)

        with np.printoptions(linewidth=1024):
            cpd = prob.check_partials(compact_print=True, method='cs', out_stream=None)
            assert_check_partials(cpd, atol=1.0E-5, rtol=1.0E-5)

max_stars_repo_name: kaushikponnapalli/dymos

max_issues_repo_name: kaushikponnapalli/dymos

max_forks_repo_name: kaushikponnapalli/dymos


text:
import os
import sys
import torch
import torch.optim as optim
import logging
import numpy as np
import argparse
from tqdm import tqdm
from tensorboardX import SummaryWriter
from .eval import meta_test
sys.path.append('..')
from datasets import dataloaders

def get_logger(filename):

    formatter = logging.Formatter(
        "[%(asctime)s] %(message)s",datefmt='%m/%d %I:%M:%S')
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    fh = logging.FileHandler(filename,"w")
    fh.setFormatter(formatter)
    logger.addHandler(fh)

    sh = logging.StreamHandler()
    sh.setFormatter(formatter)
    logger.addHandler(sh)

    return logger

def train_parser():

    parser = argparse.ArgumentParser()

    ## general hyper-parameters
    parser.add_argument("--opt",help="optimizer",choices=['adam','sgd'])
    parser.add_argument("--lr",help="initial learning rate",type=float)
    parser.add_argument("--gamma",help="learning rate cut scalar",type=float,default=0.1)
    parser.add_argument("--epoch",help="number of epochs before lr is cut by gamma",type=int)
    parser.add_argument("--stage",help="number lr stages",type=int)
    parser.add_argument("--weight_decay",help="weight decay for optimizer",type=float)
    parser.add_argument("--gpu",help="gpu device",type=int,default=0)
    parser.add_argument("--seed",help="random seed",type=int,default=42)
    parser.add_argument("--val_epoch",help="number of epochs before eval on val",type=int,default=20)
    parser.add_argument("--resnet", help="whether use resnet12 as backbone or not",action="store_true")
    parser.add_argument("--nesterov",help="nesterov for sgd",action="store_true")
    parser.add_argument("--batch_size",help="batch size used during pre-training",type=int)
    parser.add_argument('--decay_epoch',nargs='+',help='epochs that cut lr',type=int)
    parser.add_argument("--pre", help="whether use pre-resized 84x84 images for val and test",action="store_true")
    parser.add_argument("--no_val", help="don't use validation set, just save model at final timestep",action="store_true")
    parser.add_argument("--train_way",help="training way",type=int)
    parser.add_argument("--test_way",help="test way",type=int,default=5)
    parser.add_argument("--train_shot",help="number of support images per class for meta-training and meta-testing during validation",type=int)
    parser.add_argument("--test_shot",nargs='+',help="number of support images per class for meta-testing during final test",type=int)
    parser.add_argument("--train_query_shot",help="number of query images per class during meta-training",type=int,default=15)
    parser.add_argument("--test_query_shot",help="number of query images per class during meta-testing",type=int,default=16)
    parser.add_argument("--train_transform_type",help="size transformation type during training",type=int)
    parser.add_argument("--test_transform_type",help="size transformation type during inference",type=int)
    parser.add_argument("--val_trial",help="number of meta-testing episodes during validation",type=int,default=1000)
    parser.add_argument("--detailed_name", help="whether include training details in the name",action="store_true")

    args = parser.parse_args()

    return args

def get_opt(model,args):

    if args.opt == 'adam':
        optimizer = optim.Adam(model.parameters(),lr=args.lr,weight_decay=args.weight_decay)
    elif args.opt == 'sgd':
        optimizer = optim.SGD(model.parameters(),lr=args.lr,momentum=0.9,weight_decay=args.weight_decay,nesterov=args.nesterov)

    if args.decay_epoch is not None:
        scheduler = optim.lr_scheduler.MultiStepLR(optimizer,milestones=args.decay_epoch,gamma=args.gamma)

    else:
        scheduler = optim.lr_scheduler.StepLR(optimizer,step_size=args.epoch,gamma=args.gamma)

    return optimizer,scheduler

class Path_Manager:

    def __init__(self,fewshot_path,args):

        self.train = os.path.join(fewshot_path,'train')

        if args.pre:
            self.test = os.path.join(fewshot_path,'test_pre')
            self.val = os.path.join(fewshot_path,'val_pre') if not args.no_val else self.test

        else:
            self.test = os.path.join(fewshot_path,'test')
            self.val = os.path.join(fewshot_path,'val') if not args.no_val else self.test

class Train_Manager:

    def __init__(self,args,path_manager,train_func):

        seed = args.seed
        torch.manual_seed(seed)
        torch.cuda.manual_seed(seed)
        np.random.seed(seed)
        torch.cuda.set_device(args.gpu)

        if args.resnet:
            name = 'ResNet-12'
        else:
            name = 'Conv-4'

        if args.detailed_name:
            if args.decay_epoch is not None:
                temp = ''
                for i in args.decay_epoch:
                    temp += ('_'+str(i))

                suffix = '%s-lr_%.0e-gamma_%.0e-epoch_%d-drop%s-decay_%.0e-way_%d' % (args.opt,
                    args.lr,args.gamma,args.epoch,temp,args.weight_decay,args.train_way)
            else:
                suffix = '%s-lr_%.0e-gamma_%.0e-epoch_%d-stage_%d-decay_%.0e-way_%d' % (args.opt,
                    args.lr,args.gamma,args.epoch,args.stage,args.weight_decay,args.train_way)

            name = "%s-%s"%(name,suffix)

        self.logger = get_logger('%s.log' % (name))
        self.save_path = 'model_%s.pth' % (name)
        self.writer = SummaryWriter('log_%s' % (name))

        self.logger.info('display all the hyper-parameters in args:')
        for arg in vars(args):
            value = getattr(args,arg)
            if value is not None:
                self.logger.info('%s: %s' % (str(arg),str(value)))
        self.logger.info('------------------------')
        self.args = args
        self.train_func = train_func
        self.pm = path_manager

    def train(self,model):

        args = self.args
        train_func = self.train_func
        writer = self.writer
        save_path = self.save_path
        logger = self.logger

        optimizer,scheduler = get_opt(model,args)

        val_shot = args.train_shot
        test_way = args.test_way

        best_val_acc = 0
        best_epoch = 0

        model.train()
        model.cuda()

        iter_counter = 0

        if args.decay_epoch is not None:
            total_epoch = args.epoch
        else:
            total_epoch = args.epoch*args.stage

        logger.info("start training!")

        for e in tqdm(range(total_epoch)):

            iter_counter,train_acc = train_func(model=model,
                                                optimizer=optimizer,
                                                writer=writer,
                                                iter_counter=iter_counter)

            if (e+1)%args.val_epoch==0:

                logger.info("")
                logger.info("epoch %d/%d, iter %d:" % (e+1,total_epoch,iter_counter))
                logger.info("train_acc: %.3f" % (train_acc))

                model.eval()
                with torch.no_grad():
                    val_acc,val_interval = meta_test(data_path=self.pm.val,
                                                    model=model,
                                                    way=test_way,
                                                    shot=val_shot,
                                                    pre=args.pre,
                                                    transform_type=args.test_transform_type,
                                                    query_shot=args.test_query_shot,
                                                    trial=args.val_trial)
                    writer.add_scalar('val_%d-way-%d-shot_acc'%(test_way,val_shot),val_acc,iter_counter)

                logger.info('val_%d-way-%d-shot_acc: %.3f\t%.3f'%(test_way,val_shot,val_acc,val_interval))

                if val_acc > best_val_acc:
                    best_val_acc = val_acc
                    best_epoch = e+1
                    if not args.no_val:
                        torch.save(model.state_dict(),save_path)
                    logger.info('BEST!')

                model.train()

            scheduler.step()

        logger.info('training finished!')
        if args.no_val:
            torch.save(model.state_dict(),save_path)

        logger.info('------------------------')
        logger.info(('the best epoch is %d/%d') % (best_epoch,total_epoch))
        logger.info(('the best %d-way %d-shot val acc is %.3f') % (test_way,val_shot,best_val_acc))

    def evaluate(self,model):

        logger = self.logger
        args = self.args

        logger.info('------------------------')
        logger.info('evaluating on test set:')

        with torch.no_grad():

            model.load_state_dict(torch.load(self.save_path))
            model.eval()

            for shot in args.test_shot:

                mean,interval = meta_test(data_path=self.pm.test,
                                        model=model,
                                        way=args.test_way,
                                        shot=shot,
                                        pre=args.pre,
                                        transform_type=args.test_transform_type,
                                        query_shot=args.test_query_shot,
                                        trial=10000)

                logger.info('%d-way-%d-shot acc: %.2f\t%.2f'%(args.test_way,shot,mean,interval))


text:
#################################################################################
# The Institute for the Design of Advanced Energy Systems Integrated Platform
# Framework (IDAES IP) was produced under the DOE Institute for the
# Design of Advanced Energy Systems (IDAES), and is copyright (c) 2018-2021
# by the software owners: The Regents of the University of California, through
# Lawrence Berkeley National Laboratory,  National Technology & Engineering
# Solutions of Sandia, LLC, Carnegie Mellon University, West Virginia University
# Research Corporation, et al.  All rights reserved.
#
# Please see the files COPYRIGHT.md and LICENSE.md for full copyright and
# license information.
#################################################################################
__all__ = ['Parallelepiped', 'RectPrism', 'Cube', 'Rhombohedron', 'Cuboctahedron', 'Cylinder', 'CylindricalSector']

from abc import abstractmethod, ABC
from copy import deepcopy
from math import cos, sin, sqrt

import numpy as np

from .transform_func import ShiftFunc, ScaleFunc, RotateFunc, ReflectFunc
from ..util.util import areEqual

class Shape(object):
    """ """
    DBL_TOL = 1e-5
    DEFAULT_ALIGNMENT = np.array([[1, 0, 0],
                                  [0, 1, 0],
                                  [0, 0, 1]], dtype=float)

    # === STANDARD CONSTRUCTOR
    def __init__(self, Anchor, Alignment=None):
        self._Anchor = Anchor
        self._Alignment = (Shape.DEFAULT_ALIGNMENT if Alignment is None else Alignment)

    # === ASSERTION OF CLASS DESIGN
    def isConsistentWithDesign(self):
        """ """
        if self.Anchor is None:
            print('alpha')
            return False
        if type(self.Alignment) is not np.ndarray:
            print('A')
            return False
        if self.Alignment.shape != (3, 3):
            print('B')
            return False
        if not areEqual(np.linalg.det(self.Alignment), 1.0, Shape.DBL_TOL):
            print('C')
            return False
        for AlignmentAxis in self.Alignment:
            if not areEqual(np.linalg.norm(AlignmentAxis), 1.0, Shape.DBL_TOL):
                print('D')
                return False
        return True

    # === MANIPULATION METHODS
    def applyTransF(self, TransF):
        """

        Args:
            TransF:

        Returns:

        """
        if (type(TransF) is ShiftFunc or
                type(TransF) is ScaleFunc):
            TransF.transform(self._Anchor)
        elif (type(TransF) is RotateFunc or
              type(TransF) is ReflectFunc):
            TransF.transform(self._Anchor)
            TransF.transform(self._Alignment)
            assert (self.isConsistentWithDesign())
        else:
            raise TypeError
        assert (self.isConsistentWithDesign())

    def shift(self, Shift):
        """

        Args:
            Shift:

        Returns:

        """
        if type(Shift) is ShiftFunc:
            self.applyTransF(Shift)
        elif type(Shift) is np.ndarray:
            self.applyTransF(ShiftFunc(Shift))
        else:
            raise TypeError

    def scale(self, Scale, OriginOfScale=None):
        """

        Args:
            Scale: param OriginOfScale:  (Default value = None)
            OriginOfScale:  (Default value = None)

        Returns:

        """
        if type(Scale) is ScaleFunc:
            self.applyTransF(Scale)
        elif type(Scale) is np.ndarray:
            self.applyTransF(ScaleFunc(Scale, OriginOfScale))
        elif (type(Scale) is float or
              type(Scale) is int):
            self.applyTransF(ScaleFunc(np.array([Scale, Scale, Scale], dtype=float)))
        else:
            raise TypeError

    def rotate(self, Rotation, OriginOfRotation=None):
        """

        Args:
            Rotation: param OriginOfRotation:  (Default value = None)
            OriginOfRotation:  (Default value = None)

        Returns:

        """
        if type(Rotation) is RotateFunc:
            self.applyTransF(Rotation)
        elif type(Rotation) is np.ndarray:
            self.applyTransF(RotateFunc(Rotation, OriginOfRotation))
        else:
            raise TypeError

    def reflect(self, Reflection):
        """

        Args:
            Reflection:

        Returns:

        """
        if type(Reflection) is ReflectFunc:
            self.applyTransF(Reflection)
        else:
            raise TypeError

    # === PROPERTY EVALUATION METHODS
    @abstractmethod
    def isInShape(self, P):
        """

        Args:
            P:

        Returns:

        """
        raise NotImplementedError

    __contains__ = isInShape

    @abstractmethod
    def getBounds(self):
        """ """
        raise NotImplementedError

    # === BASIC QUERY METHODS
    @property
    def Anchor(self):
        """ """
        return self._Anchor

    @property
    def Alignment(self):
        """ """
        return self._Alignment

class Polyhedron(Shape, ABC):
    """ """

    # === STANDARD CONSTRUCTOR
    def __init__(self, V, F, Anchor, Alignment=None):
        Shape.__init__(self, Anchor, Alignment)
        self._V = V
        self._F = F
        self._FacetNorms = self.__calcFacetNorms()
        self._FacetDirections = self.__calcFacetDirections()
        assert (self.isConsistentWithDesign())

    # === ASSERTION OF CLASS DESIGN
    def isConsistentWithDesign(self):
        """ """
        if type(self.V) is not list:
            return False
        if len(self.V) < 4:
            return False
        if type(self.F) is not list:
            return False
        if len(self.F) < 4:
            return False
        for Facet in self.F:
            if type(Facet) is not list:
                return False
            if len(Facet) < 3:
                return False
        return Shape.isConsistentWithDesign(self)

    # === AUXILIARY METHODS
    def __calcFacetNorms(self):
        FacetNorms = []
        for Facet in self.F:
            Norm = np.cross(self.V[Facet[1]] - self.V[Facet[0]],
                            self.V[Facet[2]] - self.V[Facet[0]])
            Norm /= np.linalg.norm(Norm)
            FacetNorms.append(Norm)
        return FacetNorms

    def __calcFacetDirections(self):
        FacetDirections = []
        for Facet in self.F:
            assert (len(Facet) >= 3)
            FacetDirection = np.array([0, 0, 0], dtype=float)
            for v in Facet:
                FacetDirection += self.V[v]
            FacetDirection /= len(Facet)
            FacetDirection -= self.Anchor
            FacetDirections.append(FacetDirection)
        return FacetDirections

    # === MANIPULATION METHODS
    def applyTransF(self, TransF):
        """

        Args:
            TransF:

        Returns:

        """
        if type(TransF) is ShiftFunc:
            for v in self._V:
                TransF.transform(v)
        elif type(TransF) is ScaleFunc:
            for v in self._V:
                TransF.transform(v)
            for direction in self._FacetDirections:
                TransF.transform(direction)
            for norm in self._FacetNorms:
                norm /= TransF.Scale
        elif type(TransF) is RotateFunc:
            for v in self._V:
                TransF.transform(v)
            for direction in self._FacetDirections:
                TransF.transformDirection(direction)
            for norm in self._FacetNorms:
                TransF.transformDirection(norm)
        else:
            raise TypeError('MatOpt does not support this transformation type. Please contact MatOpt developer for '
                            'potential feature addition.')
        Shape.applyTransF(self, TransF)
        assert (self.isConsistentWithDesign())

    # === PROPERTY EVALUATION METHODS
    def isInShape(self, P, tol=Shape.DBL_TOL):
        """

        Args:
            P: param tol:  (Default value = Shape.DBL_TOL)
            tol:  (Default value = Shape.DBL_TOL)

        Returns:

        """
        for f in range(len(self.F)):
            if not self.satisfiesFacet(P, f, tol):
                return False
        return True

    __contains__ = isInShape

    def satisfiesFacet(self, P, f, tol=Shape.DBL_TOL):
        """

        Args:
            P: param f:
            tol: Default value = Shape.DBL_TOL)
            f:

        Returns:

        """
        P01 = P - self.V[self.F[f][0]]
        return np.inner(P01, self.FacetNorms[f]) < tol

    def getBounds(self):
        """ """
        MinP = deepcopy(self.V[0])
        MaxP = deepcopy(self.V[0])
        for v in self.V:
            MinP = np.minimum(MinP, v)
            MaxP = np.maximum(MaxP, v)
        return MinP, MaxP

    # === BASIC QUERY METHODS
    @property
    def V(self):
        """ """
        return self._V

    @property
    def F(self):
        """ """
        return self._F

    @property
    def FacetNorms(self):
        """ """
        return self._FacetNorms

    @property
    def FacetDirections(self):
        """ """
        return self._FacetDirections

class Cuboctahedron(Polyhedron, ABC):
    """ """

    # === STANDARD CONSTRUCTOR
    def __init__(self, R, Center=None):
        self._R = sqrt(2)
        V = [np.array([1, 0, 1], dtype=float),
             np.array([0, -1, 1], dtype=float),
             np.array([-1, 0, 1], dtype=float),
             np.array([0, 1, 1], dtype=float),
             np.array([1, 1, 0], dtype=float),
             np.array([1, -1, 0], dtype=float),
             np.array([-1, -1, 0], dtype=float),
             np.array([-1, 1, 0], dtype=float),
             np.array([1, 0, -1], dtype=float),
             np.array([0, -1, -1], dtype=float),
             np.array([-1, 0, -1], dtype=float),
             np.array([0, 1, -1], dtype=float)]
        F = [[0, 3, 2, 1],
             [1, 6, 9, 5],
             [2, 7, 10, 6],
             [3, 4, 11, 7],
             [0, 5, 8, 4],
             [8, 9, 10, 11],
             [1, 2, 6],
             [2, 3, 7],
             [0, 4, 3],
             [0, 1, 5],
             [6, 10, 9],
             [7, 11, 10],
             [4, 8, 11],
             [5, 9, 8]]
        Polyhedron.__init__(self, V, F, np.array([0, 0, 0], dtype=float))
        self.scale(R / sqrt(2))
        if Center is not None:
            self.shift(Center)

    # === MANIPULATION METHODS
    def applyTransF(self, TransF):
        """

        Args:
            TransF:

        Returns:

        """
        if isinstance(TransF, ScaleFunc):
            if TransF.isIsometric:
                self._R *= TransF.Scale[0]
            else:
                raise ValueError('Cuboctahedron applyTransF: Can only scale isometrically')
        Polyhedron.applyTransF(self, TransF)
        assert (self.isConsistentWithDesign())

class Parallelepiped(Polyhedron, ABC):
    """ """

    # === STANDARD CONSTRUCTOR
    def __init__(self, Vx, Vy, Vz, BotBackLeftCorner=None):
        self._Vx = Vx
        self._Vy = Vy
        self._Vz = Vz
        if np.inner(np.cross(Vx, Vy), Vz) < Parallelepiped.DBL_TOL:
            raise ValueError("Vx,Vy,Vz must have a positive box product.")
        if BotBackLeftCorner is None:
            BotBackLeftCorner = np.array([0, 0, 0], dtype=float)

        V = [BotBackLeftCorner,
             BotBackLeftCorner + Vx,
             BotBackLeftCorner + Vy,
             BotBackLeftCorner + Vz,
             BotBackLeftCorner + Vx + Vy,
             BotBackLeftCorner + Vx + Vz,
             BotBackLeftCorner + Vy + Vz,
             BotBackLeftCorner + Vx + Vy + Vz]
        F = [[0, 1, 5, 3],
             [0, 3, 6, 2],
             [2, 6, 7, 4],
             [1, 4, 7, 5],
             [3, 5, 7, 6],
             [0, 2, 4, 1]]
        Polyhedron.__init__(self, V, F, Anchor=deepcopy(BotBackLeftCorner))

    # === CONSTRUCTOR - From edge lengths and angles
    @classmethod
    def fromEdgesAndAngles(cls, A, B, C, alpha, beta, gamma, BotBackLeftCorner=None):
        """

        Args:
            A: param B:
            C: param alpha:
            beta: param gamma:
            BotBackLeftCorner: Default value = None)
            B:
            alpha:
            gamma:

        Returns:

        """
        Vx = np.array([A, 0, 0])
        Vy = np.array([B * np.cos(gamma), B * np.sin(gamma), 0])
        zcomp = np.sqrt(1 - pow(np.cos(alpha), 2) - pow(np.cos(beta), 2))
        Vz = np.array([C * np.cos(alpha), C * np.cos(beta), C * zcomp])
        return cls(Vx, Vy, Vz, BotBackLeftCorner)

    # === CONSTRUCTOR - From POSCAR file header information
    @classmethod
    def fromPOSCAR(cls, filename):
        """

        Args:
            filename:

        Returns:

        """
        with open(filename, 'r') as infile:
            CommentLine = infile.readline()
            GSLine = infile.readline().split()
            GS = float(GSLine[0])
            VxLine = infile.readline().split()
            Vx = GS * np.array([float(VxLine[0]),
                                float(VxLine[1]),
                                float(VxLine[2])], dtype=float)
            VyLine = infile.readline().split()
            Vy = GS * np.array([float(VyLine[0]),
                                float(VyLine[1]),
                                float(VyLine[2])], dtype=float)
            VzLine = infile.readline().split()
            Vz = GS * np.array([float(VzLine[0]),
                                float(VzLine[1]),
                                float(VzLine[2])], dtype=float)
        return cls(Vx, Vy, Vz)

    # === MANIPULATION METHODS
    def applyTransF(self, TransF):
        """

        Args:
            TransF:

        Returns:

        """
        if isinstance(TransF, ScaleFunc):
            self._Vx *= TransF.Scale[0]
            self._Vy *= TransF.Scale[1]
            self._Vz *= TransF.Scale[2]
        Polyhedron.applyTransF(self, TransF)
        assert (self.isConsistentWithDesign())

        # === PROPERTY EVALUATION METHODS

    def getFractionalCoords(self, P, blnRelativeToCenter=False):
        """

        Args:
            P: param blnRelativeToCenter:  (Default value = False)
            blnRelativeToCenter:  (Default value = False)

        Returns:

        """
        FracOrigin = (self.getCenter() if blnRelativeToCenter else self.Anchor)
        Omega = self.getVolume()
        fracX = 1.0 / Omega * np.inner(P - FracOrigin, np.cross(self.Vy, self.Vz))
        fracY = 1.0 / Omega * np.inner(P - FracOrigin, np.cross(self.Vz, self.Vx))
        fracZ = 1.0 / Omega * np.inner(P - FracOrigin, np.cross(self.Vx, self.Vy))
        return np.array([fracX, fracY, fracZ])

    def getCenter(self):
        """ """
        return sum(self.V) / len(self.V)

    def getVolume(self):
        """ """
        return np.dot(np.cross(self.Vx, self.Vy), self.Vz)

    # === BASIC QUERY METHODS
    @property
    def Vx(self):
        """ """
        return self._Vx

    @property
    def Vy(self):
        """ """
        return self._Vy

    @property
    def Vz(self):
        """ """
        return self._Vz

    @property
    def isUnit(self):
        """ """
        if not areEqual(np.linalg.norm(self.Vx), 1.0, Shape.DBL_TOL):
            return False
        if not areEqual(np.linalg.norm(self.Vy), 1.0, Shape.DBL_TOL):
            return False
        if not areEqual(np.linalg.norm(self.Vz), 1.0, Shape.DBL_TOL):
            return False
        return True

class Rhombohedron(Parallelepiped, ABC):
    """ """

    # === STANDARD CONSTRUCTOR
    def __init__(self, L, Alpha, BotBackLeftCorner=None):
        self._L = L
        self._Alpha = Alpha
        Lx = np.array([L, 0, 0])
        Ly = np.array([L * cos(Alpha), L * sin(Alpha), 0])
        Lz = np.array([L * cos(Alpha),
                       L * (cos(Alpha) - cos(Alpha) ** 2) / sin(Alpha),
                       L * sqrt(1 - 3 * cos(Alpha) ** 2 + 2 * cos(Alpha) ** 3) / sin(Alpha)])
        Parallelepiped.__init__(self, Lx, Ly, Lz, BotBackLeftCorner)

    # === MANIPULATION METHODS
    def applyTransF(self, TransF):
        """

        Args:
            TransF:

        Returns:

        """
        if isinstance(TransF, ScaleFunc):
            if TransF.isIsometric:
                self._L *= TransF.Scale[0]
            else:
                raise ValueError('Rhombohedron applyTransF: Can only scale isometrically')
        Polyhedron.applyTransF(self, TransF)
        assert (self.isConsistentWithDesign())

        # === PROPERTY EVALUATION METHODS

    @property
    def L(self):
        """ """
        return self._L

    @property
    def Alpha(self):
        """ """
        return self._Alpha

class RectPrism(Parallelepiped, ABC):
    """ """

    # === STANDARD CONSTRUCTOR
    def __init__(self, Lx, Ly, Lz, BotBackLeftCorner=None):
        self._Lx = Lx
        self._Ly = Ly
        self._Lz = Lz
        Parallelepiped.__init__(self,
                                np.array([Lx, 0, 0], dtype=float),
                                np.array([0, Ly, 0], dtype=float),
                                np.array([0, 0, Lz], dtype=float),
                                BotBackLeftCorner)

    # === CONSTRUCTOR - Bounding box of a list of points
    @classmethod
    def fromPointsBBox(cls, Pts):
        """

        Args:
            Pts:

        Returns:

        """
        # import code; code.interact(local=dict(locals(),**globals()));
        minX = min(_[0] for _ in Pts)
        maxX = max(_[0] for _ in Pts)
        minY = min(_[1] for _ in Pts)
        maxY = max(_[1] for _ in Pts)
        minZ = min(_[2] for _ in Pts)
        maxZ = max(_[2] for _ in Pts)
        return cls(maxX - minX, maxY - minY, maxZ - minZ, np.array([minX, minY, minZ]))

    # === MANIPULATION METHODS
    def applyTransF(self, TransF):
        """

        Args:
            TransF:

        Returns:

        """
        if isinstance(TransF, ScaleFunc):
            self._Lx *= TransF.Scale[0]
            self._Ly *= TransF.Scale[1]
            self._Lz *= TransF.Scale[2]
        Parallelepiped.applyTransF(self, TransF)
        assert (self.isConsistentWithDesign())

        # === BASIC QUERY METHODS

    @property
    def Lx(self):
        """ """
        return self._Lx

    @property
    def Ly(self):
        """ """
        return self._Ly

    @property
    def Lz(self):
        """ """
        return self._Lz

class Cube(RectPrism, ABC):
    """ """

    # === STANDARD CONSTRUCTOR
    def __init__(self, L, BotBackLeftCorner=None):
        self._L = L
        RectPrism.__init__(self, L, L, L, BotBackLeftCorner)

    # === MANIPULATION METHODS
    def applyTransF(self, TransF):
        """

        Args:
            TransF:

        Returns:

        """
        if isinstance(TransF, ScaleFunc):
            if TransF.isIsometric:
                self._L *= TransF.Scale[0]
            else:
                raise ValueError('Cube applyTransF: Can only scale isometrically')
        RectPrism.applyTransF(self, TransF)
        assert (self.isConsistentWithDesign())

        # === BASIC QUERY METHODS

    @property
    def L(self):
        """ """
        return self._L

class Cylinder(Shape, ABC):
    """ Object to create, manipulate and utilize Cylinder shapes. """
    # === STANDARD CONSTRUCTOR
    def __init__(self, Po, R, H, Vh=np.array([0,0,1], dtype=float), Alignment=None):
        Shape.__init__(self, Po, Alignment)
        self._R = R
        self._H = H
        self._Vh = Vh * self._H / np.linalg.norm(Vh)
        assert (self.isConsistentWithDesign())

    # === ASSERTION OF CLASS DESIGN
    def isConsistentWithDesign(self):
        if len(self.Vh) != 3:
            return False
        return self.R > 0 and self.H > 0 and Shape.isConsistentWithDesign(self)

    # === MANIPULATION METHODS
    def applyTransF(self, TransF):
        """ Apply the input transform function.

        Args:
            TransF: Transform function.
        """
        if type(TransF) is ScaleFunc:
            if areEqual(TransF.Scale[0], TransF.Scale[1], Shape.DBL_TOL):
                self._R *= TransF.Scale[0]
                self._H *= TransF.Scale[2]
                TransF.transform(self._Vh)
            else:
                raise ValueError('The scaling ratio along the radius directions are not consistent')
        elif (type(TransF) is RotateFunc) or (type(TransF) is ReflectFunc):
            TransF.transform(self._Vh)
        elif not (type(TransF) is ShiftFunc):
            raise TypeError('MatOpt does not support this transformation type. Please contact MatOpt developer for '
                            'potential feature addition.')
        Shape.applyTransF(self, TransF)
        assert (self.isConsistentWithDesign())

    # === PROPERTY EVALUATION METHODS
    def isInShape(self, P, tol=Shape.DBL_TOL):
        """ Check if a point is inside the Cylinder shape within certain tolerance.

        Args:
            P: Input point
            tol: Tolerance (Default value = Shape.DBL_TOL)

        Returns:
            True/False
        """
        OP = P - self.Anchor
        projH = np.dot(OP, self.Vh) / self.H
        if (projH > -tol) and (projH < self.H + tol):
            normOP = np.linalg.norm(OP)
            projR = sqrt(normOP ** 2 - projH ** 2 + tol)
            return projR < self.R + tol
        else:
            return False

    __contains__ = isInShape

    def getBounds(self):
        """ Get the bounds of each coordinate in the form of an artificial point."""
        MinP = np.minimum(np.zeros(3, dtype=float), self.Vh) + self.Anchor - np.ones(3, dtype=float) * self.R
        MaxP = np.maximum(np.zeros(3, dtype=float), self.Vh) + self.Anchor + np.ones(3, dtype=float) * self.R
        return MinP, MaxP

    # === BASIC QUERY METHODS
    @property
    def R(self):
        """ Radius. """
        return self._R

    @property
    def H(self):
        """ Height. """
        return self._H

    @property
    def Vh(self):
        """ Center axis vector. """
        return self._Vh

class CylindricalSector(Shape, ABC):
    """ Object to create, manipulate and utilize Cylinder shapes. """

    # === STANDARD CONSTRUCTOR
    def __init__(self, Po, R, H, Va, Vb, Vh, Alignment=None):
        Shape.__init__(self, Po, Alignment)
        self._R = R
        self._H = H
        self._Vh = Vh * self._H / np.linalg.norm(Vh)
        self._Va = Va * self._R / np.linalg.norm(Va)
        self._Vb = Vb * self._R / np.linalg.norm(Vb)
        self._Norms = self.setNorms()
        assert (self.isConsistentWithDesign())

    # === ASSERTION OF CLASS DESIGN
    def isConsistentWithDesign(self):
        if self.R > 0 and self.H > 0 and len(self.Vh) == 3 and len(self.Va) == 3 and len(self.Vb) == 3 and len(
                self.Norms) == 3:
            if areEqual(np.dot(self.Va, self.Vh), 0.0, Shape.DBL_TOL) and \
                    areEqual(np.dot(self.Vb, self.Vh), 0.0, Shape.DBL_TOL):
                return Shape.isConsistentWithDesign(self)
        return False

    # === AUXILIARY METHODS
    def setNorms(self):
        return [np.cross(self._Vb, self._Va), np.cross(self._Va, self._Vh), np.cross(self._Vh, self._Vb)]

    # === MANIPULATION METHODS
    def applyTransF(self, TransF):
        """ Apply the input transform function.

        Args:
            TransF: Transform function.
        """
        if type(TransF) is ScaleFunc:
            if areEqual(TransF.Scale[0], TransF.Scale[1], Shape.DBL_TOL):
                self._R *= TransF.Scale[0]
                self._H *= TransF.Scale[2]
                TransF.transform(self._Vh)
            else:
                raise ValueError('The scaling ratio along the radius directions are not consistent')
        elif (type(TransF) is RotateFunc) or (type(TransF) is ReflectFunc):
            TransF.transform(self._Va)
            TransF.transform(self._Vb)
            TransF.transform(self._Vh)
            self._Norms = self.setNorms()
        elif not (type(TransF) is ShiftFunc):
            raise TypeError('MatOpt does not support this transformation type. Please contact MatOpt developer for '
                            'potential feature addition.')
        Shape.applyTransF(self, TransF)
        assert (self.isConsistentWithDesign())

    # === PROPERTY EVALUATION METHODS
    def isInShape(self, P, tol=Shape.DBL_TOL):
        """ Check if a point is inside the Cylinder shape within certain tolerance.

        Args:
            P: Input point
            tol: Tolerance (Default value = Shape.DBL_TOL)

        Returns:
            True/False
        """
        OP = P - self.Anchor
        for n in self.Norms:
            if np.dot(n, OP) > tol:
                return False
        if np.dot(-self.Norms[0], OP - self.Vh) > tol:
            return False
        projH = np.linalg.norm(np.dot(OP, self.Vh)) / self.H
        normP = np.linalg.norm(OP)
        if sqrt(normP ** 2 - projH ** 2 + tol) > self.R + tol:
            return False
        return True

    __contains__ = isInShape

    # === BASIC QUERY METHODS
    @property
    def R(self):
        """ Radius. """
        return self._R

    @property
    def H(self):
        """ Height. """
        return self._H

    @property
    def Vh(self):
        """ Center axis vector. """
        return self._Vh

    @property
    def Va(self):
        """ Left edge vector. """
        return self._Va

    @property
    def Vb(self):
        """ Right edge vector. """
        return self._Vb

    @property
    def Norms(self):
        """ Norms. """
        return self._Norms

max_stars_repo_name: carldlaird/idaes-pse

max_issues_repo_name: carldlaird/idaes-pse

max_forks_repo_name: carldlaird/idaes-pse


text:
import copy
import time

import numpy as np
import torch
from torch.nn import functional as F

class Logger:
    def __init__(self, log_interval, args, verbose=True):

        self.log_interval = log_interval
        self.args = args
        self.verbose = verbose

        # highest accuracy observed on training and validation set, and on average
        self.best_train_accuracy = 0
        self.best_valid_accuracy = 0
        self.best_train_valid_accuracy = 0

        # accuracies (train/valid) we see for the best models
        self.best_model_train_stats = [0, 0]
        self.best_model_valid_stats = [0, 0]
        self.best_model_train_valid_stats = [0, 0]

        # print some infos
        if verbose:
            print(
                'n-way: ', args.n_way, 'k-shot', args.k_shot,
                'lr[in]:', args.lr_inner, ', [out]:', args.lr_meta,
                ', grad[in]:', args.num_grad_steps_inner, ', [out]:', args.num_grad_steps_eval,
                'batchs:', args.tasks_per_metaupdate,
            )

        # initialise dictionary to keep track of accuracies/losses
        # - for training tasks
        self.training_stats = {
            #
            'train_accuracy_pre_update': [],
            'test_accuracy_pre_update': [],
            'train_accuracy_post_update': [],
            'test_accuracy_post_update': [],
            #
            'train_loss_pre_update': [],
            'test_loss_pre_update': [],
            'train_loss_post_update': [],
            'test_loss_post_update': [],
        }
        # - for validation tasks
        self.validation_stats = {
            #
            'train_accuracy_pre_update': [],
            'test_accuracy_pre_update': [],
            'train_accuracy_post_update': [],
            'test_accuracy_post_update': [],
            #
            'train_loss_pre_update': [],
            'test_loss_pre_update': [],
            'train_loss_post_update': [],
            'test_loss_post_update': [],
        }

        # keep track of how long the experiment takes
        self.start_time = time.time()

    def prepare_inner_loop(self, iter_counter, mode='train'):
        """
        Called before iterating over tasks in the inner loop
        :return:
        """
        if iter_counter % self.log_interval == 0:

            if mode == 'train':
                for key in self.training_stats.keys():
                    self.training_stats[key].append([])
            elif mode == 'valid':
                for key in self.validation_stats.keys():
                    self.validation_stats[key].append([])
            else:
                raise NotImplementedError()

    def log_pre_update(self, iter_counter, support_x, support_y, query_x, query_y, model, mode='train'):

        if iter_counter % self.log_interval == 0:
            if mode == 'train':
                self.training_stats['train_accuracy_pre_update'][-1].append(
                    self.get_accuracy(support_x, support_y, model))
                self.training_stats['test_accuracy_pre_update'][-1].append(self.get_accuracy(query_x, query_y, model))
                self.training_stats['train_loss_pre_update'][-1].append(self.get_loss(support_x, support_y, model))
                self.training_stats['test_loss_pre_update'][-1].append(self.get_loss(query_x, query_y, model))
            elif mode == 'valid':
                self.validation_stats['train_accuracy_pre_update'][-1].append(
                    self.get_accuracy(support_x, support_y, model))
                self.validation_stats['test_accuracy_pre_update'][-1].append(self.get_accuracy(query_x, query_y, model))
                self.validation_stats['train_loss_pre_update'][-1].append(self.get_loss(support_x, support_y, model))
                self.validation_stats['test_loss_pre_update'][-1].append(self.get_loss(query_x, query_y, model))
            else:
                raise NotImplementedError()

    def log_post_update(self, iter_counter, support_x, support_y, query_x, query_y, model, mode='train'):

        if iter_counter % self.log_interval == 0:
            if mode == 'train':
                self.training_stats['train_accuracy_post_update'][-1].append(
                    self.get_accuracy(support_x, support_y, model))
                self.training_stats['test_accuracy_post_update'][-1].append(self.get_accuracy(query_x, query_y, model))
                self.training_stats['train_loss_post_update'][-1].append(self.get_loss(support_x, support_y, model))
                self.training_stats['test_loss_post_update'][-1].append(self.get_loss(query_x, query_y, model))
            elif mode == 'valid':
                self.validation_stats['train_accuracy_post_update'][-1].append(
                    self.get_accuracy(support_x, support_y, model))
                self.validation_stats['test_accuracy_post_update'][-1].append(
                    self.get_accuracy(query_x, query_y, model))
                self.validation_stats['train_loss_post_update'][-1].append(self.get_loss(support_x, support_y, model))
                self.validation_stats['test_loss_post_update'][-1].append(self.get_loss(query_x, query_y, model))
            else:
                raise NotImplementedError()

    def summarise_inner_loop(self, mode):
        if mode == 'train':
            for key in self.training_stats.keys():
                self.training_stats[key][-1] = np.mean(self.training_stats[key][-1])
        if mode == 'valid':
            for key in self.validation_stats.keys():
                self.validation_stats[key][-1] = np.mean(self.validation_stats[key][-1])

    def update_best_model(self, model, save_path):

        # get the current training and validation accuracy
        train_acc = self.training_stats['test_accuracy_post_update'][-1]
        valid_acc = self.validation_stats['test_accuracy_post_update'][-1]
        train_valid_acc = 0.5 * (train_acc + valid_acc)

        if train_acc > self.best_train_accuracy:

            self.best_train_accuracy = train_acc
            # save the model with the highest training accuracy so far
            self.best_model_train = copy.copy(model)
            # log what the corresponding accuracy on training and validation set are
            self.best_model_train_stats = [self.training_stats['test_accuracy_post_update'][-1],
                                           self.validation_stats['test_accuracy_post_update'][-1]]

            if save_path is not None:
                np.save(save_path + '_best_train', self.best_model_train_stats)
                save_model = self.best_model_train
                if self.args.device == 'cuda:0':
                    save_model = copy.deepcopy(self.best_model_train).to(torch.device('cpu'))
                torch.save(save_model, save_path + '_best_train')

        if valid_acc > self.best_valid_accuracy:

            self.best_valid_accuracy = valid_acc
            self.best_model_valid = copy.copy(model)
            self.best_model_valid_stats = [self.training_stats['test_accuracy_post_update'][-1],
                                           self.validation_stats['test_accuracy_post_update'][-1]]

            if save_path is not None:
                np.save(save_path + '_best_valid', self.best_model_valid_stats)
                # save model to CPU
                save_model = self.best_model_valid
                if self.args.device == 'cuda:0':
                    save_model = copy.deepcopy(self.best_model_valid).to(torch.device('cpu'))
                torch.save(save_model, save_path + '_best_valid')

        if train_valid_acc > self.best_train_valid_accuracy:

            self.best_train_valid_accuracy = train_valid_acc
            self.best_model_train_valid = copy.copy(model)
            self.best_model_train_valid_stats = [self.training_stats['test_accuracy_post_update'][-1],
                                                 self.validation_stats['test_accuracy_post_update'][-1]]

            if save_path is not None:
                np.save(save_path + '_best_train_valid', self.best_model_train_valid_stats)
                # save model to CPU
                save_model = self.best_model_train_valid
                if self.args.device == 'cuda:0':
                    save_model = copy.deepcopy(self.best_model_train_valid).to(torch.device('cpu'))
                torch.save(save_model, save_path + '_best_train_valid')

    def print(self, iter_counter, grad_inner, grad_meta):
        if self.verbose and (iter_counter % self.log_interval == 0):
            self.print_logs(iter_counter, grad_inner, grad_meta)

    def print_header(self):
        if self.verbose:
            print(
                '||------||------------------------ TRAINING -------------------------||------------------------ VALIDATION -----------------------||-------------------||-------||')
            print(
                '||------||----------- LOSS ------------|------------ ACC ------------||----------- LOSS ------------|------------ ACC ------------||------ GRAD -------||-------||')
            print(
                '|| iter ||    train     |     test     |    train     |     test     ||    train     |     test     |    train     |     test     ||  inner  |  meta   || time  ||')
            print(
                '||------||--------------|--------------|--------------|--------------||--------------|--------------|--------------|--------------||---------|---------||-------||')

    def print_logs(self, iter_counter, grad_inner, grad_meta):
        if self.verbose:
            print(
                '||{:5} || {:<5}->{:<5} | {:<5}->{:<5} | {:<5}->{:<5} | {:<5}->{:<5} || {:<5}->{:<5} | {:<5}->{:<5} | {:<5}->{:<5} | {:<5}->{:<5} || {:<7} | {:<7} || {:<5} ||'.format(
                    iter_counter,
                    # meta-train, task-train
                    np.round(self.training_stats['train_loss_pre_update'][-1], 3),
                    np.round(self.training_stats['train_loss_post_update'][-1], 3),
                    np.round(self.training_stats['test_loss_pre_update'][-1], 3),
                    np.round(self.training_stats['test_loss_post_update'][-1], 3),
                    # meta-train, task-test
                    np.round(self.training_stats['train_accuracy_pre_update'][-1], 3),
                    np.round(self.training_stats['train_accuracy_post_update'][-1], 3),
                    np.round(self.training_stats['test_accuracy_pre_update'][-1], 3),
                    np.round(self.training_stats['test_accuracy_post_update'][-1], 3),
                    # meta-valid, task-train
                    np.round(self.validation_stats['train_loss_pre_update'][-1], 3),
                    np.round(self.validation_stats['train_loss_post_update'][-1], 3),
                    np.round(self.validation_stats['test_loss_pre_update'][-1], 3),
                    np.round(self.validation_stats['test_loss_post_update'][-1], 3),
                    # meta-valid, task-test
                    np.round(self.validation_stats['train_accuracy_pre_update'][-1], 3),
                    np.round(self.validation_stats['train_accuracy_post_update'][-1], 3),
                    np.round(self.validation_stats['test_accuracy_pre_update'][-1], 3),
                    np.round(self.validation_stats['test_accuracy_post_update'][-1], 3),
                    # gradients
                    np.round(grad_inner[0].abs().mean().item(), 3),
                    np.round(grad_meta[0].abs().mean().item(), 3),
                    # time
                    np.round((time.time() - self.start_time) / 60, 2)
                ))

    def get_accuracy(self, x, y, model):
        predictions = model(x)
        num_correct = torch.argmax(F.softmax(predictions, dim=1), 1).eq(y).sum().item()
        return num_correct / len(y)

    def get_loss(self, x, y, model):
        predictions = model(x)
        return F.cross_entropy(predictions, y).item()

max_stars_repo_name: luanagbmartins/cavia

max_issues_repo_name: luanagbmartins/cavia

max_forks_repo_name: luanagbmartins/cavia