text:
import os
import typing
from typing import Any, Optional, Text, Dict, List, Type

import fasttext
import numpy as np
import rasa.utils.train_utils as train_utils
from rasa.nlu.components import Component
from rasa.nlu.featurizers.featurizer import DenseFeaturizer
from rasa.nlu.config import RasaNLUModelConfig
from rasa.nlu.training_data import Message, TrainingData
from rasa.nlu.tokenizers.tokenizer import Tokenizer
from rasa.nlu.constants import (
    DENSE_FEATURE_NAMES,
    DENSE_FEATURIZABLE_ATTRIBUTES,
    TEXT,
    TOKENS_NAMES,
)

if typing.TYPE_CHECKING:
    from rasa.nlu.model import Metadata

class FastTextFeaturizer(DenseFeaturizer):
    """This component adds fasttext features."""

    @classmethod
    def required_components(cls) -> List[Type[Component]]:
        return [Tokenizer]

    @classmethod
    def required_packages(cls) -> List[Text]:
        return ["fasttext"]

    defaults = {"file": None, "cache_dir": None}
    language_list = None

    def __init__(self, component_config: Optional[Dict[Text, Any]] = None) -> None:
        super().__init__(component_config)
        if "cache_dir" not in component_config.keys():
            raise ValueError("You need to set `cache_dir` for the FasttextFeaturizer.")
        if "file" not in component_config.keys():
            raise ValueError("You need to set `file` for the FasttextFeaturizer.")
        path = os.path.join(component_config["cache_dir"], component_config["file"])

        if not os.path.exists(component_config["cache_dir"]):
            raise FileNotFoundError(
                f"It seems that the cache dir {component_config['cache_dir']} does not exists. Please check config."
            )
        if not os.path.exists(path):
            raise FileNotFoundError(
                f"It seems that file {path} does not exists. Please check config."
            )

        self.model = fasttext.load_model(path)

    def train(
        self,
        training_data: TrainingData,
        config: Optional[RasaNLUModelConfig] = None,
        **kwargs: Any,
    ) -> None:
        for example in training_data.intent_examples:
            for attribute in DENSE_FEATURIZABLE_ATTRIBUTES:
                self.set_fasttext_features(example, attribute)

    def set_fasttext_features(self, message: Message, attribute: Text = TEXT) -> None:
        tokens = message.get(TOKENS_NAMES[attribute])

        if not tokens:
            return None

        text_vector = self.model.get_word_vector(message.text)
        word_vectors = [
            self.model.get_word_vector(t.text)
            for t in train_utils.tokens_without_cls(message, attribute)
        ]
        X = np.array(word_vectors + [text_vector])  # remember, we need one for __CLS__

        features = self._combine_with_existing_dense_features(
            message, additional_features=X, feature_name=DENSE_FEATURE_NAMES[attribute]
        )
        message.set(DENSE_FEATURE_NAMES[attribute], features)

    def process(self, message: Message, **kwargs: Any) -> None:
        self.set_fasttext_features(message)

    def persist(self, file_name: Text, model_dir: Text) -> Optional[Dict[Text, Any]]:
        pass

    @classmethod
    def load(
        cls,
        meta: Dict[Text, Any],
        model_dir: Optional[Text] = None,
        model_metadata: Optional["Metadata"] = None,
        cached_component: Optional["Component"] = None,
        **kwargs: Any,
    ) -> "Component":
        """Load this component from file."""

        if cached_component:
            return cached_component

        return cls(meta)

max_stars_repo_name: anuragshas/rasa-nlu-examples

max_issues_repo_name: anuragshas/rasa-nlu-examples

max_forks_repo_name: anuragshas/rasa-nlu-examples


text:
from typing import Set, Tuple
import networkx as nx
from pyformlang.cfg import CFG
from scipy import sparse
from scipy.sparse import dok_matrix, identity
from project import cfg_to_wcnf, is_wcnf, BooleanMatrices, graph_to_nfa

__all__ = ["hellings", "matrix", "tensor"]

def hellings(graph: nx.MultiDiGraph, cfg: CFG) -> Set[Tuple[int, str, int]]:
    """
    Hellings algorithm for solving Context-Free Path Querying problem
    for given graph and cfg

    Parameters
    ----------
    graph: nx.MultiDiGraph
        input graph
    cfg: CFG
        input cfg

    Returns
    -------
    Set[Tuple[int, str, int]]:
        set tuples (node, terminal, node)
    """
    wcnf = cfg if is_wcnf(cfg) else cfg_to_wcnf(cfg)

    eps_prod_heads = [p.head.value for p in wcnf.productions if not p.body]
    term_productions = {p for p in wcnf.productions if len(p.body) == 1}
    var_productions = {p for p in wcnf.productions if len(p.body) == 2}

    r = {(v, h, v) for v in range(graph.number_of_nodes()) for h in eps_prod_heads} | {
        (u, p.head.value, v)
        for u, v, edge_data in graph.edges(data=True)
        for p in term_productions
        if p.body[0].value == edge_data["label"]
    }

    new = r.copy()
    while new:
        n, N, m = new.pop()
        r_temp = set()

        for u, M, v in r:
            if v == n:
                triplets = {
                    (u, p.head.value, m)
                    for p in var_productions
                    if p.body[0].value == M
                    and p.body[1].value == N
                    and (u, p.head.value, m) not in r
                }
                new |= triplets
                r_temp |= triplets
        r |= r_temp
        r_temp.clear()

        for u, M, v in r:
            if u == m:
                triplets = {
                    (n, p.head.value, v)
                    for p in var_productions
                    if p.body[0].value == N
                    and p.body[1].value == M
                    and (n, p.head.value, v) not in r
                }
                new |= triplets
                r_temp |= triplets
        r |= r_temp

    return r

def matrix(graph: nx.MultiDiGraph, cfg: CFG) -> Set[Tuple[int, str, int]]:
    """
    Matrix algorithm for solving Context-Free Path Querying problem
    Parameters
    ----------
    graph: nx.MultiDiGraph
        input graph
    cfg: CFG
        input cfg
    Returns
    -------
    Set[Tuple[int, str, int]]:
        set tuples (node, terminal, node)
    """
    wcnf = cfg_to_wcnf(cfg)

    num_of_nodes = graph.number_of_nodes()
    matrices = {
        v.value: sparse.dok_matrix((num_of_nodes, num_of_nodes), dtype=bool)
        for v in wcnf.variables
    }

    term_productions = {p for p in wcnf.productions if len(p.body) == 1}
    for i, j, data in graph.edges(data=True):
        l = data["label"]
        for v in {p.head.value for p in term_productions if p.body[0].value == l}:
            matrices[v][i, j] = True

    eps_products_heads = [p.head.value for p in wcnf.productions if not p.body]
    for i in range(num_of_nodes):
        for v in eps_products_heads:
            matrices[v][i, i] = True

    changed = True
    variable_productions = {p for p in wcnf.productions if len(p.body) == 2}
    while changed:
        changed = False
        for p in variable_productions:
            old_nnz = matrices[p.head.value].nnz
            matrices[p.head.value] += (
                matrices[p.body[0].value] @ matrices[p.body[1].value]
            )
            new_nnz = matrices[p.head.value].nnz
            changed = changed or old_nnz != new_nnz

    return {
        (u, variable, v)
        for variable, var_matrix in matrices.items()
        for u, v in zip(*var_matrix.nonzero())
    }

def tensor(graph: nx.MultiDiGraph, cfg: CFG) -> Set[Tuple[int, str, int]]:
    """
    Tensor algorithm for solving Context-Free Path Querying problem
    Parameters
    ----------
    graph: nx.MultiDiGraph
        input graph
    cfg: CFG
        input cfg
    Returns
    -------
    set[Tuple[int, str, int]]:
        set tuples (node, terminal, node)
    """
    wcnf = cfg_to_wcnf(cfg)

    n = sum(len(p.body) + 1 for p in wcnf.productions)
    rsm_heads = dict()
    nonterm = set()
    boxes = dict()
    start_states = set()
    final_states = set()
    counter = 0

    nfa_by_graph = graph_to_nfa(graph)
    bm = BooleanMatrices.from_automaton(nfa_by_graph)

    for p in wcnf.productions:
        nonterm.add(p.head.value)
        start_states.add(counter)
        final_states.add(counter + len(p.body))
        rsm_heads[(counter, counter + len(p.body))] = p.head.value
        for b in p.body:
            m = boxes.get(b.value, dok_matrix((n, n), dtype=bool))
            m[counter, counter + 1] = True
            boxes[b.value] = m
            counter += 1
        counter += 1

    for p in wcnf.productions:
        if len(p.body) == 0:
            bm.bool_matrices[p.head.value] = identity(bm.num_states, dtype=bool).todok()

    changed = True
    bfa = BooleanMatrices()
    bfa.start_states = start_states
    bfa.final_states = final_states
    bfa.bool_matrices = boxes
    bfa.states_count = n

    while changed:
        changed = False
        transitive_closure = bfa.intersect(bm).transitive_closure()
        x, y = transitive_closure.nonzero()

        for (i, j) in zip(x, y):
            rfa_from = i // bm.num_states
            rfa_to = j // bm.num_states
            graph_from = i % bm.num_states
            graph_to = j % bm.num_states

            if (rfa_from, rfa_to) not in rsm_heads:
                continue

            variable = rsm_heads[(rfa_from, rfa_to)]
            m = bm.bool_matrices.get(
                variable,
                dok_matrix((bm.num_states, bm.num_states), dtype=bool),
            )
            if not m[graph_from, graph_to]:
                changed = True
                m[graph_from, graph_to] = True
                bm.bool_matrices[variable] = m

    triplets = set()
    for key, m in bm.bool_matrices.items():
        if key not in nonterm:
            continue
        for (u, v), _ in m.items():
            triplets.add((u, key, v))

    return triplets

ext: py

max_stars_repo_name: bozhnyukAlex/formal-lang-course

max_issues_repo_name: bozhnyukAlex/formal-lang-course

max_forks_repo_name: bozhnyukAlex/formal-lang-course

reason: from scipy,import networkx


text:
#!/usr/bin/env python3.7
import copy
import numpy as np
import cdms2
from vacumm.misc.grid.regridding import fill2d
from matplotlib.dates import date2num,num2date
from vcmq import create_time,grid2xy,extend2d

class InitialConditions(object):

    def __init__(self,fileout,hgrid,t0,value=None,shapefile=None,ncfile=None,var=None, logger=None):
        """ Constructor"""

        if logger:
            self.logger = logger
            self.logger.info("Processing initial conditions")

        self.hgrid=copy.deepcopy(hgrid.mesh)
        self.hgrid.longitude=hgrid.longitude
        self.hgrid.latitude=hgrid.latitude
        self.t0=t0
        self.fileout=fileout

        if ncfile:
           self._create_nc_gr3(ncfile,var)
        elif shapefile:
            pass
        else:
            if fileout.endswith('.prop'):
               self._create_constante_prop(value)
            else:
               self._create_constante_gr3(value)

    def _create_constante_prop(self,value):

        f = open(self.fileout, 'w')
        n_elems = len(self.hgrid.elements)
        attr_array=np.ones((n_elems,))
        attr_array[:]=value

        for i in range(n_elems):
            buf = "%d %d\n" % (i+1, attr_array[i])
            f.write(buf)
        f.close()

        self.logger.info("	%s exported"%self.fileout)

    def _create_constante_gr3(self,value):
        self.hgrid.values[:]=value*-1
        self.hgrid.write(self.fileout)
        self.logger.info("	%s exported"%self.fileout)

    def _create_nc_gr3(self,ncfile,var):
        data=cdms2.open(ncfile)

        lon=self.hgrid.longitude
        lat=self.hgrid.latitude
        src=fill2d(data[var][:], method='carg')
        time0=[t.torelative('days since 1-1-1').value for t in data['time'].asRelativeTime()]
        tin=create_time(np.ones(len(lon))*date2num(self.t0)+1,units='days since 1-1-1')
        tb=grid2xy(src,xo=lon,yo=lat,method='linear',to=tin)

        if np.any(tb.mask==True):
            bad=(tb.mask==True).nonzero()[0]
            tin_bad=create_time(np.ones(len(bad))*date2num(self.t0)+1,units='days since 1-1-1')
            tb[bad]=grid2xy(src,xo=np.array(lon)[bad].tolist(),yo=np.array(lat)[bad].tolist(),method='nearest',to=tin_bad)

        self._create_constante_gr3(tb)

max_stars_repo_name: mymyrere/schism-public

max_issues_repo_name: mymyrere/schism-public

max_forks_repo_name: mymyrere/schism-public

reason: import numpy


text:
import numpy as np
from keras import backend
from sklearn.metrics import mean_squared_error

def root_mean_square_error(y_true, y_pred):
    return backend.sqrt(backend.mean(backend.square(y_pred - y_true), axis=-1))

def calculate_rmse(y_true, y_pred):
    return np.sqrt(mean_squared_error(y_true, y_pred))

def normalised_mean_square_error(y_true, y_pred):
    return backend.mean(backend.square(y_pred - y_true), axis=-1) / (np.std(y_true) ** 2)

max_stars_repo_name: adrigrillo/dl

max_issues_repo_name: adrigrillo/dl

max_forks_repo_name: adrigrillo/dl


text:
import numpy as np
from collections import deque

class Node:

	def __init__(self, name, t, point, normal=None):
		self.name = name
		self.type = t
		self.point = point
		self.normal = normal
		self.neighbours = dict()

class VGraph:

	def __init__(self):
		self.V = dict()

	def addVertexDirect(self, v):
		self.V[v.name] = v

	def addVertexIntersect(self, v):
		pass

	def addEdge(self, v1, v2):
		self.V[v1.name].neighbours[v2.name] = v2
		self.V[v2.name].neighbours[v1.name] = v1

	def traverse(self):
		if len(self.V) == 0:
			return []

		traversal = []
		stack = deque()
		rand_idx = np.random.random_integers(0, len(self.V), 1)[0]
		rand_key = self.V.keys()[rand_idx]
		stack.append(rand_key)
		visited = dict()
		visited[rand_key] = True

		while len(stack):
			next_key = stack.pop()
			if self.V[next_key].type == 'Point':
				traversal.append(self.V[next_key].point)

			nb_frontier = self.V[next_key].neighbours
			for name, _ in nb_frontier.items():
				if not visited.get(name):
					stack.append(name)
					visited[name] = True
					break

		return traversal


text:
import numpy as np

from collections import deque
from functools import partials

class WollMonteCarlo:
    """ Wolff Monte Carlo simulator for the Ising model """
    def __init__(self, L, T, method = None):
        self._L = L
        self._T = T
        self._K = 2. / T
        self._method = method

        # set up the initial state
        self._state = np.random.randint(0, 2, size = [L, L])

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

    @property
    def T(self):
        return self._T

    @property
    def K(self):
        return self._K

    @property
    def state(self):
        return self._state

    def probability_add_bonds(self, x1, y1, x2, y2, state):
        """ The probability for adding a bond. """
        E = 1.0 if state[x1, y1] == state[x2, y2] else 0.0
        return 1.0 - np.exp(-self.K * E)

    def wolff_iterative(self, state):
        """ Iterative Wolff algorithm.
            This algorithm uses a doubly ended queue (deque), which provides O(1) operations for adding and removing items from both ends of a list.
        """

        # Convenient lists for indexing
        # Below, we'll use these to get the left (or 'above', etc) neighbors of a site.
        # Includes periodic boundaries! Another option would be to replace
        #    left[x1]   by   (x1 - 1) % L
        # but that does an addition and a module every step.
        left = [self.L - 1] + list(range(self.L - 1))
        right = list(range(1, self.L)) + [0]

        # Book-keeping containers
        sites_to_consider = deque()
        sites_to_flip = set()
        bonds_considered = set()

        # Initial queue of sites to consider, just consisting of a single (x,y) location
        sites_to_consider.append((
            np.random.randint(0, self.L),
            np.random.randint(0, self.L)
        ))

        # As long as there are sites to consider
        while sites_to_consider:
            # Pick a new site to consider from the queue, either using
            # breadth first or depth first
            if self._method == "BFS":
                x1, y1 = sites_to_consider.popleft()
            if self._method == "DFS":
                x1, y1 = sites_to_consider.pop()

            # For the neighbors of this site
            for x2, y2 in zip([left[x1], right[x1], x1, x1],
                              [y1, y1, left[y1], right[y1]]):

                # Check if we have not already considered this pair
                if not (x1, y1, x2, y2) in bonds_considered:
                    # Add the pair so that we don't flip it twice
                    bonds_considered.add((x1, y1, x2, y2))
                    bonds_considered.add((x2, y2, x1, y1))

                    if np.random.rand() < self.probability_add_bond(x1, y1, x2, y2, state):
                        sites_to_consider.append((x2, y2))
                        sites_to_flip.add((x1, y1))
                        sites_to_flip.add((x2, y2))

        return sites_to_flip

    def step(self):
        """Use Wolff and perform update."""

        # Get a list of sites to flip...
        to_flip = self.wolff_iterative(self._state)

        # ...and flip them
        for (x, y) in to_flip:
            self._state[x, y] = 1 - self._state[x, y]

        # Return the list of the flipped sites
        return to_flip

max_stars_repo_name: Rosetta-Tan/my-qualg

max_issues_repo_name: Rosetta-Tan/my-qualg

max_forks_repo_name: Rosetta-Tan/my-qualg


text:
# %% [markdown]
# # Hough transform
# [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/YoniChechik/AI_is_Math/blob/master/c_04b_hough_transform/hough_transform.ipynb)

# %%
# to run in google colab
import sys
if 'google.colab' in sys.modules:
    import subprocess
    subprocess.call('apt-get install subversion'.split())
    subprocess.call('svn export https://github.com/YoniChechik/AI_is_Math/trunk/c_04b_hough_transform/edge_bold.bmp'.split())
    subprocess.call('svn export https://github.com/YoniChechik/AI_is_Math/trunk/c_04b_hough_transform/building.jpg'.split())

# %%
from matplotlib import pyplot as plt
import cv2
import numpy as np

figsize = (10, 10)

# %% [markdown]
# ## Import an image
# %%
im3 = cv2.imread("edge_bold.bmp")
im = cv2.cvtColor(im3, cv2.COLOR_BGR2GRAY)

plt.figure(figsize=figsize)
plt.imshow(im, cmap='gray', vmin=0, vmax=255)
plt.show()

# %% [markdown]
# ## Find edges of an image using Canny
# For more details about Canny edge detection, look at lecture 3
# %%
mag_im = cv2.Canny(im, 50, 400)

plt.figure(figsize=figsize)
plt.imshow(mag_im)
plt.show()

# %% [markdown]
# ## Initialize accumulation matrix
# %%
# choose R size
r_step = 1
rmax = np.sqrt(im.shape[0]**2+im.shape[1]**2)
r_vec = np.arange(-rmax, rmax, r_step)

# choose theta size
t_step = np.pi/180
t_vec = np.arange(0, np.pi, t_step)

# accumulation matrix
acc_mat = np.zeros((r_vec.shape[0], t_vec.shape[0]))

# %% [markdown]
# ## Fill accumulation matrix
# %%
# get indices of edges
edge_inds = np.argwhere(mag_im > 0)

# run on all theta and edge indices and find corresponding R
for t_ind, t0 in enumerate(t_vec):
    for yx in edge_inds:
        x = yx[1]
        y = yx[0]

        r0 = x*np.cos(t0)+y*np.sin(t0)
        r_ind = np.argmin(np.abs(r0-r_vec))

        acc_mat[r_ind, t_ind] += 1

# %%
plt.figure(figsize=figsize)
plt.imshow(acc_mat, extent=[0, 180, rmax, -rmax],aspect='auto')
plt.xlabel('theta')
plt.ylabel('r')
plt.title('accumulation matrix')
plt.show()

# %% [markdown]
# ## Threshold accumulation matrix
# %%
TH = 100
acc_mat_th = acc_mat > TH

plt.figure(figsize=figsize)
plt.imshow(acc_mat_th, extent=[0, 180, rmax, -rmax],aspect='auto')
plt.xlabel('theta')
plt.ylabel('r')
plt.title('accumulation matrix TH')
plt.show()

# %% [markdown]
# ## Plot lines found by hough
# %%
# get indices of acc_mat_th
edge_inds = np.argwhere(acc_mat_th > 0)

res = im3.copy()
for r_ind, t_ind in edge_inds:
    rho = r_vec[r_ind]
    theta = t_vec[t_ind]

    print("(rho,theta): " + str((rho, theta/np.pi*180)))

    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    res = cv2.line(res, (x1, y1), (x2, y2), (0, 0, 255), thickness=1)

plt.figure(figsize=figsize)
plt.imshow(res)
plt.show()

# %% [markdown]
# ## Try cv2.HoughLines
# This implementation is faster since it was done in C
# %%
lines = cv2.HoughLines(mag_im, r_step, t_step, TH)
res = im3.copy()

for r_t in lines:
    rho = r_t[0, 0]
    theta = r_t[0, 1]

    print("(rho,theta): " + str((rho, theta/np.pi*180)))

    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    res = cv2.line(res, (x1, y1), (x2, y2), (0, 0, 255), thickness=1)

plt.figure(figsize=figsize)
plt.imshow(res)
plt.show()

# %% [markdown]
# ## Complete new example of a more complex image
# We can see on the bottom horizontal lines that the "noise" of the tree top edges is interfeering with the line detection.
# %%
im3 = cv2.imread("building.jpg")
im3 = cv2.cvtColor(im3, cv2.COLOR_BGR2RGB)
im = cv2.cvtColor(im3, cv2.COLOR_BGR2GRAY)

plt.figure(figsize=figsize)
plt.imshow(im, cmap='gray', vmin=0, vmax=255)
plt.show()

mag_im = cv2.Canny(im, 50, 400)

plt.figure(figsize=figsize)
plt.imshow(mag_im)
plt.show()

TH = 247
lines = cv2.HoughLines(mag_im, r_step, t_step, TH)
res = im3.copy()

for r_t in lines:
    rho = r_t[0, 0]
    theta = r_t[0, 1]

    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    res = cv2.line(res, (x1, y1), (x2, y2), (0, 0, 255), thickness=3)

plt.figure(figsize=(20, 20))
plt.imshow(res)
plt.show()

# %%


text:
from __future__ import print_function
import argparse
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import torch.nn.functional as F
from torch.autograd import Variable
import os
import numpy as np

import models.began as began
from folder import ImageFolder

parser = argparse.ArgumentParser()
parser.add_argument('--dataset', required=True, help='cifar10 | lsun | imagenet | folder | lfw ')
parser.add_argument('--dataroot', required=True, help='path to dataset')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=2)
parser.add_argument('--batchSize', type=int, default=16, help='input batch size')
parser.add_argument('--imageSize', type=int, default=32, help='the height / width of the input image to network')
parser.add_argument('--nz', type=int, default=64, help='size of the latent z vector')
parser.add_argument('--size', type=int, default=3, help='size of images generated by G,3:32,4:64,5:128')
parser.add_argument('--gamma', type=float, default=0.5, help='gamma, diversity ratio')
parser.add_argument('--niter', type=int, default=25, help='number of epochs to train for')
parser.add_argument('--lrD', type=float, default=0.00005, help='learning rate for Critic, default=0.00005')
parser.add_argument('--lrG', type=float, default=0.00005, help='learning rate for Generator, default=0.00005')
parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')
parser.add_argument('--cuda'  , action='store_true', help='enables cuda')
parser.add_argument('--ngpu'  , type=int, default=1, help='number of GPUs to use')
parser.add_argument('--netG', default='', help="path to netG (to continue training)")
parser.add_argument('--netD', default='', help="path to netD (to continue training)")
parser.add_argument('--Diters', type=int, default=5, help='number of D iters per each G iter')
parser.add_argument('--experiment', default=None, help='Where to store samples and models')
parser.add_argument('--adam', action='store_true', help='Whether to use adam (default is rmsprop)')
opt = parser.parse_args()
print(opt)

if opt.experiment is None:
    opt.experiment = 'samples'
os.system('mkdir {0}'.format(opt.experiment))

opt.manualSeed = random.randint(1, 10000) # fix seed
print("Random Seed: ", opt.manualSeed)
random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)

cudnn.benchmark = False
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

if torch.cuda.is_available() and not opt.cuda:
    print("WARNING: You have a CUDA device, so you should probably run with --cuda")

############# DataLoader ######################
if opt.dataset == 'lsun':
    dataset = dset.LSUN(db_path=opt.dataroot, classes=['bedroom_train'],
                        transform=transforms.Compose([
                            transforms.Scale(opt.imageSize),
                            transforms.CenterCrop(opt.imageSize),
                            transforms.ToTensor(),
                            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                        ]))
elif opt.dataset == 'CelebA':
    dataset = ImageFolder(root=opt.dataroot,
                        transform=transforms.Compose([
                            transforms.Resize(opt.imageSize),
                            transforms.CenterCrop(opt.imageSize),
                            transforms.ToTensor(),
                            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                        ]))

assert dataset
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
                                         shuffle=True, num_workers=int(opt.workers))

ngpu = int(opt.ngpu)
nz = int(opt.nz)

#custom weights initialization called on netG and netD
def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        m.weight.data.normal_(1.0, 0.02)
        m.bias.data.fill_(0)

def L_loss(x, D_x, eta=1):
    if eta==1:
        return torch.mean(torch.abs(x - D_x))
    elif eta==2:
        return torch.mean((x - D_x).pow(2))

netG = began.BEGAN_G(opt.size)
netG.apply(weights_init)
if opt.netG != '': # load checkpoint if needed
    netG.load_state_dict(torch.load(opt.netG))
print(netG)

netD = began.BEGAN_D(opt.size)
netD.apply(weights_init)
if opt.netD != '':
    netD.load_state_dict(torch.load(opt.netD))
print(netD)

input = torch.FloatTensor(opt.batchSize, 3, opt.imageSize, opt.imageSize)
noise = torch.FloatTensor(opt.batchSize, nz)
fixed_noise = torch.FloatTensor(opt.batchSize, nz).normal_(0, 1)

if opt.cuda:
    netD.cuda()
    netG.cuda()
    input = input.cuda()
    noise, fixed_noise = noise.cuda(), fixed_noise.cuda()

# setup optimizer
optimizerD = optim.Adam(netD.parameters(), lr=opt.lrD, betas=(opt.beta1, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=opt.lrG, betas=(opt.beta1, 0.999))

gen_iterations = 0
k = Variable(torch.from_numpy(np.array([0.0])).float(), requires_grad=False)
gamma = Variable(torch.from_numpy(np.array([opt.gamma])).float(), requires_grad=False)
lambda_k = Variable(torch.from_numpy(np.array([0.001])).float(),requires_grad=False)
if opt.cuda:
    k = k.cuda()
    gamma = gamma.cuda()
    lambda_k = lambda_k.cuda()

for epoch in range(opt.niter):
    data_iter = iter(dataloader)
    i = 0
    while i < len(dataloader):
        ############################
        # (1) Update D network
        ###########################
        for p in netD.parameters(): # reset requires_grad
            p.requires_grad = True # they are set to False below in netG update

        # train the discriminator Diters times
        data = data_iter.next()
        i += 1

        # train with real
        real_cpu, _ = data
        netD.zero_grad()
        batch_size = real_cpu.size(0)

        if opt.cuda:
            real_cpu = real_cpu.cuda()
        input.resize_as_(real_cpu).copy_(real_cpu)
        inputv = Variable(input)
        errD_real = netD(inputv)
        L_errD_real = L_loss(inputv, errD_real)
        # train with fake
        noise.resize_(opt.batchSize, nz).normal_(0, 1)
        noisev = Variable(noise, volatile = True) # totally freeze netG
        fake = Variable(netG(noisev).data)
        inputv = fake
        errD_fake = netD(inputv)
        L_errD_fake = L_loss(inputv, errD_fake)
        errD = L_errD_real - k.data[0] * L_errD_fake
        errD.backward()
        optimizerD.step()

        ############################
        # (2) Update G network
        ###########################
        for p in netD.parameters():
            p.requires_grad = False # to avoid computation
        netG.zero_grad()
        # in case our last batch was the tail batch of the dataloader,
        # make sure we feed a full batch of noise
        noise.resize_(opt.batchSize, nz).normal_(0, 1)
        noisev = Variable(noise)
        fake = netG(noisev)
        errG_fake = netD(fake)
        errG = L_loss(fake, errG_fake)
        errG.backward()
        optimizerG.step()
        gen_iterations += 1

        k = k + lambda_k * (gamma * L_errD_real - errG)
        #k = k + lambda_k * (gamma.data[0] * L_errD_real.data[0] - errG.data[0])
        M_global = L_errD_real + torch.abs(lambda_k * L_errD_real - errG)
        print('[%d/%d][%d/%d][%d] L_errD_real: %f L_errD_fake: %f errD: %f errG: %f k:%f M_global: %f'
            % (epoch, opt.niter, i, len(dataloader), gen_iterations,
            L_errD_real.data[0], L_errD_fake.data[0], errD.data[0], errG.data[0],
            k.data[0], M_global.data[0]))

        if gen_iterations % 500 == 0:
            real_cpu = real_cpu.mul(0.5).add(0.5)
            vutils.save_image(real_cpu, '{0}/real_samples.png'.format(opt.experiment))
            fake = netG(Variable(fixed_noise, volatile=True))
            fake.data = fake.data.mul(0.5).add(0.5)
            vutils.save_image(fake.data, '{0}/fake_samples_{1}.png'.format(opt.experiment, gen_iterations))

    # do checkpointing
    torch.save(netG.state_dict(), '{0}/netG_epoch_{1}.pth'.format(opt.experiment, epoch))
    torch.save(netD.state_dict(), '{0}/netD_epoch_{1}.pth'.format(opt.experiment, epoch))


text:
import luigi
import luigi.mock
import numpy as np
import pandas as pd

from download_data import ExtractDataset

class DropLowAttendanceSchools(luigi.Task):
    extract_schools_task = ExtractDataset('TS_ESCOLA.csv')

    def requires(self):
        return self.extract_schools_task

    def output(self):
        return luigi.LocalTarget('./dados/2013/TS_ESCOLA_with_acceptable_attendance.csv')

    def run(self):
        with self.extract_schools_task.output().open('r') as fp:
            escolas_2013_pd = pd.read_csv(fp)
        escolas_2013_pd = escolas_2013_pd[escolas_2013_pd.TAXA_PARTICIPACAO_9EF > 0.5]

        with self.output().open('w') as fp:
            escolas_2013_pd.to_csv(fp, index=False)

class ImputeMissingData(luigi.Task):
    def run(self):
        with self.input_task.output().open('r') as fp:
            escolas_2013_pd = pd.read_csv(fp)

        imputed_values = {}
        for col in self.processed_columns_names(escolas_2013_pd):
            imputed_values[col] = escolas_2013_pd[col].value_counts().index[0]

        with self.output().open('w') as fp:
            escolas_2013_pd.fillna(value=imputed_values).to_csv(fp, index=False)

class ImputeSchoolsMissingData(ImputeMissingData):
    input_task = DropLowAttendanceSchools()

    def requires(self):
        return self.input_task

    def output(self):
        return luigi.LocalTarget('./dados/2013/TS_ESCOLA_with_imputed_values.csv')

    def processed_columns_names(self, df):
        return np.append(df.filter(regex='TX_RESP_Q.*').columns.values, ['NIVEL_SOCIO_ECONOMICO'])

class ImputeTeacherMissingData(ImputeMissingData):
    input_task = ExtractDataset('TS_PROFESSOR.csv')

    def requires(self):
        return self.input_task

    def output(self):
        return luigi.LocalTarget('./dados/2013/TS_PROFESSOR_with_imputed_values.csv')

    def processed_columns_names(self, df):
        return df.filter(regex='TX_RESP_Q.*').columns.values

class ImputeDirectorMissingData(ImputeMissingData):
    input_task = ExtractDataset('TS_DIRETOR.csv')

    def requires(self):
        return self.input_task

    def output(self):
        return luigi.LocalTarget('./dados/2013/TS_DIRETOR_with_imputed_values.csv')

    def processed_columns_names(self, df):
        return df.filter(regex='TX_RESP_Q.*').columns.values


text:
__author__ = 'sibirrer'

import pytest
import numpy as np
import numpy.testing as npt
from lenstronomy.PointSource.point_source_param import PointSourceParam

class TestParam(object):

    def setup(self):
        kwargs_fixed = [{}, {}, {}]
        num_point_sources_list = [4, 1, 1]
        point_source_model_list = ['LENSED_POSITION', 'SOURCE_POSITION', 'UNLENSED']
        self.param = PointSourceParam(model_list=point_source_model_list, kwargs_fixed=kwargs_fixed,
                                      num_point_source_list=num_point_sources_list)
        self.kwargs =[{'ra_image': np.array([0, 0, 0, 0]), 'dec_image': np.array([0, 0, 0, 0]),
                       'point_amp': np.array([1, 1, 1, 1])},
                      {'ra_source': 1, 'dec_source': 1, 'point_amp': 1.},
                      {'ra_image': [1], 'dec_image': [1], 'point_amp': [1.]}]
        self.kwargs_sigma = [{'pos_sigma': 1, 'point_amp_sigma': 1}, {'pos_sigma': 1, 'point_amp_sigma': 1}, {'pos_sigma': 1, 'point_amp_sigma': 1}]

        self.kwargs_mean = []
        for i in range(len(self.kwargs)):
            kwargs_mean_k = self.kwargs[i].copy()
            kwargs_mean_k.update(self.kwargs_sigma[i])
            self.kwargs_mean.append(kwargs_mean_k)

        self.param_linear = PointSourceParam(model_list=point_source_model_list, kwargs_fixed=[{}, {}, {}],
                                      num_point_source_list=num_point_sources_list, linear_solver=False)

    def test_get_setParams(self):
        args = self.param.setParams(self.kwargs)
        kwargs_new, _ = self.param.getParams(args, i=0)
        args_new = self.param.setParams(kwargs_new)
        for k in range(len(args)):
            npt.assert_almost_equal(args[k], args_new[k], decimal=8)

        args = self.param_linear.setParams(self.kwargs)
        kwargs_new, _ = self.param_linear.getParams(args, i=0)
        args_new = self.param_linear.setParams(kwargs_new)
        for k in range(len(args)):
            npt.assert_almost_equal(args[k], args_new[k], decimal=8)

    def test_num_params(self):
        num, list = self.param.num_param()
        assert num == 12

        num, list = self.param_linear.num_param()
        assert num == 18

    def test_num_param_linear(self):
        num = self.param.num_param_linear()
        assert num == 6

        num = self.param_linear.num_param_linear()
        assert num == 0

if __name__ == '__main__':
    pytest.main()

max_stars_repo_name: tvelagapudi/lenstronomy

max_issues_repo_name: tvelagapudi/lenstronomy

max_forks_repo_name: tvelagapudi/lenstronomy


text:
"""
Uses the VGG16 pre-trained model as a starting point. The fully connected
portion of the model is trained but not the filters.
"""
# !/usr/bin/python
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
from tensorflow.keras import Model
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from sklearn.model_selection import train_test_split

# ********************************* Read in the data ********************************

# define folder paths for the data
train_humans_path = "../data/horse-or-human/train/humans/"
train_horses_path = "../data/horse-or-human/train/horses/"
validation_humans_path = "../data/horse-or-human/validation/humans/"
validation_horses_path = "../data/horse-or-human/validation/horses/"

paths = [train_humans_path, train_horses_path, validation_humans_path, validation_horses_path]
# subset path to only include the training data
paths = paths[:2]

X_data = []
y_data = []

for path in paths:
    for file in os.listdir(path):
        image = load_img(os.path.join(path, file), target_size=(224, 224))
        image = img_to_array(image)
        X_data.append(image)
        if 'human' in file:
            label = 1
        else:
            label = 0
        y_data.append(label)

X_data = np.asarray(X_data)
y_data = np.asarray(y_data)

# Next, the image pixels need to be prepared in the same way as the
# ImageNet training data was prepared. Specifically, from the paper
X_data = preprocess_input(X_data)

# *************************************** Inspect features and labels *****************

print("\nShape of X_data:", X_data.shape)
print("Shape of y_data:", y_data.shape)
print("\nData type of X_data:", type(X_data))
print("Data type of y_data:", type(y_data))

print("\nData type of 1st sample in X_data:", type(X_data[0]))
print("Data type of 1st label in y_data:", type(y_data[0]))

print("\nThe first X_data sample: \n", X_data[0])

print("\nThe y_data labels: \n", y_data)
print(f"\nNumber of y_data labels that are ones (expected 527): {y_data.sum()}")

# ****************************** Scale features ****************************************

# Scaling is done using preprocess_input() as above
# X_data = X_data / 255.0
# print("\nThe first X_data sample after normalization: \n", X_data[0])

# ****************************** Split into train and test sets *************************

X_train, X_valid, y_train, y_valid = train_test_split(X_data, y_data, test_size=0.2, random_state=42)

print(f"\nShape of X_train: {X_train.shape}")
print(f"Shape of X_valid: {X_valid.shape}")
print(f"Shape of y_train: {y_train.shape}")
print(f"Shape of y_valid: {y_valid.shape}\n")

# ****************************** Build model and fit ***********************************

# Build model
vgg_model = VGG16(include_top=False, input_shape=(224, 224, 3))
vgg_model.summary()

# freeze the convolutional layers so these don't train
for layer in vgg_model.layers:
    layer.trainable = False

# add new classifier layers (fully connected)
flat1 = Flatten()(vgg_model.layers[-1].output)
class1 = Dense(512, activation='relu')(flat1)
output = Dense(1, activation='sigmoid')(class1)
# define new model
vgg_model = Model(inputs=vgg_model.inputs, outputs=output)

# Print summary of model
vgg_model.summary()
# Compile model and fit
vgg_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = vgg_model.fit(X_train, y_train, validation_split=0.2, epochs=5, verbose=2)

# ********************************** Save Model ***************************************

vgg_model.save('vgg_model')

# ************************** Evaluate metrics on training data ************************

_, accuracy = vgg_model.evaluate(X_train, y_train)
print('\nAccuracy on training data is: {:.1%}'.format(accuracy))

# ******************************* Visualize results ***********************************

params = {'legend.fontsize': 'x-large',
          'axes.labelsize': 'x-large',
          'axes.titlesize': 'x-large',
          'xtick.labelsize': 'x-large',
          'ytick.labelsize': 'x-large'}
pylab.rcParams.update(params)

fig, axes = plt.subplots(2, figsize=(15, 15), sharex=True)
axes[0].plot(history.history['accuracy'])
axes[0].plot(history.history['val_accuracy'])
axes[0].set_title('Model Accuracy')
axes[0].set_ylabel('Accuracy')
axes[0].set_ylim(ymax=1.0)
axes[0].grid()
axes[0].legend(['Train', 'Validation'], loc='lower right')
axes[1].plot(history.history['loss'])
axes[1].plot(history.history['val_loss'])
axes[1].set_title('Model Loss')
axes[1].set_ylabel('Loss')
# axes[1].set_yscale("log")
axes[1].set_xlabel('Epoch')
axes[1].grid()
axes[1].legend(['Train', 'Validation'], loc='upper right')
plt.savefig('../results/fig_acc_loss_vgg.svg', dpi=400, bbox_inches='tight')

# ******************************* Load Model *************************************************

# reconstructed_model = models.load_model('vgg_model')  # comment out if not loading saved model

# ******************************* Evaluate metrics on validation data *******************************************

# _, accuracy = reconstructed_model.evaluate(X_valid, y_valid)  # only use when loading saved model
_, accuracy = vgg_model.evaluate(X_valid, y_valid)  # only use when using model in memory
print('\nAccuracy on validation hold-out set is: {:.1%}\n'.format(accuracy))

max_stars_repo_name: jasonepaul/cnn-horse-or-human

max_issues_repo_name: jasonepaul/cnn-horse-or-human

max_forks_repo_name: jasonepaul/cnn-horse-or-human


text:
from matplotlib.animation import FuncAnimation
from matplotlib.patches import Circle
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import quad

def cos2(x):
    return np.cos(x**2)

def sin2(x):
    return np.sin(x**2)

param = np.linspace(-6, 6, 601)

x = [quad(cos2, 0, val)[0] for val in param]
y = [quad(sin2, 0, val)[0] for val in param]

X = x - 0.5 * sin2(param) / param
Y = y + 0.5 * cos2(param) / param

fig, ax = plt.subplots()
ax.axis('equal')
ax.axhline(y=0, color='k', lw=1)
ax.axvline(x=0, color='k', lw=1)
ax.set_xlim((-1, 1))
ax.set_ylim((-1, 1))
ax.grid()

ax.plot(x, y, lw=0.8, label='1')
ax.plot(X, Y, lw=0.8, label='2')
dot = ax.plot([], [], 'r.')[0]
# radi = ax.plot([], [], 'r.')[0]

circ = Circle((X[100], Y[100]), radius=0.5/param[100],
              color='red', lw=1,fill=False)
ax.add_patch(circ)

plt.legend(loc='upper left')

def update(index):
    circ.center = (X[index], Y[index])
    circ.set_radius(0.5 / param[index])
    dot.set_data(X[index], Y[index])
    # radi.set_data(x[index], y[index])
    return circ, dot#, radi

anim = FuncAnimation(fig, update, frames=range(0, len(param), 2),
                     interval=20, blit=True)
# anim.save('line2.gif', writer='imagemagick')

plt.show()

max_stars_repo_name: aiserlex/math

max_issues_repo_name: aiserlex/math

max_forks_repo_name: aiserlex/math

reason: import numpy,from scipy


text:
r"""Kagome格子相关的内容
``````
Kagome是三角晶系，定义和triangle.py中类似
初基矢量的选择
a1 = (-1/2, \sqrt{3}/2)
a2 = (1/2, \sqrt{3}/2)
倒格子的选择
b1 = (-2\pi, 2\pi/\sqrt{3})  #(-1, 1/\sqrt{3})
b2 = ( 2\pi, 2\pi/\sqrt{3})  #(1, 1/\sqrt{3})

特别需要注意的是数值精度，使用sympy运算的时候精度更高一些
或者使用更高精度的float128
"""

import numpy
import sympy
from scipy import optimize
from basics import Hexagon, Point
from basics.point import get_absolute_angle, middle_point

def brillouin():
    '''布里渊区'''
    return Hexagon(Point(0, 0, 1), 2*numpy.pi/numpy.sqrt(3))

def get_high_symmetry_point():
    '''获得三角晶格的高对称点'''
    gamma = Point(0, 0, 1)
    mpt = Point(0, 2*numpy.pi / (numpy.sqrt(3)), 1)
    kpt = Point(-2*numpy.pi / 3, 2*numpy.pi / (numpy.sqrt(3)), 1)
    return gamma, mpt, kpt

def s_disp(kxv, kyv):#pylint: disable=unused-argument
    '''s能带的色散关系'''
    return 2

def p_disp(kxv, kyv):
    '''p能带的色散关系'''
    xval = kxv / 4
    yval = numpy.sqrt(3) * kyv / 4
    sqr = 2 * numpy.cos(2*xval - 2*yval)
    sqr += 2 * numpy.cos(2*xval + 2*yval)
    sqr += 2 * numpy.cos(4*xval) + 3
    if sqr < 0:
        if numpy.isclose(sqr, 0):
            sqr = 0
        else:
            raise RuntimeError("能带有错误")
    return - 1 + numpy.sqrt(sqr) - 0.11

def d_disp(kxv, kyv):
    '''d能带的色散关系'''
    xval = kxv / 4
    yval = numpy.sqrt(3) * kyv / 4
    sqr = 2 * numpy.cos(2*xval - 2*yval)
    sqr += 2 * numpy.cos(2*xval + 2*yval)
    sqr += 2 * numpy.cos(4*xval) + 3
    if sqr < 0:
        if numpy.isclose(sqr, 0):
            sqr = 0
        else:
            raise RuntimeError("能带有错误")
    return - 1 - numpy.sqrt(sqr)

def get_nu_numpy(kxv, kyv):
    '''获取nu变换的矩阵'''
    #pylint: disable=invalid-name
    nu1 = numpy.zeros(3)
    nu2 = numpy.zeros(3)
    nu3 = numpy.zeros(3)
    x = kxv / numpy.square(2, dtype=numpy.float128)
    y = numpy.sqrt(3, dtype=numpy.float128) * kyv / 4
    #p和d需要的根号下的数值
    sqr = 2 * numpy.cos(2*x - 2*y)
    sqr += 2 * numpy.cos(2*x + 2*y)
    sqr += 2 * numpy.cos(4*x) + 3
    if sqr < 0:
        if numpy.isclose(sqr, 0):
            sqr = 0
        else:
            raise RuntimeError("能带有错误")
    #第一组本征态
    nu1[0] = -numpy.cos(2*x)/2 + numpy.cos(2*y)/2
    nu1[1] = -numpy.cos(x-y)/2 + numpy.cos(3*x+y)/2
    nu1[2] = 1 - numpy.square(numpy.cos(x+y))
    #归一化
    n1 = numpy.square(nu1[0]) + numpy.square(nu1[1]) + numpy.square(nu1[2])
    n1 = 1.0 / numpy.sqrt(n1)
    nu1 = n1 * nu1
    #第二组本征态
    lamb2 = 1 - numpy.sqrt(sqr)
    nu2[0] = numpy.square(lamb2)/2 - 2*numpy.square(numpy.cos(x-y))
    nu2[1] = 2*numpy.cos(2*x)*numpy.cos(x-y) + numpy.cos(x+y)*lamb2
    nu2[2] = numpy.cos(2*x)*lamb2 + 2*numpy.cos(x-y)*numpy.cos(x+y)
    #归一化
    n2 = numpy.square(nu2[0]) + numpy.square(nu2[1]) + numpy.square(nu2[2])
    n2 = 1.0 / numpy.sqrt(n2)
    nu2 = n2 * nu2
    #第三组本征态
    lamb3 = 1 + numpy.sqrt(sqr)
    nu3[0] = numpy.square(lamb3)/2 - 2*numpy.square(numpy.cos(x-y))
    nu3[1] = 2*numpy.cos(2*x)*numpy.cos(x-y) + numpy.cos(x+y)*lamb3
    nu3[2] = numpy.cos(2*x)*lamb3 + 2*numpy.cos(x-y)*numpy.cos(x+y)
    #归一化
    n3 = numpy.square(nu3[0]) + numpy.square(nu3[1]) + numpy.square(nu3[2])
    n3 = 1.0 / numpy.sqrt(n3)
    nu3 = n3 * nu3
    return nu1, nu2, nu3

def get_nu(kxv, kyv):
    '''获取nu变换的矩阵'''
    #pylint: disable=invalid-name
    nu1 = sympy.zeros(3, 1)
    nu2 = sympy.zeros(3, 1)
    nu3 = sympy.zeros(3, 1)
    #x = kxv / numpy.square(2, dtype=numpy.float128)
    #y = numpy.sqrt(3, dtype=numpy.float128) * kyv / 4
    x, y = sympy.symbols("x y")
    #x = x2 / 4.0
    #y = sympy.sqrt(3) * y2 / 4.0
    #p和d需要的根号下的数值
    sqr = 2 * sympy.cos(2*x - 2*y)
    sqr += 2 * sympy.cos(2*x + 2*y)
    sqr += 2 * sympy.cos(4*x) + 3
    #第一组本征态
    nu1[0] = -sympy.cos(2*x)/2 + sympy.cos(2*y)/2
    nu1[1] = -sympy.cos(x-y)/2 + sympy.cos(3*x+y)/2
    nu1[2] = 1 - sympy.Pow(sympy.cos(x+y), 2)
    #归一化
    n1 = sympy.Pow(nu1[0], 2) + sympy.Pow(nu1[1], 2) + sympy.Pow(nu1[2], 2)
    n1 = 1.0 / sympy.sqrt(n1)
    nu1 = n1 * nu1
    #第二组本征态
    lamb2 = 1 - sympy.sqrt(sqr)
    nu2[0] = sympy.Pow(lamb2, 2)/2 - 2*sympy.Pow(sympy.cos(x-y), 2)
    nu2[1] = 2*sympy.cos(2*x)*sympy.cos(x-y) + sympy.cos(x+y)*lamb2
    nu2[2] = sympy.cos(2*x)*lamb2 + 2*sympy.cos(x-y)*sympy.cos(x+y)
    #归一化
    n2 = sympy.Pow(nu2[0], 2) + sympy.Pow(nu2[1], 2) + sympy.Pow(nu2[2], 2)
    n2 = 1.0 / sympy.sqrt(n2)
    nu2 = n2 * nu2
    #第三组本征态
    lamb3 = 1 + sympy.sqrt(sqr)
    nu3[0] = sympy.Pow(lamb3, 2)/2 - 2*sympy.Pow(sympy.cos(x-y), 2)
    nu3[1] = 2*sympy.cos(2*x)*sympy.cos(x-y) + sympy.cos(x+y)*lamb3
    nu3[2] = sympy.cos(2*x)*lamb3 + 2*sympy.cos(x-y)*sympy.cos(x+y)
    #归一化
    n3 = sympy.Pow(nu3[0], 2) + sympy.Pow(nu3[1], 2) + sympy.Pow(nu3[2], 2)
    n3 = 1.0 / sympy.sqrt(n3)
    nu3 = n3 * nu3
    #整理成numpy的结果
    nnu1 = nu1.evalf(30, subs={x: kxv/4, y: numpy.sqrt(3)*kyv/4})
    nnu2 = nu2.evalf(30, subs={x: kxv/4, y: numpy.sqrt(3)*kyv/4})
    nnu3 = nu3.evalf(30, subs={x: kxv/4, y: numpy.sqrt(3)*kyv/4})
    nnu1 = numpy.array(nnu1, dtype=numpy.float64).reshape([3])
    nnu2 = numpy.array(nnu2, dtype=numpy.float64).reshape([3])
    nnu3 = numpy.array(nnu3, dtype=numpy.float64).reshape([3])
    return nnu1, nnu2, nnu3

def band_u(uval, pinfos):
    '''带内的相互作用'''
    npat = len(pinfos)
    ret = numpy.zeros((npat, npat, npat))
    ndit = numpy.nditer(ret, flags=['multi_index'])
    while not ndit.finished:
        k1idx, k2idx, k3idx = ndit.multi_index
        #p2这个带
        k1v = pinfos[k1idx]
        k2v = pinfos[k2idx]
        k3v = pinfos[k3idx]
        k4v = shift_kv(k1v, k2v)
        k4v = shift_kv(k4v, Point(-k3v.coord[0], -k3v.coord[1], 1))
        #p这个带只需要第二个
        _, k1nu, _ = get_nu_numpy(k1v.coord[0], k1v.coord[1])
        _, k2nu, _ = get_nu_numpy(k2v.coord[0], k2v.coord[1])
        _, k3nu, _ = get_nu_numpy(k3v.coord[0], k3v.coord[1])
        _, k4nu, _ = get_nu_numpy(k4v.coord[0], k4v.coord[1])
        #计算数值
        for idx in range(3):
            ret[k1idx, k2idx, k3idx] +=\
                uval * k1nu[idx] * k2nu[idx] * k3nu[idx] * k4nu[idx]
        ndit.iternext()
    return ret

def band_v(vval, pinfos):
    '''带内的相互作用'''
    npat = len(pinfos)
    ret = numpy.zeros((npat, npat, npat))
    ndit = numpy.nditer(ret, flags=['multi_index'])
    while not ndit.finished:
        k1idx, k2idx, k3idx = ndit.multi_index
        #p2这个带
        k1v = pinfos[k1idx]
        k2v = pinfos[k2idx]
        k3v = pinfos[k3idx]
        k4v = shift_kv(k1v, k2v)
        k4v = shift_kv(k4v, Point(-k3v.coord[0], -k3v.coord[1], 1))
        #公式中的K
        ckv = shift_kv(k3v, Point(-k2v.coord[0], -k2v.coord[1], 1))
        #p这个带只需要第二个
        _, k1nu, _ = get_nu_numpy(k1v.coord[0], k1v.coord[1])
        _, k2nu, _ = get_nu_numpy(k2v.coord[0], k2v.coord[1])
        _, k3nu, _ = get_nu_numpy(k3v.coord[0], k3v.coord[1])
        _, k4nu, _ = get_nu_numpy(k4v.coord[0], k4v.coord[1])
        #计算数值
        s3_4 = 0.25*numpy.sqrt(3)
        vab = k1nu[0]*k2nu[1]*k3nu[1]*k4nu[0] + k1nu[1]*k2nu[0]*k3nu[0]*k4nu[1]
        ret[k1idx, k2idx, k3idx] += 2 * vval * \
            numpy.cos(0.25*ckv.coord[0] + s3_4*ckv.coord[1]) * vab
        vac = k1nu[0]*k2nu[2]*k3nu[2]*k4nu[0] + k1nu[2]*k2nu[0]*k3nu[0]*k4nu[2]
        ret[k1idx, k2idx, k3idx] += 2 * vval * \
            numpy.cos(0.5*ckv.coord[0]) * vac
        vbc = k1nu[1]*k2nu[2]*k3nu[2]*k4nu[1] + k1nu[2]*k2nu[1]*k3nu[1]*k4nu[2]
        ret[k1idx, k2idx, k3idx] += 2 * vval * \
            numpy.cos(-0.25*ckv.coord[0] + s3_4*ckv.coord[1]) * vbc
        ndit.iternext()
    return ret

def get_sublattice_u(umat, pinfos):
    '''将能带表示变回到子格子表示'''
    npat = len(pinfos)
    ret = numpy.zeros((3, 3, 3, 3, npat, npat, npat))
    place_holder = numpy.zeros((npat, npat, npat))
    ndit = numpy.nditer(place_holder, flags=['multi_index'])
    while not ndit.finished:
        k1idx, k2idx, k3idx = ndit.multi_index
        #p2这个带
        k1v = pinfos[k1idx]
        k2v = pinfos[k2idx]
        k3v = pinfos[k3idx]
        k4v = shift_kv(k1v, k2v)
        k4v = shift_kv(k4v, Point(-k3v.coord[0], -k3v.coord[1], 1))
        _, k1nu, _ = get_nu_numpy(k1v.coord[0], k1v.coord[1])
        _, k2nu, _ = get_nu_numpy(k2v.coord[0], k2v.coord[1])
        _, k3nu, _ = get_nu_numpy(k3v.coord[0], k3v.coord[1])
        _, k4nu, _ = get_nu_numpy(k4v.coord[0], k4v.coord[1])
        place_holder2 = numpy.zeros((3, 3, 3, 3))
        ndit2 = numpy.nditer(place_holder2, flags=['multi_index'])
        while not ndit2.finished:
            si1, si2, si3, si4 = ndit2.multi_index
            ret[si1, si2, si3, si4, k1idx, k2idx, k3idx] =\
                umat[k1idx, k2idx, k3idx] * k1nu[si1] * k2nu[si2] * k3nu[si3] * k4nu[si4]
            ndit2.iternext()
        ndit.iternext()
    return ret

def shift_kv(kpt: Point, sft: Point):
    '''将一个动量平移'''
    dest = [kpt.coord[idx] + sft.coord[idx] for idx in range(2)]
    #找到离目标最近的一个第一布里渊区的中心
    #如果这个点距离中心是最近的，那么他就在第一布里渊区里面了，如果
    #离其他的点近，就平移一次，再检查一遍
    b1x = -6.283185307179586#-2*numpy.pi
    b1y = 3.6275987284684357#2*numpy.pi/numpy.sqrt(3)
    brlu_cents = [(0, 0), (b1x, b1y), (0, 2*b1y), (-b1x, b1y),\
        (-b1x, -b1y), (0, -2*b1y), (b1x, -b1y)]
    #计算距离
    dis2cents = [numpy.square(dest[0]-cnt[0]) + numpy.square(dest[1]-cnt[1])\
        for cnt in brlu_cents]
    while numpy.argmin(dis2cents) != 0:
        ncnt = numpy.argmin(dis2cents)
        dest[0] = dest[0] - brlu_cents[ncnt][0]
        dest[1] = dest[1] - brlu_cents[ncnt][1]
        dis2cents = [numpy.square(dest[0]-cnt[0]) + numpy.square(dest[1]-cnt[1])\
            for cnt in brlu_cents]
    return Point(dest[0], dest[1], 1)

def get_von_hove_patches(npat):
    '''获取平分von Hove的patches'''
    #pylint: disable=cell-var-from-loop
    #一共有6个M点
    mpts = [
        Point(3.1415926, 3.1415926 / 1.7320508, 1),
        Point(0, 2*3.1415926 / 1.7320508, 1),
        Point(-3.1415926, 3.1415926 / 1.7320508, 1),
        Point(-3.1415926, -3.1415926 / 1.7320508, 1),
        Point(0, -2*3.1415926 / 1.7320508, 1),
        Point(3.1415926, -3.1415926 / 1.7320508, 1)
    ]
    angs = []
    pat_per_k = npat // 6
    gap = 1 / pat_per_k
    for idx in range(6):
        ridx = idx + 1 if idx + 1 < 6 else 0
        for pidx in range(pat_per_k):
            omega = (pidx + 0.5) * gap
            vpt = middle_point(mpts[idx], mpts[ridx], sc1=1-omega, sc2=omega)
            angs.append(get_absolute_angle(vpt.coord[0], vpt.coord[1]))
    pats = []
    for ang in angs:
        rrad = optimize.bisect(
            lambda rad: p_disp(rad*numpy.cos(ang), rad*numpy.sin(ang)),
            0, 2*numpy.pi/numpy.sqrt(3)
        )
        pats.append(Point(rrad*numpy.cos(ang), rrad*numpy.sin(ang), 1))
    return pats

def check_patches_converge(pat):
    '''
    检测这个点的nu值是不是合理的数值
    '''
    nu11, nu12, nu13 = get_nu(pat.coord[0], pat.coord[1])
    nu21, nu22, nu23 = get_nu(pat.coord[0]+0.01, pat.coord[1]+0.01)
    nu1 = numpy.abs(numpy.stack([nu11, nu12, nu13]))
    nu2 = numpy.abs(numpy.stack([nu21, nu22, nu23]))
    return numpy.allclose(nu1, nu2, rtol=0., atol=0.1)

def get_patches(nang, nrad):
    '''PHYSICAL REVIEW B92, 155137 (2015)
    这里面的patches取法，从K点为中心
    '''
    raise NotImplementedError()

max_stars_repo_name: maryprimary/frg

max_issues_repo_name: maryprimary/frg

max_forks_repo_name: maryprimary/frg

reason: import numpy,from scipy,import sympy


text:
import numpy as np
import os
import config
import matplotlib.pyplot as plt

np.random.seed(2)

x = np.linspace(0, 15, num=100)
f = lambda x: np.sin(x)
y = np.random.normal(f(x), scale=0.35)

gamma = 0.8

quantile_idx = int((1.0-gamma) * len(y))
best_idx = np.argsort(y)[:quantile_idx]
worst_idx = np.argsort(y)[quantile_idx:]

plt.scatter(x[worst_idx], y[worst_idx], label="other observations")
plt.scatter(x[best_idx], y[best_idx], c='r', label="{0}% best samples".format(int(np.round((1.0-gamma) * 100))))
plt.legend(loc="upper right")
plt.xlabel('Hyperparameter value')
plt.ylabel('Function value')
plt.savefig(os.path.join(config.PLOT_FOLDER, "tpe_split"))
plt.show()

max_stars_repo_name: aprams/autotune

max_issues_repo_name: aprams/autotune

max_forks_repo_name: aprams/autotune


text:
'''
All functions related to actual data go in here. Probably only
people at TAMU will want to use this.
'''

import numpy as np
import matplotlib.pyplot as plt

# data files
fnames = ['MS09_L10.mat.txt', 'MS09_L05.mat.txt',
          'MS2_L10.mat.txt', 'MS08_L12.mat.txt']

def read(varin, fname='MS2_L10.mat.txt'):
    '''Read in dataset for variable var

    :param varin: Variable for which to read in data.

    '''

    # # fname = 'MS09_L10.mat.txt'
    # # fname = 'MS09_L05.mat.txt' # has PAR
    # fname = 'MS2_L10.mat.txt' # empty PAR

    d = np.loadtxt(fname, comments='*')

    if fname == 'MS2_L10.mat.txt':
        var = ['lat', 'lon', 'depth', 'temp', 'density', 'sigma', 'oxygen',
               'voltage 2', 'voltage 3', 'fluorescence-CDOM', 'fluorescence-ECO',
               'turbidity', 'pressure', 'salinity', 'RINKO temperature',
               'RINKO DO - CTD temp', 'RINKO DO - RINKO temp', 'bottom', 'PAR']
    elif (fname == 'MS09_L05.mat.txt') or (fname == 'MS09_L10.mat.txt') or (fname == 'MS08_L12.mat.txt'):
        var = ['lat', 'lon', 'depth', 'temp', 'density', 'sigma', 'oxygen',
               'voltage 2', 'voltage 3', 'voltage 4', 'fluorescence-CDOM', 'fluorescence-ECO',
               'turbidity', 'pressure', 'salinity', 'RINKO temperature',
               'RINKO DO - CTD temp', 'RINKO DO - RINKO temp', 'bottom', 'PAR']

    # return data for variable varin
    return d[:, 0], d[:, 1], d[:, 2], d[:, var.index(varin)]

def show(cmap, var, vmin=None, vmax=None):
    '''Show a colormap for a chosen input variable var side by side with
    black and white and jet colormaps.

    :param cmap: Colormap instance
    :param var: Variable to plot.
    :param vmin=None: Min plot value.
    :param vmax=None: Max plot value.

    '''

    # get variable data
    lat, lon, z, data = read(var)

    fig = plt.figure(figsize=(16, 12))

    # Plot with grayscale
    ax = fig.add_subplot(3, 1, 1)
    map1 = ax.scatter(lon, -z, c=data, cmap='gray', s=10, linewidths=0., vmin=vmin, vmax=vmax)
    plt.colorbar(map1, ax=ax)

    # Plot with jet
    ax = fig.add_subplot(3, 1, 2)
    map1 = ax.scatter(lon, -z, c=data, cmap='jet', s=10, linewidths=0., vmin=vmin, vmax=vmax)
    plt.colorbar(map1, ax=ax)

    # Plot with cmap
    ax = fig.add_subplot(3, 1, 3)
    map1 = ax.scatter(lon, -z, c=data, cmap=cmap, s=10, linewidths=0., vmin=vmin, vmax=vmax)
    ax.set_xlabel('Longitude [degrees]')
    ax.set_ylabel('Depth [m]')
    plt.colorbar(map1, ax=ax)

    plt.suptitle(var)

def plot_data():
    '''Plot sample data up with the fancy colormaps.

    '''

    var = ['temp', 'oxygen', 'salinity', 'fluorescence-ECO', 'density', 'PAR', 'turbidity', 'fluorescence-CDOM']
    # colorbar limits for each property
    lims = np.array([[26, 33], [0, 10], [0, 36], [0, 6], [1005, 1025], [0, 0.6], [0, 2], [0, 9]])  # reasonable values
    # lims = np.array([[20,36], [26,33], [1.5,5.6], [0,4], [0,9], [0,1.5]]) # values to show colormaps

    for fname in fnames:
        fig, axes = plt.subplots(nrows=4, ncols=2)
        fig.set_size_inches(20, 10)
        fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99, wspace=0.0, hspace=0.07)
        i = 0
        for ax, Var, cmap in zip(axes.flat, var, cmaps):  # loop through data to plot up

            # get variable data
            lat, lon, z, data = test.read(Var, fname)

            map1 = ax.scatter(lat, -z, c=data, cmap=cmap, s=10, linewidths=0., vmin=lims[i, 0], vmax=lims[i, 1])
            # no stupid offset
            y_formatter = mpl.ticker.ScalarFormatter(useOffset=False)
            ax.xaxis.set_major_formatter(y_formatter)
            if i == 6:
                ax.set_xlabel('Latitude [degrees]')
                ax.set_ylabel('Depth [m]')
            else:
                ax.set_xticklabels([])
                ax.set_yticklabels([])
            ax.set_ylim(-z.max(), 0)
            ax.set_xlim(lat.min(), lat.max())
            cb = plt.colorbar(map1, ax=ax, pad=0.02)
            cb.set_label(cmap.name + ' [' + '$' + cmap.units + '$]')
            i += 1

        fig.savefig('figures/' + fname.split('.')[0] + '.png', bbox_inches='tight')

max_stars_repo_name: MBARIMike/cmocean

max_issues_repo_name: jehyunlee/cmocean

max_forks_repo_name: jehyunlee/cmocean


text:
# coding=utf-8

"""BERT finetuning runner."""

from __future__ import absolute_import, division, print_function

import argparse
import collections
import logging
import os
import random
import nltk
import numpy as np
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader, TensorDataset
from torch.utils.data.distributed import DistributedSampler
from torch.utils.data.sampler import RandomSampler, SequentialSampler
from tqdm import tqdm, trange

import tokenization

from Sentihood_model import BertForSequenceClassification
from optimization import BERTAdam
from processor import (Semeval_NLI_B_Processor, Semeval_NLI_M_Processor,
                       Semeval_QA_B_Processor, Semeval_QA_M_Processor,
                       Semeval_single_Processor, Sentihood_NLI_B_Processor,
                       Sentihood_NLI_M_Processor, Sentihood_QA_B_Processor,
                       Sentihood_QA_M_Processor, Sentihood_single_Processor)

logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s -   %(message)s',
                    datefmt = '%m/%d/%Y %H:%M:%S',
                    level = logging.INFO)
logger = logging.getLogger(__name__)

class InputFeatures(object):
    """A single set of features of data."""

    def __init__(self, input_ids, input_mask, segment_ids, label_id, label_exist_id, aspect,my_pos,dis):
        self.input_ids = input_ids
        self.input_mask = input_mask
        self.segment_ids = segment_ids
        self.label_id = label_id
        self.label_exist_id = label_exist_id
        self.aspect = aspect
        self.my_pos = my_pos
        self.dis = dis
my_dict ={'NN': 0, ':': 1, 'CD': 2, 'JJ': 3, 'IN': 4, 'DT': 5, 'RB': 6, 'NNS': 7, 'CC': 8, 'VBP': 9, 'VBZ': 10, 'VB': 11, ',': 12, 'NNP': 13, 'PRP': 14, 'TO': 15, "''": 16, 'MD': 17, 'VBN': 18, 'VBD': 19, 'VBG': 20, 'PRP$': 21, ')': 22, '(': 23, 'JJR': 24, 'EX': 25, 'WDT': 26, 'JJS': 27, 'POS': 28, 'WRB': 29, 'RP': 30, 'RBR': 31, '``': 32, 'WP': 33, 'RBS': 34, 'FW': 35, 'PDT': 36, 'SYM': 37, '.': 38, 'UH': 39, '$': 40, 'NNPS': 41,'#':42}

def convert_examples_to_features_sentihood(examples, label_list, max_seq_length, tokenizer):
    """Loads a data file into a list of `InputBatch`s."""
    aspect_map = {}
    aspect_list = ['location-1-general','location-1-price','location-1-safety','location-1-transitlocation','location-2-general','location-2-price','location-2-safety','location-2-transitlocation']
    label_map = {}
    for (i, label) in enumerate(label_list):
        label_map[label] = i
    for (i, label) in enumerate(aspect_list):
        aspect_map[label] = i

    features = []
   # my_dict = {}
    for (ex_index, example) in enumerate(tqdm(examples)):

        tokens_a = tokenizer.tokenize(example.text_a)

        tokens_b = None
        if example.text_b:
            tokens_b = tokenizer.tokenize(example.text_b)

        if tokens_b:
            # Modifies `tokens_a` and `tokens_b` in place so that the total
            # length is less than the specified length.
            # Account for [CLS], [SEP], [SEP] with "- 3"
            _truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3)
        else:
            # Account for [CLS] and [SEP] with "- 2"
            if len(tokens_a) > max_seq_length - 2:
                tokens_a = tokens_a[0:(max_seq_length - 2)]

        tokens = []
        segment_ids = []
        tokens.append("[CLS]")
        segment_ids.append(0)
        for token in tokens_a:
            tokens.append(token)
            segment_ids.append(0)
        tokens.append("[SEP]")
        segment_ids.append(0)

        if tokens_b:
            for token in tokens_b:
                tokens.append(token)
                segment_ids.append(1)
            tokens.append("[SEP]")
            segment_ids.append(1)

        input_ids = tokenizer.convert_tokens_to_ids(tokens)
        pos_tag = nltk.pos_tag(tokens)
        my_pos = []
        for item in pos_tag:
            temp = my_dict[item[1]]+1
            if temp > 16:temp = 16
            my_pos.append(temp)

          #  if item[1] == ":":
               # print(item[0])
          #  print(item)
        # T#he mask has 1 for real tokens and 0 for padding tokens. Only real
        # tokens are attended to.
        input_mask = [1] * len(input_ids)
        dis = []
        number = tokens_b[2]
        begin_id = -1
        for i in range(len(tokens)):
            if tokens_a[i] == "location"and tokens_a[i + 1] == '-' and tokens_a[i + 2] == number:
                begin_id = i
                break
        for i in range(len(tokens)):
            if i == begin_id or i == begin_id + 1 or i == begin_id + 2 :dis.append(0)
            elif i < begin_id: dis.append((begin_id - i))
            else: dis.append((i - begin_id - 2))
       # print(dis)
        # Zero-pad up to the sequence length.
        while len(input_ids) < max_seq_length:
            input_ids.append(0)
            input_mask.append(0)
            segment_ids.append(0)
            my_pos.append(0)
            dis.append(0)
      #  while len(dis) < max_seq_length:

        assert len(input_ids) == max_seq_length
        assert len(input_mask) == max_seq_length
        assert len(segment_ids) == max_seq_length
        assert len(dis) == max_seq_length
       # print(tokens_b[0])
       # print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

        #aspect = aspect_map[tokens_b[0]]
       # tempb =tokens_b[4]
      #  print(tokens_b)

        tempb = ""
        for item in tokens_b:
            tempb += item
      #  print(tempb)

        label_id = label_map[example.label]
        aspect = aspect_map[tempb]
        label_exist_id = 1
      #  print(my_pos)
       # print(input_ids)
        if label_id == 2: label_exist_id = 0
        features.append(
                InputFeatures(
                        input_ids=input_ids,
                        input_mask=input_mask,
                        segment_ids=segment_ids,
                        label_id=label_id,
                        label_exist_id = label_exist_id,
                        aspect = aspect,
                        my_pos = my_pos,
                        dis = dis
                        ))

    return features

def _truncate_seq_pair(tokens_a, tokens_b, max_length):
    """Truncates a sequence pair in place to the maximum length."""

    # This is a simple heuristic which will always truncate the longer sequence
    # one token at a time. This makes more sense than truncating an equal percent
    # of tokens from each, since if one sequence is very short then each token
    # that's truncated likely contains more information than a longer sequence.
    while True:
        total_length = len(tokens_a) + len(tokens_b)
        if total_length <= max_length:
            break
        if len(tokens_a) > len(tokens_b):
            tokens_a.pop()
        else:
            tokens_b.pop()

def main():
    parser = argparse.ArgumentParser()

    ## Required parameters
    parser.add_argument("--task_name",
                        default=None,
                        type=str,
                        required=True,
                        choices=["sentihood_single", "sentihood_NLI_M", "sentihood_QA_M", \
                                "sentihood_NLI_B", "sentihood_QA_B", "semeval_single", \
                                "semeval_NLI_M", "semeval_QA_M", "semeval_NLI_B", "semeval_QA_B"],
                        help="The name of the task to train.")
    parser.add_argument("--data_dir",
                        default=None,
                        type=str,
                        required=True,
                        help="The input data dir. Should contain the .tsv files (or other data files) for the task.")
    parser.add_argument("--vocab_file",
                        default=None,
                        type=str,
                        required=True,
                        help="The vocabulary file that the BERT model was trained on.")
    parser.add_argument("--bert_config_file",
                        default=None,
                        type=str,
                        required=True,
                        help="The config json file corresponding to the pre-trained BERT model. \n"
                             "This specifies the model architecture.")
    parser.add_argument("--output_dir",
                        default=None,
                        type=str,
                        required=True,
                        help="The output directory where the model checkpoints will be written.")
    parser.add_argument("--init_checkpoint",
                        default=None,
                        type=str,
                        required=True,
                        help="Initial checkpoint (usually from a pre-trained BERT model).")

    ## Other parameters
    parser.add_argument("--eval_test",
                        default=False,
                        action='store_true',
                        help="Whether to run eval on the test set.")
    parser.add_argument("--do_lower_case",
                        default=True,
                        action='store_true',
                        help="Whether to lower case the input text. True for uncased models, False for cased models.")
    parser.add_argument("--max_seq_length",
                        default=128,
                        type=int,
                        help="The maximum total input sequence length after WordPiece tokenization. \n"
                             "Sequences longer than this will be truncated, and sequences shorter \n"
                             "than this will be padded.")
    parser.add_argument("--train_batch_size",
                        default=32,
                        type=int,
                        help="Total batch size for training.")
    parser.add_argument("--eval_batch_size",
                        default=4,
                        type=int,
                        help="Total batch size for eval.")
    parser.add_argument("--learning_rate",
                        default=5e-5,
                        type=float,
                        help="The initial learning rate for Adam.")
    parser.add_argument("--num_train_epochs",
                        default=3.0,
                        type=float,
                        help="Total number of training epochs to perform.")
    parser.add_argument("--warmup_proportion",
                        default=0.1,
                        type=float,
                        help="Proportion of training to perform linear learning rate warmup for. "
                             "E.g., 0.1 = 10%% of training.")
    parser.add_argument("--no_cuda",
                        default=False,
                        action='store_true',
                        help="Whether not to use CUDA when available")
    parser.add_argument("--accumulate_gradients",
                        type=int,
                        default=1,
                        help="Number of steps to accumulate gradient on (divide the batch_size and accumulate)")
    parser.add_argument("--local_rank",
                        type=int,
                        default=-1,
                        help="local_rank for distributed training on gpus")
    parser.add_argument('--seed',
                        type=int,
                        default=42,
                        help="random seed for initialization")
    parser.add_argument('--gradient_accumulation_steps',
                        type=int,
                        default=1,
                        help="Number of updates steps to accumualte before performing a backward/update pass.")
    args = parser.parse_args()

#--task_name sentihood_NLI_M \
#--data_dir data/sentihood/bert-pair/ \
#--vocab_file uncased_L-12_H-768_A-12/vocab.txt \
#--bert_config_file uncased_L-12_H-768_A-12/bert_config.json \
#--init_checkpoint uncased_L-12_H-768_A-12/pytorch_model.bin \
#--eval_test \
#--do_lower_case \
#--max_seq_length 128 \
#--train_batch_size 2 \
#--learning_rate 2e-5 \
#--num_train_epochs 6.0 \
#--output_dir senti_results/sentihood/NLI_M \
#--seed 42
    if args.local_rank == -1 or args.no_cuda:
        device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
        n_gpu = torch.cuda.device_count()
    else:
        device = torch.device("cuda", args.local_rank)
        n_gpu = 1
        # Initializes the distributed backend which will take care of sychronizing nodes/GPUs
        torch.distributed.init_process_group(backend='nccl')
    logger.info("device %s n_gpu %d distributed training %r", device, n_gpu, bool(args.local_rank != -1))

    if args.accumulate_gradients < 1:
        raise ValueError("Invalid accumulate_gradients parameter: {}, should be >= 1".format(
                            args.accumulate_gradients))

    args.train_batch_size = int(args.train_batch_size / args.accumulate_gradients)

    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if n_gpu > 0:
        torch.cuda.manual_seed_all(args.seed)

    #bert_config = BertConfig.from_json_file(args.bert_config_file)

    #if args.max_seq_length > bert_config.max_position_embeddings:
     #   raise ValueError(
      #      "Cannot use sequence length {} because the BERT model was only trained up to sequence length {}".format(
      #      args.max_seq_length, bert_config.max_position_embeddings))

    if os.path.exists(args.output_dir) and os.listdir(args.output_dir):
        raise ValueError("Output directory ({}) already exists and is not empty.".format(args.output_dir))
    os.makedirs(args.output_dir, exist_ok=True)

    # prepare dataloaders
    processors = {
        "sentihood_single":Sentihood_single_Processor,
        "sentihood_NLI_M":Sentihood_NLI_M_Processor,
        "sentihood_QA_M":Sentihood_QA_M_Processor,
        "sentihood_NLI_B":Sentihood_NLI_B_Processor,
        "sentihood_QA_B":Sentihood_QA_B_Processor,
        "semeval_single":Semeval_single_Processor,
        "semeval_NLI_M":Semeval_NLI_M_Processor,
        "semeval_QA_M":Semeval_QA_M_Processor,
        "semeval_NLI_B":Semeval_NLI_B_Processor,
        "semeval_QA_B":Semeval_QA_B_Processor,
    }

    processor = processors[args.task_name]()
    label_list = processor.get_labels()

    tokenizer = tokenization.FullTokenizer(
        vocab_file=args.vocab_file, do_lower_case=args.do_lower_case)

    # training set
    train_examples = None
    num_train_steps = None
    train_examples = processor.get_train_examples(args.data_dir)
    num_train_steps = int(
        len(train_examples) / args.train_batch_size * args.num_train_epochs)

    train_features = convert_examples_to_features_sentihood(
        train_examples, label_list, args.max_seq_length, tokenizer)
    logger.info("***** Running training *****")
    logger.info("  Num examples = %d", len(train_examples))
    logger.info("  Batch size = %d", args.train_batch_size)
    logger.info("  Num steps = %d", num_train_steps)#乘完了iteration的次数

    all_input_ids = torch.tensor([f.input_ids for f in train_features], dtype=torch.long)
    all_input_mask = torch.tensor([f.input_mask for f in train_features], dtype=torch.long)
    all_segment_ids = torch.tensor([f.segment_ids for f in train_features], dtype=torch.long)
    all_label_ids = torch.tensor([f.label_id for f in train_features], dtype=torch.long)
    all__detection_label_ids = torch.tensor([f.label_exist_id for f in train_features], dtype=torch.long)
    all_aspects= torch.tensor([f.aspect for f in train_features], dtype=torch.long)
    all_pos = torch.tensor([f.my_pos for f in train_features], dtype=torch.long)
    all_dis = torch.tensor([f.dis for f in train_features], dtype=torch.long)
    train_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids,all__detection_label_ids,all_aspects,all_pos,all_dis)
    if args.local_rank == -1:
        train_sampler = RandomSampler(train_data)
    else:
        train_sampler = DistributedSampler(train_data)
    train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=args.train_batch_size)

    # test set
    if args.eval_test:
        test_examples = processor.get_test_examples(args.data_dir)
        test_features = convert_examples_to_features_sentihood(
            test_examples, label_list, args.max_seq_length, tokenizer)

        all_input_ids = torch.tensor([f.input_ids for f in test_features], dtype=torch.long)
        all_input_mask = torch.tensor([f.input_mask for f in test_features], dtype=torch.long)
        all_segment_ids = torch.tensor([f.segment_ids for f in test_features], dtype=torch.long)
        all_label_ids = torch.tensor([f.label_id for f in test_features], dtype=torch.long)
        all_detection_label_ids = torch.tensor([f.label_exist_id for f in test_features], dtype=torch.long)
        all_aspects= torch.tensor([f.aspect for f in test_features], dtype=torch.long)
        all_pos = torch.tensor([f.my_pos for f in test_features], dtype=torch.long)
        all_dis = torch.tensor([f.dis for f in test_features], dtype=torch.long)
        test_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids,all_detection_label_ids,all_aspects,all_pos,all_dis)
        test_dataloader = DataLoader(test_data, batch_size=args.eval_batch_size, shuffle=False)

    # model and optimizer

    model = BertForSequenceClassification( len(label_list))
   # model = torch.load("model_data/attention_add_model")
    #if args.init_checkpoint is not None:
       # model.bert.load_state_dict(torch.load(args.init_checkpoint, map_location='cpu'))
    model.to(device)

    if args.local_rank != -1:
        model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
                                                          output_device=args.local_rank)
    elif n_gpu > 1:
        model = torch.nn.DataParallel(model)

    no_decay = ['bias', 'gamma', 'beta']
    optimizer_parameters = [
         {'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay_rate': 0.01},#xiaobuxing
         {'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay_rate': 0.0}
         ]

    optimizer = BERTAdam(optimizer_parameters,
                         lr=args.learning_rate,
                         warmup=args.warmup_proportion,
                         t_total=num_train_steps)

    #optimizer = transformers.AdamW(optimizer_parameters)
    # train
    output_log_file = os.path.join(args.output_dir, "log.txt")
    print("output_log_file=",output_log_file)
    with open(output_log_file, "w") as writer:
        if args.eval_test:
            writer.write("epoch\tglobal_step\tloss\ttest_loss\ttest_accuracy\n")
        else:
            writer.write("epoch\tglobal_step\tloss\n")

    global_step = 0
    epoch=0
   # model = torch.load("model_data/attention_add_model6")
    #40
    for _ in trange(int(args.num_train_epochs), desc="Epoch"):
        epoch+=1
        model.train()
        tr_loss = 0
        nb_tr_examples, nb_tr_steps = 0, 0
        for step, batch in enumerate(tqdm(train_dataloader, desc="Iteration")):
            batch = tuple(t.to(device) for t in batch)
           # print()
            input_ids, input_mask, segment_ids, label_ids, exist_ids, all_aspects,pos,dis = batch
            loss, _ ,_= model(input_ids, segment_ids, input_mask, label_ids, exist_ids, all_aspects,pos,dis)
            if n_gpu > 1:
                loss = loss.mean() # mean() to average on multi-gpu.
            if args.gradient_accumulation_steps > 1:
                loss = loss / args.gradient_accumulation_steps
            loss.backward()
            tr_loss += loss.item()
            nb_tr_examples += input_ids.size(0)
            nb_tr_steps += 1
            if (step + 1) % args.gradient_accumulation_steps == 0:
                optimizer.step()    # We have accumulated enought gradients
                model.zero_grad()
                global_step += 1
           # break
        torch.save(model,"model_data/attention_add_model"+str(epoch))
         #eval_test
        if args.eval_test :
            model.eval()
            test_loss, test_accuracy,senti_test_accuracy = 0, 0,0
            nb_test_steps, nb_test_examples, senti_nb_test_examples = 0, 0, 0
            with open(os.path.join(args.output_dir, "test_ep_"+str(epoch)+".txt"),"w") as f_test:
                for input_ids, input_mask, segment_ids, label_ids, exist_ids,all_aspects,pos,dis in test_dataloader:
                    input_ids = input_ids.to(device)
                    input_mask = input_mask.to(device)
                    segment_ids = segment_ids.to(device)
                    label_ids = label_ids.to(device)
                    exist_ids = exist_ids.to(device)
                    all_aspects = all_aspects.to(device)
                    dis = dis.to(device)
                    pos = pos.to(device)
                    with torch.no_grad():
                        tmp_test_loss, detect_logits,sent_logits = model(input_ids, segment_ids, input_mask, label_ids, exist_ids,all_aspects,pos,dis)

                    detect_logits = F.softmax(detect_logits, dim=-1)
                    detect_logits = detect_logits.detach().cpu().numpy()
                    exist_ids = exist_ids.to('cpu').numpy()
                    outputs = np.argmax(detect_logits, axis=1)
                    for output_i in range(len(outputs)):
                        f_test.write(str(outputs[output_i]))
                        for ou in detect_logits[output_i]:
                            f_test.write(" "+str(ou))
                        f_test.write("\n")
                    tmp_test_accuracy=np.sum(outputs == exist_ids)

                    test_loss += tmp_test_loss.mean().item()
                    test_accuracy += tmp_test_accuracy

                    sent_logits = F.softmax(sent_logits, dim=-1)
                    sent_logits = sent_logits.detach().cpu().numpy()
                    label_ids = label_ids.to('cpu').numpy()
                    senti_outputs = np.argmax(sent_logits, axis=1)
                    for output_i in range(len(senti_outputs)):
                        f_test.write(str(senti_outputs[output_i]))
                        for ou in sent_logits[output_i]:
                            f_test.write(" "+str(ou))
                        f_test.write("\n")
                    senti_tmp_test_accuracy=np.sum(senti_outputs == label_ids)

                    senti_test_accuracy += senti_tmp_test_accuracy

                    nb_test_examples += input_ids.size(0)
                    senti_nb_test_examples += np.sum(exist_ids)
                    nb_test_steps += 1

            test_loss = test_loss / nb_test_steps
            test_accuracy = test_accuracy / nb_test_examples
            senti_accuracy = senti_test_accuracy / senti_nb_test_examples

        result = collections.OrderedDict()
        if args.eval_test:
            result = {'epoch': epoch,
                    'global_step': global_step,
                    'loss': tr_loss/nb_tr_steps,
                    'test_loss': test_loss,
                    'test_accuracy': test_accuracy,
                    'senti_test_accuracy':senti_accuracy,
                    }
        else:
            result = {'epoch': epoch,
                    'global_step': global_step,
                    'loss': tr_loss/nb_tr_steps}

        logger.info("***** Eval results *****")
        with open(output_log_file, "a+") as writer:
            for key in result.keys():
                logger.info("  %s = %s\n", key, str(result[key]))
                writer.write("%s\t" % (str(result[key])))
            writer.write("\n")

if __name__ == "__main__":
    main()


text:
from __future__ import annotations
import datetime
from dateutil.parser import parse
import numpy as np

# %% utility functions
def mag_vector2incl_decl(x: float, y: float, z: float) -> tuple[float, float]:
    """
    Inputs:
    -------
    vectors of the geomagnetic field.
    x: north componment
    y: east component
    z: down (by convention) component

    outputs:
    --------
    declination [degrees]
    inclination [degrees]

    http://geomag.nrcan.gc.ca/mag_fld/comp-en.php
    """

    decl = np.degrees(np.arctan2(y, x))

    incl = np.degrees(np.arctan2(z, np.hypot(x, y)))

    return decl, incl

def latlon2colat(glat: float, glon: float) -> tuple[np.ndarray, np.ndarray]:
    # atleast_1d for iteration later
    colat = 90 - np.atleast_1d(glat)
    elon = (360 + np.atleast_1d(glon)) % 360

    return colat, elon

def latlonworldgrid(latstep: int = 5, lonstep: int = 5) -> tuple[np.ndarray, np.ndarray]:
    lat = np.arange(-90.0, 90 + latstep, latstep)
    lon = np.arange(-180.0, 180 + lonstep, lonstep)
    glon, glat = np.meshgrid(lon, lat)

    return glat, glon

def datetime2yeardec(time: str | datetime.datetime | datetime.date) -> float:
    """
    Convert a datetime into a float. The integer part of the float should
    represent the year.
    Order should be preserved. If adate<bdate, then d2t(adate)<d2t(bdate)
    time distances should be preserved: If bdate-adate=ddate-cdate then
    dt2t(bdate)-dt2t(adate) = dt2t(ddate)-dt2t(cdate)
    """

    if isinstance(time, float):
        # assume already year_dec
        return time
    if isinstance(time, str):
        t = parse(time)
    elif isinstance(time, datetime.datetime):
        t = time
    elif isinstance(time, datetime.date):
        t = datetime.datetime.combine(time, datetime.datetime.min.time())
    elif isinstance(time, (tuple, list, np.ndarray)):
        return np.asarray([datetime2yeardec(t) for t in time])
    else:
        raise TypeError("unknown input type {}".format(type(time)))

    year = t.year

    boy = datetime.datetime(year, 1, 1)
    eoy = datetime.datetime(year + 1, 1, 1)

    return year + ((t - boy).total_seconds() / ((eoy - boy).total_seconds()))


text:
#!/usr/bin/env python
"""
obssim.py

    Observation Simulator wrapper for webbPSF.

    This package lets you easily script simulations of things more complicated than just a single point source.

"""
import os
import numbers
import numpy as np
import scipy.interpolate, scipy.ndimage
import matplotlib.pyplot as plt
import matplotlib
import pysynphot
import logging
import poppy

from . import webbpsf_core

_log = logging.getLogger('webbpsf')
#
###########################################################################
#
#

class TargetScene(object):
    """ This class allows the user to specify some scene consisting of a central star
    plus one or more companions at specified separation, spectral type, etc. It automates the
    necessary calculations to perform a simulated JWST observation of that target.

    pysynphot is required for this.

    """

    def __init__(self):
        self.sources = []

    def addPointSource(self, sptype_or_spectrum, name="unnamed source", separation=0.0, PA=0.0, normalization=None):
        """ Add a point source to the list for a given scene

        Parameters
        -----------
        sptype_or_spectrum : string or pysynphot.Spectrum
            spectrum of the source
        name : str
            descriptive string
        separation : float
            arcsec
        PA : float
            deg from N
        normalization : scalar or tuple TBD
            Simple version: this is a float to multiply the PSF by.
            Complex version: Probably tuple of arguments to spectrum.renorm().

        How normalization works:
            First the PSF for that source is calculated, using calc_psf(norm='first')
            i.e. the input intensity through the telescope pupil is set to 1.
            The resulting output PSF total counts will be proportional to the
            throughput through the OTE+SI (including filters, coronagraphs etc)

            Then we apply the normalization:
                1) if it's just a number, we just multiply by it.
                2) if it's something else: Then we use a separate bandpass object and parameters
                   passed in here to figure out the overall normalization, and apply that as a
                   multiplicative factor to the resulting PSF itself?
        """
        if type(sptype_or_spectrum) is str:
            spectrum = poppy.specFromSpectralType(sptype_or_spectrum)
        else:
            spectrum = sptype_or_spectrum

        self.sources.append(   {'spectrum': sptype_or_spectrum, 'separation': separation, 'PA': PA,
            'normalization': normalization, 'name': name})

    def calc_image(self, instrument, outfile=None, noise=False, rebin=True, clobber=True,
            PA=0, offset_r=None, offset_PA=0.0, **kwargs):
        """ Calculate an image of a scene through some instrument

        Parameters
        -----------
        instrument : webbpsf.jwinstrument instance
            A configured instance of an instrument class
        outfile : str
            filename to save to
        rebin : bool
            passed to calc_psf
        PA : float
            postion angle for +Y direction in the output image
        offset_r, offset_PA : float
            Distance and angle to offset the target center from the FOV center.
            This is to simulate imperfect acquisition + alignment.
        noise : bool
            add read noise? TBD
        clobber : bool
            overwrite existing files? default True

        It may also be useful to pass arguments to the calc_psf() call, which is supported through the **kwargs
        mechanism. Such arguments might include fov_arcsec, fov_pixels, oversample, etc.
        """

        sum_image = None
        image_PA = PA

        for obj in self.sources:
            _log.info('Now propagating for '+obj['name'])
            # set  companion spectrum and position
            src_spectrum = obj['spectrum']

            if offset_r is None:
                instrument.options['source_offset_r'] = obj['separation']
                instrument.options['source_offset_theta'] = obj['PA'] - image_PA
            else:
                # combine the actual source position with the image offset position.
                obj_x = obj['separation'] * np.cos(obj['PA'] * np.pi/180)
                obj_y = obj['separation'] * np.sin(obj['PA'] * np.pi/180)
                offset_x = offset_r * np.cos(offset_PA * np.pi/180)
                offset_y = offset_r * np.sin(offset_PA * np.pi/180)

                src_x = obj_x + offset_x
                src_y = obj_y + offset_y
                src_r = np.sqrt(src_x**2+src_y**2)
                src_pa = np.arctan2(src_y, src_x) * 180/np.pi
                instrument.options['source_offset_r'] = src_r
                instrument.options['source_offset_theta'] = src_pa - image_PA
                #stop()

            _log.info('  post-offset & rot pos: %.3f  at %.1f deg' % (instrument.options['source_offset_r'], instrument.options['source_offset_theta']))

            src_psf =  instrument.calc_psf(source = src_spectrum, outfile=None, save_intermediates=False, rebin=rebin,
                **kwargs)

            # figure out the flux ratio
            if obj['normalization'] is not None:
                # use the explicitly-provided normalization:
                if isinstance(obj['normalization'], numbers.Number):
                    src_psf[0].data *= obj['normalization']
                    fluxlogstring = "                with source flux = {}".format(obj['normalization'])
                else:
                    raise NotImplementedError("Not Yet")
            else:
                # use the flux level already implicitly set by the source spectrum.
                # i.e. figure out what the flux of the source is, inside the selected bandpass
                bp = instrument._get_synphot_bandpass()
                effstim_Jy = pysynphot.Observation(src_spectrum, bp).effstim('Jy')
                fluxlogstring = "                with effstim = %.3g Jy" % effstim_Jy
                src_psf[0].data *= effstim_Jy

            # add the scaled companion PSF to the stellar PSF:
            if sum_image is None:
                sum_image = src_psf
                sum_image[0].header.add_history("obssim : Creating an image simulation with multiple PSFs")
                sum_image[0].header['IMAGE_PA'] = ( image_PA,'PA of scene in simulated image')
                sum_image[0].header['OFFSET_R'] = (0 if offset_r is None else offset_r ,'[arcsec] Offset of target center from FOV center')
                sum_image[0].header['OFFSETPA'] = (0 if offset_PA is None else offset_PA ,'[deg] Position angle of target offset from FOV center')

                if offset_r is None:
                    sum_image[0].header.add_history("Image is centered on target (perfect acquisition)")
                else:
                    sum_image[0].header.add_history("Image is offset %.2f arcsec at PA=%.1f from target" % (offset_r, offset_PA))

            else:
                sum_image[0].data += src_psf[0].data
            #update FITS header history
            sum_image[0].header.add_history("Added source %s at r=%.3f, theta=%.2f" % (obj['name'], obj['separation'], obj['PA']))
            sum_image[0].header.add_history(fluxlogstring)
            sum_image[0].header.add_history("                counts in image: %.3g" % src_psf[0].data.sum())
            sum_image[0].header.add_history("                pos in image: %.3g'' at %.1f deg" % (instrument.options['source_offset_r'],  instrument.options['source_offset_theta'])  )

        if noise:
            raise NotImplementedError("Not Yet")

        sum_image[0].header['NSOURCES'] = ( len(self.sources), "Number of point sources in sim")
            #add noise in image - photon and read noise, mainly.

        # downsample?
        if rebin and sum_image[0].header['DET_SAMP'] > 1:
            # throw away the existing rebinned extension
            sum_image.pop()
            # and generate a new one from the summed image
            _log.info(" Downsampling summed image to detector pixel scale.")
            rebinned_sum_image = sum_image[0].copy()
            detector_oversample = sum_image[0].header['DET_SAMP']
            rebinned_sum_image.data = poppy.utils.rebin_array(rebinned_sum_image.data, rc=(detector_oversample, detector_oversample))
            rebinned_sum_image.header['OVERSAMP'] = ( 1, 'These data are rebinned to detector pixels')
            rebinned_sum_image.header['CALCSAMP'] = ( detector_oversample, 'This much oversampling used in calculation')
            rebinned_sum_image.header['EXTNAME'] = ( 'DET_SAMP')
            rebinned_sum_image.header['PIXELSCL'] *= detector_oversample
            sum_image.append(rebinned_sum_image)

        if outfile is not None:
            sum_image[0].header["FILENAME"] = ( os.path.basename (outfile), "Name of this file")
            sum_image.writeto(outfile, clobber=clobber)
            _log.info("Saved image to "+outfile)
        return sum_image

    def display(self):
        plt.clf()
        for obj in self.sources:
            X = obj['separation'] * -np.sin(obj['PA'] * np.pi/180)
            Y = obj['separation'] * np.cos(obj['PA'] * np.pi/180)

            plt.plot([X],[Y],'*')
            plt.text(X,Y, obj['name'])

def test_obssim(nlambda=3, clobber=False):
    s = TargetScene()

    s.addPointSource('G0V', name='G0V star', separation = 0.1, normalization=1.)
    s.addPointSource('K0V', name='K0V star', separation = 1.0, PA=45,  normalization=0.4)
    s.addPointSource('M0V', name='M0V star', separation = 1.5, PA=245,  normalization=0.3)

    inst = webbpsf_core.NIRCam()

    for filt in ['F115W', 'F210M', 'F360M']:
        inst.filter = filt
        outname = "test_scene_%s.fits"% filt
        if not os.path.exists(outname) or clobber:
            s.calc_image(inst, outfile=outname, fov_arcsec=5, nlambda=nlambda)

if __name__ == "__main__":

    logging.basicConfig(level=logging.INFO,format='%(name)-10s: %(levelname)-8s %(message)s')

reason: import numpy,import scipy


text:
# load numpy array from npy file
from numpy import load
# load array
data = load('/home/mbarnig/compre-taco2-mb/Comprehensive-Tacotron2/preprocessed_data/Marylux/text/Marylux-text-lb-wiki-0161.npy')
# print the array
print(data)

reason: from numpy