text:
from condor_tensorflow.labelencoder import CondorOrdinalEncoder
import pytest
import tensorflow as tf
import numpy as np
import pandas as pd

def test_labelencoder_basic():
    NUM_CLASSES = 5
    labels = np.arange(NUM_CLASSES)
    enc_labs = CondorOrdinalEncoder(nclasses=NUM_CLASSES).fit_transform(labels)
    expected = np.array([[0., 0., 0., 0.],
                         [1., 0., 0., 0.],
                         [1., 1., 0., 0.],
                         [1., 1., 1., 0.],
                         [1., 1., 1., 1.]])
    np.testing.assert_allclose(enc_labs, expected)

def test_labelencoder_advanced1():
    labels = np.array(['a', 'b', 'c', 'd', 'e'])
    enc_labs = CondorOrdinalEncoder().fit_transform(labels)
    expected = np.array([[0., 0., 0., 0.],
                         [1., 0., 0., 0.],
                         [1., 1., 0., 0.],
                         [1., 1., 1., 0.],
                         [1., 1., 1., 1.]])
    np.testing.assert_allclose(enc_labs, expected)

def test_labelencoder_advanced2():
    labels = ['a', 'b', 'c', 'd', 'e']
    enc_labs = CondorOrdinalEncoder().fit_transform(labels)
    expected = np.array([[0., 0., 0., 0.],
                         [1., 0., 0., 0.],
                         [1., 1., 0., 0.],
                         [1., 1., 1., 0.],
                         [1., 1., 1., 1.]])
    np.testing.assert_allclose(enc_labs, expected)

def test_labelencoder_advanced3():
    labels = ['low', 'med', 'high']
    enc = CondorOrdinalEncoder(categories=[['low', 'med', 'high']])
    enc_labs = enc.fit_transform(labels)
    expected = np.array([[0., 0.],
                         [1., 0.],
                         [1., 1.]])
    np.testing.assert_allclose(enc_labs, expected)

ext: py

reason: import numpy


text:
#!/usr/bin/env python3

import matplotlib
matplotlib.use("Agg")
import numpy
import pylab
import sys
import os

#pylab.rcParams["font.size"]=8
#pylab.rcParams["legend.fontsize"]=8
#pylab.rcParams["lines.linewidth"]=1
#pylab.rcParams["axes.linewidth"]=2
#pylab.rcParams["axes.labelsize"]="large"
#pylab.rcParams["axes.labelweight"]="bold"
pylab.rcParams["xtick.major.size"]=0
pylab.rcParams["xtick.minor.size"]=0
pylab.rcParams["ytick.major.size"]=0
pylab.rcParams["ytick.minor.size"]=0
#pylab.rcParams["xtick.direction"]="out"
#pylab.rcParams["ytick.direction"]="out"
#pylab.rcParams["figure.figsize"]=(3, 3)
colormap="gist_heat_r"

data=numpy.loadtxt(sys.argv[1], delimiter=",")
pos=numpy.loadtxt(sys.argv[2], delimiter=",")
xlen=int(sys.argv[3])
ylen=int(sys.argv[4])

time=data[:,0]
data=data[:,1:]*1000.0
pos=pos[:,1:]

data_max=200.0
data[data>data_max]=data_max
data_min=numpy.min(data)

plot_pitch=2

posA=[0.5*float(xlen), 0.3*float(ylen)]
posB=[0.5*float(xlen), 0.7*float(ylen)]
posC1=[0.9*float(xlen), 0.7*float(ylen)]
posD1=[0.9*float(xlen), 0.3*float(ylen)]
posC2=[0.1*float(xlen), 0.7*float(ylen)]
posD2=[0.1*float(xlen), 0.3*float(ylen)]

count=0
for t in range(len(time)):
    if (t*10)/len(time)==(t*10)//len(time):
        print((t*100)//len(time),"%...")
    if t%plot_pitch==0:
        trialnum=int(1+time[t]/15)
        if trialnum%2==1:
            goal="D1"
        else:
            goal="D2"
        explain=""

        pylab.close()
        pylab.imshow((data[t,:].reshape(xlen,ylen)).T, vmin=data_min, vmax=data_max, interpolation="none", cmap=colormap)
        pylab.plot([posA[0],posB[0]], [posA[1], posB[1]], color="gray")
        pylab.plot([posB[0],posC1[0]], [posB[1], posC1[1]], color="gray")
        pylab.plot([posB[0],posC2[0]], [posB[1], posC2[1]], color="gray")
        pylab.plot([posC1[0],posD1[0]], [posC1[1], posD1[1]], color="gray")
        pylab.plot([posC2[0],posD2[0]], [posC2[1], posD2[1]], color="gray")
        pylab.text(posA[0],posA[1],"A")
        pylab.text(posB[0],posB[1],"B")
        pylab.text(posC1[0],posC1[1],"C1")
        pylab.text(posC2[0],posC2[1],"C2")
        pylab.text(posD1[0],posD1[1],"D1")
        pylab.text(posD2[0],posD2[1],"D2 (reward)")
        pylab.plot(pos[t,0], pos[t,1], "o", color="blue")
        pylab.xlim([0,xlen-1])
        pylab.ylim([ylen-1,0])
        pylab.colorbar()
        pylab.title(str(numpy.round(time[t],decimals=1))+" s Trial "+str(trialnum)+": travel A -> "+goal)
        pylab.text(25,48,explain, verticalalignment="bottom", horizontalalignment="center", fontsize=11)
        pylab.tight_layout()

        if count<10:
            tag="000"+str(count)
        elif count<100:
            tag="00"+str(count)
        elif count<1000:
            tag="0"+str(count)
        elif count<10000:
            tag=str(count)
        pylab.savefig("img"+tag+".png")
        count=count+1

#create movie
#os.system("avconv -r 25 -i img%04d.png -vsync cfr movie.avi")
os.system("avconv -r 50 -i img%04d.png -vsync cfr movie.avi")
os.system("rm img*.png")


text:
import numpy as np

MAX_IMAGE_ID = 2**31 - 1

def array_to_blob(array):
    return array.tostring()

def blob_to_array(blob, dtype, shape=(-1,)):
    if blob is None:
        return None
    return np.fromstring(blob, dtype=dtype).reshape(*shape)

def image_ids_to_pair_id(image_id1, image_id2):
    if image_id1 > image_id2:
        image_id1, image_id2 = image_id2, image_id1
    return image_id1 * MAX_IMAGE_ID + image_id2

def pair_id_to_image_ids(pair_id):
    image_id2 = pair_id % MAX_IMAGE_ID
    image_id1 = (pair_id - image_id2) // MAX_IMAGE_ID
    return image_id1, image_id2

def binary_search_matches(keypoint_id, matches):
    # assuming the id is in the first column
    left = 0
    right = len(matches) - 1
    mid = (left + right) // 2
    while left <= right and keypoint_id != matches[mid, 0]:
        if keypoint_id < matches[mid, 0]:
            right = mid - 1
        if keypoint_id > matches[mid, 0]:
            left = mid + 1

        mid = (left + right) // 2

    if left <= right:
        assert(keypoint_id == matches[mid, 0])
        return matches[mid, 1]
    else:
        # there might be no keypoint asscoiated with `keypoint_id` in this match
        return -1

    # assert(mid >= 0 and mid < len(matches) and keypoint_id == matches[mid, 0])

def IntersectList(v1, v2, need_diff=True):
    """
        intersected: intersection of v1 and v2
        non_intersected: v2 - v1
    """
    intersected = []

    seen = set(v1)
    if need_diff:
        non_intersected = []
        for element in v2:
            if element in seen:
                intersected.append(element)
            else:
                non_intersected.append(element)
        return intersected, non_intersected
    else:
        for element in v2:
            if element in seen:
                intersected.append(element)
        return intersected

max_stars_repo_name: lxxue/colmap

max_issues_repo_name: lxxue/colmap

max_forks_repo_name: lxxue/colmap


text:
import pandas as pd
import numpy as np
from collections import OrderedDict

from sklearn.model_selection import KFold
from dateutil.relativedelta import relativedelta

#import matlab.engine
#import matlab
#eng = matlab.engine.start_matlab()
from sklearn.metrics import f1_score

from main import train_and_evaluate

def reshape(x,input_dim):
    '''
    Reshapes x into a matrix with input_dim columns

    '''
    x = np.array(x)
    if x.size ==input_dim:
        x = x.reshape((1,input_dim))
    return x

class KnowledgeGraphBlackbox:

    def __init__(self,  bounds=None,sd=None):
        self.input_dim = 4

        # NOTE FOR ME: Put here all parameters to hyperparameters, with
        #   - name
        #   - range

        # batches_per_epoch: from 50 to 200 (int)
        # learning rate: from 0.1 to 0.0001 (real)
        # embedding_size: from 50 to 300 (int)
        # ent_neg_rate: from 1 to 10 (int)

        if bounds == None:  # n_node: 512, dropout 0.2, 512, 0.2, 10 # learning rate, decay, momentum
            self.bounds = OrderedDict([('batches_per_epoch',(50, 200)),
                                       ('learning_rate',(0.1, 0.0001)),
                                      ('embedding_size',(50, 300)),
                                       ('ent_neg_rate',(1,10))])

        else:
            self.bounds = bounds

        self.min = [(0.)*self.input_dim]
        self.fstar = 20
        self.ismax=-1   # NOTE FOR ME: this means the score must me minimized
        self.name='KnowledgeGraphBlackbox'
        self.X_train=None
        self.X_test=None
        self.Y_train=None
        self.Y_test=None

    # input set of hyperparam values
    def run_Blackbox(self, X):

        batches_per_epoch = np.int(X[0])
        learning_rate = X[1]
        embedding_size = np.int(X[2])
        ent_neg_rate = np.int(X[3])
        utility = train_and_evaluate(batches_per_epoch=batches_per_epoch,
                                     learning_rate=learning_rate,
                                     embedding_size=embedding_size,
                                     ent_neg_rate=ent_neg_rate,
                                     clean_everything_afterwards=True)

        return utility

    def func(self,X):
        X=np.asarray(X)

        np.random.seed(1)  # for reproducibility

        if len(X.shape)==1: # 1 data point
            Utility=self.run_Blackbox(X)
        else:
            Utility=np.apply_along_axis( self.run_Blackbox,1,X)

        return Utility*self.ismax


text:
import numpy
import pytest
from matchms import Spectrum
from matchms.similarity import FingerprintSimilarityParallel

def test_fingerprint_similarity_parallel_cosine_empty_fingerprint():
    """Test cosine score matrix with empty fingerprint."""
    fingerprint1 = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarityParallel(similarity_measure="cosine")
    score_matrix = similarity_measure([spectrum1, spectrum2],
                                      [spectrum1, spectrum2])
    assert score_matrix == pytest.approx(numpy.array([[0, 0],
                                                      [0, 1.]]), 0.001), "Expected different values."

def test_fingerprint_similarity_parallel_jaccard_empty_fingerprint():
    """Test jaccard score matrix with empty fingerprint."""
    fingerprint1 = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarityParallel(similarity_measure="jaccard")
    score_matrix = similarity_measure([spectrum1, spectrum2],
                                      [spectrum1, spectrum2])
    assert score_matrix == pytest.approx(numpy.array([[0, 0],
                                                      [0, 1.]]), 0.001), "Expected different values."

def test_fingerprint_similarity_parallel_dice_empty_fingerprint():
    """Test dice score matrix with empty fingerprint."""
    fingerprint1 = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarityParallel(similarity_measure="dice")
    score_matrix = similarity_measure([spectrum1, spectrum2],
                                      [spectrum1, spectrum2])
    assert score_matrix == pytest.approx(numpy.array([[0, 0],
                                                      [0, 1.]]), 0.001), "Expected different values."

def test_fingerprint_similarity_parallel_cosine():
    """Test cosine score matrix with known values."""
    spectrum0 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={})

    fingerprint1 = numpy.array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarityParallel(similarity_measure="cosine")
    score_matrix = similarity_measure([spectrum0, spectrum1, spectrum2],
                                      [spectrum0, spectrum1, spectrum2])
    assert score_matrix[1:, 1:] == pytest.approx(numpy.array([[1., 0.84515425],
                                                              [0.84515425, 1.]]), 0.001), "Expected different values."
    assert numpy.all(numpy.isnan(score_matrix[:, 0])), "Expected 'nan' entries."
    assert numpy.all(numpy.isnan(score_matrix[0, :])), "Expected 'nan' entries."

def test_fingerprint_similarity_parallel_jaccard():
    """Test jaccard index matrix with known values."""
    spectrum0 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={})

    fingerprint1 = numpy.array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarityParallel(similarity_measure="jaccard")
    score_matrix = similarity_measure([spectrum0, spectrum1, spectrum2],
                                      [spectrum0, spectrum1, spectrum2])
    assert score_matrix[1:, 1:] == pytest.approx(numpy.array([[1., 0.71428571],
                                                              [0.71428571, 1.]]), 0.001), "Expected different values."
    assert numpy.all(numpy.isnan(score_matrix[:, 0])), "Expected 'nan' entries."
    assert numpy.all(numpy.isnan(score_matrix[0, :])), "Expected 'nan' entries."

def test_fingerprint_similarity_parallel_dice():
    """Test dice score matrix with known values."""
    spectrum0 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={})

    fingerprint1 = numpy.array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarityParallel(similarity_measure="dice")
    score_matrix = similarity_measure([spectrum0, spectrum1, spectrum2],
                                      [spectrum0, spectrum1, spectrum2])
    assert score_matrix[1:, 1:] == pytest.approx(numpy.array([[1., 0.83333333],
                                                              [0.83333333, 1.]]), 0.001), "Expected different values."
    assert numpy.all(numpy.isnan(score_matrix[:, 0])), "Expected 'nan' entries."
    assert numpy.all(numpy.isnan(score_matrix[0, :])), "Expected 'nan' entries."

def test_fingerprint_similarity_parallel_cosine_set_empty_to_0():
    """Test cosine score matrix with known values. Set non-exising values to 0."""
    spectrum0 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={})

    fingerprint1 = numpy.array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0])
    spectrum1 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint1})

    fingerprint2 = numpy.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1])
    spectrum2 = Spectrum(mz=numpy.array([], dtype="float"),
                         intensities=numpy.array([], dtype="float"),
                         metadata={"fingerprint": fingerprint2})

    similarity_measure = FingerprintSimilarityParallel(set_empty_scores=0, similarity_measure="cosine")
    score_matrix = similarity_measure([spectrum0, spectrum1, spectrum2],
                                      [spectrum0, spectrum1, spectrum2])
    assert score_matrix == pytest.approx(numpy.array([[0, 0, 0],
                                                      [0, 1., 0.84515425],
                                                      [0, 0.84515425, 1.]]), 0.001), "Expected different values."


text:
import numpy as np
import pandas as pd

from .base import SplitStrategy
from pandas import DataFrame

class Temporal(SplitStrategy):
    """Random.

    A strategy that splits the user-item interactions based
    on timestamp. The idea is to avoid introducing a bias in users'
    consumption history by breaking the chronological order of the
    events.
    """

    def get_test_uids(
        self,
        data: np.ndarray,
        num_test_users: int
    ) -> np.array:
        """get_test_uids.
        Performs the temporal splitting strategy.
        Args:
            data (ndarray): the data to be split.
            num_test_users (int): total number of users in the test set.
        Returns:
            A list of the users IDs that will be in the test set.
        """
        data_df = pd.DataFrame(data, columns=["userId", "itemId", "rating", "timestamp"])
        test_candidate_users = self._get_users_candidate(data_df)
        test_candidate_users = np.array(test_candidate_users, dtype=int)
        users_start_time = data_df.groupby("userId").min()["timestamp"].to_dict()
        users_start_time = [users_start_time[tcu] for tcu in test_candidate_users]

        test_uids = np.array(
            list(
                test_candidate_users[
                    list(reversed(np.argsort(users_start_time)))
                ]
            )[:num_test_users]
        )
        return test_uids

max_stars_repo_name: irec-org/irec

max_issues_repo_name: irec-org/irec

max_forks_repo_name: irec-org/irec


text:
#! /usr/bin/env python
# -- coding: utf-8 --

from __future__ import print_function, unicode_literals

import pyATP
import lineZ
from lineZ import Polar

import shutil
import glob
import itertools
import numpy as np

np.set_printoptions(linewidth=120, precision=4)

# =============================================================================
# Configure file names and bus names

proj_dir = 'C:/Users/pdbrown/Documents/ATPdata/work/L1241Phasing/'
models = ['']
model_weights = [1.]

# Run all data in a temporary directory since the ATPDraw won't know
# to rebuild all the .pch files that this program monkeys with.
tmp_dir = 'C:/Users/pdbrown/Documents/ATPdata/work/L1241Phasing/tmp/'

# =============================================================================
# Enter line configuration information.

# Buses for monitoring voltage unbalance
buses = ['NPLAT', 'THEDF', 'STAPL', 'MAXWE', 'CALLA', 'LOUPC', 'NLOUP', 'BBWIN',
         'CROOK', 'ORD',   'SPALD', 'BROKE', 'MUDDY']
Pos = {}
Str_names = {}

# H-frame structures
Pos['H'] = ['EHS side', 'Center', 'OPGW side']
Str_names['H'] = 'H-frame'

# Single-pole structures
Pos['SP'] = ['Bot', 'Mid', 'Top']
Str_names['SP'] = 'Single-pole'

# Segment lengths & Configurations
sections = [(0,       'SP', 'Muddy Creek Sub',   'L241A'),
            ( 5.98,   'H',  'Transition',        'L241B'),
            ( 6.73,   'SP', 'Transition',        'L241C'),
            (11.69,   'H',  'Transition',        'L241D'),
            (16.16,   'SP*', 'Transition',       'L241E'),
            (23.27,   'H', 'Special Transition', 'L241F'),
            (30.01,   'SP', 'Transition',        'L241G'),
            (39.63,   '',   'Ord Sub', '')]

# =============================================================================
# Define transition possibilities for this study
def transitions(from_str, to_str, bot_to_center=True):
    """ Returns a list of the possible transitions from from_str to to_str
        bot_to_center indicates whether the transition from delta to horizontal
        should allow the bottom conductor in the delta to move to the center
        position in the horizontal configuration.

        limit_SP indicates whether the transition from delta to vertical and
        back to delta should allow all phase change combinations or whether
        only the top two phases should be allowed to swap. Set to True to
        limit the transition to only swapping the top two phases.

        _ indicates a non-transposing continuation
        * indicates a transition that sets bot_to_center to true.
    """
    if from_str[-1] == '_':
        # Non-transposing continuation
        return [(0, 1, 2)]
    if from_str[-1] == '*':
        bot_to_center = True
    from_str2 = from_str.rstrip('_*')
    to_str2 = to_str.rstrip('_*')
    if from_str2 == 'SP' and to_str2 == 'SP':
        # For this study, vertical structures are kept the same top-to-bottom
        # as delta. If transition is allowed, it is just inverting positions
        # of the top two phases.
        return (0, 1, 2), (0, 2, 1)
    if from_str2 == 'H' and to_str2 == 'SP':
        return ((1, 0, 2), (2, 0, 1)) if bot_to_center else ((2, 0, 1),)

    if to_str2 == 'H' and from_str2 == 'SP':
        return ((1, 0, 2), (1, 2, 0)) if bot_to_center else ((1, 2, 0),)

    if from_str2 == 'H' and to_str2 == 'H':
        # H-frame transposition structure lets either outside phase move to
        # center
        return (0, 2, 1), (1, 0, 2), (0, 1, 2)
    raise ValueError('from_str = '+from_str+', to_str = '+to_str)

# =============================================================================
# Divide data into some different arrays and print output to the screen

PIs = np.array([s[0] for s in sections])
PI_desc = [s[2] for s in sections]
section_ATPname = [s[3] for s in sections[:-1]]
# L is an array of the lengths of the segments
md_list = ['### Line Sections:']
for n, s_start, s_end in zip(range(1,len(sections)),
                             sections[:-1],
                             sections[1:]):
    md_list.append('Mile %.3f: %s' % (s_start[0], s_start[2]))
    md_list.append('    Section %d: %s (%.3f mi)' %
                   (n, Str_names[s_start[1].rstrip('_*')],
                    s_end[0] - s_start[0]))
md_list.append('Mile %.3f: %s' % (s_end[0], s_end[2]))
print('\n'.join(md_list))
L = PIs[1:] - PIs[:-1]

str_types = [s[1] for s in sections[:-1]]

transitions_list = [((0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 1, 0),
                    (2, 0, 1))] + [transitions(from_str, to_str,
                                               bot_to_center=False)
                    for from_str, to_str in zip(str_types[:-1], str_types[1:])]

# =============================================================================
# Generate list of all possible transitions
all_transitions_dict = lineZ.make_transitions_dict(transitions_list, str_types)
print('Number of phasing combinations by number of transpositions:')

all_transitions_list = []
for k, v in all_transitions_dict.items():
    print(k, len(v))
    all_transitions_list.extend(v)

# =============================================================================
# Define evaluation criteria and filter to get the non-dominated results.
L1240A_segs = ['L40A1', 'L40A2', 'L40A3']
_, L1240A_params = pyATP.get_line_params_from_pch(
    proj_dir, L1240A_segs)
L1240A_Z21 = L1240A_params['Zsum_s'][2, 1]

L1240B_segs = ['L240B']
_, L1240B_params = pyATP.get_line_params_from_pch(
    proj_dir, L1240B_segs)
L1240B_Z21 = L1240B_params['Zsum_s'][2, 1]

def Z21_magnitude(r, as_str=False):
    """
    :param r: (summary_line_data, seg_data)
    :param as_str:
    :return:
    """
    if r[0] is None:
        return 0.
    Z21 = r[0]['Zsum_s'][2, 1]
    if not as_str:
        return abs(Z21)
    else:
        return '\n'.join(['Z21: {:.4f} Ohms'.format(Polar(Z21)),
                          'Z21: {:.4f} Ohms series w/ L1240A'
                          .format(Polar(Z21 + L1240A_Z21))])
Z21_magnitude.description = 'Z21 magnitude'
Z21_magnitude.units = 'Ohms'

def Z_imbalance(r, as_str=False):
    """
    :param r: (summary_line_data, seg_data)
    :param as_str:
    :return:
    """
    if r[0] is None:
        return 0.
    rtn = lineZ.impedance_imbalance(r[0]['Zsum'])
    if not as_str:
        return rtn
    else:
        Zph = lineZ.phase_impedances(r[0]['Zsum'])
        return 'Impedance imbalance: {} {:.4f} %'.format(Zph, rtn)
Z_imbalance.description = 'Impedance imbalance'
Z_imbalance.units = '%'

criteria = [Z21_magnitude, Z_imbalance]
criteria_weights = [1., 1.]

# Hold results of each model in a dict indexed by the ATP model name
results_dict = lineZ.new_results_dict(all_transitions_list, models)

for model in models:
    # =============================================================================
    # Copy working files to temp directory
    # Line segment data files
    for f in itertools.chain(glob.glob(proj_dir + '*.dat'),
                             glob.glob(proj_dir + '*.lib')):
        shutil.copy(f, tmp_dir)

    # =============================================================================
    # Run analysis of all data cases.
    # Time to run is approximately 1-2 s per case for this model.
    results = []
    print('Running calcs....')
    for n, t in all_transitions_dict.items():
        for n2, l in enumerate(t):
            print('For %d transpositions, case %d of %d' % (n, n2, len(t)))

            # Set phasing of line sections in ATP .dat files & re-run line
            # constants.
            for Pt, s in zip(lineZ.cum_Pt(l), section_ATPname):
                line_const = tmp_dir + s + '.dat'

                with open(line_const, 'r') as f:
                    inlines = f.read().splitlines()
                line_data = pyATP.LineConstCards()
                line_data.read(inlines)

                # Modify the phasing in .dat file.
                for idx, ph in enumerate(Pt):
                    line_data.data['conductors'][idx]['IP'] = ph + 1

                outlines = line_data.write()

                with open(line_const, 'w') as f:
                    f.writelines(outlines)
                # Run ATP on the .dat file to create .pch file.
                pyATP.run_ATP(line_const)

            # Read in line impedance parameters from PCH files
            seg_data_dict, summary_data_dict = \
                pyATP.get_line_params_from_pch(tmp_dir, section_ATPname)

            results_dict[l][model] = ((summary_data_dict,
                                       seg_data_dict),)

# =============================================================================
# Filter to non-dominated results across all models.

filtered_results_dict = \
    lineZ.filter_nondominated_results_multimodel(results_dict, criteria)

# Compute weighted criteria results
nondom_only = True
soln_list, weighted_results = lineZ.apply_criteria_weighting(
    filtered_results_dict if nondom_only else results_dict,
    criteria, model_weights, criteria_weights)
weights_results_dict = {soln: wt for soln, wt in
                        zip(soln_list, weighted_results)}

# =============================================================================
# Print results

print('-'*80)
print('Results of best option(s)')
subtitle = 'Weights:\n'
if len(models) > 1:
    subtitle += ', '.join([' %s: %.0f' % (m, m_wt)
                           for m, m_wt in zip(models, model_weights)])
    subtitle += '\n'
subtitle += ', '.join(['%s: %.0f' % (f.description, f_wt)
                       for f, f_wt in zip(criteria, criteria_weights)])
print(subtitle)

for soln, wt in sorted(zip(soln_list, weighted_results), key=lambda k: k[1]):
    r = results_dict[soln]
    print('-'*80)

    print('Solution key:', soln[0], soln[5])
    for model in models:
        if len(models) > 1:
            print('Model:', model)
        for c in criteria:
            print('%s' % c(r[model][0], as_str=True))
    print('Weighted evaluation: %.4f' % wt)

print('-'*80)

# Print dominated solutions.
dominated_solns = [soln for soln in all_transitions_list
                   if soln not in filtered_results_dict]
print('Dominated solutions:')
for soln in dominated_solns:
    print(soln[0], soln[5])
# =============================================================================
# Summarize selected solution
# selected_soln = ((1, 2, 0), (1, 0, 2))  # Option A
selected_soln = ((0, 1, 2), (1, 0, 2))  # Option B
if selected_soln is not None:
    full_soln_tuple = tuple([s for s in results_dict
                             if (s[0], s[5]) == selected_soln][0])
    r = results_dict[full_soln_tuple]
    summary_data_dict = r[models[0]][0][0]
    seg_data_dict = r[models[0]][0][1]

    print('-'*80)
    print('Selected solution: ', selected_soln)

    print('Total line impedance:')
    Z_s = summary_data_dict['Zsum_s']
    print('Z1 = {:.4f}, Z0 = {:.4f}, Z21 = {:.4f}'
          .format(Z_s[1, 1],
                  Z_s[0, 0],
                  Polar(Z_s[2, 1])))

    for model in models:
        if len(models) > 1:
            print('Model:', model)
        for c in criteria:
            print('%s' % c(r[model][0], as_str=True))

    phasing_info = lineZ.Pt_list_to_phasing(full_soln_tuple, str_types, Pos,
                                            Phase_list=('A', 'B', 'C'))
    print('Line Sections:')
    for n, phasing, s_start, s_end in zip(range(1, len(sections)),
                                          phasing_info,
                                          sections[:-1], sections[1:]):
        print(('Mile %.3f: %s' % (s_start[0], s_start[2]))
              + (' (Transposition)'
                 if n > 1
                    and sections[n-2][1].rstrip('_*')
                        == sections[n-1][1].rstrip('_*')
                    and full_soln_tuple[n-1] != (0, 1, 2)
                 else ''))
        print('Section %d: %s (%.3f mi)' %
              (n, Str_names[s_start[1].rstrip('_*')],
               s_end[0] - s_start[0]))
        print(phasing)
        Z = seg_data_dict[section_ATPname[n - 1]].Z
        Z_s = lineZ.ph_to_seq_m(Z)
        print('    Z1 = {:.4f}, Z0 = {:.4f}, Z21 = {:.4f}'
              .format(Z_s[1, 1],
                      Z_s[0, 0],
                      Polar(Z_s[2, 1])))
        print('    Phase impedances & imbalance: {}, {:.2f}%'.format(
            lineZ.phase_impedances(Z),
            lineZ.impedance_imbalance(Z)))
    print('Mile %.3f: %s' % (s_end[0], s_end[2]))


text:
import numpy as np
import xgboost as xgb
import optuna

from supervised.utils.metric import Metric
from supervised.algorithms.registry import BINARY_CLASSIFICATION
from supervised.algorithms.registry import MULTICLASS_CLASSIFICATION
from supervised.algorithms.registry import REGRESSION

EPS = 1e-8

class XgboostObjective:
    def __init__(
        self,
        ml_task,
        X_train,
        y_train,
        sample_weight,
        X_validation,
        y_validation,
        sample_weight_validation,
        eval_metric,
        n_jobs,
    ):
        self.dtrain = xgb.DMatrix(X_train, label=y_train, weight=sample_weight)
        self.dvalidation = xgb.DMatrix(
            X_validation, label=y_validation, weight=sample_weight_validation
        )
        self.X_validation = X_validation
        self.y_validation = y_validation
        self.eval_metric = eval_metric
        self.n_jobs = n_jobs

        self.learning_rate = 0.0125
        self.rounds = 1000
        self.early_stopping_rounds = 50
        self.seed = 123

        self.objective = ""
        self.eval_metric_name = ""
        self.num_class = None
        if ml_task == BINARY_CLASSIFICATION:
            self.objective = "binary:logistic"
            # the mapping is the same
            # allowed metrics: logloss, auc
            self.eval_metric_name = self.eval_metric.name

        elif ml_task == MULTICLASS_CLASSIFICATION:
            self.objective = "multi:softprob"
            self.eval_metric_name = "mlogloss"
            self.num_class = len(np.unique(y_train))
        else:  # ml_task == REGRESSION
            self.objective = "reg:squarederror"
            # the mapping is the same
            # allowed metrics: rmse, mae, mape
            self.eval_metric_name = self.eval_metric.name

    def __call__(self, trial):
        param = {
            "objective": self.objective,
            "eval_metric": self.eval_metric_name,
            "tree_method": "hist",
            "booster": "gbtree",
            "eta":  trial.suggest_categorical("eta",
                    [0.0125, 0.025, 0.05, 0.1]),
            "max_depth": trial.suggest_int("max_depth", 2, 12),
            "lambda": trial.suggest_float("lambda", EPS, 10.0, log=True),
            "alpha": trial.suggest_float("alpha", EPS, 10.0, log=True),
            "colsample_bytree": min(
                trial.suggest_float("colsample_bytree", 0.3, 1.0 + EPS), 1.0
            ),
            "subsample": min(trial.suggest_float("subsample", 0.3, 1.0 + EPS), 1.0),
            "min_child_weight": trial.suggest_int("min_child_weight", 1, 100),
            "n_jobs": self.n_jobs,
            "seed": self.seed,
        }
        if self.num_class is not None:
            param["num_class"] = self.num_class
        try:
            pruning_callback = optuna.integration.XGBoostPruningCallback(
                trial, f"validation-{self.eval_metric_name}"
            )
            bst = xgb.train(
                param,
                self.dtrain,
                self.rounds,
                evals=[(self.dvalidation, "validation")],
                early_stopping_rounds=self.early_stopping_rounds,
                callbacks=[pruning_callback],
                verbose_eval=False,
            )
            preds = bst.predict(self.dvalidation, ntree_limit=bst.best_ntree_limit)
            score = self.eval_metric(self.y_validation, preds)
            if Metric.optimize_negative(self.eval_metric.name):
                score *= -1.0
        except optuna.exceptions.TrialPruned as e:
            raise e
        except Exception as e:
            print("Exception in XgboostObjective", str(e))
            return None

        return score

max_stars_repo_name: rterbush/mljar-supervised

max_issues_repo_name: rterbush/mljar-supervised

max_forks_repo_name: rterbush/mljar-supervised


text:
from sympy import sqrt, symbols

def norm(vec):
    return sqrt(vec.dot(vec))

reason: from sympy


text:
"""
===========================
Enhancing Off-limb emission
===========================

This example shows how to enhance emission above the limb.
"""
import numpy as np

import matplotlib.pyplot as plt

import astropy.units as u
from astropy.visualization.mpl_normalize import ImageNormalize

import sunpy.map
from sunpy.data.sample import AIA_171_IMAGE

###############################################################################
# We first create the Map using the sample data.

aia = sunpy.map.Map(AIA_171_IMAGE)

###############################################################################
# Next we build two arrays which include all of the x and y pixel indices.
# We must not forget to add the correct units because we will next pass this
# into a function which requires them.

x, y = np.meshgrid(*[np.arange(v.value) for v in aia.dimensions]) * u.pix

###############################################################################
# Now we can convert this to helioprojective coordinates and create a new
# array which contains the normalized radial position for each pixel

hpc_coords = aia.pixel_to_world(x, y)
r = np.sqrt(hpc_coords.Tx ** 2 + hpc_coords.Ty ** 2) / aia.rsun_obs

###############################################################################
# Let's check how emission above the limb depends on distance

rsun_step_size = 0.01
rsun_array = np.arange(1, r.max(), rsun_step_size)
y = np.array([aia.data[(r > this_r) * (r < this_r + rsun_step_size)].mean()
              for this_r in rsun_array])

###############################################################################
# Next let's plot it along with a fit to the data. We perform the fit in
# linear-log space.  We fit the logarithm of the intensity since the intensity
# drops of very quickly as a function of distance from the limb.

params = np.polyfit(rsun_array[rsun_array < 1.5],
                    np.log(y[rsun_array < 1.5]), 1)

###############################################################################
# Tell matplotlib to use LaTeX for all the text, make the fontsize bigger, and
# then plot the data and the fit.

fontsize = 14
plt.plot(rsun_array, y, label='data')
best_fit = np.exp(np.poly1d(params)(rsun_array))
label = r'best fit: {:.2f}$e^{{{:.2f}r}}$'.format(best_fit[0], params[0])
plt.plot(rsun_array, best_fit, label=label)
plt.yscale('log')
plt.ylabel(r'mean DN', fontsize=fontsize)
plt.xlabel(r'radius r ($R_{\odot}$)', fontsize=fontsize)
plt.xticks(fontsize=fontsize)
plt.yticks(fontsize=fontsize)
plt.title(r'observed off limb mean DN and best fit', fontsize=fontsize)
plt.legend(fontsize=fontsize)
plt.tight_layout()
plt.show()

###############################################################################
# We now create our scaling array.  At the solar radius, the scale factor is 1.
# Moving away from the disk, the scaling array increases in value.  Finally,
# in order to not affect the emission on the disk, we set the scale factor to
# unity for values of r less than 1.

scale_factor = np.exp((r-1)*-params[0])
scale_factor[r < 1] = 1

###############################################################################
# Let's now plot and compare the results.  The scaled map uses the same image
# stretching function as the original image (set by the keyword 'stretch')
# clipped to the same range (set by the keywords 'vmin' and 'vmax').

scaled_map = sunpy.map.Map(aia.data * scale_factor, aia.meta)
scaled_map.plot_settings['norm'] = ImageNormalize(stretch=aia.plot_settings['norm'].stretch,
                                                  vmin=aia.data.min(), vmax=aia.data.max())

fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(121, projection=aia)
aia.plot()
aia.draw_limb()
ax = fig.add_subplot(122, projection=aia)
scaled_map.plot()
scaled_map.draw_limb()
plt.show()

reason: import numpy,import astropy,from astropy


text:
from distutils.core import setup, Extension
import numpy.distutils.misc_util

setup(
    ext_modules=[Extension("pyarome", ["pyarome.c"],library_dirs=['/usr/local/lib/'], libraries=['arome'])],
    include_dirs=numpy.distutils.misc_util.get_numpy_include_dirs() + ['/usr/local/include/'],
)

max_stars_repo_name: andres-jordan/PyARoME

max_issues_repo_name: andres-jordan/PyARoME

max_forks_repo_name: andres-jordan/PyARoME


text:
# coding: utf-8
"""
Name: eady_growth_rate.py

Calcurate eady_growth rate. (JRA-55 can calcurate eady_growth_ratemore easily. Not Reccomend to use.)

example: python3 eady_growth_rate.py --file <ncfile>

Author: Ryosuke Tomita
Date: 2021/10/22
"""
import argparse
import numpy as np
from ncmagics import japanmap, meteotool

def parse_args() -> dict:
    """parse_args.
    set file path.

    Args:

    Returns:
        dict:
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--file", help="set ncfile.", type=str)
    p = parser.parse_args()
    args = {"file": p.file}
    return args

def cal_brunt_vaisala_frequency(d_ln_theta: np.ndarray, d_z: np.ndarray) -> np.ndarray:
    """cal_brunt_vaisala_frequency.
    brunt vaisala frequency [s^-2]
    N^2 = (g/theta) * (d_theta/dz)

    Args:
        d_ln_theta (np.ndarray): d_ln_theta
        d_z (np.ndarray): d_z

    Returns:
        np.ndarray:
    """
    gravity = 9.8
    N = np.sqrt(gravity * d_ln_theta / np.array(d_z))

    return N

def cal_eady_growh_rate(N: np.ndarray, d_u: np.ndarray, d_z: np.ndarray,
                        lat: np.ndarray, lon: np.ndarray) -> np.ndarray:
    """cal_eady_growh_rate.
    Eady growth rate [day^(-1)]
    sigma = 0.31 * (f/N) |round_u/round_z|
    N: brunt vaisala frequency calcurated by cal_brunt_vaisala_frequency()

    Args:
        N (np.ndarray): N
        d_u (np.ndarray): d_u
        d_z (np.ndarray): d_z
        lat (np.ndarray): lat
        lon (np.ndarray): lon

    Returns:
        np.ndarray:
    """
    omega = 6.28 / 86400
    _, lat_2d = np.meshgrid(lon, lat)
    f = 2 * omega * np.sin(np.radians(lat_2d))

    sigma = 0.31 * (f/N) * np.abs(d_u/d_z) * 3600 * 24
    print(np.mean(sigma))
    return sigma

def main():
    """main.
    1. get args.
    2. Make CalcPhysics instance.
    3. get 700 hPa and 800 hPa 's physical values.
    4. calcurate eady_growth_rate.
    5. plot.
    """
    #gh_file = "/home/tomita/jra55/isobaric_surface/geo_p/anl_p125_hgt.202001_nc"
    #u_file = "/home/tomita/jra55/isobaric_surface/wind/anl_p125_ugrd.202001_nc"
    #tfile = "/home/tomita/jra55/isobaric_surface/temp/anl_p125_tmp.202001_nc"
    gh_file = "/home/tomita/jra55/isobaric_surface/geo_p/anl_p125_hgt.202101_nc"
    u_file = "/home/tomita/jra55/isobaric_surface/wind/anl_p125_ugrd.202101_nc"
    tfile = "/home/tomita/jra55/isobaric_surface/temp/anl_p125_tmp.202101_nc"

    meteo_tool = meteotool.MeteoTools(gh_file)
    lat, lon = meteo_tool.get_lat_lon()

    temperature_k, gh, u_wind, w_wind, potential_temperature = [], [], [], [], []
    for pressure in (70000, 85000):
        gh.append(
            (meteo_tool.get_parameter('gh', isobaric_surface=pressure, ncfile=gh_file) +
            meteo_tool.get_parameter('gh', isobaric_surface=pressure, ncfile=gh_file.replace("202101", "202012"))) / 2
        )
        u_wind.append(
            (meteo_tool.get_parameter('u', isobaric_surface=pressure, ncfile=u_file) +
            meteo_tool.get_parameter('u', isobaric_surface=pressure, ncfile=u_file.replace("202101", "202012"))) / 2
        )
        #w_wind.append(meteo_tool.get_parameter('w', isobaric_surface=pressure))

        # calcurate potential_temperature
        temperature_k = (meteo_tool.get_parameter('t', isobaric_surface=pressure, ncfile=tfile) +  meteo_tool.get_parameter('t', isobaric_surface=pressure, ncfile=tfile.replace("202101", "202012")) / 2)
        potential_temperature.append(meteo_tool.cal_potential_temperature(temperature_k, pressure))

    # calcurate brunt vaisala frequency
    d_z = gh[0] - gh[1]
    d_ln_theta = (np.log(np.array(potential_temperature[0]))
        - np.log(np.array(potential_temperature[1])))
    N = cal_brunt_vaisala_frequency(d_ln_theta, d_z)

    # calcurate Eady growth rate.
    d_u = u_wind[0] - u_wind[1]
    sigma = cal_eady_growh_rate(N, d_u, d_z, lat, lon)

    # plot data
    jp_map = japanmap.JpMap()
    jp_map.contour_plot(lon, lat, sigma)
    jp_map.shade_plot(lon, lat, sigma, label="eady growth rate (day$^{-1}$)", color_bar_label_min=0, color_bar_label_max=1,)
    jp_map.save_fig("sigma", "Eady growth rate (day$^{-1}$)")

if __name__ == "__main__":
    main()

max_stars_repo_name: RyosukeDTomita/meteoplot

max_issues_repo_name: RyosukeDTomita/meteoplot

max_forks_repo_name: RyosukeDTomita/meteoplot


text:
import multiprocessing
import secrets
from getpass import getpass

import numpy as np
from experiment_collection import Experiment, ExperimentCollectionRemote
from numba import njit
from tqdm.auto import tqdm

@njit
def trace1(LAM: float, MU: float, n: int, prob=1.0, T=1000, seed=None):
    """
    Функция вычисляет среднее время ожидания в очереди системы
    и времена ухода клиентов.
    :param LAM: Интенсивность входящего потока.
    :param MU: Интенсивность обслуживания одним каналом.
    :param n: Количество каналов обслуживания.
    :param prob: Вероятность заявки пройти в следующий узел.
    :param T: Общее время моделирования.
    :param seed: random state.
    :return: Cреднее время ожидания в очереди системы
    и времена ухода клиентов.
    """
    np.random.seed(seed)
    wait_time = 0  # Общее время ожидания в очереди системы
    out_time = []  # Времена ухода клиентов
    L = 0  # Текущая длина очереди
    t_out = np.zeros(n)  # Времена освобождения каналов обслуживания
    t = 0  # Текущее время
    k = 0  # Общее число требований, обслужившихся в системе
    t_in = np.random.exponential(1 / LAM)  # Время прихода очередного требования
    while T >= t_in or L:
        t0 = t
        if T >= t_in:  # Если время моделирования еще не вышло
            t = np.min(np.array([t_in, np.min(t_out)]))
        else:
            t = np.min(t_out)
        h = t - t0
        wait_time += L * h

        if t == np.min(t_out) and L > 0:  # Если освободится канал и очередь непустая
            j = np.argmin(t_out)
            t_out[j] += np.random.exponential(1 / MU)  # Отправляем одного человека из очереди на обслуживание
            if np.random.binomial(1, prob):
                out_time.append(t_out[j])
            L -= 1
        elif t == np.min(t_out) and L == 0:  # Если осбоводился канал и очередь пустая
            j = np.argmin(t_out)
            t_out[j] = t_in + np.random.exponential(1 / MU)  # Пришедшее требование сразу отправляем на обслуживание
            if np.random.binomial(1, prob):
                out_time.append(t_out[j])
            t_in += np.random.exponential(1 / LAM)
            k += 1
        else:  # Новое требование пришло раньше, чем освободился канал (все каналы заняты)
            L += 1
            t_in += np.random.exponential(1 / LAM)
            k += 1

    out_time.sort()

    return wait_time / k, np.array(out_time)

@njit
def trace2(LAM, MU, n, prob=1.0, seed=None):
    """
    Функция возвращает среднее время ожидания в очереди системы
    и времена ухода клиентов.
    :param LAM: Времена прихода клиентов.
    :param MU: Интенсивность обслуживания одним каналом.
    :param n: Количество каналов обслуживания.
    :param prob: Вероятность заявки пройти в следующий узел.
    :param seed: random state.
    :return: Cреднее время ожидания в очереди системы
    и времена ухода клиентов.
    """
    np.random.seed(seed)
    wait_time = 0  # Общее время ожидания в очереди системы.
    out_time = []  # Времена ухода клиентов.
    L = 0  # Текущая длина очереди
    k = 1  # Общее число требований, обслужившихся в системе
    t_out = np.zeros(n)  # Времена освобождения каналов обслуживания
    t = 0  # Текущее время
    T = LAM[-1]  # Продолжительность моделирования
    t_in = LAM[0]  # Время прихода очередного требования
    LAM = LAM[1:]

    while T >= t_in or L:
        t0 = t
        if T >= t_in:  # Если время моделирования еще не вышло
            t = np.min(np.array([t_in, np.min(t_out)]))
        else:
            t = np.min(t_out)
        h = t - t0
        wait_time += L * h

        if t == np.min(t_out) and L > 0:  # Если освободился канал обслуживания и очередь непустая
            j = np.argmin(t_out)
            t_out[j] += np.random.exponential(1 / MU)  # Отправляем одного человека из очереди на обслуживание
            if np.random.binomial(1, prob):
                out_time.append(t_out[j])
            L -= 1
        elif t == np.min(t_out) and L == 0:  # Если осбоводился канал обслуживания и очередь пустая
            j = np.argmin(t_out)
            t_out[j] = t_in + np.random.exponential(1 / MU)  # Пришедшее требование сразу отправляем на обслуживание
            if np.random.binomial(1, prob):
                out_time.append(t_out[j])
            if len(LAM):  # Генерируем новое требование
                t_in = LAM[0]
                LAM = LAM[1:]
                k += 1
            else:
                t_in += 1
        else:  # Новое требование пришло раньше, чем освободился канал (все каналы заняты)
            L += 1
            if len(LAM):  # Генерируем новое требование
                t_in = LAM[0]
                LAM = LAM[1:]
                k += 1
            else:
                t_in += 1

    out_time.sort()

    return wait_time / k, np.array(out_time)

def experiment(k):
    mu1 = [1 / 87, 1 / 30, 1 / 70]  # Интенсивности обслуживания в узлах
    lam1 = 28659.2 / (24 * 60 * 60)  # Поток одиночных пассажиров в секунду
    p1 = [0.999, 0.999]  # Вероятности перехода в следующий узел
    t1, out1 = trace1(lam1, mu1[0], 30, p1[0], T=10_000_000, seed=k)
    t2, out2 = trace2(out1, mu1[1], 11, p1[1], seed=k + 1)
    t3, _ = trace2(out2, mu1[2], 24, seed=k + 2)
    return t1 + t2 + t3, k

if __name__ == '__main__':
    tasks = [secrets.randbelow(2 ** 32) for i in range(1000)]
    exps = ExperimentCollectionRemote('experiments.asciishell.ru', 'main', getpass(), True)
    exps = exps.create_namespace('masha_exp_30_11_24_10000000')
    with multiprocessing.Pool(multiprocessing.cpu_count()) as pool:
        for t, seed in tqdm(pool.imap(experiment, tasks), total=len(tasks)):
            exp = Experiment('name_{}'.format(seed), params={'seed': seed}, metrics={'time': t})
            exps.add_experiment(exp, ignore_included=True)
    print(exps.get_experiments()['metrics_time'].mean())

reason: import numpy,from numba


text:
#!/usr/bin/env python3
import pandas as pd
import sys
import multiprocessing
import numpy as np
import pandas as pd
import sys
import importlib.machinery
import Bio; from Bio import SeqIO
import sequtils
import shutil
import algo
import os
import subprocess
from collections import defaultdict
import json
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
from Bio.Seq import translate
import sys
from collections import Counter
from io import StringIO
import uniprot
import pickle
from io import StringIO
import yaml
import blast
from Bio.Blast import NCBIXML
import gff3

def gff3_contig_export(recs):
    contig_columns = []
    for rec in recs:
        id = rec.id
        reference_sequence = id.split('|')[0]
        size = len(str(rec.seq))
        start = '1'
        end = str(len(str(rec.seq)))
        strand = '.'
        source = 'Assembly'
        type='contig'
        score ='.'
        strand='.'
        phase='.'
        attributes='Name={}'.format(reference_sequence)
        row = reference_sequence + '\t' + source + '\t' + type + '\t' + start + '\t' + end     + '\t' + score + '\t' + strand + '\t' + phase + '\t' + attributes
        contig_columns.append(row)
    return contig_columns

def orfblastp2gff3(blast_records, query_fasta, orf_fasta,  eval_cutoff, subject_cutoff):
    mapdict = {}
    subject_count = {}

    recdict = defaultdict(list)
    for blast_record in blast_records:
        top_eval=None
        aln_count = 1
        qdesc = query_fasta[blast_record.query.split()[0]].description
        qid = query_fasta[blast_record.query.split()[0]].id
        qseq = str(query_fasta[blast_record.query.split()[0]].seq)
        qlen = len(qseq)
        blast_record.alignments.sort(key=lambda align: max(hsp.score for hsp in align.hsps), reverse=True)
        rec_count = 1
        for alignment in blast_record.alignments:
            alignment.hsps.sort(key=lambda hsp :  hsp.score , reverse=True)
            if not qseq in mapdict:
                mapdict[qseq] = None
                subject_count[qseq] = 0
            for hsp in alignment.hsps:
                if mapdict[qseq] != hsp.sbjct:
                    mapdict[qseq] = hsp.sbjct
                    subject_count[qseq] += 1
                if (subject_count[qseq] <= subject_cutoff) and (hsp.expect <= eval_cutoff):
                    orf_rec = orf_fasta[alignment.hit_def.split()[0]]
                    orf_seq = str(orf_rec.seq)
                    orf_coords = orf_rec.id.split('|')[-1]
                    orf_start = int(orf_coords.split(':')[0].split(')')[1])
                    orf_end = int(orf_coords.split(':')[1])
                    orf_strand = orf_coords.split('(')[1].split(')')[0]
                    hitseq = hsp.sbjct.replace('-','')
                    amino_offset = orf_seq.find(hitseq)
                    b2g = blast.blast2genome(fasta_id = qid, query_seq = hsp.query, match_seq=hsp.match, hit_seq = orf_seq, hit_from=orf_start, hit_to=orf_end, hit_strand=orf_strand, amino_offset=amino_offset)

                    contig = orf_rec.id.split('_')[-3]
                    source = "BLASTP"
                    type="polypeptide"
                    score = hsp.expect
                    phase= '.'
                    attributes="ID=match{}-{};Name={}".format(rec_count, qseq, qseq)
                    start = b2g.alignment_start
                    end = b2g.alignment_end
                    strand = orf_strand
                    row = contig +'\t' + source +'\t' + type + '\t' + str(start) + '\t' + str(end) + '\t' + str(score) + '\t' + strand + '\t' + phase + '\t' + attributes

                    recdict[qseq].append(row)

                    for rec in b2g.records:
                        start = rec[1]
                        end = rec[2]
                        strand = rec[3]
                        score = hsp.expect
                        phase= '.'
                        attributes="Parent=match{}-{};Name={}".format(rec_count, qseq, qseq)
                        row = contig +'\t' + source +'\t' + type + '\t' + str(start) + '\t' + str(end) + '\t' + str(score) + '\t' + strand + '\t' + phase + '\t' + attributes
                        recdict[qseq].append(row)
                    rec_count += 1
    return recdict

def tblastn2gff3(blast_records, query_fasta, eval_cutoff, subject_cutoff):
    mapdict = {}

    subject_count = {}

    recdict = defaultdict(list)

    query_dict = SeqIO.to_dict(query_fasta)

    for blast_record in blast_records:
        top_eval=None
        aln_count = 1
        qid = blast_record.query.split()[0]
        newids = blast_record.query.split()[1].split(';')
        newids = [i.split('|')[1] for i in newids]
        qseq = str(query_dict[blast_record.query.split()[0]].seq)
        qlen = len(qseq)
        blast_record.alignments.sort(key=lambda align: max(hsp.score for hsp in align.hsps), reverse=True)

        rec_count = 1

        for alignment in blast_record.alignments:
            alignment.hsps.sort(key=lambda hsp :  hsp.score , reverse=True)
            if not qseq in mapdict:
                mapdict[qseq] = None
                subject_count[qseq] = 0

            for hsp in alignment.hsps:

                if mapdict[qseq] != hsp.sbjct:
                    mapdict[qseq] = hsp.sbjct
                    subject_count[qseq] += 1

                if (subject_count[qseq] <= subject_cutoff) and (hsp.expect <= eval_cutoff):
                    start = hsp.sbjct_start
                    end = hsp.sbjct_end
                    frame  = hsp.frame[1]
                    if frame < 0:
                        strand='-'
                    else:
                        strand='+'
                    hitseq = hsp.sbjct.replace('-','')
                    b2g = blast.blast2genome(fasta_id = qid, query_seq = hsp.query, match_seq=hsp.match, hit_seq = hitseq, hit_from=start, hit_to=end, hit_strand=strand, amino_offset=0)
                    contig = alignment.hit_def
                    source = "BLASTP"
                    type="polypeptide"
                    score = hsp.expect
                    phase= '.'
                    start = b2g.alignment_start
                    end = b2g.alignment_end
                    for newid in newids:
                        attributes="ID=match{}-{};Name={}".format(rec_count, newid, newid)
                        row = contig +'\t' + source +'\t' + type + '\t' + str(start) + '\t' + str(end) + '\t' + str(score) + '\t' + strand + '\t' + phase + '\t' + attributes
                        recdict[newid].append(row)
                    for rec in b2g.records:
                        contig = alignment.hit_def
                        source = "BLASTP"
                        type="polypeptide"
                        start = rec[1]
                        end = rec[2]
                        strand = rec[3]
                        score = hsp.expect
                        phase= '.'
                        for newid in newids:
                            attributes="Parent=match{}-{};Name={}".format(rec_count, newid, newid)
                            row = contig +'\t' + source +'\t' + type + '\t' + str(start) + '\t' + str(end) + '\t' + str(score) + '\t' + strand + '\t' + phase + '\t' + attributes
                            recdict[newid].append(row)
                    rec_count += 1
    return recdict

max_stars_repo_name: reid-wagner/proteomics-pipelines

max_issues_repo_name: reid-wagner/proteomics-pipelines

max_forks_repo_name: reid-wagner/proteomics-pipelines


text:
import cdms2 as cdms
import MV2 as MV
import cdutil
import matplotlib.pyplot as plt
import numpy as np
import genutil.statistics as stat
import skill_metrics as sm
from eofs.cdms         import Eof

# Compare with observations
def get_data(flag):
    fmask=cdms.open('mask_subregions.nc')
    f1=cdms.open('tas_ERA_1901_2010.nc')
    f2=cdms.open('tas_NOAA_1836_2015.nc')
    f3=cdms.open('tas_observation.nc')
    if flag == 'HadCRUT':
        mask = fmask('vHadCRUT_mask')
        ERA = f1('tas_ERA_HadCRUT') * mask
        NOA = f2('tas_NOAAv3_HadCRUT')[65:-5] * mask
        OBS = f3('tas_HadCRUT')[51:161] * mask
    if flag == 'BerkEAR':
        mask = fmask('vBerkEar_mask')
        ERA = f1('tas_ERA_BerkEAR') * mask
        NOA = f2('tas_NOAAv3_BerkEAR')[65:-5] * mask
        OBS = f3('tas_BerkEar')[51:161] * mask
    fmask.close()
    f1.close()
    f2.close()
    f3.close()
    return ERA, NOA, OBS

# Prepare data
ERAsp, NOAsp, OBSsp = get_data('HadCRUT') #'BerkEAR'; 'HadCRUT'
ERAgm = cdutil.averager(ERAsp,axis='yx')
NOAgm = cdutil.averager(NOAsp,axis='yx')
OBSgm = cdutil.averager(OBSsp + 273.15,axis='yx')
ERAgmCent = np.array(ERAgm - cdutil.averager(ERAgm, axis=0, weight='equal'))
NOAgmCent = np.array(NOAgm - cdutil.averager(NOAgm, axis=0, weight='equal'))
OBSgmCent = np.array(OBSgm - cdutil.averager(OBSgm, axis=0, weight='equal'))
ERAsp = ERAsp - np.array(cdutil.averager(ERAsp, axis='yx'))[:, None, None]
NOAsp = NOAsp - np.array(cdutil.averager(NOAsp, axis='yx'))[:, None, None]
OBSsp = OBSsp - np.array(cdutil.averager(OBSsp, axis='yx'))[:, None, None]
ERAsp = ERAsp - cdutil.averager(ERAsp, axis=0, weights='equal')
NOAsp = NOAsp - cdutil.averager(NOAsp, axis=0, weights='equal')
OBSsp = OBSsp - cdutil.averager(OBSsp, axis=0, weights='equal')

# Test EOFs
def get_constant_term(eofs):
    Predictor_T = (eofs[0]*0+1).ravel()
    for i in range(int(eofs.shape[0])):
        Predictor_T = np.ma.vstack([Predictor_T, eofs[i].ravel()])
    Predictor = Predictor_T.T
    return Predictor
def set_time_axis(length):
    Time = cdms.createAxis(np.arange(length)+1)
    Time.id='time'
    return Time
def get_EOFs(var1, num=3, scaling=2):
    lat = var1.getAxis(1)
    lon = var1.getAxis(2)
    var = MV.array(var1-np.mean(var1,axis=0))
    var.setAxis(1, lat)
    var.setAxis(2, lon)
    solver = Eof(var, weights='area')
    eofs   = solver.eofs(neofs=num, eofscaling=scaling)
    pc     = solver.pcs(npcs=num, pcscaling=scaling)
    vari   = solver.varianceFraction(num)
    eigv   = solver.eigenvalues(num)
    return eofs, pc, vari, eigv
ERAsp.setAxis(0, set_time_axis(ERAsp.shape[0]))
NOAsp.setAxis(0, set_time_axis(NOAsp.shape[0]))
OBSsp.setAxis(0, set_time_axis(OBSsp.shape[0]))
eofs1, pc1, vari1, eigv1 = get_EOFs(ERAsp, num=3, scaling=0)
eofs2, pc2, vari2, eigv2 = get_EOFs(NOAsp, num=3, scaling=0)
eofs3, pc3, vari3, eigv3 = get_EOFs(OBSsp, num=3, scaling=0)
cor11 = np.abs(stat.correlation(eofs1[0], eofs3[0], axis='yx'))
cor21 = np.abs(stat.correlation(eofs2[0], eofs3[0], axis='yx'))
cor12 = np.abs(stat.correlation(eofs1[1], eofs3[1], axis='yx'))
cor22 = np.abs(stat.correlation(eofs2[1], eofs3[1], axis='yx'))
cor13 = np.abs(stat.correlation(eofs1[2], eofs3[2], axis='yx'))
cor23 = np.abs(stat.correlation(eofs2[2], eofs3[2], axis='yx'))
print (cor11, cor21)
print (cor12, cor22)
print (cor13, cor23)
print ()
print ((cor21 - cor11)/cor11*100)
print ((cor22 - cor12)/cor12*100)
print ((cor23 - cor13)/cor13*100)
plt.bar(np.r_[0, 4, 8], np.r_[cor11, cor12, cor13], width=1)
plt.bar(np.r_[1, 5, 9], np.r_[cor21, cor22, cor23], width=1)
plt.ylim(0, 1)
plt.show()
plt.clf()

# Test taylor diagrom here:
TSstd1, TSstd2 = [], []
TSrms1, TSrms2 = [], []
TScor1, TScor2 = [], []
flag = 1
for idx in range(OBSsp.shape[0]):
    aaa = ERAsp[idx]
    bbb = NOAsp[idx]
    ccc = OBSsp[idx]
    std0 = stat.std(ccc, axis='yx')
    std1 = stat.std(aaa, axis='yx') / std0
    std2 = stat.std(bbb, axis='yx') / std0
    cor1 = stat.correlation(aaa, ccc, axis='yx')
    cor2 = stat.correlation(bbb, ccc, axis='yx')
    rms1 = stat.rms(aaa, ccc, axis='yx', centered=0)
    rms2 = stat.rms(bbb, ccc, axis='yx', centered=0)
    TSstd1.append(std1)
    TSstd2.append(std2)
    TSrms1.append(rms1)
    TSrms2.append(rms2)
    TScor1.append(cor1)
    TScor2.append(cor2)
    if flag == 1:
        sm.taylor_diagram(np.array([1.0, np.mean(TSstd1)]),
                          np.array([0.0, np.mean(TSrms1)]),
                          np.array([1.0, np.mean(TScor1)]),
                          markerColor = 'firebrick',
                          colOBS = 'black', markerobs = 'x')
        flag += 1
    else:
        sm.taylor_diagram(np.array([1.0, np.mean(TSstd1)]),
                          np.array([0.0, np.mean(TSrms1)]),
                          np.array([1.0, np.mean(TScor1)]),
                          markerColor = 'firebrick',
                          colOBS = 'black', markerobs = 'x',
                          overlay = 'on')
    sm.taylor_diagram(np.array([1.0, np.mean(TSstd2)]),
                      np.array([0.0, np.mean(TSrms2)]),
                      np.array([1.0, np.mean(TScor2)]),
                      markerColor = 'royalblue',
                      colOBS = 'black', markerobs = 'x',
                      overlay = 'on')
plt.show()
plt.clf()


text:
import numpy as np
from qtpy.QtWidgets import QFileDialog, QApplication
from qtpy import QtCore, QtGui
import os

from __code.circular_profile_of_a_ring.configuration_handler import ConfigurationHandler
from __code.file_handler import make_ascii_file

class ExportProfiles:

    def __init__(self, parent=None):
        self.parent = parent

    def run(self):
        default_file_name = os.path.abspath(os.path.basename(self.parent.working_dir)) + "_profiles.csv"
        working_dir = os.path.dirname(self.parent.working_dir)
        directory = os.path.join(working_dir, default_file_name)
        export_file_name = QFileDialog.getSaveFileName(self.parent,
                                                       caption="Select or define filename ...",
                                                       directory=directory,
                                                       filter="ascii(*.csv)",
                                                       initialFilter='ascii')

        if export_file_name[0]:

            export_file_name = export_file_name[0]
            self.parent.ui.statusbar.showMessage("Saving {} ... IN PROGRESS".format(os.path.basename(export_file_name)))
            self.parent.ui.statusbar.setStyleSheet("color: blue")

            QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
            QtGui.QGuiApplication.processEvents()

            dict_profile = self.parent.dict_profile

            _config = ConfigurationHandler(parent=self.parent)
            dict_config = _config.get_config_dict()

            metadata = ["# working dir: {}".format(os.path.abspath(self.parent.working_dir))]
            metadata.append("# ring central pixel (x, y):({}, {})".format(dict_config['ring']['central_pixel']['x'],
                                                                        dict_config['ring']['central_pixel']['y']))
            metadata.append("# ring inner radius (pixel): {}".format(dict_config['ring']['radius']))
            metadata.append("# ring thickness (pixel): {}".format(dict_config['ring']['thickness']))

            metadata.append("# Label of columns:")
            nbr_row = len(dict_profile.keys())
            list_files = self.parent.list_short_files
            for _row in np.arange(nbr_row):
                _file = list_files[_row]
                metadata.append("# column {}: {}".format(_row+1, _file))

            metadata.append("#")
            metadata.append("Angle from top vertical (degrees), mean counts (see label of columns above)")

            data = []
            list_angles = self.parent.list_angles
            for _index_angle, _angle in enumerate(list_angles):
                _entry = [str(_angle)]
                for _row_index in dict_profile.keys():
                    _data_of_that_row = dict_profile[_row_index]['y_profile']
                    _entry.append(str(_data_of_that_row[_index_angle]))
                _str_entry = ", ".join(_entry)
                data.append(_str_entry)

            make_ascii_file(metadata=metadata,
                            data=data,
                            output_file_name=export_file_name,
                            dim='1d')

            self.parent.ui.statusbar.showMessage("{} ... Saved!".format(os.path.basename(export_file_name)), 10000)
            self.parent.ui.statusbar.setStyleSheet("color: green")

            QApplication.restoreOverrideCursor()
            QtGui.QGuiApplication.processEvents()


text:
import csv
import random
from functools import partial
from typing import Callable, Optional
from pdb import set_trace as st
import os
import random
import pandas as pd

import numpy as np
import tensorflow as tf
from foolbox.attacks import (
    FGSM,
    Attack,
    DeepFoolAttack,
    IterativeGradientSignAttack,
    SaliencyMapAttack,
)
# from foolbox.criteria import TargetClass
# from foolbox.models import TensorFlowModel
from tensorflow.python.training import saver
from tensorflow.python.training.session_manager import SessionManager
import tensorflow as tf
import numpy as np
import sklearn.metrics as metrics
import matplotlib.pyplot as plt
plt.switch_backend('Agg')

from model.config import LENET
import nninst_mode as mode
from dataset import mnist
from dataset.config import MNIST_TRAIN
from dataset.mnist_transforms import *
from trace.lenet_mnist_class_trace_v2 import (
    data_config,
)
from trace.common import (
    class_trace,
)
from tf_utils import new_session_config
from nninst_statistics import calc_trace_side_overlap
from nninst_trace import TraceKey
from nninst_utils.numpy import arg_approx
from nninst_utils.ray import ray_init
from nninst_utils.fs import ensure_dir

from .common import get_overlay_summary, clean_overlap_ratio, \
                translation_overlap_ratio, attack_overlap_ratio
from .cw_attack import cw_generate_adversarial_example
from .cw_attacks import CarliniL2

# Model config
model_label = "augmentation"
model_dir = f"result/lenet/model_{model_label}"
# Trace config
trace_dir =  f"{model_dir}/traces"
trace_name = "9translation"
key = TraceKey.POINT
# Result dir
result_name = key.split('.')[1]
result_dir = f"result/lenet/model_augmentation/visualize"
# result_dir = f"result/lenet/test"
images_per_class = 10
attack_name = "FGSM"

attacks = {
    "FGSM": [FGSM],
    "BIM": [IterativeGradientSignAttack],
    "JSMA": [SaliencyMapAttack],
    "DeepFool": [DeepFoolAttack],
    # "DeepFool_full": [DeepFoolAttack, dict(subsample=None)],
    # "CWL2": [CarliniL2],
}

from typing import Any, Callable, Dict, Iterable, List, Tuple, Union
from tensorflow.python.training.session_run_hook import SessionRunHook
from nninst_utils.fs import CsvIOAction, ImageIOAction, IOAction, abspath
from nninst_graph import AttrMap, Graph, GraphAttrKey
from nninst_utils.ray import ray_iter, ray_map, ray_map_reduce
from dataset.config import MNIST_PATH

class LeNet:
    """Class that defines a graph to recognize digits in the MNIST dataset."""

    def __init__(self, data_format: str = "channels_first"):
        """Creates a model for classifying a hand-written digit.

        Args:
          data_format: Either "channels_first" or "channels_last".
            "channels_first" is typically faster on GPUs while "channels_last" is
            typically faster on CPUs. See
            https://www.tensorflow.org/performance/performance_guide#data_formats
        """
        # if data_format == "channels_first":
        #     self._input_shape = [-1, 1, 28, 28]
        # else:
        #     assert data_format == "channels_last"
        #     self._input_shape = [-1, 28, 28, 1]

        self.conv1 = tf.layers.Conv2D(
            6, 5, data_format=data_format, activation=tf.nn.relu
        )
        self.conv2 = tf.layers.Conv2D(
            16, 5, data_format=data_format, activation=tf.nn.relu
        )
        self.fc1 = tf.layers.Dense(120, activation=tf.nn.relu)
        self.fc2 = tf.layers.Dense(84, activation=tf.nn.relu)
        self.fc3 = tf.layers.Dense(10)
        self.max_pool2d = tf.layers.MaxPooling2D(
            (2, 2), (2, 2), padding="same", data_format=data_format
        )
        self.dropout = tf.layers.Dropout(rate=0.5)

    def __call__(self, inputs, training=False):
        """Add operations to classify a batch of input images.

        Args:
          inputs: A Tensor representing a batch of input images.
          training: A boolean. Set to True to add operations required only when
            training the classifier.

        Returns:
          A logits Tensor with shape [<batch_size>, 10].
        """
        # y = tf.reshape(inputs, self._input_shape)
        # y = self.conv1(y)
        # for i in range(28):
        #     inputs = tf.Print(inputs, [inputs[0][0][i]], "Inputs", summarize=30)
        # inputs = tf.Print(inputs, [tf.shape(inputs)], summarize=5)
        # tf.summary.image('input', inputs)

        # inputs = tf.Print(inputs, [tf.shape(inputs)], "Inputs: ", summarize=20)
        y = self.conv1(inputs)
        # y = tf.Print(y, [tf.shape(y)], "Conv1: ", summarize=20)
        y = self.max_pool2d(y)
        # y = tf.Print(y, [tf.shape(y)], "Pool1: ", summarize=20)
        y = self.conv2(y)
        # y = tf.Print(y, [tf.shape(y)], "Conv2: ", summarize=20)
        y = self.max_pool2d(y)
        # y = tf.Print(y, [tf.shape(y)], "Pool2: ", summarize=20)
        y = tf.layers.flatten(y)
        # y = tf.Print(y, [tf.shape(y)], "Flatten: ", summarize=20)
        y = self.fc1(y)
        # y = tf.Print(y, [tf.shape(y)], "Fc1: ", summarize=20)
        y = self.dropout(y, training=training)
        y = self.fc2(y)
        # y = tf.Print(y, [tf.shape(y)], "Fc2: ", summarize=20)
        y = self.dropout(y, training=training)
        return self.fc3(y)

    @classmethod
    def create_graph(cls, input_name: str = "IteratorGetNext:0") -> Graph:
        input = tf.placeholder(tf.float32, shape=(1, 1, 28, 28))
        new_graph = build_graph([input], [cls()(input)])
        new_graph.rename(new_graph.id(input.name), input_name)
        return new_graph

    @classmethod
    def graph(cls, path="result/lenet/graph/lenet.pkl") -> IOAction[Graph]:
        # path = "store/graph/lenet.pkl"
        return IOAction(path, init_fn=lambda: LeNet.create_graph())

def forward_propagate(
    create_model,
    input_fn,
    forward_fn: Callable[[tf.Tensor], tf.Tensor],
    model_dir: str,
    data_format: str = "channels_first",
    parallel: int = 1,
    prediction_hooks: List[SessionRunHook] = None,
    tag: str = "noop",
) -> Union[int, float]:

    def model_fn(features, labels, mode, params):
        image = features
        if isinstance(image, dict):
            image = features["image"]

        # Save inputs for visualization in tensorboard
        nonlocal prediction_hooks
        feature_trans = tf.transpose(image, perm=[0,2,3,1])
        image_summary = tf.summary.image(f"input_{tag}", feature_trans, max_outputs=100)
        eval_summary_hook = tf.train.SummarySaverHook(
                            summary_op=image_summary,
                            save_steps=2,
                            output_dir=result_dir)
        if prediction_hooks is None:
            prediction_hooks = [eval_summary_hook]
        else:
            prediction_hooks.append(eval_summary_hook)

        if mode == tf.estimator.ModeKeys.PREDICT:
            logits = create_model()(image, training=False)
            predictions = {"classes": forward_fn(logits)}
            return tf.estimator.EstimatorSpec(
                mode=tf.estimator.ModeKeys.PREDICT,
                predictions=predictions,
                prediction_hooks=prediction_hooks,
                export_outputs={
                    "classify": tf.estimator.export.PredictOutput(predictions)
                },
            )

    model_dir = abspath(model_dir)
    model_function = model_fn
    if data_format is None:
        data_format = (
            "channels_first" if tf.test.is_built_with_cuda() else "channels_last"
        )
    estimator_config = tf.estimator.RunConfig(
        session_config=new_session_config(parallel=parallel)
    )
    if not os.path.exists(model_dir):
        raise RuntimeError(f"model directory {model_dir} is not existed")
    # st()
    classifier = tf.estimator.Estimator(
        model_fn=model_function,
        model_dir=model_dir,
        params={"data_format": data_format},
        config=estimator_config,
    )

    result = list(classifier.predict(input_fn=input_fn))
    return result[0]["classes"]

def predict(
    create_model,
    input_fn,
    model_dir: str,
    data_format: str = "channels_first",
    parallel: int = 1,
    prediction_hooks: List[SessionRunHook] = None,
    tag: str = "noop",
) -> int:
    return int(
        forward_propagate(
            create_model=create_model,
            input_fn=input_fn,
            forward_fn=lambda logits: tf.argmax(logits, axis=1),
            model_dir=model_dir,
            data_format=data_format,
            parallel=parallel,
            prediction_hooks=prediction_hooks,
            tag = tag,
        )
    )

# Compute the mean overlap ratio of attacked image
def test_data_forward(
    class_trace_fn: Callable[[int], IOAction[AttrMap]],
    select_fn: Callable[[np.ndarray], np.ndarray],
    overlap_fn: Callable[[AttrMap, AttrMap, str], float],
    path: str,
    per_channel: bool = False,
    per_node: bool = False,
    images_per_class: int = 1,
    num_gpus: float = 0.2,
    model_dir = "result/lenet/model_augmentation",
    transforms = None,
    **kwargs,
):
    def forward_one_image(class_id: int, image_id: int) -> Dict[str, Any]:
        nonlocal model_dir
        mode.check(False)
        data_dir = abspath(MNIST_PATH)
        model_dir = abspath(model_dir)
        ckpt_dir = f"{model_dir}/ckpts"
        create_model = lambda: LeNet(data_format="channels_first")
        graph = LeNet.graph().load()
        # model_fn = partial(
        #     model_fn_with_fetch_hook,
        #     create_model=create_model, graph=graph
        # )

        for transform, name in transforms:
            predicted_label = predict(
                create_model=create_model,
                input_fn=lambda: mnist.test(
                                            data_dir,
                                            transforms = transform
                                            )
                .filter(
                    lambda image, label: tf.equal(
                        tf.convert_to_tensor(class_id, dtype=tf.int32), label
                    )
                )
                .skip(image_id)
                .take(1)
                .batch(1),
                model_dir=ckpt_dir,
                tag = name,
            )

    results = ray_iter(
        forward_one_image,
        (
            (class_id, image_id)
            for image_id in range(0, images_per_class)
            for class_id in range(0, 1)
        ),
        chunksize=1,
        out_of_order=True,
        num_gpus=num_gpus,
    )

    results = [result for result in results]

def visualize_features(
    transforms,
    result_dir = "result/lenet/model_augmentation/visualize"
):

    name = attack_name

    lenet_mnist_class_trace = class_trace(
                                trace_name,
                                model_config=LENET,
                                data_config=data_config,
                            )

    threshold = 0.5

    # DeepFool will shutdown when num_gpu<0.2
    num_gpus = 0.2

    overlap_fn = calc_trace_side_overlap
    per_channel = False

    path = os.path.join(result_dir, f"{name}_overlap.csv")

    print(f"Computing {name}")

    lenet_overlap_ratio = test_data_forward(
        class_trace_fn=lambda class_id: lenet_mnist_class_trace(
            class_id,
            threshold,
            label=model_label,
            trace_dir = trace_dir,
        ),
        select_fn=lambda input: arg_approx(input, threshold),
        overlap_fn=overlap_fn,
        path=path,
        per_channel=per_channel,
        preprocessing=(0.1307, 0.3081),
        image_size=28,
        class_num=10,
        norm_fn=mnist.normalize,
        data_format="channels_first",
        **(attacks[attack_name][1] if len(attacks[attack_name]) == 2 else {}),
        images_per_class=images_per_class,
        model_dir=model_dir,
        num_gpus = num_gpus,
        transforms = transforms,
    )

def visualize_features_exp():
    exp_to_roc = {}

    os.makedirs(result_dir, exist_ok=True)

    transforms = [
        [None, "noop"],
        # [Translate(dx=-5,dy=-5), "leftup"],
    ]

    trace_frame = visualize_features(
        transforms,
        result_dir=result_dir
    )

if __name__ == "__main__":

    mode.debug()
    # mode.local()

    # ray_init("gpu")
    ray_init(
        log_to_driver=False
    )

    tf.set_random_seed(3)
    np.random.seed(3)
    random.seed(3)

    visualize_features_exp()


text:
from sympy.logic.boolalg import *

import equibel as eb

def test_global_completion_cardinality():
    G = eb.star_graph(3)
    G.add_formula(1, 'p')
    G.add_formula(2, 'p')
    G.add_formula(3, '~p')
    R_semantic = eb.global_completion(G, method=eb.SEMANTIC, opt_type=eb.CARDINALITY, simplify=True)
    assert(R_semantic.formula_conj(0) == eb.parse_formula('p'))

    R_syntactic = eb.global_completion(G, method=eb.SYNTACTIC, opt_type=eb.CARDINALITY, simplify=True)
    assert(R_syntactic == R_semantic)

def test_global_completion_two_nodes():
    G = eb.path_graph(2)
    G.add_formula(0, 'p')
    atoms = G.atoms()
    R_semantic = eb.global_completion(G, method=eb.SEMANTIC, simplify=True)
    print(R_semantic.formulas())
    assert(R_semantic.formulas() == {0: atoms, 1: atoms})

    R_syntactic = eb.global_completion(G, method=eb.SYNTACTIC, simplify=True)
    assert(R_semantic == R_syntactic)

def test_global_completion_chain_1():
    p = eb.parse_formula('p')
    G = eb.path_graph(5)
    G.add_formula(0, p)
    G.add_formula(4, ~p)
    R_semantic = eb.global_completion(G, method=eb.SEMANTIC, simplify=True)
    print(R_semantic.formulas())
    assert(R_semantic.formulas() == {0: set([p]), 1: set([]), 2: set([]), 3: set([]), 4: set([~p])})

    R_syntactic = eb.global_completion(G, method=eb.SYNTACTIC, simplify=True)
    assert(R_semantic == R_syntactic)

def test_global_completion_chain_2():
    p = eb.parse_formula('p')
    q = eb.parse_formula('q')
    G = eb.path_graph(4)
    G.add_formula(0, p & q)
    G.add_formula(3, ~p)
    R_semantic = eb.global_completion(G, simplify=True)
    assert(R_semantic.formulas() == {0: set([p & q]), 1: set([q]), 2: set([q]), 3: set([~p & q])})

    R_syntactic = eb.global_completion(G, method=eb.SYNTACTIC, simplify=True)
    assert(R_semantic == R_syntactic)

def test_global_completion_big_chain():
    p = eb.parse_formula('p')
    q = eb.parse_formula('q')
    G = eb.path_graph(10)
    G.add_formula(0, 'p & q')
    G.add_formula(9, '~p & ~q')
    R_semantic = eb.global_completion(G, simplify=True)
    for node in range(1,9):
        assert(R_semantic.formulas(node) == set([]))
    assert(R_semantic.formulas(0) == set([p & q]))
    assert(R_semantic.formulas(9) == set([~p & ~q]))

    R_syntactic = eb.global_completion(G, method=eb.SYNTACTIC, simplify=True)
    assert(R_semantic == R_syntactic)

if __name__ == '__main__':
    test_global_completion_cardinality()
    test_global_completion_two_nodes()
    test_global_completion_chain_1()
    test_global_completion_chain_2()
    test_global_completion_big_chain()

max_stars_repo_name: asteroidhouse/equibel

max_issues_repo_name: asteroidhouse/equibel

max_forks_repo_name: asteroidhouse/equibel


text:
#!/usr/bin/env python3
"""Generate a table of cross-identifications for SWIRE objects.

Matthew Alger <matthew.alger@anu.edu.au>
Research School of Astronomy and Astrophysics
The Australian National University
2017
"""

import collections
import itertools
import re

import astropy.io
import astropy.table
import numpy

import pipeline

def print_table(field='cdfs'):
    swire_names, swire_coords, _ = pipeline.generate_swire_features(overwrite=False, field=field)
    swire_labels = pipeline.generate_swire_labels(swire_names, swire_coords, overwrite=False, field=field)
    (_, atlas_test_sets), (_, swire_test_sets) = pipeline.generate_data_sets(swire_coords, swire_labels, overwrite=False, field=field)
    cids = list(pipeline.cross_identify_all(swire_names, swire_coords, swire_labels, swire_test_sets, swire_labels[:, 0], field=field))

    atlas_to_swire = collections.defaultdict(dict)  # ATLAS -> predictor -> SWIRE

    swire_name_to_coord = {}
    for name, coord in zip(swire_names, swire_coords):
        swire_name_to_coord[name] = coord

    atlas_to_swire_expert = {}
    key_to_atlas = {}
    atlas_to_ras = {}
    atlas_to_decs = {}
    id_to_atlas = {}
    atlas_to_id = {}
    atlas_to_zooniverse_id = {}
    if field == 'cdfs':
        table = astropy.io.ascii.read(pipeline.TABLE_PATH)
        for row in table:
            name = row['Component Name (Franzen)']
            if not name:
                continue
            id_to_atlas[row['Component ID (Franzen)']] = name
            atlas_to_id[name] = row['Component ID (Franzen)']
            atlas_to_zooniverse_id[name] = row['Component Zooniverse ID (RGZ)']
            key_to_atlas[row['Key']] = name
            swire = row['Source SWIRE (Norris)']
            atlas_to_swire_expert[name] = swire
            atlas_to_ras[name] = row['Component RA (Franzen)']
            atlas_to_decs[name] = row['Component DEC (Franzen)']
    else:
        swire_scoords = astropy.coordinates.SkyCoord(ra=swire_coords[:, 0],
                                                     dec=swire_coords[:, 1],
                                                     unit='deg')
        with astropy.io.fits.open(pipeline.MIDDELBERG_TABLE4_PATH) as elais_components_fits:
            elais_components = elais_components_fits[1].data
            component_to_name = {}
            for i, component in enumerate(elais_components):
                name = component['ATELAIS']
                id_to_atlas[component['CID']] = name
                atlas_to_id[name] = component['CID']
                atlas_to_zooniverse_id[name] = ''
                key_to_atlas[i] = name
                coord = astropy.coordinates.SkyCoord(
                    ra='{} {} {}'.format(component['RAh'], component['RAm'], component['RAs']),
                    dec='-{} {} {}'.format(component['DEd'], component['DEm'], component['DEs']),
                    unit=('hourangle', 'deg'))
                coord = (coord.ra.deg, coord.dec.deg)
                atlas_to_ras[name] = coord[0]
                atlas_to_decs[name] = coord[1]
        # Load SWIRE cross-identification from Table 5.
        with open(pipeline.MIDDELBERG_TABLE5_PATH) as elais_file:
            lines = [line.split('|') for line in elais_file]
            for line in lines:
                if 'ATELAISJ' not in line[0]:
                    continue

                line_cids = line[1]
                if 'C0' not in line_cids and 'C1' not in line_cids:
                    continue

                line_cids = [cid.strip() for cid in line_cids.split(',')]
                swire_coord_re = re.search(r'SWIRE4J(\d\d)(\d\d)(\d\d\.\d\d)(-\d\d)(\d\d)(\d\d\.\d)', line[2])
                if not swire_coord_re:
                    continue
                swire_coord_list = swire_coord_re.groups()
                coord = astropy.coordinates.SkyCoord(
                    ra='{} {} {}'.format(*swire_coord_list[:3]),
                    dec='{} {} {}'.format(*swire_coord_list[3:]),
                    unit=('hourangle', 'deg'))
                # Nearest SWIRE...
                seps = coord.separation(swire_scoords)
                nearest = numpy.argmin(seps)
                dist = seps[nearest]
                if dist.deg > 5 / 60 / 60:
                    continue
                name = swire_names[nearest]
                for cid in line_cids:
                    atlas_to_swire_expert[id_to_atlas[cid]] = name

    atlas_to_rgz = {}
    atlas_to_radio_consensus = {}
    atlas_to_ir_consensus = {}
    if field == 'cdfs':
        for row in astropy.io.ascii.read(pipeline.RGZ_PATH):
            name = id_to_atlas[row['atlas_id']]
            atlas_to_radio_consensus[name] = row['consensus.radio_level']
            atlas_to_ir_consensus[name] = row['consensus.ir_level']
            atlas_to_rgz[name] = row['SWIRE.designation']

    titlemap = {
        'RGZ & Norris & compact': 'Compact',
        'RGZ & Norris & resolved': 'Resolved',
        'RGZ & Norris': 'All',
        'RGZ & compact': 'Compact',
        'RGZ & resolved': 'Resolved',
        'RGZ': 'All',
    }

    known_predictors = set()

    for cid in cids:
        if cid.labeller == 'norris' and 'Norris' not in cid.dataset_name:
            continue

        if cid.classifier in {'Groundtruth', 'Random'}:
            continue

        if field == 'cdfs':
            atlas_keys = atlas_test_sets[:, pipeline.SET_NAMES['RGZ & Norris'], cid.quadrant].nonzero()[0]
        else:
            atlas_keys = atlas_test_sets[:, 0, 0].nonzero()[0]

        atlas_to_swire_predictor = dict(zip(cid.radio_names, cid.ir_names))
        n_total = 0
        n_correct = 0
        n_skipped = 0
        if 'Norris' in cid.dataset_name and cid.labeller == 'rgz':
            labeller = 'RGZ N'
            continue
        elif cid.labeller == 'rgz':
            labeller = 'RGZ'
        else:
            labeller = 'Norris'
        predictor_name = '{}({} / {})'.format(
            {'LogisticRegression': 'LR', 'CNN': 'CNN', 'RandomForestClassifier': 'RF'}[cid.classifier],
            labeller, titlemap[cid.dataset_name])
        known_predictors.add(predictor_name)
        for i in atlas_keys:
            name = key_to_atlas[i]
            swire_predictor = atlas_to_swire_predictor.get(name, '')
            atlas_to_swire[name][predictor_name] = swire_predictor

    known_predictors = sorted(known_predictors)

    atlases = sorted(atlas_to_swire)
    ras = []
    decs = []
    expert_xids = []
    expert_xid_ras = []
    expert_xid_decs = []
    rgzs = []
    rgz_ras = []
    rgz_decs = []
    rcs = []
    ircs = []
    cids = []
    zids = []
    predictor_columns = collections.defaultdict(list)
    predictor_ras = collections.defaultdict(list)
    predictor_decs = collections.defaultdict(list)
    for atlas in atlases:
        for predictor in known_predictors:
            predictor_columns[predictor].append(atlas_to_swire[atlas].get(predictor, ''))
            predictor_ras[predictor].append(swire_name_to_coord.get(atlas_to_swire[atlas].get(predictor, ''), (None, None))[0])
            predictor_decs[predictor].append(swire_name_to_coord.get(atlas_to_swire[atlas].get(predictor, ''), (None, None))[1])
        ras.append(atlas_to_ras[atlas])
        decs.append(atlas_to_decs[atlas])
        rgzs.append(atlas_to_rgz.get(atlas, ''))
        cids.append(atlas_to_id[atlas])
        zids.append(atlas_to_zooniverse_id[atlas])
        rgz_ras.append(swire_name_to_coord.get(atlas_to_rgz.get(atlas, ''), (None, None))[0])
        rgz_decs.append(swire_name_to_coord.get(atlas_to_rgz.get(atlas, ''), (None, None))[1])
        rcs.append(atlas_to_radio_consensus.get(atlas, 0.0))
        ircs.append(atlas_to_ir_consensus.get(atlas, 0.0))
        expert_xids.append(atlas_to_swire_expert.get(atlas, ''))
        expert_xid_ras.append(swire_name_to_coord.get(atlas_to_swire_expert.get(atlas, ''), (None, None))[0])
        expert_xid_decs.append(swire_name_to_coord.get(atlas_to_swire_expert.get(atlas, ''), (None, None))[1])

    expert = 'Norris' if field == 'cdfs' else 'Middelberg'
    table = astropy.table.Table(
        data=[atlases, ras, decs,
              cids, zids,
              expert_xids, expert_xid_ras, expert_xid_decs,
              rgzs, rgz_ras, rgz_decs,
              rcs, ircs] + [k for p in known_predictors for k in (predictor_columns[p], predictor_ras[p], predictor_decs[p])],
        names=['ATLAS', 'RA', 'Dec',
               'CID', 'Zooniverse ID',
               expert, expert + ' RA', expert + ' Dec',
               'RGZ', 'RGZ RA', 'RGZ Dec',
               'RGZ radio consensus', 'RGZ IR consensus'] + [k for p in known_predictors for k in (p, p + ' RA', p + ' Dec')])
    table['RGZ radio consensus'].format = '{:.4f}'
    table['RGZ IR consensus'].format = '{:.4f}'
    table.write('/Users/alger/data/Crowdastro/predicted_cross_ids_table_21_03_18_{}.csv'.format(field), format='csv')
    table.write('/Users/alger/data/Crowdastro/predicted_cross_ids_table_21_03_18_{}.tex'.format(field), format='latex')

if __name__ == '__main__':
    print_table(field='cdfs')
    print_table(field='elais')

max_stars_repo_name: chengsoonong/crowdastro-projects

max_issues_repo_name: chengsoonong/crowdastro-projects

max_forks_repo_name: chengsoonong/crowdastro-projects

reason: import numpy,import astropy


text:
import os.path
import yaml
import random
import datetime
from sqlalchemy import text, create_engine, MetaData, select
from sqlalchemy import Table, Column, String, Float, Integer
from sqlalchemy.orm import registry, Session
from sqlalchemy.orm import declarative_base
import pandas as pd
import plotly.express as px
import numpy as np
import plotly.graph_objects as go

yamlfilepath = os.path.expanduser('~\\yamlfiles\\information.yaml')
with open(yamlfilepath) as f:
    login = yaml.load(f, Loader=yaml.FullLoader)

address = f'postgresql://{login["user"]}:{login["pw"]}@localhost:{login["port"]}/{login["db"]}'
engine = create_engine(address)
metadata_obj = MetaData()
Base = declarative_base()

age = Table(
    "age",
    metadata_obj,
    Column('age', String(30), primary_key=True),
    Column('rate', Float)
)

resion = Table(
    "resion",
    metadata_obj,
    Column('resion', String(30), primary_key=True),
    Column('rate', Float)
)

sex = Table(
    "sex",
    metadata_obj,
    Column('sex', String(30), primary_key=True),
    Column('rate', Float)
)

lastname = Table(
    "lastname",
    metadata_obj,
    Column('lastname', String(30), primary_key=True),
    Column('rate', Float)
)

firstname = Table(
    "firstname",
    metadata_obj,
    Column('firstname', String(30), primary_key=True),
    Column('rate', Float)
)
zero = Table(
    "zero",
    metadata_obj,
    Column('zero', String(30), primary_key=True),
    Column('rate', Float)
)
class Age(Base):
    __tablename__ = 'age'
    age = Column(String(30), primary_key=True)
    rate = Column(Float)

    def __repr__(self):
        return f"{self.age!r},{self.rate!r}"

class Resion(Base):
    __tablename__ = 'resion'
    resion = Column(String(30), primary_key=True)
    rate = Column(Float)

    def __repr__(self):
        return f"{self.resion!r},{self.rate!r}"

class Sex(Base):
    __tablename__ = 'sex'
    sex = Column(String(30), primary_key=True)
    rate = Column(Float)

    def __repr__(self):
        return f"{self.sex!r},{self.rate!r}"

class LastName(Base):
    __tablename__ = 'lastname'
    lastname = Column(String(30), primary_key=True)
    rate = Column(Float)

    def __repr__(self):
        return f"{self.lastname!r},{self.rate!r}"

class FirstName(Base):
    __tablename__ = 'firstname'
    firstname = Column(String(30), primary_key=True)
    rate = Column(Float)

    def __repr__(self):
        return f"{self.firstname!r},{self.rate!r}"
class Zero(Base):
    __tablename__ = 'zero'
    zero = Column(String(30), primary_key=True)
    rate = Column(Float)

customer = Table(
    "customer",
    metadata_obj,
    Column('cid', Integer, primary_key=True),
    Column('lastname', String(30)),
    Column('firstname', String(30)),
    Column('sex', String(30)),
    Column('dateofbirth', String(30)),
    Column('subdate', String(30))
)
class Customer(Base):
    __tablename__ = 'customer'

    cid = Column(Integer, primary_key=True)
    lastname = Column(String(30))
    firstname = Column(String(30))
    sex = Column(String(30))
    dateofbirth = Column(String(30))
    subdate = Column(String(30))

    def __repr__(self):
        return f"{self.lastname!r},{self.firstname!r},{self.sex!r},{self.dateofbirth!r},{self.subdate!r}"

metadata_obj.create_all(engine)

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
from dash.dependencies import Input, Output, State
from dash import callback_context

app = dash.Dash(__name__)

app.layout = html.Div(children=[html.H1(children='Hello Dash'),

    html.Div(children='''
        Choice it to see the graph of rate.
    '''),
    dcc.Dropdown(
        id='demo-dropdown',
        options=[
            {'label': i, 'value': i}
            for i in ['성별 비율', '성 비율', '이름 비율', '나이 비율']
        ],
        value='성별 비율'
    ),

    dcc.Graph(id='dd-output-container'),

    html.Button("비교하기", id="btn-bongraph", n_clicks=0),
    dcc.Graph(id="bongraph")

])

@app.callback(
    Output('dd-output-container', 'figure'),
    Input('demo-dropdown', 'value')
)
def update_output(value):
    if value =='성별 비율':
        df = pd.read_sql(select(Customer), engine)
        dfsex = df.groupby(['sex'], as_index=False).count()
        dffig = dfsex.assign(rate = dfsex.cid.transform(func = lambda x : x / len(df.index)))
        fig = px.pie(dffig, values='rate', names='sex', color='sex', title='고객 테이블 성별')

    elif value =='성 비율':
        df = pd.read_sql(select(Customer), engine)
        dflast = df.groupby(['lastname'], as_index=False).count()
        dffig = dflast.assign(rate = dflast.cid.transform(func = lambda x : x / len(df.index)))
        dffig = dffig.sort_values(by='rate' ,ascending=False).head(30)
        fig = px.bar(dffig, x="lastname", y="rate", title='고객 테이블 성(姓) 상위 30개 ', color='rate', labels={'rate': '비율(%)', 'lastname':'성(姓)'})

    elif value =='이름 비율':
        df = pd.read_sql(select(Customer), engine)
        dffir = df.groupby(['firstname'], as_index=False).count()
        dffig = dffir.assign(rate = dffir.cid.transform(func = lambda x : x / len(df.index)))
        dffig = dffig.sort_values(by='rate' ,ascending=False).head(15)
        labelss = dffig['firstname']
        valuess = dffig['rate']
        fig = go.Figure(data=[go.Pie(labels=labelss, values=valuess, hole=.3)],\
                     layout=go.Layout(title=go.layout.Title(text="이름")))

    elif value =='나이 비율':
        df = pd.read_sql(select(Customer), engine)
        df = df.assign(year = df['dateofbirth'].str[:4])
        dfdate = df.groupby(['year'], as_index=False).count()
        dffig = dfdate.assign(rate = dfdate.cid.transform(func = lambda x : x / len(df.index)))
        fig = px.scatter(dffig, x="year", y="rate", title='고객 테이블 생년월일', color='rate', labels={'rate': '비율(%)', 'age': '나이(세)'})

    return fig

@app.callback(
    Output('bongraph', 'figure'),
    Input('btn-bongraph', 'n_clicks'),
    Input('demo-dropdown', 'value')
)
def update_output(n_clicks, value):
    changed_id = [p['prop_id'] for p in callback_context.triggered][0]
    if value =='성별 비율' and ('btn-bongraph' in changed_id):
        dfsex = pd.read_sql(select(Sex), engine)
        fig = px.pie(dfsex, values='rate', names='sex', color='sex', color_discrete_map={'남':'cyan', '여':'pink'}, title='본 성별 테이블')

    elif value =='성 비율' and ('btn-bongraph' in changed_id):
        dflastname = pd.read_sql(select(LastName), engine).head(30)
        fig = px.bar(dflastname, x='lastname', y='rate', title='본 성(姓) 테이블 상위 30개', color='rate', labels={'rate': '비율(%)', 'lastname':'성(姓)'})
        fig.update_yaxes(title_font=dict(size=20))

    elif value =='이름 비율' and ('btn-bongraph' in changed_id):
        dffirstname = pd.read_sql(select(FirstName), engine).head(15)
        labelss = dffirstname['firstname']
        valuess = dffirstname['rate']
        fig = go.Figure(data=[go.Pie(labels=labelss, values=valuess, hole=.3)],\
                     layout=go.Layout(title=go.layout.Title(text="이름")))
        fig.update_traces(title='본 이름 테이블 상위 15개')

    elif value =='나이 비율' and ('btn-bongraph' in changed_id):
        dfage = pd.read_sql(select(Age), engine)
        fig = px.scatter(dfage, x="age", y="rate", title='본 나이 테이블', color='rate', labels={'rate': '비율(%)', 'age': '나이(세)'})

    else:
        dfsex = pd.read_sql(select(Zero), engine)
        fig = px.bar(dfsex, x='rate', y='zero')

    return fig

if __name__ == '__main__':
    app.run_server(debug=True, use_reloader=False)

max_stars_repo_name: yoonseulgi/customerpackage

max_issues_repo_name: yoonseulgi/customerpackage

max_forks_repo_name: yoonseulgi/customerpackage