text:
import numpy as np
from scipy.stats import lognorm
from scipy.optimize import curve_fit
from abc import ABC, abstractmethod

from icecube_tools.detector.effective_area import (
    R2015AeffReader,
    R2015_AEFF_FILENAME,
)
from icecube_tools.utils.data import IceCubeData, find_files, data_directory

"""
Module for handling the energy resolution
of IceCube using publicly available information.
"""

GIVEN_ETRUE = 0
GIVEN_ERECO = 1

_supported_dataset_ids = ["20150820"]

class EnergyResolutionBase(ABC):
    """
    Abstract base class for energy resolution.
    Stores information on how the reconstructed
    energy in the detector relates to the true
    neutrino energy.
    """

    @property
    def values(self):
        """
        A 2D histogram of probabilities normalised
        over reconstructed energy.

        x-axis <=> true_energy
        y-axis <=> reco_energy
        """

        return self._values

    @values.setter
    def values(self, values):

        if len(np.shape(values)) > 2:

            raise ValueError(str(values) + " is not a 2D array.")

        else:

            self._values = values

    @property
    def true_energy_bins(self):

        return self._true_energy_bins

    @true_energy_bins.setter
    def true_energy_bins(self, value):

        self._true_energy_bins = value

    @property
    def reco_energy_bins(self):

        return self._reco_energy_bins

    @reco_energy_bins.setter
    def reco_energy_bins(self, value):

        self._reco_energy_bins = value

    @abstractmethod
    def sample(self):

        pass

class EnergyResolution(EnergyResolutionBase):
    """
    Muon neutrino energy resolution using public data.
    Makes use of the 2015 effective area release and its
    corresponding reader class.

    Based on implementation by C. Haack (@chrhck).
    """

    supported_datasets = _supported_dataset_ids

    def __init__(self, filename, conditional=GIVEN_ETRUE, **kwargs):
        """
        Muon neutrino energy resolution using public data.
        Makes use of the 2015 effective area release and its
        corresponding reader class.

        Based on implementation by C. Haack (@chrhck).

        :param filename: Name of file to be read in.
        :param kwargs: year and/or nu_type can be specified.

        See release for more info.
        Link: https://icecube.wisc.edu/science/data/HE_NuMu_diffuse.
        """

        super().__init__()

        self._conditional = conditional

        self._reader = R2015AeffReader(filename, **kwargs)

        self.true_energy_bins = self._reader.true_energy_bins

        self.reco_energy_bins = self._reader.reco_energy_bins

        self.values = self._integrate_out_cos_zenith()
        self.values = self._get_conditional()
        self.values = self._normalise()

        self._fit_lognormal()
        self._fit_polynomial()

    @classmethod
    def from_dataset(cls, dataset_id, fetch=True, **kwargs):
        """
        Load energy resolution from publicly
        available data.

        :param dataset_id: Date identifying the dataset
        e.g. "20181018"
        :param fetch: If true, download dataset if missing
        """

        if dataset_id not in _supported_dataset_ids:

            raise NotImplementedError("This dataset is not currently supported")

        if fetch:

            data_interface = IceCubeData()

            dataset = data_interface.find(dataset_id)

            data_interface.fetch(dataset)

            dataset_dir = data_interface.get_path_to(dataset[0])

        else:

            dataset_dir = data_directory

        if dataset_id == "20150820":

            files = find_files(dataset_dir, R2015_AEFF_FILENAME)

            eres_file_name = files[0]

        return cls(eres_file_name, **kwargs)

    def _integrate_out_cos_zenith(self):
        """
        We are only interested in the energy
        dependence.
        """

        dim_to_int = self._reader._label_order["cos_zenith"]

        return np.sum(self._reader.effective_area_values, axis=dim_to_int)

    def _get_conditional(self):
        """
        From the joint distribution of Etrue and Ereco
        we want the conditional of Ereco | Etrue OR Etrue | Ereco.
        """

        if self._conditional == GIVEN_ETRUE:

            true_energy_dist = self.values.T.sum(axis=0)

            # To avoid zero division
            true_energy_dist[true_energy_dist == 0] = 1e-10

            conditional = np.nan_to_num(self.values.T / true_energy_dist).T

        elif self._conditional == GIVEN_ERECO:

            reco_energy_dist = self.values.sum(axis=0)

            conditional = np.nan_to_num(self.values / reco_energy_dist)

        else:

            raise ValueError("conditional must be GIVEN_ETRUE or GIVEN_ERECO")

        return conditional

    def _normalise(self):
        """
        Normalise over the reconstruted energy so
        at each Etrue bin the is a probability
        distribution over Ereco.
        """

        if self._conditional == GIVEN_ETRUE:

            normalised = np.zeros(
                (len(self.true_energy_bins[:-1]), len(self.reco_energy_bins[:-1]))
            )

            for i, Etrue in enumerate(self.true_energy_bins[:-1]):

                norm = 0

                for j, Ereco in enumerate(self.reco_energy_bins[:-1]):

                    delta_Ereco = self.reco_energy_bins[j + 1] - Ereco

                    norm += self.values[i][j] * delta_Ereco

                # Avoid zero division
                if norm == 0:
                    norm = 1e-10

                normalised[i] = self.values[i] / norm

        elif self._conditional == GIVEN_ERECO:

            normalised = np.zeros(
                (len(self.true_energy_bins[:-1]), len(self.reco_energy_bins[:-1]))
            ).T

            for i, Ereco in enumerate(self.reco_energy_bins[:-1]):

                norm = 0

                for j, Etrue in enumerate(self.true_energy_bins[:-1]):

                    delta_Etrue = self.true_energy_bins[j + 1] - Etrue

                    norm += self.values.T[i][j] * delta_Etrue

                normalised[i] = self.values.T[i] / norm

            normalised = normalised.T

        return normalised

    def _fit_lognormal(self):
        """
        Fit a lognormal distribution for each Etrue
        and store its parameters.
        """

        def _lognorm_wrapper(E, mu, sigma):

            return lognorm.pdf(E, sigma, loc=0, scale=mu)

        self._mu = []
        self._sigma = []

        if self._conditional == GIVEN_ETRUE:

            self.reco_energy_bin_cen = (
                self.reco_energy_bins[:-1] + self.reco_energy_bins[1:]
            ) / 2

            for i, Etrue in enumerate(self.true_energy_bins[:-1]):

                try:

                    fit_vals, _ = curve_fit(
                        _lognorm_wrapper,
                        self.reco_energy_bin_cen,
                        np.nan_to_num(self.values[i]),
                        p0=(Etrue, 0.5),
                    )

                    self._mu.append(fit_vals[0])
                    self._sigma.append(fit_vals[1])

                except:

                    self._mu.append(np.nan)
                    self._sigma.append(np.nan)

        elif self._conditional == GIVEN_ERECO:

            self.true_energy_bin_cen = (
                self.true_energy_bins[:-1] + self.true_energy_bins[1:]
            ) / 2

            for i, Ereco in enumerate(self.reco_energy_bins[:-1]):

                try:

                    fit_vals, _ = curve_fit(
                        _lognorm_wrapper,
                        self.true_energy_bin_cen,
                        np.nan_to_num(self.values.T[i]),
                        p0=(Ereco, 0.5),
                    )

                    self._mu.append(fit_vals[0])
                    self._sigma.append(fit_vals[1])

                except:

                    self._mu.append(np.nan)
                    self._sigma.append(np.nan)

    def _fit_polynomial(self):
        """
        Fit a polynomial to approximate the lognormal
        params at extreme energies where there are
        little statistics.
        """

        # polynomial degree
        degree = 5

        mu_sel = np.where(np.isfinite(self._mu))
        mu = np.array(self._mu)[mu_sel]

        sigma_sel = np.where(np.isfinite(self._sigma))
        sigma = np.array(self._sigma)[sigma_sel]

        if self._conditional == GIVEN_ETRUE:

            # hard coded values for excluding low statistics
            imin = 5
            imax = 210

            true_energy_bin_cen = (
                self.true_energy_bins[:-1] + self.true_energy_bins[1:]
            ) / 2

            Etrue_cen_mu = true_energy_bin_cen[mu_sel]

            Etrue_cen_sigma = true_energy_bin_cen[sigma_sel]

            mu_pars = np.polyfit(
                np.log10(Etrue_cen_mu[imin:imax]), np.log10(mu[imin:imax]), degree
            )

            sigma_pars = np.polyfit(
                np.log10(Etrue_cen_sigma[imin:imax]), np.log10(sigma[imin:imax]), degree
            )

        elif self._conditional == GIVEN_ERECO:

            # hard coded values for exluding low statistics
            imin = 5
            imax = 45

            reco_energy_bin_cen = (
                self.reco_energy_bins[:-1] + self.reco_energy_bins[1:]
            ) / 2

            Ereco_cen_mu = reco_energy_bin_cen[mu_sel]

            Ereco_cen_sigma = reco_energy_bin_cen[sigma_sel]

            mu_pars = np.polyfit(
                np.log10(Ereco_cen_mu[imin:imax]), np.log10(mu[imin:imax]), degree
            )

            sigma_pars = np.polyfit(
                np.log10(Ereco_cen_sigma[imin:imax]), np.log10(sigma[imin:imax]), degree
            )

        self._mu_poly = np.poly1d(mu_pars)

        self._sigma_poly = np.poly1d(sigma_pars)

    def _get_lognormal_params(self, E):
        """
        Returns params for lognormal representing
        P(Ereco | Etrue) OR P(Etrue | Ereco).

        :param E: The true/reco energy if GIVEN_ETRUE/GIVEN_ERECO [GeV]
        """

        mu = np.power(10, self._mu_poly(np.log10(E)))

        sigma = np.power(10, self._sigma_poly(np.log10(E)))

        return mu, sigma

    def sample(self, E):
        """
        Sample a reco/true energy given a true/reco energy.
        """

        mu, sigma = self._get_lognormal_params(E)

        return lognorm.rvs(sigma, loc=0, scale=mu)


text:
import pickle
import numpy as np
with open('X', 'rb') as fp:
    X_cnn = pickle.load(fp)
with open('Y', 'rb') as fp:
    Y_cnn = pickle.load(fp)
valid_hindi = (list(set("ऀँंःऄअआइईउऊऋऌऍऎएऐऑऒओऔकखगघङचछजझञटठडढणतथदधनऩपफबभमयरऱलळऴवशषसहऺऻ़ऽािीुूृॄॅॆेैॉॊोौ्ॎॏॐ॒॑॓॔ॕॖॗक़ख़ग़ज़ड़ढ़फ़य़ॠॡॢॣ।॥॰ॱॲॳॴॵॶॷॸॹॺॻॼॽॾॿ-")))

split1 = int(len(Y_cnn)*0.8)
X_train = X_cnn
Y_train = Y_cnn
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalAveragePooling1D, MaxPooling1D, Bidirectional, LSTM

max_features = len(valid_hindi)+1

test = ["अभिनेता","लेखक","यात्री","भाई","व्यवसायी","रसोइया","बेटी","अभियंता","पिता","दादा","दादी","वकील","माता","आरक्षक","चिकित्सक","प्रधानमंत्री","विक्रेता","बहन","सिपाही","बेटा","दरजी","अध्यापक","लेखक","चिड़िया","भौंरा","तितली","बिल्ली","चूज़ा","गाय","हिरन","कुत्ता","गधा","बत्तख","हाथी","मछली","घोड़ा","शेर","बन्दर","मोर","सुअर","खरगोश","झींगा","दगाबाज़","हंस","बाघ","कछुआ","सेब","केला","तुलसी","मिर्च","नारियल","अंगूर","चमेली","दाल","गंधना","आम","पुदीना","सिन्दूर","प्याज़","आलू","कद्दू","मूली","गुलाब","चंदन","सूर्यमुखी","अजवायन","टमाटर","तरबूज","एशिया","कतार","ब्रिटेन","भवन","शहर","महाद्वीप","देश","घर","गंगा","दुकान","भारत","पुस्तकालय","पर्वत","सगर","पाकिस्तान","उद्यान","भोजनालय","नदी","विद्यालय","दुकान","देवालय","अमरीका","चिड़ियाघर","पलंग","डिब्बा","रोटी","मक्खन","कुरसी","द्वार","डोसा","घर","तेल","पतलून","अङ्कनी","चावल","नमक","समोसा","साड़ी","कमीज","मसाला","शक्कर","मिठाई","मेज़","चाय","पानी","खिड़की","वास्तुकला","कला","बैडमिंटन","जीव-विज्ञान","शतरंज","क्रिकेट","नाच","खेल","इतिहास","भूगोल","पत्रकारिता","साहित्य","गणित","म्यूज़िक","जीवाश्मिकी","कविता","विज्ञान","पहेली","दौड","अध्ययन","तैराकी","टेनिस","दूरी","योग","सुन्दरता","रंग","आत्मविश्वास","ईर्ष्या","समय","हास्य","भूख","करुणा","ज्योति","प्यार","मनस","एकता","गौरव","ऋतु","आकाश","आवाज","चाल","तीव्रता","भोर","सूर्यास्त","समय","मौसम","विद्या"]
print(len(test))
testy = ["masculine","masculine","masculine","masculine","masculine","masculine","feminine","masculine","masculine","masculine","feminine","masculine","feminine","masculine","masculine","masculine","masculine","feminine","masculine","masculine","masculine","masculine","masculine","feminine","masculine","feminine","feminine","masculine","feminine","masculine","masculine","masculine","feminine","masculine","feminine","masculine","masculine","masculine","masculine","masculine","masculine","masculine","masculine","masculine","masculine","masculine","masculine","masculine","feminine","feminine","masculine","masculine","feminine","feminine","feminine","masculine","masculine","masculine","masculine","masculine","masculine","feminine","masculine","masculine","masculine","masculine","masculine","masculine","masculine","feminine","masculine","masculine","masculine","masculine","masculine","masculine","feminine","feminine","masculine","masculine","masculine","masculine","masculine","masculine","masculine","feminine","masculine","feminine","masculine","masculine","masculine","masculine","masculine","feminine","masculine","feminine","masculine","masculine","masculine","masculine","feminine","feminine","masculine","masculine","masculine","feminine","feminine","masculine","feminine","feminine","feminine","feminine","masculine","feminine","feminine","feminine","masculine","masculine","masculine","masculine","masculine","feminine","masculine","masculine","feminine","masculine","masculine","masculine","feminine","feminine","masculine","feminine","feminine","masculine","feminine","masculine","feminine","masculine","feminine","masculine","masculine","feminine","masculine","masculine","feminine","feminine","feminine","feminine","masculine","feminine","masculine","feminine","masculine","feminine","feminine","feminine","feminine","masculine","masculine","masculine","feminine"]
def encodex(text):
    s = list(text)

    indices = []
    for i in s:
        indices.append(valid_hindi.index(i))
    encoded = np.zeros([18, len(valid_hindi)+1], dtype="int")
    #print(len(a)+1)
    k = 0
    for i in indices:
        encoded[k][i] = 1
        k = k + 1
    for i in range(18-len(list(s))):
        encoded[k+i][len(valid_hindi)] = 1
    return encoded
fc= 0
mc = 0
def encodey(text):
    if text == "f":
        return [1]#,0,0]
    elif text == "m":
        return [0]#,0,1]

    else:
        return [0,1,0]
l1 = []
for a,b in zip(test,testy):
    if len(a) > 18:
        continue
    l1.append((a, b[0]))

l1 = list(set(l1))

xt = []
yt = []
for a,b in l1:
    if(b[0]=='m'):
        mc+=1
    else:
        fc+=1
    if mc >=55 and b[0] == 'm':
        continue
    xt.append(encodex(a))
    yt.append(encodey(b[0]))

print(fc ,mc)
#xt = np.array(xt)
#yt = np.array(yt)
Xu = (X_cnn).tolist()
Yu = (Y_cnn).tolist()
for t,yt3 in zip(xt, yt):
    if t.tolist() in Xu:

        print(Xu.index(t.tolist()))
        print(yt3, Y[Xu.index(t)])
    else:
        print("NEW")
print(X_train.shape)
print(xt.shape)
for loss_f in ['binary_crossentropy']:
    for opt in ['nadam','adam','nadam','sgd']:
        for lstm_len in [256,64,128]:
            for dropout in [0.6,0.45,0.5,0.55,0.6]:
                model = Sequential()
                #model.add(Embedding(120, output_dim=18))

                model = Sequential()
                model.add(Bidirectional(LSTM(lstm_len)))
                model.add(Dropout(dropout))
                model.add(Dense(1, activation='sigmoid'))

                model.compile(loss='binary_crossentropy',
                              optimizer=opt,
                              metrics=['accuracy'])
                print("Training new model, loss:"+'binary_crossentropy'+", optimizer="+opt+", lstm_len="+str(lstm_len)+", dropoff="+str(dropout))
                model.fit(X_train, Y_train, validation_data = (xt,yt), batch_size=512,epochs=10,verbose =2)
                score = model.evaluate(xt, yt, batch_size=512, verbose = 0)
                print("")
                print("test score: " + str(score))
                print("")
                print("")
                yt2 = model.predict(xt, batch_size=512,verbose=0)
                m = 0
                f = 0
                a = 0
                for i,y2 in zip(range(0,len(yt2)),yt2):
                    y= y2
                    print(test[i])
                    if y < 0.5:
                        print("f " + testy[i][0])
                        f+=1
                    else:
                        print("m " + testy[i][0])
                        a+=1
                print(m,f,a)

ext: py

reason: import numpy


text:
import os

import fire
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_hub as hub
from tensorflow import keras

module_selection = ("mobilenet_v2_100_224", 224)
handle_base, pixels = module_selection
MODULE_HANDLE = "https://tfhub.dev/google/imagenet/{}/feature_vector/4".format(handle_base)
IMAGE_SIZE = (pixels, pixels)
print("Using {} with input size {}".format(MODULE_HANDLE, IMAGE_SIZE))

BATCH_SIZE = 32

class TextClassification(object):
    def __init__(self, ):

        (self.train_data, self.validation_data, self.test_data), info = tfds.load('tf_flowers',
                                                                                split=['train[:70%]',
                                                                                'train[70%:80%]',
                                                                                'train[80%:]'],
                                                                                as_supervised=True,
                                                                                with_info=True)

        def normalize(image, label):
            resized_image = tf.image.resize(image, [pixels, pixels])
            final_image = keras.applications.mobilenet.preprocess_input(resized_image)
            return final_image, label

        self.test_data = self.test_data.map(normalize).batch(BATCH_SIZE).prefetch(1)
        self.validation_data = self.validation_data.map(normalize).batch(BATCH_SIZE).prefetch(1)
        self.train_data = self.train_data.map(normalize).batch(BATCH_SIZE).prefetch(1)

        do_fine_tuning = False

        print("Building model with", MODULE_HANDLE)
        model = tf.keras.Sequential([
            tf.keras.layers.InputLayer(input_shape=IMAGE_SIZE + (3,)),
            hub.KerasLayer(MODULE_HANDLE, trainable=do_fine_tuning),
            tf.keras.layers.Dense(512),
            tf.keras.layers.Dropout(rate=0.2),
            tf.keras.layers.Dense(info.features['label'].num_classes)
        ])
        model.build((None,) + IMAGE_SIZE + (3,))
        model.summary()

        print(model.summary())

        model.compile(
            optimizer=tf.keras.optimizers.SGD(lr=0.01),
            loss=tf.keras.losses.CategoricalCrossentropy(),
            metrics=['accuracy'])

        self.model = model

    def train(self,
              save_model_path):
        if not os.path.isdir(save_model_path):
            raise ValueError("Missing output directory {} for reporting".format(save_model_path))

        self.model.fit(
            self.train_data,
            epochs=3,
            validation_data=self.validation_data,
            verbose=1)
        self.model.save(os.path.join(save_model_path, "model.h5"))

    def test(self,
             model_path):
        model = tf.keras.models.load_model(
            model_path,
            custom_objects={'KerasLayer': hub.KerasLayer})
        print(model.summary())

        pred = model.predict(
            self.test_data,
            verbose=2)
        print(pred)

        results = model.evaluate(
            self.test_data,
            verbose=2)

        for name, value in zip(model.metrics_names, results):
            print("%s: %.3f" % (name, value))

    def save_test_set(self,
                      out_path,
                      batch_size=BATCH_SIZE,
                      num_batches=16):
        if not os.path.isdir(out_path):
            raise ValueError("out_path {} not found".format(out_path))

        lst = list(tfds.as_numpy(self.test_data.take(num_batches)))

        # TODO: actually we concatenate all batches in one, since mldiag take only one batch..
        # Improve MLDIAG to take multiple batches
        images = np.concatenate([x[0] for x in lst])
        images = np.array([x.tobytes() for x in images])
        labels = np.concatenate([x[1] for x in lst])
        test_set = np.array([(images, labels)])

        # test_set = np.array(list(tfds.as_numpy(self.test_data.map(create_images).batch(batch_size).take(
        # num_batches))))

        print("test set shape = {}".format(test_set.shape))
        test_set_path = os.path.join(out_path, 'test')
        np.save(test_set_path, test_set)
        print("saved in {}".format(test_set_path + ".npy"))

if __name__ == '__main__':
    fire.Fire(TextClassification)


text:
"""
Mandel & Agol Python routine. Written by Jason Eastman.
"""

from numpy import size,zeros,where,arccos,sqrt,pi,log

# Computes Hasting's polynomial approximation for the complete
# elliptic integral of the first (ek) and second (kk) kind
def ellke(k):
    m1=1.-k**2
    logm1 = log(m1)

    a1=0.44325141463
    a2=0.06260601220
    a3=0.04757383546
    a4=0.01736506451
    b1=0.24998368310
    b2=0.09200180037
    b3=0.04069697526
    b4=0.00526449639
    ee1=1.+m1*(a1+m1*(a2+m1*(a3+m1*a4)))
    ee2=m1*(b1+m1*(b2+m1*(b3+m1*b4)))*(-logm1)
    ek = ee1+ee2

    a0=1.38629436112
    a1=0.09666344259
    a2=0.03590092383
    a3=0.03742563713
    a4=0.01451196212
    b0=0.5
    b1=0.12498593597
    b2=0.06880248576
    b3=0.03328355346
    b4=0.00441787012
    ek1=a0+m1*(a1+m1*(a2+m1*(a3+m1*a4)))
    ek2=(b0+m1*(b1+m1*(b2+m1*(b3+m1*b4))))*logm1
    kk = ek1-ek2

    return [ek,kk]

# Computes the complete elliptical integral of the third kind using
# the algorithm of Bulirsch (1965):
def ellpic_bulirsch(n,k):
    kc=sqrt(1.-k**2); p=n+1.
    if(min(p) < 0.):
        print 'Negative p'
    m0=1.; c=1.; p=sqrt(p); d=1./p; e=kc
    while 1:
        f = c; c = d/p+c; g = e/p; d = 2.*(f*g+d)
        p = g + p; g = m0; m0 = kc + m0
        if max(abs(1.-kc/g)) > 1.e-8:
            kc = 2*sqrt(e); e=kc*m0
        else:
            return 0.5*pi*(c*m0+d)/(m0*(m0+p))

#   Python translation of IDL code.
#   This routine computes the lightcurve for occultation of a
#   quadratically limb-darkened source without microlensing.  Please
#   cite Mandel & Agol (2002) and Eastman, Gaudi, & Agol (2013) if you make
#   use of this routine in your research.  Please report errors or bugs to
#   jdeast@astronomy.ohio-state.edu
def mandel_agol(z,u1,u2,p0):
    """
    Adaptation of the Mandel & Agol transit light curve for quadratic limb
    darkening.

    Parameters
    ----------
    z : ndarray
        Impact parameters at which to evaluate the flux.
    u1 : float
        Linear limb darkening term
    u2 : float
        Quadratic limb darkening term
    p0 : float
        Radius ratio of the transiting object to the occulted object

    Returns
    -------
    fdark : ndarray
        Limb darkened flux at all points in z
    fflat : ndarray
        Flux without any limb darkening.
    """
    nz = size(z)
    lambdad = zeros(nz)
    etad = zeros(nz)
    lambdae = zeros(nz)
    omega=1.-u1/3.-u2/6.

    ## tolerance for double precision equalities
    ## special case integrations
    tol = 1e-14

    p = abs(p0)

    z = where(abs(p-z) < tol,p,z)
    z = where(abs((p-1)-z) < tol,p-1.,z)
    z = where(abs((1-p)-z) < tol,1.-p,z)
    z = where(z < tol,0.,z)

    x1=(p-z)**2.
    x2=(p+z)**2.
    x3=p**2.-z**2.

    ## trivial case of no planet
    if p <= 0.:
        muo1 = zeros(nz) + 1.
        mu0  = zeros(nz) + 1.
        #return [muo1,mu0]
        return muo1
    ## Case 1 - the star is unocculted:
    ## only consider points with z lt 1+p
    notusedyet = where( z < (1. + p) )
    notusedyet = notusedyet[0]
    if size(notusedyet) == 0:
        muo1 =1.-((1.-u1-2.*u2)*lambdae+(u1+2.*u2)*(lambdad+2./3.*(p > z))+ \
                  u2*etad)/omega
        mu0=1.-lambdae
        #return [muo1,mu0]
        return muo1

    # Case 11 - the  source is completely occulted:
    if p >= 1.:
        occulted = where(z[notusedyet] <= p-1.)#,complement=notused2)
        if size(occulted) != 0:
            ndxuse = notusedyet[occulted]
            etad[ndxuse] = 0.5 # corrected typo in paper
            lambdae[ndxuse] = 1.
            # lambdad = 0 already
            notused2 = where(z[notusedyet] > p-1)
            if size(notused2) == 0:
                muo1 =1.-((1.-u1-2.*u2)*lambdae+(u1+2.*u2)*(lambdad+2./3.* \
                                                 (p > z))+u2*etad)/omega
                mu0=1.-lambdae
                #return [muo1,mu0]
                return muo1
            notusedyet = notusedyet[notused2]

    # Case 2, 7, 8 - ingress/egress (uniform disk only)
    inegressuni = where((z[notusedyet] >= abs(1.-p)) & (z[notusedyet] < 1.+p))
    if size(inegressuni) != 0:
        ndxuse = notusedyet[inegressuni]
        tmp = (1.-p**2.+z[ndxuse]**2.)/2./z[ndxuse]
        tmp = where(tmp > 1.,1.,tmp)
        tmp = where(tmp < -1.,-1.,tmp)
        kap1 = arccos(tmp)
        tmp = (p**2.+z[ndxuse]**2-1.)/2./p/z[ndxuse]
        tmp = where(tmp > 1.,1.,tmp)
        tmp = where(tmp < -1.,-1.,tmp)
        kap0 = arccos(tmp)
        tmp = 4.*z[ndxuse]**2-(1.+z[ndxuse]**2-p**2)**2
        tmp = where(tmp < 0,0,tmp)
        lambdae[ndxuse] = (p**2*kap0+kap1 - 0.5*sqrt(tmp))/pi
        # eta_1
        etad[ndxuse] = 1./2./pi*(kap1+p**2*(p**2+2.*z[ndxuse]**2)*kap0- \
           (1.+5.*p**2+z[ndxuse]**2)/4.*sqrt((1.-x1[ndxuse])*(x2[ndxuse]-1.)))

    # Case 5, 6, 7 - the edge of planet lies at origin of star
    ocltor = where(z[notusedyet] == p)#, complement=notused3)
    t = where(z[notusedyet] == p)
    if size(ocltor) != 0:
        ndxuse = notusedyet[ocltor]
        if p < 0.5:
            # Case 5
            q=2.*p  # corrected typo in paper (2k -> 2p)
            Ek,Kk = ellke(q)
            # lambda_4
            lambdad[ndxuse] = 1./3.+2./9./pi*(4.*(2.*p**2-1.)*Ek+\
                                              (1.-4.*p**2)*Kk)
            # eta_2
            etad[ndxuse] = p**2/2.*(p**2+2.*z[ndxuse]**2)
            lambdae[ndxuse] = p**2 # uniform disk
        elif p > 0.5:
            # Case 7
            q=0.5/p # corrected typo in paper (1/2k -> 1/2p)
            Ek,Kk = ellke(q)
            # lambda_3
            lambdad[ndxuse] = 1./3.+16.*p/9./pi*(2.*p**2-1.)*Ek-\
                              (32.*p**4-20.*p**2+3.)/9./pi/p*Kk
            # etad = eta_1 already
        else:
            # Case 6
            lambdad[ndxuse] = 1./3.-4./pi/9.
            etad[ndxuse] = 3./32.
        notused3 = where(z[notusedyet] != p)
        if size(notused3) == 0:
            muo1 =1.-((1.-u1-2.*u2)*lambdae+(u1+2.*u2)*\
                      (lambdad+2./3.*(p > z))+u2*etad)/omega
            mu0=1.-lambdae
            #return [muo1,mu0]
            return muo1
        notusedyet = notusedyet[notused3]

    # Case 2, Case 8 - ingress/egress (with limb darkening)
    inegress = where( ((z[notusedyet] > 0.5+abs(p-0.5)) & \
                       (z[notusedyet] < 1.+p))  | \
                      ( (p > 0.5) & (z[notusedyet] > abs(1.-p)) & \
                        (z[notusedyet] < p)) )#, complement=notused4)
    if size(inegress) != 0:

        ndxuse = notusedyet[inegress]
        q=sqrt((1.-x1[ndxuse])/(x2[ndxuse]-x1[ndxuse]))
        Ek,Kk = ellke(q)
        n=1./x1[ndxuse]-1.

        # lambda_1:
        lambdad[ndxuse]=2./9./pi/sqrt(x2[ndxuse]-x1[ndxuse])*\
                         (((1.-x2[ndxuse])*(2.*x2[ndxuse]+x1[ndxuse]-3.)-\
                           3.*x3[ndxuse]*(x2[ndxuse]-2.))*Kk+(x2[ndxuse]-\
                           x1[ndxuse])*(z[ndxuse]**2+7.*p**2-4.)*Ek-\
                          3.*x3[ndxuse]/x1[ndxuse]*ellpic_bulirsch(n,q))

        notused4 = where( ( (z[notusedyet] <= 0.5+abs(p-0.5)) | \
                            (z[notusedyet] >= 1.+p) ) & ( (p <= 0.5) | \
                            (z[notusedyet] <= abs(1.-p)) | \
                            (z[notusedyet] >= p) ))
        if size(notused4) == 0:
            muo1 =1.-((1.-u1-2.*u2)*lambdae+(u1+2.*u2)*(lambdad+2./3.*\
                                                     (p > z))+u2*etad)/omega
            mu0=1.-lambdae
            #return [muo1,mu0]
            return muo1
        notusedyet = notusedyet[notused4]

    # Case 3, 4, 9, 10 - planet completely inside star
    if p < 1.:
        inside = where(z[notusedyet] <= (1.-p))#, complement=notused5)
        if size(inside) != 0:
            ndxuse = notusedyet[inside]

            ## eta_2
            etad[ndxuse] = p**2/2.*(p**2+2.*z[ndxuse]**2)

            ## uniform disk
            lambdae[ndxuse] = p**2

            ## Case 4 - edge of planet hits edge of star
            edge = where(z[ndxuse] == 1.-p)#, complement=notused6)
            if size(edge[0]) != 0:
                ## lambda_5
                lambdad[ndxuse[edge]] = 2./3./pi*arccos(1.-2.*p)-\
                                      4./9./pi*sqrt(p*(1.-p))*(3.+2.*p-8.*p**2)
                if p > 0.5:
                    lambdad[ndxuse[edge]] -= 2./3.
                notused6 = where(z[ndxuse] != 1.-p)
                if size(notused6) == 0:
                    muo1 =1.-((1.-u1-2.*u2)*lambdae+(u1+2.*u2)*\
                              (lambdad+2./3.*(p > z))+u2*etad)/omega
                    mu0=1.-lambdae
                    #return [muo1,mu0]
                    return muo1
                ndxuse = ndxuse[notused6[0]]

            ## Case 10 - origin of planet hits origin of star
            origin = where(z[ndxuse] == 0)#, complement=notused7)
            if size(origin) != 0:
                ## lambda_6
                lambdad[ndxuse[origin]] = -2./3.*(1.-p**2)**1.5
                notused7 = where(z[ndxuse] != 0)
                if size(notused7) == 0:
                    muo1 =1.-((1.-u1-2.*u2)*lambdae+(u1+2.*u2)*\
                              (lambdad+2./3.*(p > z))+u2*etad)/omega
                    mu0=1.-lambdae
                    #return [muo1,mu0]
                    return muo1
                ndxuse = ndxuse[notused7[0]]

            q=sqrt((x2[ndxuse]-x1[ndxuse])/(1.-x1[ndxuse]))
            n=x2[ndxuse]/x1[ndxuse]-1.
            Ek,Kk = ellke(q)

            ## Case 3, Case 9 - anywhere in between
            ## lambda_2
            lambdad[ndxuse] = 2./9./pi/sqrt(1.-x1[ndxuse])*\
                              ((1.-5.*z[ndxuse]**2+p**2+x3[ndxuse]**2)*Kk+\
                               (1.-x1[ndxuse])*(z[ndxuse]**2+7.*p**2-4.)*Ek-\
                               3.*x3[ndxuse]/x1[ndxuse]*ellpic_bulirsch(n,q))

        ## if there are still unused elements, there's a bug in the code
        ## (please report it)
        notused5 = where(z[notusedyet] > (1.-p))
        if notused5[0] != 0:
            print "ERROR: the following values of z didn't fit into a case:"
            return [-1,-1]

        muo1 =1.-((1.-u1-2.*u2)*lambdae+(u1+2.*u2)*(lambdad+2./3.*(p > z))+\
                  u2*etad)/omega
        mu0=1.-lambdae
        #return [muo1,mu0]
        return muo1

reason: from numpy


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

pastas = os.listdir("gestures")
dataset = []
classes = []
temp = [0 for i in range(len(pastas))]

for i,pasta in enumerate(pastas):
    images = os.listdir("gestures/"+pasta)
    copy = temp.copy()
    copy[i] = 1
    for img in images:
        st = "gestures/"+pasta+"/"+img
        mr = cv2.imread(st)
        dataset.append(mr)
        classes.append(copy.copy())
    copy.clear()


text:
# -*- coding: utf-8 -*-
"""
Load and save Paper2_OptimizingRestoration model data
from the postProcessing folders in each model folder and
save the data out to disk.

This script is a "Pythonic" version of a MATLAB script I wrote
last year.

BKN - USGS PCMSC 2022
"""
import os
import re
import time
import zipfile
import shutil
import numpy as np
import pandas as pd
import loadModelFiles
from pathlib import Path

version_no = 'V1'  # file version; appends to model save file

# Define file paths:
model_source = Path('e:/BKN-FIELD/Models/Paper2_OptimizingRestoration/ModelRuns/Scenarios/Reprocessed/')
model_dest = Path('h:/Models/Paper2_OptimizingRestoration/ModelRuns/Scenarios/')
save_data_path = str(model_dest) + "\\rawResults\\"
model_info = 'adjustModelSampling_RESTART5_TEST.csv'

# Begin program
# 1. Load CSV
t = time.localtime()
current = time.strftime("%H:%M:%S", t)
print('The current time is: ' + current)
csv_file = model_dest / model_info
model_info = pd.read_csv(csv_file.open())

# 2. Get model names from the model directory
filenames = next(os.walk(str(model_source)))[2]  # For later, [1] is dirs and [2] is files

# 3. Load files in model_source directory
t1 = time.time()
for file in filenames:
    t2 = time.time()

    # Only process ZIP files!
    if '.zip' in file:
        # 3a. Check if file is in model_info, only process if so
        scenario_number = re.search('\_(.*)\.', file).group(1)
        idx = np.where(int(scenario_number) == model_info['orgScenarioNumber'])
        if idx[0].size > 0:
            idx = int(idx[0])
            print('Processing ' + file)

            # 3b. Unzip model file to model_source
            print('Unzipping file from source directory...')
            model_name = file.split('.')[0]  # model folder without extension
            with zipfile.ZipFile(str(model_source / file), 'r') as zip_file:
                zip_file.extractall(str(model_dest / model_name))

            # 3c. Run loading functions on model files
            model = loadModelFiles.loadModelFiles(str(model_dest), model_name, model_info['wavePeriod'][idx])
            timestep = model.createTimestep()
            freeSurf = model.loadFreeSurface(timestep)
            fields = model.loadFields(timestep)

            # 3d. Save out binary files to disk
            model.saveData(freeSurf, 'freeSurf', save_data_path, version_no)
            model.saveData(fields, 'fields', save_data_path, version_no)

            # 3e. Clear memory, remove model folder on disk to preserve space
            print('Removing model folder from dest directory...')
            del model, timestep, freeSurf, fields
            full_path = str(model_dest / model_name)
            shutil.rmtree(full_path)

            # Timer
            executionTime = (time.time() - t2) / 60
            print(f'File {file} processed in: {executionTime:.2f} minutes\n')

# End timer
executionTime = (time.time() - t1) / 60
print(f'Total runtime: {executionTime:.2f} minutes\n')

max_stars_repo_name: bknorris/Postdoc

max_issues_repo_name: bknorris/Postdoc

max_forks_repo_name: bknorris/Postdoc