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

import torch
import torch.utils.checkpoint

from functools import partial
from torch import nn
from torch.nn import functional as F
from transformers.utils import logging

from .. import core as qc
from ..core import utils as qu
from ..core import forward as qf
from ..core import output as qo
from ..core import attention as qa
from ..core.embed import Embeds
from ..core.ffnet import Classifier, FFNet, Masker, Pool
from ..prep.config.fnet import PreTrained

log = logging.get_logger(__name__)

from torch.nn import CrossEntropyLoss

from ...utils import is_scipy_available

if is_scipy_available():
    from scipy import linalg

from ...pytorch_utils import apply_chunking_to_forward

LIST = ["google/fnet-base", "google/fnet-large"]

# Adapted from https://github.com/google-research/google-research/blob/master/f_net/fourier.py
def _two_dim_matmul(x, matrix_dim_one, matrix_dim_two):
    seq_length = x.shape[1]
    matrix_dim_one = matrix_dim_one[:seq_length, :seq_length]
    x = x.type(torch.complex64)
    return torch.einsum("bij,jk,ni->bnk", x, matrix_dim_two, matrix_dim_one)

# # Adapted from https://github.com/google-research/google-research/blob/master/f_net/fourier.py
def two_dim_matmul(x, matrix_dim_one, matrix_dim_two):
    return _two_dim_matmul(x, matrix_dim_one, matrix_dim_two)

# Adapted from https://github.com/google-research/google-research/blob/master/f_net/fourier.py
def fftn(x):
    out = x
    for axis in reversed(range(x.ndim)[1:]):  # We don't need to apply FFT to last axis
        out = torch.fft.fft(out, axis=axis)
    return out

class FNetEmbeddings(qc.Module):
    def __init__(self, config):
        super().__init__()
        self.word_embeddings = qc.Embed(config.s_vocab, config.d_model, padding_idx=config.PAD)
        self.position_embeddings = qc.Embed(config.n_pos, config.d_model)
        self.token_type_embeddings = qc.Embed(config.n_typ, config.d_model)
        self.norm = qc.LayerNorm(config.d_model, eps=config.eps)
        self.projection = qc.Linear(config.d_model, config.d_model)
        self.drop = qc.Dropout(config.drop)
        self.register_buffer("position_ids", torch.arange(config.n_pos).expand((1, -1)))
        self.register_buffer(
            "token_type_ids",
            torch.zeros(self.position_ids.size(), dtype=torch.long),
            persistent=False,
        )

    def forward(self, input_ids=None, token_type_ids=None, position_ids=None, inputs_embeds=None):
        if input_ids is not None:
            input_shape = input_ids.size()
        else:
            input_shape = inputs_embeds.size()[:-1]

        seq_length = input_shape[1]

        if position_ids is None:
            position_ids = self.position_ids[:, :seq_length]
        if token_type_ids is None:
            if hasattr(self, "token_type_ids"):
                buffered_token_type_ids = self.token_type_ids[:, :seq_length]
                buffered_token_type_ids_expanded = buffered_token_type_ids.expand(
                    input_shape[0], seq_length
                )
                token_type_ids = buffered_token_type_ids_expanded
            else:
                token_type_ids = torch.zeros(
                    input_shape, dtype=torch.long, device=self.position_ids.device
                )

        if inputs_embeds is None:
            inputs_embeds = self.word_embeddings(input_ids)
        token_type_embeddings = self.token_type_embeddings(token_type_ids)

        embeddings = inputs_embeds + token_type_embeddings

        position_embeddings = self.position_embeddings(position_ids)
        embeddings += position_embeddings
        embeddings = self.norm(embeddings)
        embeddings = self.projection(embeddings)
        embeddings = self.drop(embeddings)
        return embeddings

class FNetBasicFourierTransform(qc.Module):
    def __init__(self, config):
        super().__init__()
        self._init_fourier_transform(config)

    def _init_fourier_transform(self, config):
        if not config.use_tpu_fourier_optimizations:
            self.fourier_transform = partial(torch.fft.fftn, dim=(1, 2))
        elif config.n_pos <= 4096:
            if is_scipy_available():
                self.register_buffer(
                    "dft_mat_hidden",
                    torch.tensor(linalg.dft(config.d_model), dtype=torch.complex64),
                )
                self.register_buffer(
                    "dft_mat_seq",
                    torch.tensor(linalg.dft(config.tpu_short_seq_length), dtype=torch.complex64),
                )
                self.fourier_transform = partial(
                    two_dim_matmul,
                    matrix_dim_one=self.dft_mat_seq,
                    matrix_dim_two=self.dft_mat_hidden,
                )
            else:
                self.fourier_transform = fftn
        else:
            self.fourier_transform = fftn

    def forward(self, hiddens):
        outputs = self.fourier_transform(hiddens).real
        return (outputs,)

class FNetBasicOutput(qc.Module):
    def __init__(self, config):
        super().__init__()
        self.norm = qc.LayerNorm(config.d_model, eps=config.eps)

    def forward(self, hiddens, input_tensor):
        hiddens = self.norm(input_tensor + hiddens)
        return hiddens

class FNetFourierTransform(qc.Module):
    def __init__(self, config):
        super().__init__()
        self.self = FNetBasicFourierTransform(config)
        self.output = FNetBasicOutput(config)

    def forward(self, hiddens):
        self_outputs = self.self(hiddens)
        fourier_output = self.output(self_outputs[0], hiddens)
        outputs = (fourier_output,)
        return outputs

# Copied from transformers.models.bert.modeling_bert.BertIntermediate with Bert->FNet
class FNetIntermediate(qc.Module):
    def __init__(self, cfg):
        super().__init__()
        self.dense = qc.Linear(cfg.d_model, cfg.d_ff)
        self.act = qu.activation(cfg.act)

    def forward(self, x):
        y = self.dense(x)
        y = self.act(y)
        return y

# Copied from transformers.models.bert.modeling_bert.BertOutput with Bert->FNet
class FNetOutput(qc.Module):
    def __init__(self, config):
        super().__init__()
        self.dense = qc.Linear(config.d_ff, config.d_model)
        self.norm = qc.LayerNorm(config.d_model, eps=config.eps)
        self.drop = qc.Dropout(config.drop)

    def forward(self, hiddens, input_tensor):
        hiddens = self.dense(hiddens)
        hiddens = self.drop(hiddens)
        hiddens = self.norm(hiddens + input_tensor)
        return hiddens

class FNetLayer(qc.Module):
    def __init__(self, config):
        super().__init__()
        self.chunk_size_feed_forward = config.chunk_size_feed_forward
        self.seq_len_dim = 1  # The dimension which has the sequence length
        self.fourier = FNetFourierTransform(config)
        self.intermediate = FNetIntermediate(config)
        self.output = FNetOutput(config)

    def forward(self, hiddens):
        self_fourier_outputs = self.fourier(hiddens)
        fourier_output = self_fourier_outputs[0]
        layer_output = apply_chunking_to_forward(
            self.feed_forward_chunk, self.chunk_size_feed_forward, self.seq_len_dim, fourier_output
        )
        outputs = (layer_output,)
        return outputs

    def feed_forward_chunk(self, fourier_output):
        intermediate_output = self.intermediate(fourier_output)
        layer_output = self.output(intermediate_output, fourier_output)
        return layer_output

class FNetEncoder(qc.Module):
    def __init__(self, config):
        super().__init__()
        self.config = config
        self.layer = nn.ModuleList([FNetLayer(config) for _ in range(config.n_lays)])
        self.gradient_checkpointing = False

    def forward(self, hiddens, output_hidden_states=False, return_dict=True):
        all_hidden_states = () if output_hidden_states else None

        for i, layer_module in enumerate(self.layer):
            if output_hidden_states:
                all_hidden_states = all_hidden_states + (hiddens,)

            if self.gradient_checkpointing and self.training:

                def create_custom_forward(module):
                    def custom_forward(*inputs):
                        return module(*inputs)

                    return custom_forward

                layer_outputs = torch.utils.checkpoint.checkpoint(
                    create_custom_forward(layer_module), hiddens
                )
            else:
                layer_outputs = layer_module(hiddens)

            hiddens = layer_outputs[0]

        if output_hidden_states:
            all_hidden_states = all_hidden_states + (hiddens,)

        if not return_dict:
            return tuple(v for v in [hiddens, all_hidden_states] if v is not None)

        return qo.Base(y=hiddens, hiddens=all_hidden_states)

# Copied from transformers.models.bert.modeling_bert.BertPreTrainingHeads with Bert->FNet
class FNetPreTrainingHeads(qc.Module):
    def __init__(self, config):
        super().__init__()
        self.predictions = Masker(config)
        self.seq_relationship = qc.Linear(config.d_model, 2)

    def forward(self, sequence_output, pooled_output):
        prediction_scores = self.predictions(sequence_output)
        seq_relationship_score = self.seq_relationship(pooled_output)
        return prediction_scores, seq_relationship_score

class Model(PreTrained):
    def __init__(self, config, add_pooling_layer=True):
        super().__init__(config)
        self.config = config
        self.embeddings = FNetEmbeddings(config)
        self.encoder = FNetEncoder(config)
        self.pooler = Pool(config) if add_pooling_layer else None

    def forward(
        self,
        input_ids=None,
        token_type_ids=None,
        position_ids=None,
        inputs_embeds=None,
        output_hidden_states=None,
        return_dict=None,
    ):
        output_hidden_states = (
            output_hidden_states
            if output_hidden_states is not None
            else self.config.output_hidden_states
        )
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict

        if input_ids is not None and inputs_embeds is not None:
            raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
        elif input_ids is not None:
            input_shape = input_ids.size()
            batch_size, seq_length = input_shape
        elif inputs_embeds is not None:
            input_shape = inputs_embeds.size()[:-1]
            batch_size, seq_length = input_shape
        else:
            raise ValueError("You have to specify either input_ids or inputs_embeds")

        if (
            self.config.use_tpu_fourier_optimizations
            and seq_length <= 4096
            and self.config.tpu_short_seq_length != seq_length
        ):
            raise ValueError(
                "The `tpu_short_seq_length` in FNetConfig should be set equal to the sequence length being passed to the model when using TPU optimizations."
            )

        device = input_ids.device if input_ids is not None else inputs_embeds.device

        if token_type_ids is None:
            if hasattr(self.embeddings, "token_type_ids"):
                buffered_token_type_ids = self.embeddings.token_type_ids[:, :seq_length]
                buffered_token_type_ids_expanded = buffered_token_type_ids.expand(
                    batch_size, seq_length
                )
                token_type_ids = buffered_token_type_ids_expanded
            else:
                token_type_ids = torch.zeros(input_shape, dtype=torch.long, device=device)

        embedding_output = self.embeddings(
            input_ids=input_ids,
            position_ids=position_ids,
            token_type_ids=token_type_ids,
            inputs_embeds=inputs_embeds,
        )
        encoder_outputs = self.encoder(
            embedding_output,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )
        sequence_output = encoder_outputs[0]

        pools = self.pooler(sequence_output) if self.pooler is not None else None

        if not return_dict:
            return (sequence_output, pools) + encoder_outputs[1:]

        return qo.BaseWithPooling(
            y=sequence_output,
            pools=pools,
            hiddens=encoder_outputs.hiddens,
        )

class FNetForPreTraining(PreTrained):
    def __init__(self, config):
        super().__init__(config)
        self.fnet = Model(config)
        self.cls = FNetPreTrainingHeads(config)
        self.post_init()

    def forward(
        self,
        input_ids=None,
        token_type_ids=None,
        position_ids=None,
        inputs_embeds=None,
        labels=None,
        next_sentence_label=None,
        output_hidden_states=None,
        return_dict=None,
    ):
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict

        outputs = self.fnet(
            input_ids,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            inputs_embeds=inputs_embeds,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        sequence_output, pooled_output = outputs[:2]
        prediction_scores, seq_relationship_score = self.cls(sequence_output, pooled_output)

        total_loss = None
        if labels is not None and next_sentence_label is not None:
            loss_fct = CrossEntropyLoss()
            masked_lm_loss = loss_fct(
                prediction_scores.view(-1, self.config.s_vocab), labels.view(-1)
            )
            next_sentence_loss = loss_fct(
                seq_relationship_score.view(-1, 2), next_sentence_label.view(-1)
            )
            total_loss = masked_lm_loss + next_sentence_loss

        if not return_dict:
            output = (prediction_scores, seq_relationship_score) + outputs[2:]
            return ((total_loss,) + output) if total_loss is not None else output

        return qo.LossSeq(
            loss=total_loss,
            logits=prediction_scores,
            orders=seq_relationship_score,
            hiddens=outputs.hiddens,
        )

class ForMasked(PreTrained):
    def __init__(self, **kw):
        super().__init__(**kw)
        self.get_cfg(kw)
        self.model = Model(**kw)
        self.proj = Masker(**kw)

    forward = qf.forward_masked

class FNetForNextSentencePrediction(PreTrained):
    def __init__(self, config):
        super().__init__(config)

        self.fnet = Model(config)
        self.cls = qc.Linear(config.d_model, 2)
        self.post_init()

    def forward(
        self,
        input_ids=None,
        token_type_ids=None,
        position_ids=None,
        inputs_embeds=None,
        labels=None,
        output_hidden_states=None,
        return_dict=None,
        **kw,
    ):
        if "next_sentence_label" in kw:
            labels = kw.pop("next_sentence_label")

        return_dict = return_dict if return_dict is not None else self.config.use_return_dict

        outputs = self.fnet(
            input_ids,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            inputs_embeds=inputs_embeds,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        pooled_output = outputs[1]

        seq_relationship_scores = self.cls(pooled_output)

        next_sentence_loss = None
        if labels is not None:
            loss_fct = CrossEntropyLoss()
            next_sentence_loss = loss_fct(seq_relationship_scores.view(-1, 2), labels.view(-1))

        if not return_dict:
            output = (seq_relationship_scores,) + outputs[2:]
            return ((next_sentence_loss,) + output) if next_sentence_loss is not None else output

        return NextSentencePredictorOutput(
            loss=next_sentence_loss,
            logits=seq_relationship_scores,
            hiddens=outputs.hiddens,
        )

class ForMultiChoice(PreTrained):
    def __init__(self, config):
        super().__init__(config)
        self.fnet = Model(config)
        self.drop = qc.Dropout(config.drop)
        self.classifier = qc.Linear(config.d_model, 1)
        self.post_init()

    def forward(
        self,
        input_ids=None,
        token_type_ids=None,
        position_ids=None,
        inputs_embeds=None,
        labels=None,
        output_hidden_states=None,
        return_dict=None,
    ):
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        num_choices = input_ids.shape[1] if input_ids is not None else inputs_embeds.shape[1]

        input_ids = input_ids.view(-1, input_ids.size(-1)) if input_ids is not None else None
        token_type_ids = (
            token_type_ids.view(-1, token_type_ids.size(-1)) if token_type_ids is not None else None
        )
        position_ids = (
            position_ids.view(-1, position_ids.size(-1)) if position_ids is not None else None
        )
        inputs_embeds = (
            inputs_embeds.view(-1, inputs_embeds.size(-2), inputs_embeds.size(-1))
            if inputs_embeds is not None
            else None
        )

        outputs = self.fnet(
            input_ids,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            inputs_embeds=inputs_embeds,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        pooled_output = outputs[1]

        pooled_output = self.drop(pooled_output)
        logits = self.classifier(pooled_output)
        reshaped_logits = logits.view(-1, num_choices)

        loss = None
        if labels is not None:
            loss_fct = CrossEntropyLoss()
            loss = loss_fct(reshaped_logits, labels)

        if not return_dict:
            output = (reshaped_logits,) + outputs[2:]
            return ((loss,) + output) if loss is not None else output

        return qo.WithLoss(loss=loss, logits=reshaped_logits, hiddens=outputs.hiddens)

class ForSeqClassifier(PreTrained):
    def __init__(self, **kw):
        super().__init__(**kw)
        cfg = self.get_cfg(kw)
        self.model = Model(**kw)
        self.proj = Classifier(**kw)

    forward = qf.forward_seq

class ForTokClassifier(PreTrained):
    def __init__(self, **kw):
        super().__init__(**kw)
        self.get_cfg(kw)
        self.model = Model(**kw)
        self.proj = Classifier(**kw)

    forward = qf.forward_tok

class ForQA(PreTrained):
    def __init__(self, **kw):
        super().__init__(**kw)
        cfg = self.get_cfg(kw)
        self.model = Model(**kw)
        self.proj = qc.Linear(cfg.d_model, cfg.n_labels, **kw)

    forward = qf.forward_qa

reason: from scipy


text:
"""Principal component analysis"""
from preprocess.preprocess import center, minmax_scale
from typing import Tuple
import numpy as np
from numpy.random import default_rng
from numpy import linalg as LA
from pandas import DataFrame
import matplotlib.pyplot as plt
from sklearn.utils.extmath import svd_flip

def svd(A: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    """
    Singular value decomposition.

    Params
    --------
    A : np.ndarray

    Returns
    --------
    U : np.ndarray - left singular vectors
    Sigma: np.ndarray - matrix of singular values on the diagonal
    V : np.ndarray - right singular vectors
    """
    # _, U = LA.eig(np.matmul(A, A.transpose()))
    lamdas, V = LA.eig(np.matmul(A.transpose(), A))
    S = np.sqrt(lamdas)

    # Get U based on V and S
    U = np.dot(A, LA.inv(V.transpose())) / S  # inefficient, but ¯\_(ツ)_/¯
    return U, S, V.transpose()

class PCA:
    @staticmethod
    def _calc_exlpained_variance_ratio(singular_values: np.ndarray) -> np.ndarray:
        """Estimates explained variance for the singular values"""
        n = singular_values.shape[0]
        explained_variance = (singular_values ** 2) / (n - 1)
        total_var = np.sum(explained_variance)
        explained_variance_ratio = explained_variance / total_var

        return explained_variance_ratio

    def fit(self, df: DataFrame, centered: bool = False, scaled: bool = False) -> None:
        """fits data"""
        self.labels = df.columns
        self.data = df.values

        if not centered:
            self.data = center(self.data)
        if not scaled:
            self.data = minmax_scale(self.data)

        U, S, Vt = svd(self.data)
        U, Vt = svd_flip(U, Vt)  # Ensures deterministic output

        self.components = Vt
        self.singular_values = np.flip(np.sort(S))
        self.explained_variance_ratio = PCA._calc_exlpained_variance_ratio(
            self.singular_values
        )

    def transform(self, n_components: int) -> np.ndarray:
        """
        Transforms data into specified number of principal components
        """
        return np.dot(self.data, self.components[:n_components].transpose())

    def loading_plot(self, index: int) -> Tuple[plt.Figure, plt.Axes]:
        """
        Creates a chart with the weights for the specified component.
        """
        fig, ax = plt.subplots(1, 1)
        ax.bar(self.labels, self.components[index])
        return fig, ax

    def explained_variance_plot(self) -> Tuple[plt.Figure, plt.Axes]:
        """
        Creates a plot with the cumulative explained variance.
        """
        fig, ax = plt.subplots(1, 1)
        n = self.explained_variance_ratio.shape[0]
        cumu_explained_var = [
            np.sum(self.explained_variance_ratio[0:x]) for x in np.arange(0, n)
        ]

        ax.plot(np.arange(0, n), cumu_explained_var)
        ax.set_xlabel("N components")
        ax.set_ylabel("Explained variance")

        return fig, ax

ext: py

max_stars_repo_name: BakerAugust/natelib

max_issues_repo_name: BakerAugust/natelib

max_forks_repo_name: BakerAugust/natelib

reason: import numpy,from numpy


text:
#!/usr/bin/env python
"""
Interface to Point Grey's Spinnaker C library. This uses a
helper C library (spinshim.dll) that provides a callback
for the Spinnaker ImageEvent. This is done in order to try
capture every frame from the camera, which spinCameraGetNextImage()
does not seem to gaurantee.

You will need the folder where the library is located in your
windows path. For me this is:

C:\Program Files\Point Grey Research\Spinnaker\bin64\vs2013\

FIXME: Fixed length film support?

NOTE: FLIR Imaging now provides Spinnaker support in Python:
      https://www.ptgrey.com/support/downloads

Hazen 05/17
"""

import ctypes
import numpy
import os

import storm_control.c_libraries.loadclib as loadclib

# Spinnaker error codes.
SPINNAKER_ERR_SUCCESS = 0

# Spin shim library error codes.
SPINSHIM_ERR_SUCCESS = 0
SPINSHIM_ERR_NO_NEW_IMAGES = -2005

# Global variables.
h_system = None
spindll = None
spinshimdll = None

class SpinnakerException(Exception):
    """
    Use this for serious errors.
    """
    pass

class SpinnakerExceptionNotFound(SpinnakerException):
    """
    Use this for property names that are not found.
    """
    pass

class SpinnakerExceptionValue(SpinnakerException):
    """
    Use this for unacceptable node/property values.
    """
    pass

#
# Functions.
#
def checkErrorCode(error_code, fn_name = "Unknown"):
    if (error_code != SPINNAKER_ERR_SUCCESS):
        print("Error " + str(error_code) + " in call to " + fn_name)

def loadSpinnakerDLL(dll_name):
    """
    Load the Spinnaker C library.
    """
    global spindll
    global spinshimdll
    if spindll is None:
        spindll = ctypes.windll.LoadLibrary(dll_name)
        spinshimdll = loadclib.loadCLibrary("spinshim")

def spinGetCamera(cam_id):
    [h_camera_list, n_cameras] = spinGetCameras()
    h_camera = ctypes.c_void_p(0)
    if isinstance(cam_id, int):
        c_index = ctypes.c_size_t(cam_id)
        checkErrorCode(spindll.spinCameraListGet(h_camera_list, c_index, ctypes.byref(h_camera)),
                       "spinCameraListGet")
        spinReleaseCameras(h_camera_list)
        return SpinCamera(h_camera)

def spinGetCameras():
    """
    Returns a pointer to a list of cameras and the number of cameras. This
    combines the following into a single call:

    spinCameraListCreateEmpty()
    spinSystemGetCameras()
    spinCameraListGetSize()
    """
    global h_system
    h_camera_list = ctypes.c_void_p(0)
    checkErrorCode(spindll.spinCameraListCreateEmpty(ctypes.byref(h_camera_list)), "spinCameraListCreateEmpty")
    checkErrorCode(spindll.spinSystemGetCameras(h_system, h_camera_list), "spinSystemGetCameras")
    num_cameras = ctypes.c_long(0)
    checkErrorCode(spindll.spinCameraListGetSize(h_camera_list, ctypes.byref(num_cameras)), "spinCameraListGetSize")
    return [h_camera_list, num_cameras.value]

def spinGetInterfaces():
    """
    Returns a pointer to a list of interfaces and the number of interfaces. This
    combines the following into a single call:

    spinInterfaceListCreateEmpty()
    spinSystemGetInterfaces()
    spinInterfaceListGetSize()
    """
    global h_system
    h_interface_list = ctypes.c_void_p(0)
    checkErrorCode(spindll.spinInterfaceListCreateEmpty(ctypes.byref(h_interface_list)), "spinInterfaceListCreateEmpty")
    checkErrorCode(spindll.spinSystemGetInterfaces(h_system, h_interface_list), "spinSystemGetInterfaces")
    num_interfaces = ctypes.c_long(0)
    checkErrorCode(spindll.spinInterfaceListGetSize(h_interface_list, ctypes.byref(num_interfaces)), "spinInterfaceListGetSize")
    return [h_interface_list, num_interfaces.value]

def spinReleaseCameras(h_camera_list):
    checkErrorCode(spindll.spinCameraListClear(h_camera_list), "spinCameraListClear")
    checkErrorCode(spindll.spinCameraListDestroy(h_camera_list), "spinCameraListDestroy")

def spinReleaseInterfaces(h_interface_list):
    checkErrorCode(spindll.spinInterfaceListClear(h_interface_list), "spinInterfaceListClear")
    checkErrorCode(spindll.spinInterfaceListDestroy(h_interface_list), "spinInterfaceListDestroy")

def spinSystemGetInstance():
    """
    Get singleton reference to the system object.
    """
    global h_system
    if h_system is None:
        h_system = ctypes.c_void_p(0)
        checkErrorCode(spindll.spinSystemGetInstance(ctypes.byref(h_system)), "spinSystemGetInstance")

def spinSystemReleaseInstance():
    global h_system
    if h_system is not None:
        checkErrorCode(spindll.spinSystemReleaseInstance(h_system), "spinSystemReleaseInstance")
        h_system = None

#
# Classes
#

class SCamData(object):
    """
    Storage of camera data.
    """
    def __init__(self, size = None, **kwds):
        super().__init__(**kwds)
        self.size = size
        self.np_array = numpy.ascontiguousarray(numpy.empty(size, dtype = numpy.uint16))

#    def copyData(self, data_ptr):
#        ctypes.memmove(self.np_array.ctypes.data, data_ptr, self.size)

    def getData(self):
        return self.np_array

    def getDataPtr(self):
        return self.np_array.ctypes.data

class ShimImage(ctypes.Structure):
    _fields_ = [("pixel_format", ctypes.c_int),
                ("height", ctypes.c_size_t),
                ("im_size", ctypes.c_size_t),
                ("width", ctypes.c_size_t),
                ("data", ctypes.c_void_p)]

class SpinCamera(object):
    """
    The interface to a single camera.

    Note: Currently this only works with Mono8, Mono12p, Mono12Packed and Mono16.
    """
    def __init__(self, h_camera = None, **kwds):
        super().__init__(**kwds)
        self.aoi_width = None
        self.aoi_height = None
        self.buffer_len = 50
        self.encoding = 'utf-8'
        self.h_camera = h_camera
        self.im_event = None
        self.pixel_format = None
        self.properties = {}
        self.verbose = True

        # Initialize.
        checkErrorCode(spindll.spinCameraInit(self.h_camera), "spinCameraInit")

        # Get handle to camera properties.
        self.h_node_map = ctypes.c_void_p(0)
        checkErrorCode(spindll.spinCameraGetNodeMap(self.h_camera, ctypes.byref(self.h_node_map)),
                       "spinCameraGetNodeMap")

        # Get handle to TL device properties.
        self.h_tl_device_node_map = ctypes.c_void_p(0)
        checkErrorCode(spindll.spinCameraGetTLDeviceNodeMap(self.h_camera, ctypes.byref(self.h_tl_device_node_map)),
                       "spinCameraGetTLDeviceNodeMap")

        # Get handle to TL stream properties.
        self.h_tl_stream_node_map = ctypes.c_void_p(0)
        checkErrorCode(spindll.spinCameraGetTLStreamNodeMap(self.h_camera, ctypes.byref(self.h_tl_stream_node_map)),
                       "spinCameraGetTLStreamNodeMap")

        self.node_maps = [self.h_node_map, self.h_tl_device_node_map, self.h_tl_stream_node_map]

        # Get stream buffer count property.
        self.stream_buffer_count = self.getProperty("StreamDefaultBufferCount")

    def getFrames(self):
        frames = []

        # Get all the images that are currently available.
        while True:

            #
            # Create an image, possibly wasteful as if we don't get an
            # image we'll immediately throw this object away.
            #
            image_size = self.aoi_width * self.aoi_height
            s_cam_data = SCamData(size = image_size)
            image = ShimImage(ctypes.c_int(self.pixel_format),
                              ctypes.c_size_t(self.aoi_height),
                              ctypes.c_size_t(image_size),
                              ctypes.c_size_t(self.aoi_width),
                              ctypes.c_void_p(s_cam_data.getDataPtr()))

            # Get the image.
            err_code = spinshimdll.getNextImage(self.im_event, ctypes.byref(image))

            # Check if we actually got an image.
            if (err_code != SPINSHIM_ERR_SUCCESS):
                break

            # Add to the list of images if we did.
            frames.append(s_cam_data)

        if (err_code != SPINSHIM_ERR_NO_NEW_IMAGES):
            raise SpinnakerException("Image acquisition error!", err_code)

        return [frames, [self.aoi_width, self.aoi_height]]

    def getProperty(self, p_name):
        """
        Get a camera property, loading it if we don't already have it cached.
        """
        if self.h_camera is None:
            return

        if not p_name in self.properties:

            # Get node by searching the node maps to see if we can find it.
            c_p_name = ctypes.c_char_p(p_name.encode(self.encoding))
            h_node = ctypes.c_void_p(0)
            for node_map in self.node_maps:
                checkErrorCode(spindll.spinNodeMapGetNode(node_map, c_p_name, ctypes.byref(h_node)),
                               "spinNodeMapGetNode")
                if h_node.value is not None:
                    break

            if h_node.value is None:
                raise SpinnakerException("Property " + p_name + " not found")

            # Get node type.
            p_type = ctypes.c_int(0)
            checkErrorCode(spindll.spinNodeGetType(h_node, ctypes.byref(p_type)),
                           "spinNodeGetType")
            p_type = p_type.value

            # Value (basically integer?) type.
            if (p_type == 0):
                self.properties[p_name] = SpinNodeValue(h_node = h_node)

            # Integer type.
            elif (p_type == 2):
                self.properties[p_name] = SpinNodeInteger(h_node = h_node)

            # Boolean type.
            elif (p_type == 3):
                self.properties[p_name] = SpinNodeBoolean(h_node = h_node)

            # Float type.
            elif (p_type == 4):
                self.properties[p_name] = SpinNodeFloat(h_node = h_node)

            # String type.
            elif (p_type == 6):
                self.properties[p_name] = SpinNodeString(h_node = h_node)

            # Enumeration type.
            elif (p_type == 8):
                self.properties[p_name] = SpinNodeEnumeration(h_node = h_node)

            else:
                print("Unknown node type", p_type)
                return None

        return self.properties[p_name]

    def listAllProperties(self):
        """
        This is strictly informational, calling it will print out all the
        available properties of the camera, of which there are a lot..
        """
        for i, node_map in enumerate(self.node_maps):

            # Get the number of nodes in the node map.
            p_value = ctypes.c_size_t(0)
            checkErrorCode(spindll.spinNodeMapGetNumNodes(node_map, ctypes.byref(p_value)),
                           "spinNodeMapGetNumNodes")
            p_value = p_value.value

            print("Node " + str(i) + " has " + str(p_value) + " properties.")

            # List all of the node names.
            for j in range(p_value):
                c_index = ctypes.c_size_t(j)
                h_node = ctypes.c_void_p(0)
                error_code = spindll.spinNodeMapGetNodeByIndex(node_map, c_index, ctypes.byref(h_node))

                if (error_code == SPINNAKER_ERR_SUCCESS):
                    spin_node = SpinNode(h_node)
                    print("  " + spin_node.name + " / " + spin_node.description)
            print("")

    def release(self):
        """
        Release the camera when finished.
        """
        if self.h_camera is None:
            return

        # Should we also de-initialize?
        checkErrorCode(spindll.spinCameraRelease(self.h_camera), "spinCameraRelease")
        self.h_camera = None

    def setProperty(self, pname, pvalue):
        """
        Set a camera property. The property must already exist in the cache in order to be set.
        """
        if self.h_camera is None:
            return

        if not pname in self.properties:
            raise SpinnakerExceptionNotFound("Property " + pname + " not in cache, cannot be set.")

        if self.verbose:
            print(">spinnaker setProperty", pname)
        self.properties[pname].spinNodeSetValue(pvalue)

    def shutdown(self):
        self.release()
        spinSystemReleaseInstance()

    def startAcquisition(self):
        if self.h_camera is None:
            return

        self.aoi_height = self.getProperty("Height").spinNodeGetValue()
        self.aoi_width = self.getProperty("Width").spinNodeGetValue()
        self.pixel_format = self.getProperty("PixelFormat").spinNodeGetValue(enum_value = True)

        # Configure number of stream buffers to match what we want.
        #
        # FIXME: Should depend on the exposure time. Maybe need enough buffer for up to 1 second?
        #
        self.stream_buffer_count.spinNodeSetValue(self.buffer_len)

        # Configure C shim library that handles camera ImageEvent.
        self.im_event = ctypes.c_void_p(0)
        c_buffer_len = ctypes.c_int(self.buffer_len)
        checkErrorCode(spinshimdll.configureImageEvent(self.h_camera, ctypes.byref(self.im_event), c_buffer_len),
                       "configureImageEvent")

        # Start acquisition.
        checkErrorCode(spindll.spinCameraBeginAcquisition(self.h_camera), "spinCameraBeginAcquisition")

    def stopAcquisition(self):
        if self.h_camera is None:
            return

        checkErrorCode(spindll.spinCameraEndAcquisition(self.h_camera), "spinCameraEndAcquisition")

        # Release C shim library.
        checkErrorCode(spinshimdll.releaseImageEvent(self.h_camera, self.im_event),
                       "releaseImageEvent")

class SpinNode(object):
    """
    Base class for accessing and configuring camera properties.
    """
    def __init__(self, h_node = None, **kwds):
        super().__init__(**kwds)
        self.encoding = 'utf-8'
        self.h_node = h_node
        self.value = None

        self.description = self.spinNodeGetDescription()
        self.name = self.spinNodeGetName()

    def spinNodeGetDescription(self):
        max_len = ctypes.c_size_t(1000)
        p_buf = ctypes.create_string_buffer(max_len.value)
        checkErrorCode(spindll.spinNodeGetDescription(self.h_node, p_buf, ctypes.byref(max_len)),
                       "spinNodeGetDescription")
        return p_buf.value.decode(self.encoding)

    def spinNodeGetName(self):
        max_len = ctypes.c_size_t(100)
        p_buf = ctypes.create_string_buffer(max_len.value)
        checkErrorCode(spindll.spinNodeGetName(self.h_node, p_buf, ctypes.byref(max_len)),
                       "spinNodeGetName")
        return p_buf.value.decode(self.encoding)

    def spinNodeGetValue(self):
        """
        Sub-classes should all implement this in order to work with Parameters.
        """
        pass

    def spinNodeIsReadable(self, warn = True):
        pb_result = ctypes.c_int8(0)
        checkErrorCode(spindll.spinNodeIsReadable(self.h_node, ctypes.byref(pb_result)),
                       "spinNodeIsReadable")
        readable = (pb_result.value == 1)
        if warn and not readable:
            print("Property " + self.name + " is not readable.")
        return readable

    def spinNodeIsWritable(self, warn = True):
        pb_result = ctypes.c_int8(0)
        checkErrorCode(spindll.spinNodeIsWritable(self.h_node, ctypes.byref(pb_result)),
                       "spinNodeIsWritable")
        writeable = (pb_result.value == 1)
        if warn and not writeable:
            print("Property " + self.name + " is not writeable.")
        return writeable

    def spinNodeSetValue(self):
        """
        Sub-classes should all implement this in order to work with Parameters.
        """
        pass

class SpinNodeBoolean(SpinNode):
    """
    Boolean values.
    """
    def __init__(self, **kwds):
        super().__init__(**kwds)
        self.value = self.spinNodeGetValue()

    def spinNodeGetValue(self):
        if self.spinNodeIsReadable():
            b_value = ctypes.c_int8(0)
            checkErrorCode(spindll.spinBooleanGetValue(self.h_node, ctypes.byref(b_value)),
                           "spinBooleanGetValue")
            self.value = (b_value.value == 1)
            return self.value

    def spinNodeSetValue(self, new_value):
        if (new_value != self.value):
            if not isinstance(new_value, bool):
                raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is not a boolean.")
            b_value = ctypes.c_int8(new_value)
            checkErrorCode(spindll.spinBooleanSetValue(self.h_node, b_value),
                           "spinBooleanSetValue")
            self.value = new_value
            return self.value

class SpinNodeEnumeration(SpinNode):
    """
    Enumerated values.
    """
    def __init__(self, **kwds):
        super().__init__(**kwds)

        # Get a list of all the possible value.
        p_value = ctypes.c_size_t(0)
        checkErrorCode(spindll.spinEnumerationGetNumEntries(self.h_node, ctypes.byref(p_value)),
                       "spinEnumerationGetNumEntries")
        n_entries = p_value.value

        self.values = []
        for i in range(n_entries):
            index = ctypes.c_size_t(i)
            h_entry = ctypes.c_void_p(0)
            checkErrorCode(spindll.spinEnumerationGetEntryByIndex(self.h_node, index, ctypes.byref(h_entry)),
                           "spinEnumerationEntryByIndex")
            self.values.append(self.spinEnumerationEntryGetSymbolic(h_entry))

        self.value = self.spinNodeGetValue()

    def spinEnumerationEntryGetEnumValue(self, h_entry):
        p_value = ctypes.c_uint64(0)
        checkErrorCode(spindll.spinEnumerationEntryGetEnumValue(h_entry, ctypes.byref(p_value)),
                       "spinEnumerationEntryGetEnumValue")
        return int(p_value.value)

    def spinEnumerationEntryGetSymbolic(self, h_entry):
        max_len = ctypes.c_size_t(100)
        p_buf = ctypes.create_string_buffer(max_len.value)
        checkErrorCode(spindll.spinEnumerationEntryGetSymbolic(h_entry, p_buf, ctypes.byref(max_len)),
                       "spinEnumerationEntryGetSymbolic")
        return p_buf.value.decode(self.encoding)

    def spinNodeGetValue(self, enum_value = False):
        if self.spinNodeIsReadable():
            h_entry = ctypes.c_void_p(0)
            checkErrorCode(spindll.spinEnumerationGetCurrentEntry(self.h_node, ctypes.byref(h_entry)),
                           "spinEnumerationGetCurrentEntry")
            self.value = self.spinEnumerationEntryGetSymbolic(h_entry)
            if enum_value:
                return self.spinEnumerationEntryGetEnumValue(h_entry)
            else:
                return self.value

    def spinNodeSetValue(self, new_value):
        if (new_value != self.value):
            if not isinstance(new_value, str):
                raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is not a string")
            if not new_value in self.values:
                raise SpinnakerExceptionValue("Value for " + self.name + " of " + new_value + " is not in " + str(self.values))

            if self.spinNodeIsWritable():
                h_choice = ctypes.c_void_p(0)
                c_value = ctypes.c_char_p(new_value.encode(self.encoding))
                checkErrorCode(spindll.spinEnumerationGetEntryByName(self.h_node, c_value, ctypes.byref(h_choice)),
                               "spinEnumerationGetEntryByName")
                choice = ctypes.c_uint64(0)
                checkErrorCode(spindll.spinEnumerationEntryGetIntValue(h_choice, ctypes.byref(choice)),
                               "spinEnumerationEntryGetEnumValue")
                checkErrorCode(spindll.spinEnumerationSetIntValue(self.h_node, choice),
                               "spinEnumerationEntrySetIntValue")

                self.value = new_value
                return self.value

class SpinNodeFloat(SpinNode):
    """
    Float camera property.
    """
    def __init__(self, **kwds):
        super().__init__(**kwds)

        # Get unit.
        max_len = ctypes.c_size_t(20)
        p_buf = ctypes.create_string_buffer(max_len.value)
        checkErrorCode(spindll.spinFloatGetUnit(self.h_node, p_buf, ctypes.byref(max_len)),
                       "spinFloatGetUnit")
        self.v_unit = p_buf.value.decode(self.encoding)

        self.value = self.spinNodeGetValue()

    def spinNodeGetMaximum(self):
        p_value = ctypes.c_double(0)
        checkErrorCode(spindll.spinFloatGetMax(self.h_node, ctypes.byref(p_value)),
                       "spinFloatGetMax")
        return p_value.value

    def spinNodeGetMinimum(self):
        p_value = ctypes.c_double(0)
        checkErrorCode(spindll.spinFloatGetMin(self.h_node, ctypes.byref(p_value)),
                       "spinFloatGetMin")
        return p_value.value

    def spinNodeGetValue(self):
        if self.spinNodeIsReadable():
            p_value = ctypes.c_double(0.0)
            checkErrorCode(spindll.spinFloatGetValue(self.h_node, ctypes.byref(p_value)),
                           "spinFloatGetValue")
            self.value = p_value.value
            return self.value

    def spinNodeSetValue(self, new_value):
        if (new_value != self.value):
            if not isinstance(new_value, float):
                raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is not a float")
            if self.spinNodeIsWritable():
                v_max = self.spinNodeGetMaximum()
                v_min = self.spinNodeGetMinimum()
                if (new_value < v_min):
                    raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is less than minumum of " + str(v_min))
                if (new_value > v_max):
                    raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is greater than maximum of " + str(v_max))

                value = ctypes.c_double(new_value)
                checkErrorCode(spindll.spinFloatSetValue(self.h_node, value),
                               "spinFloatSetValue")
                self.value = value.value
                return self.value

class SpinNodeInteger(SpinNode):
    """
    Integer camera property.
    """
    def __init__(self, **kwds):
        super().__init__(**kwds)

        # Get the current value.
        self.value = self.spinNodeGetValue()

    def spinNodeGetIncrement(self):
        p_value = ctypes.c_uint64(0)
        checkErrorCode(spindll.spinIntegerGetInc(self.h_node, ctypes.byref(p_value)),
                       "spinIntegerGetInc")
        return int(p_value.value)

    def spinNodeGetMaximum(self):
        p_value = ctypes.c_uint64(0)
        checkErrorCode(spindll.spinIntegerGetMax(self.h_node, ctypes.byref(p_value)),
                       "spinIntegerGetMax")
        return int(p_value.value)

    def spinNodeGetMinimum(self):
        p_value = ctypes.c_uint64(0)
        checkErrorCode(spindll.spinIntegerGetMin(self.h_node, ctypes.byref(p_value)),
                       "spinIntegerGetMin")
        return int(p_value.value)

    def spinNodeGetValue(self):
        if self.spinNodeIsReadable():
            p_value = ctypes.c_uint64(0)
            checkErrorCode(spindll.spinIntegerGetValue(self.h_node, ctypes.byref(p_value)),
                           "spinIntegerGetValue")
            self.value = int(p_value.value)
            return self.value

    def spinNodeSetValue(self, new_value):
        if (new_value != self.value):
            if not isinstance(new_value, int):
                raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is not an integer")
            if self.spinNodeIsWritable():
                v_inc = self.spinNodeGetIncrement()
                v_max = self.spinNodeGetMaximum()
                v_min = self.spinNodeGetMinimum()
                if (new_value < v_min):
                    raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is less than minumum of " + str(v_min))
                if (new_value > v_max):
                    raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is greater than maximum of " + str(v_max))
                if ((new_value % v_inc) != 0):
                    raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is not divisible by " + str(v_inc))

                value = ctypes.c_uint64(new_value)
                checkErrorCode(spindll.spinIntegerSetValue(self.h_node, value),
                               "spinIntegerSetValue")
                self.value = value.value
                return self.value

class SpinNodeString(SpinNode):
    """
    String camera property.
    """
    def spinNodeGetValue(self):
        if self.spinNodeIsReadable():
            max_len = ctypes.c_uint64(0)
            checkErrorCode(spindll.spinStringGetMaxLength(self.h_node, ctypes.byref(max_len)),
                           "spinStringGetMaxLength")
            max_len = ctypes.c_size_t(max_len.value + 1)
            p_buf = ctypes.create_string_buffer(max_len.value)
            checkErrorCode(spindll.spinStringGetValue(self.h_node, p_buf, ctypes.byref(max_len)),
                           "spinStringGetValue")
            self.value = p_buf.value.decode(self.encoding)
            return self.value

class SpinNodeValue(SpinNode):
    """
    ValueNode / spinEnumeration camera property.
    """
    def __init__(self, **kwds):
        super().__init__(**kwds)

        self.value = self.spinNodeGetValue()

    def spinNodeGetValue(self):
        if self.spinNodeIsReadable():
            p_value = ctypes.c_uint64(0)
            checkErrorCode(spindll.spinEnumerationEntryGetIntValue(self.h_node, ctypes.byref(p_value)),
                           "spinEnumerationEntryGetIntValue")
            self.value = p_value.value
            return self.value

    def spinNodeSetValue(self, new_value):
        if (new_value != self.value):
            if not isinstance(new_value, int):
                raise SpinnakerExceptionValue("Value for " + self.name + " of " + str(new_value) + " is not an integer")
            if self.spinNodeIsWritable():
                value = ctypes.c_uint64(new_value)
                checkErrorCode(spindll.spinEnumerationSetIntValue(self.h_node, value),
                               "spinEnumerationSetIntValue")
                self.value = value.value
                return self.value

if (__name__ == "__main__"):
    import os
    import time

    # Load the Spinnaker C library.
    dll_name = r'C:\Program Files\Point Grey Research\Spinnaker\bin64\vs2013\SpinnakerC_v120.dll'
    if os.path.exists(dll_name):
        loadSpinnakerDLL(dll_name)
    else:
        print(dll_name + " not found")
        exit()

    # Initialize.
    spinSystemGetInstance()

    # Query interfaces.
    [h_interface_list, num_interfaces] = spinGetInterfaces()
    print("Found " + str(num_interfaces) + " interfaces.")

    # Query cameras.
    [h_camera_list, num_cameras] = spinGetCameras()
    print("Found " + str(num_cameras) + " cameras.")

    # Get the first camera.
    cam = spinGetCamera(0)

    if False:
        # Print out all the available properties.
        cam.listAllProperties()

    if True:
        # Get some properties from the camera.
        pnames = ["DeviceModelName",
                  "AcquisitionFrameRate",
                  "AcquisitionFrameRateAuto",
                  "BlackLevel",
                  "BlackLevelClampingEnable",
                  "ExposureTime",
                  "ExposureAuto",
                  "Gain",
                  "GammaEnabled",
                  "OnBoardColorProcessEnabled",
                  "PixelFormat",
                  "pgrDefectPixelCorrectionEnable",
                  "SharpnessEnabled",
                  "VideoMode"]
        for pname in pnames:
            prop = cam.getProperty(pname)
            print(pname, prop.spinNodeGetValue())

    if True:
        # Set some properties of the camera.
        cam.setProperty("AcquisitionFrameRate",10.0)
        cam.setProperty("ExposureTime", 99000.0)
        cam.setProperty("BlackLevel", 5.0)
        cam.setProperty("Gain", 20.0)

        # Capture a few frames.
        frames = []
        cam.startAcquisition()
        for i in range(10):
            frames.extend(cam.getFrames()[0])
            time.sleep(0.2)
        cam.stopAcquisition()

        # Print frame statistics.
        for i, frame in enumerate(frames):
            np_array = frame.getData()
            print(i, np_array[0:5])
            print(i, numpy.mean(np_array), numpy.std(np_array), numpy.max(np_array))
            print("")

    # Clean up.
    cam.release()
    spinReleaseInterfaces(h_interface_list)
    spinReleaseCameras(h_camera_list)
    spinSystemReleaseInstance()

#
# The MIT License
#
# Copyright (c) 2016 Zhuang Lab, Harvard University
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

max_stars_repo_name: jeffmoffitt/storm-control

max_issues_repo_name: jeffmoffitt/storm-control

max_forks_repo_name: jeffmoffitt/storm-control

reason: import numpy


text:
from sympy import Rational as frac
from sympy import pi, sqrt

from ..helpers import book, fsd, pm, pm_array0, untangle
from ._helpers import Enr2Scheme
from ._stenger import stenger_7a as stroud_enr2_7_3a
from ._stenger import stenger_7b as stroud_enr2_7_3b
from ._stenger import stenger_9a as stroud_enr2_9_1a
from ._stenger import stenger_9b as stroud_enr2_9_1b
from ._stenger import stenger_11a as stroud_enr2_11_1a
from ._stenger import stenger_11b as stroud_enr2_11_1b
from ._stroud_1967_5 import stroud_1967_5_a as stroud_enr2_5_1a
from ._stroud_1967_5 import stroud_1967_5_b as stroud_enr2_5_1b
from ._stroud_1967_7 import stroud_1967_7_2a as stroud_enr2_7_1a
from ._stroud_1967_7 import stroud_1967_7_2b as stroud_enr2_7_1b
from ._stroud_1967_7 import stroud_1967_7_4 as stroud_enr2_7_2
from ._stroud_secrest import stroud_secrest_1 as stroud_enr2_3_1
from ._stroud_secrest import stroud_secrest_2 as stroud_enr2_3_2
from ._stroud_secrest import stroud_secrest_4 as stroud_enr2_5_2

citation = book(
    authors=["Arthur Stroud"],
    title="Approximate Calculation of Multiple Integrals",
    publisher="Prentice Hall",
    year="1971",
)

def stroud_enr2_5_3(n):
    assert n > 2

    r = sqrt(frac(n + 2, 4))
    s = sqrt(frac(n + 2, 2 * (n - 2)))
    A = frac(4, (n + 2) ** 2)
    B = frac((n - 2) ** 2, 2 ** n * (n + 2) ** 2)

    data = [(A, fsd(n, (r, 1))), (B, pm(n, s))]
    points, weights = untangle(data)
    weights *= sqrt(pi) ** n
    return Enr2Scheme("Stroud Enr2 5-3", n, weights, points, 5, citation)

def stroud_enr2_5_4(n):
    # Spherical product Lobatto
    B0 = frac(2, (n + 2))
    data = [(B0, [n * [0]])]
    for k in range(1, n + 1):
        rk = sqrt(frac(k + 2, 2))
        s = sqrt(frac(1, 2))
        arr = [rk] + (n - k) * [s]
        idx = list(range(k - 1, n))
        alpha = frac(2 ** (k - n), (k + 1) * (k + 2))
        data += [(alpha, pm_array0(n, arr, idx))]

    points, weights = untangle(data)
    weights *= sqrt(pi) ** n
    return Enr2Scheme("Stroud Enr2 5-4", n, weights, points, 5, citation)

def _stroud_5_5(n, variant_a):
    p_m = +1 if variant_a else -1

    # r is complex-valued for n >= 3
    r = sqrt((n + 2 + p_m * (n - 1) * sqrt(2 * (n + 2))) / (2 * n))
    s = sqrt((n + 2 - p_m * sqrt(2 * (n + 2))) / (2 * n))
    A = frac(2, n + 2)
    B = frac(1, 2 ** n * (n + 2))

    data = [(A, [n * [0]]), (B, fsd(n, (r, 1), (s, n - 1)))]

    points, weights = untangle(data)
    weights *= sqrt(pi) ** n
    name = "Stroud Enr2 5-5{}".format("a" if variant_a else "b")
    return Enr2Scheme(name, n, weights, points, 5, citation)

def stroud_enr2_5_5a(n):
    return _stroud_5_5(n, True)

def stroud_enr2_5_5b(n):
    return _stroud_5_5(n, False)

def stroud_enr2_5_6(n):
    assert n >= 5

    sqrt2 = sqrt(2)
    sqrt2n1 = sqrt(2 * (n + 1))
    r = sqrt((n - sqrt2 + (n - 1) * sqrt2n1) / (2 * n))
    s = sqrt((n - sqrt2 - sqrt2n1) / (2 * n))
    t = sqrt((1 + sqrt2) / 2)
    A = frac(1, 2 ** n * (n + 1))

    data = [(A, fsd(n, (r, 1), (s, n - 1))), (A, pm(n, t))]

    points, weights = untangle(data)
    weights *= sqrt(pi) ** n
    return Enr2Scheme("Stroud Enr2 5-6", n, weights, points, 5, citation)

__all__ = [
    "stroud_enr2_3_1",
    "stroud_enr2_3_2",
    "stroud_enr2_5_1a",
    "stroud_enr2_5_1b",
    "stroud_enr2_5_2",
    "stroud_enr2_5_3",
    "stroud_enr2_5_4",
    "stroud_enr2_5_5a",
    "stroud_enr2_5_5b",
    "stroud_enr2_5_6",
    "stroud_enr2_7_1a",
    "stroud_enr2_7_1b",
    "stroud_enr2_7_2",
    "stroud_enr2_7_3a",
    "stroud_enr2_7_3b",
    "stroud_enr2_9_1a",
    "stroud_enr2_9_1b",
    "stroud_enr2_11_1a",
    "stroud_enr2_11_1b",
]

max_stars_repo_name: dariusarnold/quadpy

max_issues_repo_name: dariusarnold/quadpy

max_forks_repo_name: dariusarnold/quadpy

reason: from sympy


text:
import os
import unittest
import json
import networkx as nx
from graphene import graphgen

node_mapper_filename   = './node_mapper.json'
edge_mapper_filename   = './edge_mapper.json'
data_filename          = './test_data.txt'

class TestReadEdges(unittest.TestCase):

    def setUp(self):
        self.node_mapper = None
        self.edge_mapper = None
        self.data = []
        with open(node_mapper_filename) as f:
            self.node_mapper = json.load(f)
        with open(edge_mapper_filename) as f:
            self.edge_mapper = json.load(f)
        with open(data_filename) as f:
            for item in f:
                self.data.append(json.loads(item))

    def test_genEdges(self):
        g = nx.Graph()
        g = graphgen.create_graph(g,
                graph_mapper = self.edge_mapper,
                data_provider = self.data, add_type_to_key = True)
        # print('\nNODES1:', list(g.nodes(data = True)))
        self.assertEqual(nx.number_of_nodes(g), 15)
        self.assertEqual(nx.number_of_edges(g), 10)
        # get node with key.
        key1 = ('TypeA', 'a_val2_1')
        key2 = ('TypeB', 'b_val2_1', 'b_val1_1')
        key3 = ('TypeB', 'b_val2_3', 'b_val1_3')
        key4 = ('TypeC', 'c_val1_3')
        self.assertTrue(key1 in g.nodes)
        self.assertTrue(key2 in g.nodes)
        # print(g.node[key1])
        # print(g.node[key2])
        # check eges with data
        self.assertTrue(g.has_edge(key1, key2))
        edge_data = g.get_edge_data(key1, key2)
        self.assertTrue(edge_data != {})
        # print('e1:', edge_data)
        self.assertTrue(g.has_edge(key3, key4))
        edge_data = g.get_edge_data(key3, key4)
        self.assertTrue(edge_data != {})
        # print('e2:', edge_data)
        key5 = ('TypeC', 'ABCDEF') # invalid node key
        self.assertFalse(key5 in g)
        self.assertFalse(g.has_edge(key2, key5))

    def test_genNodesAndEdges(self):
        g = nx.Graph()
        g = graphgen.create_graph(g,
                graph_mapper = self.node_mapper,
                data_provider = self.data, add_type_to_key = True)
        g = graphgen.create_graph(g,
                graph_mapper= self.edge_mapper,
                data_provider= self.data, add_type_to_key= True)
        # print('\nNODES2:', list(g.nodes(data = True)))
        self.assertEqual(nx.number_of_nodes(g), 15)
        self.assertEqual(nx.number_of_edges(g), 10)
        # locate an edge
        key1 = ('TypeA', 'a_val2_3')
        key2 = ('TypeB', 'b_val2_3', 'b_val1_3')
        self.assertTrue(g.has_node(key1))
        self.assertTrue(g.has_node(key2))
        self.assertTrue(key2 in g)
        self.assertTrue(g.has_edge(key1, key2))
        # check node data
        node_data = g.nodes[key1]
        self.assertTrue(node_data != {})
        # print('node_data:', node_data)
        # check edge data
        edge_data = g.get_edge_data(key1, key2)
        self.assertTrue(edge_data != {})
        # the graph is bidirectional
        self.assertTrue(g.has_edge(key2, key1))
        # print('edge:', g.edges[(key2, key1)])

# if __name__ == '__main__':
#     unittest.main()

max_stars_repo_name: genimind/graphene

max_issues_repo_name: genimind/graphene

max_forks_repo_name: genimind/graphene

reason: import networkx


text:
from numpy.core.fromnumeric import ravel
import plotly.figure_factory as ff
import plotly.graph_objects as go
import plotly.express as px
import statistics as st
import random as rd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split as tts
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score as AS
from sklearn.metrics import confusion_matrix as CXM

data_file = pd.read_csv("c117/heart.csv")

age =  data_file["age"]
target =  data_file["target"]

age_train,age_test,target_train,target_test =  tts(age,target,test_size=0.25,random_state=0)

# to train the data using reshape()
X = np.reshape(age_train.ravel(),(len(age_train),1))
Y = np.reshape(target_train.ravel(),(len(target_train),1))

classifier = LogisticRegression(random_state=0)
classifier.fit(X,Y)

X_test = np.reshape(age_test.ravel(),(len(age_test),1))
Y_test = np.reshape(target_test.ravel(),(len(target_test),1))

y_predict = classifier.predict(X_test)
predict_values = []

for i in y_predict:
    if i == 0:
        predict_values.append("no")
    else:
        predict_values.append("yes")

actual_values = []

for x in Y_test.ravel():
    if x == 0:
        actual_values.append("no")
    else:
        actual_values.append("yes")

# to plot graph data

labels = ["no","yes"]

cm_result = CXM(actual_values,predict_values)

graph_heat_map  = plt.subplot()
sns.heatmap(cm_result, annot=True, ax = graph_heat_map)
graph_heat_map.set_xlabel("predict")
graph_heat_map.set_ylabel("actual")
graph_heat_map.set_title("confusion matrix")
graph_heat_map.xaxis.set_ticklabels(labels); graph_heat_map.yaxis.set_ticklabels(labels)
# plt.show()

true_positve = 36
false_positve = 17
true_negative = 16
false_negative = 7

result = (true_positve+true_negative)/(true_positve+true_negative+false_negative+false_positve)
print(result)

max_stars_repo_name: Gyaanendra/all-class-code-whitehatjr

max_issues_repo_name: Gyaanendra/all-class-code-whitehatjr

max_forks_repo_name: Gyaanendra/all-class-code-whitehatjr


text:
# -*- coding: utf-8 -*-

from sqpdfo.runtime import *
from numpy import inf, array
from copy import copy
from sqpdfo.sqpdfo_values import sqpdfoValues

def sqpdfo_options_(info_=None,options_=None,*args,**kwargs):
    """
# [info,options,values] = sqpdfo_options (info,options)
#
# Set the default options of the optimizer SQPDFO
    """
    info = copy(info_)
    if options_ is None:
        class options:
            pass
    else:
        options = copy(options_)

    # Define sqpdfo constant values

    values = sqpdfoValues()

    # Initialization

    info.flag = values.success

    # Set options

    if isfield_(options, 'fout'):
        if options.fout < 0:
            fprintf_('\n### sqpdfo_options: options.fout = "%0i" is not a valid file identifier' % (options.fout))
            fprintf_('\n    options.fout is set to 1\n\n')
            options.fout = 1
    else:
        options.fout = 1  # default value

    if isfield_(options, 'verbose'):
        if (options.verbose < 0) or (options.verbose > 6):
            fprintf_(options.fout,
                     '\n### sqpdfo_options: options.verbose = "%0i" and should be in [0,6], reset to 1\n\n' % (options.verbose))
            options.verbose = 1
    else:
        options.verbose = 1  # default value

    if isfield_(options, 'algo_descent'):
        algo_descent_str = options.algo_descent.lower().replace(" ", "")
        if (algo_descent_str == 'powell'):
            options.algo_descent = values.powell
        elif (algo_descent_str == 'wolfe'):
            options.algo_descent = values.wolfe
        else:
            if options.verbose:
                fprintf_(options.fout,
                         '\n### sqpdfo_options: options.algo_descent "%s" not recognized\n\n' % (options.algo_descent))
            info.flag = values.fail_on_argument
            return info, options, values
    else:
        options.algo_descent = values.powell  # default value

    if isfield_(options, 'dxmin'):
        if (options.dxmin <= 0):
            if options.verbose:
                fprintf_(options.fout, '\n### sqpdfo_options: options.dxmin = %g must be > 0\n\n' % (options.dxmin))
            info.flag = values.fail_on_argument
            return info, options, values
    else:
        options.dxmin = 1e-08  # default value

    if isfield_(options, 'inf'):
        if options.inf <= 0:
            if options.verbose:
                fprintf_('\n### sqpdfo_options: incorrect value of options.inf %g (should be > 0)\n\n' % (options.inf))
            info.flag = values.fail_on_argument
            return info, options, values
    else:
        options.inf = inf  # default value

    if isfield_(options, 'miter'):
        if options.miter <= 0:
            if options.verbose:
                fprintf_('\n### sqpdfo_options: incorrect value of options.miter %g (should be > 0)\n\n' % (options.miter))
            info.flag = values.fail_on_argument
            return info, options, values
    else:
        options.miter = 1000  # default value

    if isfield_(options,'msimul'):
        if options.msimul <= 0:
           if options.verbose:
              fprintf_('\n### sqpdfo_options: incorrect value of options.msimul %g (should be > 0)\n\n',options.msimul)
           info.flag = values.fail_on_argument;
           return info, options, values
    else:
        options.msimul = 1000  # default value

    if isfield_(options, 'tol_grad'):
        if (options.tol_grad < 0):
            if options.verbose:
                fprintf_('\n### sqpdfo_options: incorrect value of some options.tol_grad (should be > 0)\n\n')
            info.flag = values.fail_on_argument
            return info, options, values
        elif options.tol_grad==0:
                options.tol_grad=1e-5
    else:
        options.tol_grad = 1e-5  # default value

    if isfield_(options, 'tol_feas'):
        if (options.tol_feas < 0):
            if options.verbose:
                fprintf_('\n### sqpdfo_options: incorrect value of some options.tol_feas (should be > 0)\n\n')
            info.flag = values.fail_on_argument
            return info, options, values
        elif options.tol_feas==0:
                options.tol_feas=1e-5
    else:
        options.tol_feas = 1e-5  # default value

    if isfield_(options, 'tol_bnds'):
        if (options.tol_bnds < 0):
            if options.verbose:
                fprintf_('\n### sqpdfo_options: incorrect value of some options.tol_bnds (should be > 0)\n\n')
            info.flag = values.fail_on_argument
            return info, options, values
        elif options.tol_bnds==0:
                options.tol_bnds=1e-5
    else:
        options.tol_bnds = 1e-5  # default value

    if isfield_(options, 'hess_approx'):
        if 'bfgs' == options.hess_approx.lower().replace(" ", ""):
            options.hess_approx = values.bfgs
        elif 'model' == options.hess_approx.lower().replace(" ", ""):
            options.hess_approx = values.model
        else:
            if options.verbose:
                fprintf_(options.fout,
                         '\n### sqpdfo_options: options.hess_approx "%s" not recognized' % (options.hess_approx))
                fprintf_('\n    options.hess_approx must be either "model" or "bfgs"\n\n')
            info.flag = values.fail_on_argument
            return info, options, values
    else:
        options.hess_approx = values.model  # default value

    if isfield_(options, 'whichmodel'):
        if 'subbasis' == options.whichmodel.lower().replace(" ", ""):
            options.whichmodel = values.subbasis
        elif 'frobnorm' == options.whichmodel.lower().replace(" ", ""):
            options.whichmodel = values.frobnorm
        elif 'l2norm' == options.whichmodel.lower().replace(" ", ""):
            options.whichmodel = values.l2norm
        elif 'regression' == options.whichmodel.lower().replace(" ", ""):
            options.whichmodel = values.regression
        else:
            if options.verbose:
                fprintf_(options.fout,
                         '\n### sqpdfo_options: options.whichmodel "%s" not recognized' % (options.whichmodel))
                fprintf_('\n    options.whichmodel must be either "Subbasis", "Frobnorm", "L2norm" or "Regression"\n\n')
            info.flag = values.fail_on_argument
            return info, options, values
    else:
        options.whichmodel = 0  # default value

    if isfield_(options, 'final_degree'):
        if 'linear' == options.final_degree.lower().replace(" ", ""):
            options.final_degree = values.linear
        elif 'diagonal' == options.final_degree.lower().replace(" ", ""):
            options.final_degree = values.diagonal
        elif 'quadratic' == options.final_degree.lower().replace(" ", ""):
            options.final_degree = values.quadratic
        else:
            if options.verbose:
                fprintf_(options.fout,
                         '\n### sqpdfo_options: options.final_degree "%s" not recognized' % (options.final_degree))
                fprintf_('\n    options.final_degree must be either "linear", "diagonal" or "quadratic"\n\n')
            info.flag = values.fail_on_argument
            return info, options, values
    else:
        options.final_degree = values.diagonal  # default value

    return info, options, values

max_stars_repo_name: DLR-SC/sqpdfo

max_issues_repo_name: DLR-SC/sqpdfo

max_forks_repo_name: DLR-SC/sqpdfo

reason: from numpy


text:
import numpy as np

from prml.nn.function import Function

class Square(Function):
    @staticmethod
    def _forward(x):
        return np.square(x)

    @staticmethod
    def _backward(delta, x):
        return 2 * delta * x

def square(x):
    return Square().forward(x)

max_stars_repo_name: alexandru-dinu/PRML

max_issues_repo_name: alexandru-dinu/PRML

max_forks_repo_name: alexandru-dinu/PRML


text:
from parcels import FieldSet, ParticleSet, JITParticle
from parcels import Variable, ErrorCode, Field
from datetime import timedelta
from datetime import datetime
from parcels import GeographicPolar, Geographic
import numpy as np
import sys
import local_kernels as kernels
import xarray as xr

series = 8
resusTime = 171
shoreTime = 10
start_time = datetime.strptime('2016-04-01 12:00:00',
                               '%Y-%m-%d %H:%M:%S')
# end_time = '2020-08-31'
n_days = 1600  # number of days to simulate
K_bar = 10  # diffusion value
stored_dt = 24  # hours
loc = sys.argv[1]

# data = '../data/mercatorpsy4v3r1_gl12_mean_20180101_R20180110.nc'
data = '/data/oceanparcels/input_data/CMEMS/' + \
    'GLOBAL_ANALYSIS_FORECAST_PHY_001_024_SMOC/*.nc'  # Gemini hourly
output_path = f'/scratch/cpierard/sa-s{series:02d}-{loc}.nc'

# loading the fields that have to do with the coastline.
coastal_fields = xr.load_dataset('coastal_fields.nc')
# time range 2018-01-01 to 2019-11-27
filesnames = {'U': data,
              'V': data}

variables = {'U': 'utotal',
             'V': 'vtotal'}  # Use utotal

dimensions = {'lat': 'latitude',
              'lon': 'longitude',
              'time': 'time'}

indices = {'lat': range(*coastal_fields.index_lat),
           'lon': range(*coastal_fields.index_lon)}

fieldset = FieldSet.from_netcdf(filesnames, variables, dimensions,
                                allow_time_extrapolation=True,
                                indices=indices)

###############################################################################
# Adding the border current, which applies for all scenarios except for 0     #
###############################################################################
u_border = coastal_fields.coastal_u.values
v_border = coastal_fields.coastal_v.values
fieldset.add_field(Field('borU', data=u_border,
                         lon=fieldset.U.grid.lon, lat=fieldset.U.grid.lat,
                         mesh='spherical'))
fieldset.add_field(Field('borV', data=v_border,
                         lon=fieldset.U.grid.lon, lat=fieldset.U.grid.lat,
                         mesh='spherical'))
fieldset.borU.units = GeographicPolar()
fieldset.borV.units = Geographic()

###############################################################################
# Adding in the  land cell identifiers                                        #
###############################################################################
landID = coastal_fields.landmask.values
fieldset.add_field(Field('landID', landID,
                         lon=fieldset.U.grid.lon, lat=fieldset.U.grid.lat,
                         mesh='spherical'))

###############################################################################
# Adding the horizontal diffusion                                             #
###############################################################################
size2D = (fieldset.U.grid.ydim, fieldset.U.grid.xdim)
K_h = K_bar * np.ones(size2D)
nx, ny = np.where(landID == 1)
K_h[nx, ny] = 0
fieldset.add_field(Field('Kh_zonal', data=K_h,
                         lon=fieldset.U.grid.lon, lat=fieldset.U.grid.lat,
                         mesh='spherical'))
fieldset.add_field(Field('Kh_meridional', data=K_h,
                         lon=fieldset.U.grid.lon, lat=fieldset.U.grid.lat,
                         mesh='spherical'))

###############################################################################
# Distance to the shore                                                       #
###############################################################################
distance = coastal_fields.distance2shore.values
fieldset.add_field(Field('distance2shore', distance,
                         lon=fieldset.U.grid.lon, lat=fieldset.U.grid.lat,
                         mesh='spherical'))

class SimpleBeachingResuspensionParticle(JITParticle):
    # Now the beaching variables
    # 0=open ocean, 1=beached
    beach = Variable('beach', dtype=np.int32,
                     initial=0)
    # Now the setting of the resuspension time and beaching time
    resus_t = Variable('resus_t', dtype=np.float32,
                       initial=resusTime, to_write=False)
    coastPar = Variable('coastPar', dtype=np.float32,
                        initial=shoreTime, to_write=False)
    # Finally, I want to keep track of the age of the particle
    age = Variable('age', dtype=np.int32, initial=0)

    # Distance of the particle to the coast
    distance = Variable('distance', dtype=np.float32, initial=0)

#####################################
# Opening file with positions and sampling dates.
# river_sources = np.load('river_sources.npy', allow_pickle=True).item()
release_positions = np.load('release_positions.npy', allow_pickle=True).item()
n_points = release_positions[loc].shape[0]

np.random.seed(0)  # to repeat experiment in the same conditions
# time = datetime.datetime.strptime('2018-01-01 12:00:00', '%Y-%m-%d %H:%M:%S')

lon_cluster = release_positions[loc]['X_bin'].values
lat_cluster = release_positions[loc]['Y_bin'].values
beached = np.zeros_like(lon_cluster)
age_par = np.zeros_like(lon_cluster)

# date_cluster = np.repeat(start_time, n_points)
date_cluster = np.empty(n_points, dtype='O')
for i in range(n_points):
    random_date = start_time + timedelta(days=np.random.randint(0, 365),
                                         hours=np.random.randint(0, 23))
    date_cluster[i] = random_date

# creating the Particle set
pset = ParticleSet.from_list(fieldset=fieldset,
                             pclass=SimpleBeachingResuspensionParticle,
                             lon=lon_cluster,
                             lat=lat_cluster,
                             time=date_cluster,
                             beach=beached, age=age_par)

###############################################################################
# And now the overall kernel                                                  #
###############################################################################

def delete_particle(particle, fieldset, time):
    particle.delete()

totalKernel = pset.Kernel(kernels.AdvectionRK4_floating) + \
    pset.Kernel(kernels.AntiBeachNudging) + \
    pset.Kernel(kernels.BrownianMotion2D) + \
    pset.Kernel(kernels.beach)

# Output file
output_file = pset.ParticleFile(
    name=output_path,
    outputdt=timedelta(hours=stored_dt))

# Execute!
pset.execute(totalKernel,
             runtime=timedelta(days=n_days),
             dt=timedelta(hours=1),
             output_file=output_file,
             recovery={ErrorCode.ErrorOutOfBounds: delete_particle})
output_file.close()


text:
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
from Distributions import Porosity_Distribution

###############################################################################
### Load or calculate data
###############################################################################

FCC_2_1=dict(
     r2 = dict(pmean=0.296,pstd=0.145),
     r8  = dict(pmean=0.29,pstd=0.09),
     r32 = dict(pmean=0.28,pstd=0.044),
    )

M1 = Porosity_Distribution(**FCC_2_1['r2'])
M2 = Porosity_Distribution(**FCC_2_1['r8'])
M3 = Porosity_Distribution(**FCC_2_1['r32'])
px = np.arange(0,1.0,0.001)         # por-values for plotting

M1.pdf_stats()

print("Mean of truncated normal distribution (n=2): {:.3f}".format(M1.pmean_trunc))
print("Mean of parent normal distribution (n=2): {:.3f}".format(M1.pmean))
print("Relative difference in mean: {:.1f} % \n".format(100*abs(M1.pmean_trunc-M1.pmean)/M1.pmean_trunc))

print("Std of truncated normal distribution (n=2): {:.4f}".format(M1.pstd_trunc))
print("Std of parent normal distribution (n=2): {:.4f}".format(M1.pstd))
print("Relative difference in std: {:.1f} % \n".format(100*abs(M1.pstd_trunc-M1.pstd)/M1.pstd_trunc))

###############################################################################
### Plottings
###############################################################################
lw = 2
textsize = 8
plt.close('all')

plt.figure(figsize=[3.75,2.5])
ax=plt.subplot(111)

ax.plot(px,M1.connectivity_func(px),ls='-',c='k',lw=lw+1,label=r'$p_\mathrm{con}(\theta)$',zorder=5)

ax.plot(px,M1.pdf_porosity(px),ls='-',c='C0',lw=lw,label=r'$2 \mu m$',zorder=4)
ax.plot(px,M1.pdf_porosity_connected(px),ls='--',c='C0',lw=lw)
ax.fill_between(px,M1.pdf_porosity(px),M1.pdf_porosity_connected(px),color='C0',zorder=1,alpha = 0.5)

ax.plot(px,M2.pdf_porosity(px),ls='-',c='C2',lw=lw,label=r'$8 \mu m$',zorder=3)
ax.plot(px,M2.pdf_porosity_connected(px),ls='--',c='C2',zorder=1,lw=lw)
ax.fill_between(px,M2.pdf_porosity(px),M2.pdf_porosity_connected(px),color='C2',zorder=1,alpha = 0.5)

ax.plot(px,M3.pdf_porosity(px),ls='-',c='C9',lw=lw,label=r'$32 \mu m$',zorder=2)
ax.plot(px,M3.pdf_porosity_connected(px),ls='--',c='C9',zorder=1,lw=lw)
ax.fill_between(px,M3.pdf_porosity(px),M3.pdf_porosity_connected(px),color='C9',zorder=1,alpha = 0.5)

ax.set_xlabel(r'Porosity $\theta$',fontsize=textsize)
ax.set_ylabel('Probability',fontsize=textsize)
ax.legend(loc='upper right',fontsize=textsize)
ax.tick_params(axis="both",which="major",labelsize=textsize)
ax.grid(True)
ax.set_xlim([0,0.6])
ax.set_ylim([0,7])

plt.tight_layout()
# plt.savefig('../results/Fig01_pdf_porosity.png',dpi=300)
plt.savefig('../results/Fig01_pdf_porosity.pdf')

print("Total dis-connectivity for n=2:  {:.3f}".format(1-M1.connectivity_total))
print("Total dis-connectivity for n=8:  {:.3f}".format(1-M2.connectivity_total))
print("Total dis-connectivity for n=32: {:.3f}".format(1-M3.connectivity_total))

max_stars_repo_name: AlrauneZ/TransportAbility

max_issues_repo_name: AlrauneZ/TransportAbility

max_forks_repo_name: AlrauneZ/TransportAbility


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import glob
import os
from xml.dom import minidom

import astropy.units as u
from astropy.io import ascii
from astropy.table import Table, QTable

try:
    from .version import __version__
except ImportError:
    __version__ = "unknown"
__all__ = []

_package_directory = os.path.dirname(os.path.abspath(__file__))
_data_directory = os.path.abspath(os.path.join(_package_directory, 'data'))

_metadata_files = glob.glob(os.path.join(_data_directory, '*.xml'))

# hard code the expected meta data in each meta file
_column_data = {"datafilename": [], "description": [], "keywords": [], "event_id": [],
                "instruments": [], "reference_doi": [], "acknowledgment": [], "notes": [],
                "col_units": []}

for this_metadata_file in _metadata_files:
    mydoc = minidom.parse(this_metadata_file)
    metas = mydoc.getElementsByTagName('meta')

    for elem in metas:
        _column_data[elem.attributes['name'].value].append(elem.firstChild.data)

dataset_index = Table(_column_data)
dataset_index.add_index('datafilename')

def load_dataset(filename):
    """Load a dataset.

    Returns
    -------
    result: QTable
    """
    datafile_path = os.path.join(_data_directory, filename)
    result = QTable(ascii.read(datafile_path, format='csv', fast_reader=False))

    # add the meta data
    this_row = dataset_index.loc['dem_quiet_sun.csv']
    meta_dict = dict(zip(this_row.colnames, this_row))
    result.meta = meta_dict

    if dataset_index.loc[filename]['col_units'] != "None":
        units_list = dataset_index.loc[filename]['col_units'].split(',')
        for this_unit, this_col in zip(units_list, result.columns):
            result[this_col].unit = u.Unit(this_unit)
    return result

reason: import astropy,from astropy


text:
import numpy as np
from FireflyAlgorithm import FireflyAlgorithm

def sphere(x):
    return np.sum(x ** 2)

best = FireflyAlgorithm(function=sphere, dim=10, lb=-5, ub=5, max_evals=10000)

print(best)

max_stars_repo_name: zStupan/FireflyAlgorithm

max_issues_repo_name: zStupan/FireflyAlgorithm

max_forks_repo_name: zStupan/FireflyAlgorithm


text:
"""
Module containing code to handle generative models.
"""

from __future__ import division

from builtins import object
from future.utils import with_metaclass

from abc import ABCMeta, abstractmethod
import kmod.net as net
# import numpy as np
import torch
import torch.autograd

class NoiseTransformer(with_metaclass(ABCMeta, object)):
    """
    Class representing functions which transform random noise vectors into
    objects of interests. A Generative Adversarial Network (GAN) model is one
    such an example.
    """

    @abstractmethod
    def forward(self, *input):
        """
        Compute the output of this function given the input.
        """
        pass

    @abstractmethod
    def sample_noise(self, n):
        """
        Sample n noise vectors from the distribution suitable for this function
        to transform.

        Return the n noise vectors.
        """
        pass

    @abstractmethod
    def in_out_shapes(self):
        """
        Return a tuple (I, O), where
        * I is a tuple describing the input shape of one noise vector
        * O is a tuple describing the output shape of one transformed object.
        """
        pass

    @abstractmethod
    def sample(self, n):
        """
        Sample n objects from this transformer. This involves sampling n noise
        vector from sample_noise(), then transforming them to genereate n
        objects.

        Return the n objects.
        """
        pass

# end class NoiseTransformer

class PTNoiseTransformer(NoiseTransformer, net.SerializableModule):
    """
    A Pytorch implementation of NoiseTransformer, meaning that all input and
    output are given by Pytorch tensors.
    """
    pass

# end class PTNoiseTransformer

class PTNoiseTransformerAdapter(PTNoiseTransformer):
    """
    A PTNoiseTransformer whose components are specified manually as input.
    Adapter pattern.
    """
    def __init__(self, module, f_sample_noise, in_out_shapes,
            tensor_type=torch.cuda.FloatTensor
            ):
        """
        ptmodule: a instance of torch.nn.Module represent a function to transform
            noise vectors.
        f_sample_noise: a function or a callable object n |-> (n x
            in_out_shapes[0] ) to sample noise vectors.
        """
        self.module = module.eval()
        self.f_sample_noise = f_sample_noise
        self.in_out_shapes = in_out_shapes
        self.tensor_type = tensor_type
        # print(self.tensor_type)

        # minimal compatibility check
        try:
            self.sample(1)
        except:
            raise ValueError("Noise sampled from f_sample_noise may be incompatible with the specified transformer module")

    def forward(self, *input):
        return self.module.forward(*input)

    def sample_noise(self, n):
        f = self.f_sample_noise
        sampled = f(n)
        sampled = sampled.type(self.tensor_type)
        return sampled

    def in_out_shapes(self):
        """
        Return a tuple (I, O), where
        * I is a tuple describing the input shape of one noise vector
        * O is a tuple describing the output shape of one transformed object.
        """
        return self.in_out_shapes

    def sample(self, n):
        """
        Sample n objects from this transformer. This involves sampling n noise
        vector from sample_noise(), then transforming them to genereate n
        objects.

        Return the n objects.
        """
        with torch.no_grad():
            Z = self.sample_noise(n).type(self.tensor_type)
            Zvar = torch.autograd.Variable(Z)
            X = self.module(Zvar)
        return X

    def __str__(self):
        return str(self.module)

# PTNoiseTransformerAdapter

max_stars_repo_name: wittawatj/kernel-mod

max_issues_repo_name: wittawatj/kernel-mod

max_forks_repo_name: wittawatj/kernel-mod