text:
""" Pipelines expressed as dask graphs
"""

from astropy import units as u
from astropy.coordinates import SkyCoord
from dask import delayed

from arl.calibration.operations import apply_gaintable, create_gaintable_from_blockvisibility
from arl.data.persist import arl_dump, arl_load
from arl.data.polarisation import PolarisationFrame
from arl.graphs.graphs import create_predict_wstack_graph
from arl.util.testing_support import create_named_configuration, simulate_gaintable, \
    create_low_test_image_from_gleam, create_low_test_beam
from arl.visibility.base import create_blockvisibility, create_visibility
from arl.data.parameters import get_parameter

import logging

log = logging.getLogger(__name__)

def create_simulate_vis_graph(config='LOWBD2-CORE',
                              phasecentre=SkyCoord(ra=+15.0 * u.deg, dec=-60.0 * u.deg, frame='icrs', equinox='J2000'),
                              frequency=None, channel_bandwidth=None, times=None,
                              polarisation_frame=PolarisationFrame("stokesI"), order='frequency',
                              format='blockvis',
                              **kwargs) -> delayed:
    """ Create a graph to simulate an observation

    The simulation step can generate a single BlockVisibility or a list of BlockVisibility's.
    The parameter keyword determines the way that the list is constructed.
    If order='frequency' then len(frequency) BlockVisibility's with all times are created.
    If order='time' then  len(times) BlockVisibility's with all frequencies are created.
    If order = 'both' then len(times) * len(times) BlockVisibility's are created each with
    a single time and frequency. If order = None then all data are created in one BlockVisibility.

    The output format can be either 'blockvis' (for calibration) or 'vis' (for imaging)

    :param config: Name of configuration: def LOWBDS-CORE
    :param phasecentre: Phase centre def: SkyCoord(ra=+15.0 * u.deg, dec=-60.0 * u.deg, frame='icrs', equinox='J2000')
    :param frequency: def [1e8]
    :param channel_bandwidth: def [1e6]
    :param times: Observing times in radians: def [0.0]
    :param polarisation_frame: def PolarisationFrame("stokesI")
    :param order: 'time' or 'frequency' or 'both' or None: def 'frequency'
    :param format: 'blockvis' or 'vis': def 'blockvis'
    :param kwargs:
    :return: vis_graph_list with different frequencies in different elements
    """
    if format == 'vis':
        create_vis = create_visibility
    else:
        create_vis = create_blockvisibility

    if times is None:
        times = [0.0]
    if channel_bandwidth is None:
        channel_bandwidth = [1e6]
    if frequency is None:
        frequency = [1e8]
    conf = create_named_configuration(config, **kwargs)

    if order == 'time':
        log.debug("create_simulate_vis_graph: Simulating distribution in %s" % order)
        vis_graph_list = list()
        for i, time in enumerate(times):
            vis_graph_list.append(delayed(create_vis, nout=1)(conf, [times[i]], frequency=frequency,
                                                              channel_bandwidth=channel_bandwidth,
                                                              weight=1.0, phasecentre=phasecentre,
                                                              polarisation_frame=polarisation_frame, **kwargs))

    elif order == 'frequency':
        log.debug("create_simulate_vis_graph: Simulating distribution in %s" % order)
        vis_graph_list = list()
        for j, _ in enumerate(frequency):
            vis_graph_list.append(delayed(create_vis, nout=1)(conf, times, frequency=[frequency[j]],
                                                              channel_bandwidth=[channel_bandwidth[j]],
                                                              weight=1.0, phasecentre=phasecentre,
                                                              polarisation_frame=polarisation_frame, **kwargs))

    elif order == 'both':
        log.debug("create_simulate_vis_graph: Simulating distribution in time and frequency")
        vis_graph_list = list()
        for i, _ in enumerate(times):
            for j, _ in enumerate(frequency):
                vis_graph_list.append(delayed(create_vis, nout=1)(conf, [times[i]], frequency=[frequency[j]],
                                                                  channel_bandwidth=[channel_bandwidth[j]],
                                                                  weight=1.0, phasecentre=phasecentre,
                                                                  polarisation_frame=polarisation_frame, **kwargs))

    elif order is None:
        log.debug("create_simulate_vis_graph: Simulating into single %s" % format)
        vis_graph_list = list()
        vis_graph_list.append(delayed(create_vis, nout=1)(conf, times, frequency=frequency,
                                                          channel_bandwidth=channel_bandwidth,
                                                          weight=1.0, phasecentre=phasecentre,
                                                          polarisation_frame=polarisation_frame, **kwargs))
    else:
        raise NotImplementedError("order $s not known" % order)
    return vis_graph_list

def create_predict_gleam_model_graph(vis_graph_list, frequency, channel_bandwidth,
                                     npixel=512, cellsize=0.001,
                                     c_predict_graph=create_predict_wstack_graph, **kwargs):
    """ Create a graph to fill in a model with the gleam sources and predict into a vis_graph_list

    :param vis_graph_list:
    :param npixel: 512
    :param cellsize: 0.001
    :param c_predict_graph: def create_predict_wstack_graph
    :param kwargs:
    :return: vis_graph_list
    """

    # Note that each vis_graph has it's own model_graph

    predicted_vis_graph_list = list()
    for i, vis_graph in enumerate(vis_graph_list):
        facets = {}
        if get_parameter(kwargs, "facets", False):
            facets = {'facets': get_parameter(kwargs, "facets", False)}
        model_graph = create_gleam_model_graph(vis_graph, frequency, channel_bandwidth, npixel=npixel,
                                               cellsize=cellsize, **facets)
        predicted_vis_graph_list.append(c_predict_graph([vis_graph], model_graph, **kwargs)[0])
    return predicted_vis_graph_list

def create_gleam_model_graph(vis_graph: delayed, frequency, channel_bandwidth, npixel=512, cellsize=0.001,
                             facets=4):
    """ Create a graph to fill in a model with the gleam sources

    This spreads the work over facet**2 nodes

    :param vis_graph: Single vis_graph
    :param frequency:
    :param channel_bandwidth:
    :param npixel: 512
    :param cellsize: 0.001
    :param facets: def 4
    :return: graph
    """

    def calculate_gleam_model(vis):
        model = create_low_test_image_from_gleam(npixel=npixel, frequency=frequency,
                                                 channel_bandwidth=channel_bandwidth,
                                                 cellsize=cellsize, phasecentre=vis.phasecentre)
        beam = create_low_test_beam(model)
        model.data *= beam.data
        return model

    return delayed(calculate_gleam_model, nout=1)(vis_graph)

def create_corrupt_vis_graph(vis_graph_list, gt_graph=None, **kwargs):
    """ Create a graph to apply gain errors to a vis_graph_list

    :param vis_graph_list:
    :param gt_graph: Optional gain table graph
    :param kwargs:
    :return:
    """

    def corrupt_vis(vis, gt, **kwargs):
        if gt is None:
            gt = create_gaintable_from_blockvisibility(vis, **kwargs)
            gt = simulate_gaintable(gt, **kwargs)
        return apply_gaintable(vis, gt)

    return [delayed(corrupt_vis, nout=1)(vis_graph, gt_graph, **kwargs) for vis_graph in vis_graph_list]

def create_dump_vis_graph(vis_graph_list, name='imaging_dask') -> delayed:
    """ Create a graph to save a vis_graph_list

    :param vis_graph_list:
    :param name:
    :return:
    """
    def save_all(vis):
        for i, v in enumerate(vis):
            arl_dump(v, "%s_%d.pickle" % (name, i))

    return delayed(save_all)(vis_graph_list)

def create_load_vis_graph(name='imaging_dask'):
    """ Load's pickled data with the glob of "%s_*.pickle" % (name)

    :param name: Root of name
    :return: vis_graph_list
    """
    import glob
    name = "%s_*.pickle" % (name)
    persist_list = glob.glob(name)
    assert len(persist_list)
    return [delayed(arl_load, nout=1)(name) for name in persist_list]

ext: py

reason: from astropy


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

"""
A simple Discrete Ordinates S$_N$ computer code for multi energy group neutron
calculations in the slab geometry. This program is only intended for basic
education purposes of students at the Master program in Nuclear Engineering.
The solving algorithm is based on the transport sweep and the common diamond
differences scheme, see chapter 3 of [1].

Cross sections data of different materials must be entered according to
the following dictionary. Please, use zeroed arrays for chi and nsf for
non-fissile media.

xs_media = {
    'name_of_media_1':{
         'st': np.array with G elements
         'ss': np.array with G*G*(anisotropy_order + 1) elements
        'chi': np.array with G elements
        'nsf': np.array with G elements
    }
    'name_of_media_2':{ ... }
    ...
}

A list of lists is used to assign the materials to the geometry cells
of the mesh, like for example:

media = [
    ['name_of_media_1', x_right_medium_1],
    ['name_of_media_2', x_right_medium_2],
    ...
    ['name_of_media_N', x_right_medium_N]
]
where by definition it is always x_left_medium_1 = 0, and
x_right_medium_(i) = x_left_medium_(i+1) for all i < N.

...

Bibliography
------------

[1] Lewis, Elmer E and Miller, Warren F, Computational methods of neutron
    transport (1984).
}
"""

# Owned
__author__ = "Daniele Tomatis"
__email__ = "daniele.tomatis@cea.fr"
__company__ = "DEN/DANS/DM2S/SERMA/LPEC CEA Saclay, France"
__date__ = "03/04/2019"
__license__ = "MIT License"
__copyright__ = "Copyright 2019, CEA Saclay France"
__status__ = "Dev"
__credits__ = [__author__]
__version__ = "0.1.0"

import logging as lg
import sys, os
import warnings as wrn
import numpy as np
from scipy.special import roots_legendre, legendre

sys.path.append(
    os.path.join(os.path.dirname(__file__), '..', 'FD1dMGdiff')
)
from FDsDiff1D import input_data, solver_options, unfold_xs

def get_dirs_and_weights(N=None, L=0, qtype="Gauss-Legendre"):
    """Compute directions and weights of the S_N quadrature set, and the
    (polynomial) functions for the flux expansion."""
    if N == None: raise ValueError("Missing input nb. of directions.")
    if (N <= 0): raise ValueError("Invalid N <= 0")
    print("Input nb. of directions is "+str(N))
    Nh, isodd = N//2, N%2 == 1
    # mn and wn are N directions and weights
    if(N == 1):
        mn, wn = np.full(1, 0.), np.full(1, 1.)
    else:
        if isodd: wrn.warn("Odd nb. of directions detected")
        if qtype == "Gauss-Legendre":
            mn, wn = roots_legendre(N, False)
            pl = [legendre(l) for l in range(L+1)]
            # remind that the weights must sum to 2
        else: raise ValueError("Unsupported quadrature type")
    return mn, wn, pl

class quad_data:
    """Object collecting quadrature data."""

    def __init__(self, N=16, L=0, qtype='Gauss-Legendre'):
        self.N, self.L, self.qtype = N, L, qtype
        self.mn, self.wn, self.pl = get_dirs_and_weights(N, L, self.qtype)
        #self.check_input()

def differencing_by_SC(aflx, bflx, hm):
    """Differencing scheme similar to step characteristics; aflx and bflx are
    the cell-averaged and the cell-edge fluxes, respectively; hm is dx over the
    direction cosine mn. See Azmy, lowest order nodal integral method."""
    betan = np.exp(- hm)
    betan = (1 + betan) / (1 - betan) - 2. / hm
    betan *= np.sign(hm)  # to account for both +/- directions
    return (2 * aflx - (1 - betan) * bflx) / (1 + betan)

def differencing_by_DD(aflx, bflx):
    """Differencing scheme based on unweighted diamond difference; aflx and
    bflx are the cell-averaged and the cell-edge fluxes, respectively."""
    return 2 * aflx - bflx

def tr_sweep(dx, st, qli, qdata, lbc=0, rbc=0):
    """Get new flux moments by the transport sweep. Boundary conditions: 0 for
    vacuum, 1 specular reflection, periodic translation otherwise. r/lbc, right
    or left."""
    mn, wn, pl = qdata.mn, qdata.wn, qdata.pl # qdata, quadrature data
    N = len(mn)
    L, I = qli.shape
    Nh = N // 2
    maxits = 4 if (lbc == rbc == 1) else 2

    # compute the angular source once
    qmi = np.zeros((N,I),)
    for j, m in enumerate(mn):
        #qmi[j,:] = np.dot( pl[:](m), qli )
        for l in range(L):
            qmi[j,:] += qli[l,:] * pl[l](m)

    aflx, afbc = np.zeros_like(qmi), np.zeros((N,2),)
    for it in range(maxits):
        for m in range(Nh):
            # positive directions
            mp = N-1-m
            if lbc == 0:
                aflxm1 = 0.
            elif lbc == 1:
                aflxm1 = afbc[m,0]
            else:
                aflxm1 = afbc[mp,1]
            for i in range(I): # advance forward, abs(mn[mp]) = mn[mp]
                aflx[mp, i] = qmi[mp, i] + 2 * mn[mp] * aflxm1
                aflx[mp, i] /= (2 * mn[mp] / dx[i] + st[i])
                # aflxm1 = 2 * aflx[mp, i] / dx[i] - aflxm1  # simple DD scheme
                aflxm1 = differencing_by_DD(aflx[mp, i] / dx[i], aflxm1)
                # aflxm1 = differencing_by_SC(aflx[mp, i] / dx[i], aflxm1,
                #                             st[i] * dx[i] / mn[mp])
                if aflxm1 < 0.: raise ValueError("negative aflx!")
            afbc[mp,1] = aflxm1

            # negative directions
            if rbc == 0:
                aflxm1 = 0.
            elif rbc == 1:
                aflxm1 = afbc[mp,1]
            else:
                aflxm1 = afbc[m,0]
            for i in range(I-1,-1,-1): # advance backward, abs(mn[m]) = mn[mp]
                aflx[m, i] = (qmi[m, i] + 2 * mn[mp] * aflxm1)
                aflx[m, i] /= (2 * mn[mp] / dx[i] + st[i])
                # aflxm1 = 2 * aflx[m, i] / dx[i] - aflxm1  # simple DD scheme
                aflxm1 = differencing_by_DD(aflx[m, i] / dx[i], aflxm1)
                # aflxm1 = differencing_by_SC(aflx[m, i] / dx[i], aflxm1,
                #                             st[i] * dx[i] / mn[m])
                if aflxm1 < 0.: raise ValueError("negative aflx!")
            afbc[m,0] = aflxm1

        if it == 0:
            # leave the loop if vacuum at both sides or reflection at right
            if (lbc == 0) and (0 <= rbc <= 1): break

    # compute the new flux moments
    flxm = np.zeros((L,I),)
    for l in range(L):
        flxm[l,:] = np.dot(wn * pl[l](mn), aflx)
    return flxm

def solve_inners(dx, st, ss, qdata, flxm, nsff, lbc, rbc, \
                 itsmax = 10, toll=1.e-5, vrbs=True):
    "Solve the inner iterations on groups to update the scattering source."
    G, L, I = flxm.shape

    # compute the source terms, starting from scattering
    src = np.zeros_like(flxm)
    for g in range(G):
        for l in range(L):
            src[g,l,:] = np.sum(ss[g,:,l,:] * flxm[:,l,:], axis=0) \
                       * (2 * l + 1.) / 2.
    # add the fission contribution
    src[:,0,:] += nsff

    emax_inners, it = 1.e+20, 0
    while (emax_inners > toll) and (it < itsmax):
        old_flxm = np.array(flxm, copy=True)

        for g in range(G):
            # apply the transport sweep per each group equation
            flxm[g,:,:] = tr_sweep(dx, st[g,:], src[g,:,:], qdata, lbc, rbc)

            # update the scattering source
            for gg in range(g,G):
                for l in range(L):
                    src[gg,l,:] += (2 * l + 1.) / 2. * ss[gg,g,l,:] \
                        * (flxm[g,l,:] - old_flxm[g,l,:])

        # compute the residual error
        eflx = np.where( flxm > 0., 1. - old_flxm / flxm, old_flxm - flxm )
        emax_inners = np.max(np.abs(eflx))
        it += 1
        if vrbs: print("it ={:3d}, emax = {:13.6g}".format(it,emax_inners))
    return flxm

def compute_fiss_src(nsf, chi, flxm):
    "Compute the fission source"
    nsff = np.sum(nsf * flxm[:, 0, :], axis=0) # sum on groups!
    # apply the emission spectrum, and add to the final source
    return 0.5 * chi * nsff

def solve_outers(dx, xs, qdata, flxm, k, oitsmax=20, toll=1.e-5,
                 lbc=0, rbc=0, vrbs=True):
    "Solve the criticality problem by outer iterations."
    it, emax_outers = 0, 1.e+20
    st, ss, chi, nsf = xs  # cross sections data
    mn, wn, pl = qdata.mn, qdata.wn, qdata.pl # quadrature data
    #flxn = np.sum(flxm[:,0,:]) # volume-norm
    # (initial) compute the fission contribution
    nsff = compute_fiss_src(nsf, chi, flxm)
    while (it < oitsmax) and (emax_outers > toll):
        old_flxm, old_k = np.array(flxm, copy=True), k

        # solve the inner iterations taking on the scattering source
        flxm = solve_inners(dx, st, ss, qdata, flxm, nsff / k, lbc, rbc, \
                        itsmax = 100, toll=toll, vrbs=False)

        # compute the fission contribution
        old_nsff = np.array(nsff, copy=True)
        nsff = compute_fiss_src(nsf, chi, flxm)

        # get the new estimate of the eigenvalue
        k *=  np.sum(flxm[:,0,:] * nsff) / np.sum(flxm[:,0,:] * old_nsff)
        # compute the residual error
        e_k = 1.e+5 * (k - old_k)
        eflx = np.where( flxm > 0., 1. - old_flxm / flxm, old_flxm - flxm )
        emax_outers = np.max(np.abs(eflx))
        it += 1
        if vrbs:
            line = "it ={:3d}, k = {:6.5f}, e_k = {:6.1f}, eflx = {:13.6g}"
            print(line.format(it, k, e_k, emax_outers))
    return flxm, k

def solve_sn(idata, slvr_opts, qdata):
    '''Run SN solver.'''
    lg.info("Prepare input data")
    # xs = [st, ss, chi, nsf]
    xs = unfold_xs(idata, diff_calc=False)
    # initialize the cell-integrated flux moments
    # (which are the only unknowns stored in memory at this higher level)
    # and the multiplication factor (eigenvalue)
    flxm = np.zeros((idata.G, qdata.L+1, idata.I),)
    flxm[:, 0, :], k = 1., 1.
    lg.info("-o"*22)

    # start SN iterations
    lg.info("Start the SN iterations")
    flxm, k = solve_outers(idata.Di, xs, qdata, flxm, k,
                           slvr_opts.oitmax, slvr_opts.toll,
                           idata.LBC, idata.RBC)

    lg.info("-o"*22)
    lg.info("*** NORMAL END OF CALCULATION ***")
    return flxm, k

if __name__ == '__main__':

    # input general data
    I = 100 # nb. of cells
    a = 21.5 / 2. # slab width (cm)
    N = 16 # nb. of directions
    G = 2 # nb. of energy groups
    # L, maximum level of scattering anisotropy
    # (so that we will only compute flux moments up to the order L)
    L = 0

    # boundary conditions
    lbc, rbc = 1, 0  # left / right b.c.

    # input cross section data
    chi = np.array([1., 0.])
    st = np.array([5.3115e-1, 1.30058e+0])
    nsf = np.array([7.15848e-3, 1.41284e-1])
    ss0 = np.array([[5.04664e-1, 2.03884e-3], [1.62955e-2, 1.19134e+0]])
    finf = np.linalg.solve(np.diag(st) - ss0, chi)
    kinf = np.dot(nsf, finf)
    print("The k-infty of the homogeneous infinite slab is {:8.6f}.".format( \
        kinf))
    # (reference) k_\infty = 1.07838

    # fill input containers with the cross sections
    chi = np.zeros((G, I),)
    chi[0, :] = 1.
    st = np.tile(st, (I, 1)).T
    nsf = np.tile(nsf, (I, 1)).T
    ss = np.zeros((G, G, L+1, I),)
    for i in range(I): ss[:,:,0,i] = ss0
    xs = [st, ss, chi, nsf]

    # define the spatial mesh
    dx = np.linspace(0., a, I+1)
    dx = dx[1:] - dx[:-1]

    # calculate the directions using the Gauss-Legendre quadrature
    qdata = quad_data(N, L)

    # initialize the cell-integrated flux moments
    # (which are the only unknowns stored in memory at this higher level)
    # and the multiplication factor (eigenvalue)
    flxm = np.zeros((G, L+1, I),)
    flxm[:, 0, :], k = 1., 1.

    # solve the criticality problem by power iterations
    flxm, k = solve_outers(dx, xs, qdata, flxm, k, oitsmax=100, toll=1.e-7,
                           lbc=lbc, rbc=rbc)
    basefilen = "LBC%dRBC%d_I%d_N%d" % (lbc, rbc, I, N)
    # np.save(basefilen + ".npy", np.array([k, flxm]), allow_pickle=True)
    np.savez(basefilen + ".npz", k=k, flxm=flxm)

reason: import numpy,from scipy


text:
from sys import setrecursionlimit
from time import clock
from hamilton import hamiltonian
from nygga_list import ListGraph

import numpy as np

probe_sizes = [0.1, 0.2, 0.3, 0.4, 0.5,]

repeats = 3

def measure_exe_time(algo):
	start = clock()
	algo()
	stop = clock()
	return stop - start

def single_algo(algo, once):
	dd = np.zeros((1 + repeats, len(probe_sizes)))
	dd[0, :] = probe_sizes

	#to fill

	probeIndex = 0
	for probe in probe_sizes:
		print 'Probe size ', probe, '(Index ', probeIndex, ')...'
		matrix = ListGraph(15)
		matrix.fill(probe)
		for row in range(1, repeats + 1):
			print 'Repeat ', row, '...'
			try:
				start = clock()
				algo(matrix.graph, once)
				stop = clock()
				perf = stop - start
				dd[row, probeIndex] = perf
				print perf
			except RuntimeError:
				dd[row, probeIndex] = -10

		probeIndex += 1

	return dd

if __name__ == '__main__':
	setrecursionlimit(999999)
	m = single_algo(hamiltonian, once=False)
	s = single_algo(hamiltonian, once=True)

	np.savetxt('hamilton_Single_15', s)
	np.savetxt('hamilton_Multi_15', s)

max_stars_repo_name: mbednarski/PUT-AiSD

max_issues_repo_name: mbednarski/PUT-AiSD

max_forks_repo_name: mbednarski/PUT-AiSD

reason: import numpy


text:
# HESystem server application
from flask import Flask, request, jsonify, render_template
from tenseal import context
from tenseal import SCHEME_TYPE
import hesystem.server as sv
import numpy as np

# parameters
#poly_mod_degree = 2**13 # value: 8192
#coeff_mod_bit_sizes = [40, 21, 21, 21, 21, 21, 21, 40]
#ctx_training = context(SCHEME_TYPE.CKKS, poly_mod_degree, -1, coeff_mod_bit_sizes)
#ctx_training.global_scale = 2 ** 21

#ctx_training.generate_galois_keys()
#secret_key = ctx_training.secret_key()
#ctx_training.make_context_public()

ctx_train = context(SCHEME_TYPE.CKKS, 2**13, -1, [40, 21, 21, 21, 21, 21, 21, 40])
ctx_train.global_scale = 2 ** 21
ctx_train.generate_galois_keys()
secret_key_train = ctx_train.secret_key()
ctx_train.make_context_public()
ctx_rotation = context(SCHEME_TYPE.CKKS, 2**13, -1, [60, 30, 60])
ctx_rotation.global_scale = 2 ** 30
ctx_rotation.generate_galois_keys()
secret_key_rotation = ctx_rotation.secret_key()
ctx_rotation.make_context_public()
ctx_eval = context(SCHEME_TYPE.CKKS, 2**12, -1, [40, 20, 40])
ctx_eval.global_scale = 2 ** 20
ctx_eval.generate_galois_keys()
secret_key_eval = ctx_eval.secret_key()
ctx_eval.make_context_public()

# Organization information
my_address, private_key, endpoint_url = sv.retrieve_user_info()

# Server configuration
app = Flask(__name__)
conn = sv.set_database('DATABASE_URL')
scheduler = sv.schedule_buyer_confirmation()
web3 = sv.set_web3(endpoint_url, my_address)

# Contract information
pending_confirmation = []
meta_link, test_link, test_result_link, data_link, result_link, num_rows, value, expiration_time, condition = sv.retrieve_contract_info()
contract_data = sv.ContractData(value, expiration_time, meta_link, test_link, test_result_link, data_link, result_link)
#contract_deployed = contract_data.deploy_contract(web3, my_address, private_key)
contract_deployed = contract_data.use_contract(web3, '0x8694A9ed96179d39DFDBD81d8eE3536e8fDaDA71')
print('Contract address: ' + contract_deployed.address)

# Restaurant Routes
@app.route('/restaurants/data/result', methods=['POST'])
def restaurantsdataresult():
    print('Decrypted result has been requested')
    global my_address
    global condition
    global private_key
    buyer = request.json.get('buyer')
    json_obj = request.json.get('obj')
    params = request.json.get('params')
    global ctx_train
    global secret_key_train
    global ctx_eval
    global secret_key_eval
    global ctx_rotation
    global secret_key_rotation
    if params == 'train':
        ctx_final = ctx_train
        secret_key = secret_key_train
    elif params == 'eval':
        ctx_final = ctx_eval
        secret_key = secret_key_eval
    elif params == 'rotation':
        ctx_final = ctx_rotation
        secret_key = secret_key_rotation
    else:
        ctx_final = ctx_rotation
        secret_key = secret_key_rotation
    result = np.array(sv.deserialize_decrypt(ctx_final, json_obj, secret_key))

    global num_rows
    global condition
    secure_result = sv.check_result(result, num_rows, condition)
    if secure_result:
        key = contract_deployed.functions.send_decrypted(buyer).buildTransaction({
            'nonce': web3.eth.getTransactionCount(my_address),
            'gas': 1728712,
            'gasPrice': web3.toWei('21','gwei')
        })
        signed_tx = web3.eth.account.signTransaction(key, private_key=private_key)
        hash_tx = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
        receipt_tx = web3.eth.waitForTransactionReceipt(hash_tx)
        if receipt_tx.status:
            print('Result sent to: ' + buyer)
            return jsonify(result.tolist())
        else:
            return jsonify({'message':'You already requested and received a result'})
    else:
        return jsonify({'message':'Your result dimensions should be less than the original dimensions divided by' + condition})
@app.route('/restaurants/test/result', methods=['POST'])
def restaurantstestresult():
    print('Decrypted test result requested')
    json_obj = request.json.get('obj')
    params = request.json.get('params')
    global ctx_train
    global secret_key_train
    global ctx_eval
    global secret_key_eval
    global ctx_rotation
    global secret_key_rotation
    if params == 'train':
        ctx_final = ctx_train
        secret_key = secret_key_train
    elif params == 'eval':
        ctx_final = ctx_eval
        secret_key = secret_key_eval
    elif params == 'mean':
        ctx_final = ctx_rotation
        secret_key = secret_key_rotation
    else:
        ctx_final = ctx_rotation
        secret_key = secret_key_rotation
    result = np.array(sv.deserialize_decrypt(ctx_final, json_obj, secret_key))

    global num_rows
    global condition
    secure_result = sv.check_result(result, num_rows, condition)
    if secure_result:
        return jsonify(result.tolist())
    else:
        return jsonify({'message':'Your result dimensions should be less than the original dimensions divided by' + condition})
@app.route('/restaurants/data', methods=['POST'])
def restaurantsdata():
    print('Real data has been requested')
    buyer = request.json.get('address')
    index = int(request.json.get('index'))
    flag_pay = contract_deployed.functions.f_buyer_payed(buyer).call()
    if flag_pay:
        print('The buyer is: ' + buyer)
        cur = conn.cursor()
        #command = "SELECT lat, lon, duracion, business_size, categoria_CALZADO, categoria_CAFETERIA, categoria_PANADERIA, categoria_FARMACIA, categoria_RESTAURANTE, categoria_TIENDA "
        #command += "FROM RUCs r INNER JOIN businesscategory b on CAST(r.actividad_economica as INT )=b.id_actividad "
        #command += "WHERE b.business_category = 'RESTAURANTE'"
        command = "SELECT male, age, cigsPerDay, prevalentStroke, prevalentHyp, totchol, sysbp, heartRate, glucose, TenYearCHD FROM framingham LIMIT 1000"

        cur.execute(command)
        restaurants = cur.fetchall()
        cur.close()

        array = np.array(restaurants)
        array = (array - array.mean()) / array.std()
        new_array = array[:,index]
        print('standarized selected array element:')
        print('[ ' + str(new_array[0]) + ', ... ]')
        print(f'Sum: {sum(new_array)}')
        new_array = [[e] for e in new_array]
        array = np.delete(array, index, 1)
        array = array.tolist()

        params = request.json.get('params')
        global ctx_train
        global ctx_eval
        global ctx_rotation
        if params == 'train':
            ctx_final = ctx_train
        elif params == 'eval':
            ctx_final = ctx_eval
        elif params == 'mean':

            ctx_final = ctx_rotation
        else:
            ctx_final = ctx_rotation

        ckks_serialized = sv.encrypt_serialize(ctx_final, array)
        col_serialized = sv.encrypt_serialize(ctx_final, new_array)
        ctx_serialized = sv.b64encode(ctx_final.serialize()).decode()

        global pending_confirmation
        pending_confirmation.append(buyer)

        return jsonify({
        'ckks': ckks_serialized,
        'col': col_serialized,
        'ctx': ctx_serialized,
        'params': params,
        })
    else:
        return jsonify({'message':'There is no payment yet'})
@app.route('/restaurants/test', methods=['POST'])
def restaurantstest():
    print('Test data has been requested')
    index = int(request.json.get('index'))
    array = np.ones(num_rows)
    new_array = array[:,index]
    new_array = [[e] for e in new_array]
    array = np.delete(array, index, 1)
    array = array.tolist()

    params = request.json.get('params')
    global ctx_train
    global ctx_eval
    global ctx_rotation
    if params == 'train':
        ctx_final = ctx_train
    elif params == 'eval':
        ctx_final = ctx_eval
    elif params == 'mean':
        ctx_final = ctx_rotation
    else:
        ctx_final = ctx_rotation

    ckks_serialized = sv.encrypt_serialize(ctx_final, array)
    col_serialized = sv.encrypt_serialize(ctx_final, new_array)
    ctx_serialized = sv.b64encode(ctx_final.serialize()).decode()

    return jsonify({
    'ckks': ckks_serialized,
    'col': col_serialized,
    'ctx': ctx_serialized
    })
@app.route('/restaurants/details', methods=['GET'])
def restaurantsdetails():
    print('Contract details have been requested')
    data = {
    'address': contract_data.address,
    'value': contract_data.value + contract_data.guarantee,
    'abi': contract_data.abi
    }
    return jsonify(data)

# Information Website Routes
@app.route('/')
def index():
    return render_template('index.html')
@app.route('/data')
def data():
    return render_template('data.html')
@app.route('/buytutorial')
def buy():
    return render_template('buying.html')
@app.route('/connectiontutorial')
def connection():
    return render_template('connection.html')
@app.route('/librarytutorial')
def library():
    return render_template('library.html')
@app.route('/testtutorial')
def testing():
    return render_template('testing.html')
@app.route('/wallettutorial')
def wallet():
    return render_template('wallet.html')

if __name__ == '__main__':
    # Threaded option to enable multiple instances for multiple user access support
    app.run(port=5000, use_reloader=False)

max_stars_repo_name: NicoSerranoP/HEsystem

max_issues_repo_name: NicoSerranoP/HEsystem

max_forks_repo_name: NicoSerranoP/HEsystem


text:
""" General all-purpose utilities """
import sys
import torch
import torch.nn.functional as F
import numpy as np
import gurobipy as gb
import matplotlib.pyplot as plt
import io
import contextlib
import tempfile
import time
import re
import pickle
import inspect
import glob
import os

COMPLETED_JOB_DIR = os.path.join(os.path.dirname(__file__), 'jobs', 'completed')
# ===============================================================================
# =           Helpful all-purpose functions                                     =
# ===============================================================================

class ParameterObject:
	def __init__(self, **kwargs):
		self.attr_list = []
		assert 'attr_list' not in kwargs
		for k,v in kwargs.items():
			setattr(self, k, v)
			self.attr_list.append(k)

	def change_attrs(self, **kwargs):
		new_kwargs = {}
		for attr in self.attr_list:
			if attr in kwargs:
				new_kwargs[attr] = kwargs[attr]
			else:
				new_kwargs[attr] = getattr(self, attr)
		return self.__class__(**new_kwargs)

class Factory(ParameterObject):
	def __init__(self, constructor, **kwargs):
		self.constructor = constructor
		super(Factory, self).__init__(**kwargs)

	def __call__(self, **kwargs):
		cons_args = inspect.getfullargspec(self.constructor).args
		# Make default args from attributes
		args = {k: getattr(self, k) for k in self.attr_list if k in cons_args}

		# Update the default args
		for k,v in kwargs.items():
			if k in cons_args:
				args[k] = v

		# Build object
		return self.constructor(**args)

	def __repr__(self):
		return '<Factory: %s>' % self.constructor.__self__.__name__

class DoEvery:

	@classmethod
	def dummy(cls, *args, **kwargs):
		pass

	def __init__(self, func, freq):
		""" Simple class that holds onto a function and it returns
			this function every freq iterations
		ARGS:
			func: function object to be returned every freq iterations
			freq: int - how often to return the function
		"""
		self.func = func
		self.freq = freq
		self.i = 0

	def __call__(self, *args, **kwargs):
		if self.i % self.freq == 0:
			returner = self.func
		else:
			returner = self.dummy
		self.i += 1
		return returner(*args, **kwargs)

class Timer:
	def __init__(self, start_on_init=True):
		if start_on_init:
			self.start()

	def start(self):
		self.start_time = time.time()

	def stop(self):
		self.stop_time = time.time()
		return self.stop_time - self.start_time

	def reset(self):
		self.start_time = self.stop_time = None

def cpufy(tensor_iter):
	""" Takes a list of tensors and safely pushes them back onto the cpu"""
	return [_.cpu() for _ in tensor_iter]

def cudafy(tensor_iter):
	""" Takes a list of tensors and safely converts all of them to cuda"""
	def safe_cuda(el):
		try:
			return el.cuda()
		except AssertionError:
			return el
	return [safe_cuda(_) for _ in tensor_iter]

def prod(num_iter):
	""" returns product of all elements in this iterator *'ed together"""
	cumprod = 1
	for el in num_iter:
		cumprod *= el
	return cumprod

def partition(n, m):
	""" Given ints n > m, partitions n into an iterable where all
		elements are m, except for the last one which is (n % m)
	"""
	count = 0
	while count < n:
		yield min([m, n - count])
		count += m

def flatten_list(lol):
	""" Given list of lists, flattens it into a single list. """

	output = []
	for el in lol:
		if not isinstance(el, list):
			output.append(el)
			continue
		output.extend(flatten_list(el))
	return output

def partition_by_suffix(iterable, func):
	""" Given an iterable and a boolean-valued function which takes in
		elements of that iterable, outputs a list of lists, where each list
		ends in an element for which the func returns true, (except for the
		last one)
		e.g.
		iterable := [1, 2, 3, 4, 5,5, 5]
		func := lambda x: (x % 2) == 0
		returns [[1,2], [3,4], [5, 5, 5]]
	"""
	output = []
	sublist = []
	for el in iterable:
		sublist.append(el)
		if func(el):
			output.append(sublist)
			sublist = []

	if len(sublist) > 0:
		output.append(sublist)
	return output

def arraylike(obj):
	return isinstance(obj, (torch.Tensor, np.ndarray))

def as_numpy(tensor_or_array):
    """ If given a tensor or numpy array returns that object cast numpy array
    """
    if isinstance(tensor_or_array, torch.Tensor):
        tensor_or_array = tensor_or_array.cpu().detach().numpy()
    return tensor_or_array

def two_col(l, r):
	""" Takes two numpy arrays of size N and makes a numpy array of size Nx2
	"""
	return np.vstack([l, r]).T

def split_pos_neg(x):
	if isinstance(x, torch.Tensor):
		return split_tensor_pos_neg(x)
	else:
		return split_ndarray_pos_neg(x)

def split_tensor_pos_neg(x):
	""" Splits a tensor into positive and negative components """
	pos = F.relu(x)
	neg = -F.relu(-x)
	return pos, neg

def split_ndarray_pos_neg(x):
	""" Splits a numpy ndarray into positive and negative components """
	pos = x * (x >= 0)
	neg = x * (x <= 0)
	return pos, neg

def swap_axes(x, source, dest):
	""" Swaps the dimensions of source <-> dest for torch/numpy
	ARGS:
		x : numpy array or tensor
		source : int index
		dest : int index
	RETURNS
		x' - object with same data as x, but with axes swapped
	"""
	if isinstance(x, torch.Tensor):
		return x.transpose(source, dest)
	else:
		return np.moveaxis(x, source, dest)

def build_var_namer(k):
	return lambda d: '%s[%s]' % (k, d)

@contextlib.contextmanager
def silent():
	save_stdout = sys.stdout
	temp = tempfile.TemporaryFile(mode='w')
	sys.stdout = temp
	yield
	sys.stdout = save_stdout
	temp.close()

def ia_mm(matrix, intervals, lohi_dim, matrix_or_vec='matrix'):
	""" Interval analysis matrix(-vec) multiplication for torch/np intervals

	ARGS:
		matrix : tensor or numpy array of shape (m,n) -
		intervals : tensor or numpy array with shape (n1, ..., 2, n_i, ...) -
				    "vector" of intervals to be multiplied by a matrix
				    one such n_i must be equal to n (from matrix shape)
		lohi_dim : int - which dimension (index) of intervals corresponds
				   		  to the lo/hi split
		matrix_or_vec : string - must be matrix or vec, corresponds to whether
						intervals is to be treated as a matrix or a vector.
						If a v
	RETURNS:
		object of same type as intervals, but with the shape slightly
		different: len(output[-1/-2]) == m
	"""

	# asserts for shapes and things
	assert isinstance(matrix, torch.Tensor) # TENSOR ONLY FOR NOW
	assert isinstance(intervals, torch.Tensor)
	m, n = matrix.shape
	assert intervals.shape[lohi_dim] == 2
	assert matrix_or_vec in ['matrix', 'vec']

	if matrix_or_vec == 'vec':
		intervals = intervals.unsqueeze(-1)

	assert lohi_dim != intervals.dim() - 2
	assert intervals[dim][-2] == n

	# define operators based on tensor/numpy case
	matmul = lambda m, x: m.matmul(x)
	stack = lambda a, b: torch.stack([a, b])

	# now do IA stuff
	intervals = swap_axes(intervals, 0, lohi_dim)
	matrix_pos, matrix_neg = split_pos_neg(matrix)
	los, his = intervals

	new_los = matmul(matrix_pos, los) + matmul(matrix_neg, his)
	new_his = matmul(matrix_pos, his) + matmul(matrix_neg, los)

	intervals = swap_axes(stack(new_los, new_his), 0, lohi_dim)
	if matrix_or_vec == 'vec':
		intervals = interval.squeeze(-1)
	return intervals

# =============================================================================
# =           Image display functions                                         =
# =============================================================================

def display_images(image_rows, figsize=(8, 8)):
	""" Given either a tensor/np.array (or list of same), will display each
		element in the row or tensor
	ARGS:
		image_rows: tensor or np.array or tensor[], np.array[] -
				    image or list of images to display
	RETURNS: None, but displays images
	"""

	if not isinstance(image_rows, list):
		image_rows = [image_rows]

	np_rows = [as_numpy(row) for row in image_rows]

	# Transpose channel to last dimension and stack to make rows
	np_rows = [np.concatenate(_.transpose([0, 2, 3, 1]), axis=1)
			   for _ in np_rows]

	# Now stack rows
	full_image = np.concatenate(np_rows, axis=0)

	# And then show image
	imshow_kwargs = {}
	if full_image.shape[-1] == 1:
		full_image = full_image.squeeze()
		imshow_kwargs['cmap'] = 'gray'

	fig = plt.figure(figsize=figsize)
	ax = fig.add_subplot()
	ax.axis('off')
	ax.imshow(full_image, **imshow_kwargs)
	plt.show()

# ======================================================
# =           Pytorch helpers                          =
# ======================================================

def seq_append(seq, module):
	""" Takes a nn.sequential and a nn.module and creates a nn.sequential
		with the module appended to it
	ARGS:
		seq: nn.Sequntial object
		module: <inherits nn.Module>
	RETURNS:
		nn.Sequential object
	"""
	seq_modules = [seq[_] for _ in range(len(seq))] + [module]
	return nn.Sequential(*seq_modules)

def cpufy(tensor_iter):
	""" Takes a list of tensors and safely pushes them back onto the cpu"""
	output = []
	for el in tensor_iter:
		if isinstance(el, tuple):
			output.append(tuple(_.cpu() for _ in el))
		else:
			output.append(el.cpu())
	return output

def cudafy(tensor_iter):
	""" Takes a list of tensors and safely converts all of them to cuda"""
	def safe_cuda(el):
		try:
			if isinstance(el, tuple):
				return tuple(_.cuda() for _ in el)
			else:
				return el.cuda()
		except AssertionError:
			return el
	return [safe_cuda(_) for _ in tensor_iter]

# =======================================
# =           Polytope class            =
# =======================================

class Polytope:
	INPUT_KEY = 'input'
	SLACK_KEY = 'slack'
	def __init__(self, A, b):
		""" Represents a polytope of the form {x | AX <= b}
		    (where everything is a numpy array)
		"""
		self.A = A
		self.b = b

	def _input_from_model(self, model):
		var_namer = build_var_namer(self.INPUT_KEY)
		return np.array([model.getVarByName(var_namer(i)).X
 						 for i in range(self.A.shape[1])])

	def _build_model(self, slack=False):
		""" Builds a gurobi model of this object """
		with silent():
			model = gb.Model()

		input_namer = build_var_namer(self.INPUT_KEY)
		input_vars = [model.addVar(lb=-gb.GRB.INFINITY, ub=gb.GRB.INFINITY,
								   name=input_namer(i))
		 		      for i in range(self.A.shape[1])]
		if slack == True:
			slack_var = model.addVar(lb=0, ub=1.0, name=self.SLACK_KEY)
		else:
			slack_var = 0

		for i, row in enumerate(self.A):
			model.addConstr(gb.LinExpr(row, input_vars) + slack_var <= self.b[i])
		model.update()
		return model

	def contains(self, x, tolerance=1e-6):
		return all(self.A @ x <= self.b + tolerance)

	def interior_point(self):
		model = self._build_model(slack=True)
		slack_var = model.getVarByName(self.SLACK_KEY)
		model.setObjective(slack_var, gb.GRB.MAXIMIZE)
		model.update()
		model.optimize()

		assert model.Status == 2
		return self._input_from_model(model)

	def intersects_hbox(self, hbox):
		""" If this intersects a given hyperbox, returns a
			point contained in both
		"""

		model = self._build_model(slack=True)
		input_namer = build_var_namer(self.INPUT_KEY)

		for i, (lb, ub) in enumerate(hbox):
			var = model.getVarByName(input_namer(i))
			model.addConstr(lb <= var <= ub)

		slack_var = model.getVarByName(self.SLACK_KEY)
		model.setObjective(slack_var, gb.GRB.MAXIMIZE)
		model.update()
		model.optimize()

		assert model.Status == 2
		return self._input_from_model(model)

# =========================================================
# =           experiment.Result object helpers            =
# =========================================================

def filename_to_epoch(filename):
	return int(re.search(r'_EPOCH\d{4}_', filename).group()[-5:-1])

def read_result_files(result_files):
	output = []
	for result_file in result_files:
		try:
			with open(result_file, 'rb') as f:
				output.append((result_file, pickle.load(f)))
		except Exception as err:
			print("Failed on file: ", result_file, err)
	return output

def job_out_series(job_outs, eval_style, method,
				   value_or_time='value', avg_stdev='avg'):
	""" Takes in some result or resultList objects and
		a 'method', and desired object, and returns these objects
		in a list
	ARGS:
		results: Result[] or ResultList[], results to consider
		eval_style: str - which method of Experiment we look at
		method: str - which Lipschitz-estimation technique to consider
		value_or_time: 'value' or 'time' - which number to return
		avg_stdev: 'avg' or 'stdev' - for ResultList[], we can
				   get average or stdev values
	RETURNS:
		list of floats
	"""
	# check everything is the same type
	assert value_or_time in ['value', 'time']
	assert avg_stdev in ['avg', 'stdev']
	assert eval_style in ['do_random_evals', 'do_unit_hypercube_eval',
						      'do_data_evals', 'do_large_radius_evals']

	results = [job_out[eval_style] for job_out in job_outs]
	output = []
	for result in results:
		try: #Result object case
			if value_or_time == 'value':
				output.append(result.values(method))
			else:
				output.append(result.compute_times(method))
		except:
			triple = result.average_stdevs(value_or_time)[method]
			if avg_stdev == 'avg':
				output.append(triple[0])
			else:
				output.append(triple[1])
	return output

def collect_result_outs(filematch):
	""" Uses glob to collect and load result objects matching a series
	ARGS:
		filematch: string with *'s associated with it
				   e.g. 'NAME*SUBNAME*GLOBAL.result'
	RESULTS:
		list of (filename, experiment.Result) objects
	"""
	search_str = os.path.join(COMPLETED_JOB_DIR, filematch)
	sorted_filenames = sorted(glob.glob(search_str))
	return read_result_files(sorted_filenames)

def collect_epochs(filename_list):
	""" Given a list of (filename) objects, converts
		the filenames into integers, pulling the EPOCH attribute from
		the filename
	str[] -> int[]
	"""
	def epoch_gleamer(filename):
		basename = os.path.basename(filename)
		return int(re.search('_EPOCH\d+_', filename).group()[6:-1])
	return [epoch_gleamer(_) for _ in filename_list]

def data_from_results(result_iter, method, lip_estimator, time_or_value='value',
					  avg_or_stdev='avg'):
	""" Given a list of experiment.Result or experiment.ResultList objects
		will return the time/value for the lip_estimator of the method
		for result (or avg/stdev if resultList objects)
		e.g., data_from_results('do_unit_hypercube_eval', 'LipMIP',
								 'value') gets a list of values of the
								 LipMIP over the unitHypercube domain
	ARGS:
		method: str - name of one of the experimental methods
		lip_estimator : str - name of the class of lipschitz estimator to use
		time_or_value : 'time' or 'value' - returning the time or value here
		avg_or_stdev  : 'avg' or 'stdev' - returning either avg or stdev of
						results from ResultListObjects
	"""
	assert method in ['do_random_evals', 'do_data_evals',
					  'do_unit_hypercube_eval']
	assert lip_estimator in ['LipMIP', 'FastLip', 'LipLP', 'CLEVER',
							 'LipSDP', 'NaiveUB', 'RandomLB', 'SeqLip']
	assert time_or_value in ['time', 'value']
	assert avg_or_stdev in ['avg', 'stdev']

	def datum_getter(result_obj):
		if not hasattr(result_obj, 'average_stdevs'):
			if time_or_value == 'value':
				return result_obj[method].values(lip_estimator)
			else:
				return result_obj[method].compute_times(lip_estimator)
		else:
			triple = result_obj.average_stdevs(time_or_value)
			if avg_or_stdev == 'avg':
				return triple[0]
			else:
				return triple[1]

	return [datum_getter(_) for _ in result_iter]

max_stars_repo_name: gandhiy/lipMIP

max_issues_repo_name: gandhiy/lipMIP

max_forks_repo_name: gandhiy/lipMIP


text:
import tensorflow as tf
import numpy as np

a = 3
b = 2

x = tf.placeholder(tf.float32, shape=(a, b))
y = tf.add(x, x)

data = np.random.rand(a, b)

sess = tf.Session()

print(sess.run(y,feed_dict={x: data}))


text:
from scipy import signal
from scipy import misc
import numpy as np
from skimage import io

image_mask = io.imread(r'C:\Users\bunny\Desktop\mask_kalasatama_ss.png')[..., 0]
image_depth = io.imread(r'C:\Users\bunny\Desktop\depth_s.png')[..., 0]
corr = signal.correlate2d(image_mask, image_depth, boundary='fill', mode='same')
y, x = np.unravel_index(np.argmax(corr), corr.shape)
print(y, x)

max_stars_repo_name: juyingnan/siie

max_issues_repo_name: juyingnan/siie

max_forks_repo_name: juyingnan/siie


text:
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: RainbowSecret
## Microsoft Research
## yuyua@microsoft.com
## Copyright (c) 2019
##
## This source code is licensed under the MIT-style license found in the
## LICENSE file in the root directory of this source tree
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import os
import sys
import cv2
import pdb
import numpy as np
import scipy.io as sio

def get_autonue21_colors():
    """
    https://github.com/AutoNUE/public-code/blob/master/helpers/anue_labels.py
    """
    num_cls = 26
    colors = [0] * (num_cls * 3)
    colors[0:3] = (128, 64, 128)
    colors[3:6] = (250, 170, 160)
    colors[6:9] = (244, 35, 232)
    colors[9:12] = (230, 150, 140)
    colors[12:15] = (220, 20, 60)
    colors[15:18] = (255, 0, 0)
    colors[18:21] = (0, 0, 230)
    colors[21:24] = (119, 11, 32)
    colors[24:27] = (255, 204, 54)
    colors[27:30] = (0, 0, 142)
    colors[30:33] = (0, 0, 70)
    colors[33:36] = (0, 60, 100)
    colors[36:39] = (0, 0, 90)
    colors[39:42] = (220, 190, 40)
    colors[42:45] = (102, 102, 156)
    colors[45:48] = (190, 153, 153)
    colors[48:51] = (190, 153, 153)
    colors[51:54] = (180, 165, 180)
    colors[54:57] = (174, 64, 67)
    colors[57:60] = (220, 220, 0)
    colors[60:63] = (250, 170, 30)
    colors[63:66] = (153, 153, 153)
    colors[66:69] = (169, 187, 214)
    colors[69:72] = (70, 70, 70)
    colors[72:75] = (150, 100, 100)
    colors[75:78] = (107, 142, 35)
    colors[78:81] = (70, 130, 180)
    return colors

# Sky = [128,128,128]
# 	Building = [128,0,0]
# 	Pole = [192,192,128]
# 	Road = [128,64,128]
# 	Pavement = [60,40,222]
# 	Tree = [128,128,0]
# 	SignSymbol = [192,128,128]
# 	Fence = [64,64,128]
# 	Car = [64,0,128]
# 	Pedestrian = [64,64,0]
# 	Bicyclist = [0,128,192]
# 	Unlabelled = [0,0,0]
def get_camvid_colors():
    """ Returns the color map for visualizing the segmentation mask.
    Args:
        num_cls: Number of classes
    Returns:
        The color map
    """
    num_cls = 12
    colors = [0] * (num_cls * 3)
    colors[0:3] = (128, 128, 128)
    colors[3:6] = (128, 0, 0)
    colors[6:9] = (192, 192, 128)
    colors[9:12] = (128, 64, 128)
    colors[12:15] = (60, 40, 222)
    colors[15:18] = (128, 128, 0)
    colors[18:21] = (192, 128, 128)
    colors[21:24] = (64, 64, 128)
    colors[24:27] = (64, 0, 128)
    colors[27:30] = (64, 64, 0)
    colors[30:33] = (0, 128, 192)
    colors[33:36] = (0, 0, 0)
    return colors

def get_cityscapes_colors():
    """ Returns the color map for visualizing the segmentation mask.
    Args:
        num_cls: Number of classes
    Returns:
        The color map
    """
    num_cls = 20
    colors = [0] * (num_cls * 3)
    colors[0:3] = (128, 64, 128)  # 0: 'road'
    colors[3:6] = (244, 35, 232)  # 1 'sidewalk'
    colors[6:9] = (70, 70, 70)  # 2''building'
    colors[9:12] = (102, 102, 156)  # 3 wall
    colors[12:15] = (190, 153, 153)  # 4 fence
    colors[15:18] = (153, 153, 153)  # 5 pole
    colors[18:21] = (250, 170, 30)  # 6 'traffic light'
    colors[21:24] = (220, 220, 0)  # 7 'traffic sign'
    colors[24:27] = (107, 142, 35)  # 8 'vegetation'
    colors[27:30] = (152, 251, 152)  # 9 'terrain'
    colors[30:33] = (70, 130, 180)  # 10 sky
    colors[33:36] = (220, 20, 60)  # 11 person
    colors[36:39] = (255, 0, 0)  # 12 rider
    colors[39:42] = (0, 0, 142)  # 13 car
    colors[42:45] = (0, 0, 70)  # 14 truck
    colors[45:48] = (0, 60, 100)  # 15 bus
    colors[48:51] = (0, 80, 100)  # 16 train
    colors[51:54] = (0, 0, 230)  # 17 'motorcycle'
    colors[54:57] = (119, 11, 32)  # 18 'bicycle'
    colors[57:60] = (105, 105, 105)
    return colors

def get_ade_colors():
    colors = sio.loadmat(os.path.dirname(os.path.abspath(__file__)) + '/color150.mat')['colors']
    colors = colors[:, ::-1, ]
    colors = np.array(colors).astype(int).tolist()
    colors.insert(0, [0, 0, 0])
    colors = sum(colors, [])
    return colors

def get_pascal_context_colors():
    colors = sio.loadmat(os.path.dirname(os.path.abspath(__file__)) + '/color60.mat')['color60']
    colors = colors[:, ::-1, ]
    colors = np.array(colors).astype(int).tolist()
    colors = sum(colors, [])
    return colors

def get_lip_colors():
    """ Returns the color map for visualizing the segmentation mask.
    Args:
        num_cls: Number of classes
    Returns:
        The color map
    """
    n = 20
    colors = [0] * (n * 3)
    for j in range(0, n):
        lab = j
        colors[j * 3 + 0] = 0
        colors[j * 3 + 1] = 0
        colors[j * 3 + 2] = 0
        i = 0
        while lab:
            colors[j * 3 + 0] |= (((lab >> 0) & 1) << (7 - i))
            colors[j * 3 + 1] |= (((lab >> 1) & 1) << (7 - i))
            colors[j * 3 + 2] |= (((lab >> 2) & 1) << (7 - i))
            i += 1
            lab >>= 3
    return colors

def get_cocostuff_colors():
    """ Returns the color map for visualizing the segmentation mask.
    Args:
        num_cls: Number of classes
    Returns:
        The color map
    """
    n = 171
    colors = [0] * (n * 3)
    for j in range(0, n):
        lab = j
        colors[j * 3 + 0] = 0
        colors[j * 3 + 1] = 0
        colors[j * 3 + 2] = 0
        i = 0
        while lab:
            colors[j * 3 + 0] |= (((lab >> 0) & 1) << (7 - i))
            colors[j * 3 + 1] |= (((lab >> 1) & 1) << (7 - i))
            colors[j * 3 + 2] |= (((lab >> 2) & 1) << (7 - i))
            i += 1
            lab >>= 3
    return colors

def get_pascal_voc_colors():
    """Load the mapping that associates pascal classes with label colors
        Returns:
            np.ndarray with dimensions (21, 3)
        """
    return np.asarray(
        [
            [0, 0, 0],
            [128, 0, 0],
            [0, 128, 0],
            [128, 128, 0],
            [0, 0, 128],
            [128, 0, 128],
            [0, 128, 128],
            [128, 128, 128],
            [64, 0, 0],
            [192, 0, 0],
            [64, 128, 0],
            [192, 128, 0],
            [64, 0, 128],
            [192, 0, 128],
            [64, 128, 128],
            [192, 128, 128],
            [0, 64, 0],
            [128, 64, 0],
            [0, 192, 0],
            [128, 192, 0],
            [0, 64, 128],
        ]
    )

max_stars_repo_name: littleSunlxy/contrastive-seg-lin

max_issues_repo_name: littleSunlxy/contrastive-seg-lin

max_forks_repo_name: littleSunlxy/contrastive-seg-lin

reason: import numpy,import scipy


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

# Dependency imports
import numpy as np

import tensorflow.compat.v2 as tf

from tensorflow_probability.python import math as tfp_math
from tensorflow_probability.python.bijectors import identity as identity_bijector
from tensorflow_probability.python.bijectors import softplus as softplus_bijector
from tensorflow_probability.python.distributions import distribution
from tensorflow_probability.python.internal import assert_util
from tensorflow_probability.python.internal import custom_gradient as tfp_custom_gradient
from tensorflow_probability.python.internal import dtype_util
from tensorflow_probability.python.internal import parameter_properties
from tensorflow_probability.python.internal import prefer_static as ps
from tensorflow_probability.python.internal import reparameterization
from tensorflow_probability.python.internal import samplers
from tensorflow_probability.python.internal import special_math
from tensorflow_probability.python.internal import tensor_util
from tensorflow_probability.python.math.numeric import log1psquare

__all__ = [
    'TwoPieceNormal',
]

NUMPY_MODE = False

class TwoPieceNormal(distribution.AutoCompositeTensorDistribution):
  """The Two-Piece Normal distribution.

  The Two-Piece Normal generalizes the Normal distribution with an additional
  shape parameter. Under the general formulation proposed by [Fernández and
  Steel (1998)][2], it is parameterized by location `loc`, scale `scale`, and
  shape `skewness`. If `skewness` is above one, the distribution becomes right-
  skewed (or positively skewed). If `skewness` is greater than zero and less
  than one, the distribution becomes left-skewed (or negatively skewed). The
  Normal distribution is retrieved when `skewness` is equal to one.

  This distribution is also called the Fernández-Steel Skew Normal distribution
  [(Castillo et al., 2011)][1], the Skew Normal Type 2 distribution [(Rigby et
  al., 2019, Section 18.3.5, p380)][3], and the [Split Normal distribution][4].
  The Fernández and Steel's formulation is mathematically equivalent to the main
  parameterization discussed in the last reference.

  #### Mathematical details

  The probability density function (pdf) is,

  ```none
  pdf(x; loc, scale, skewness) =
      k * normal_pdf(y * skewness; 0, 1) when x < loc, and
      k * normal_pdf(y / skewness; 0, 1) when x >= loc
  where
      k = (2 * skewness) / ((1 + skewness**2) * scale)
      y = (x - loc) / scale
  ```

  where `loc` is the location, `scale` is the scale, `skewness` is the shape
  parameter, and `normal_pdf(x; 0, 1)` is the pdf of the Normal distribution
  with zero mean and unit variance.

  The cumulative distribution function (cdf) is,

  ```none
  cdf(x; loc, scale, skewness) =
      k0 * normal_cdf(y * skewness; 0, 1) when x < loc, and
      k1 + k2 * normal_cdf(y / skewness; 0, 1) when x >= loc
  where
      k0 = 2 / (1 + skewness**2)
      k1 = (1 - skewness**2) / (1 + skewness**2)
      k2 = (2 * skewness**2) / (1 + skewness**2)
      y = (x - loc) / scale
  ```

  where `normal_cdf(x; 0, 1)` is the cdf of the Normal distribution with zero
  mean and unit variance.

  The quantile function (inverse cdf) is,

  ```none
  quantile(p; loc, scale, skewness) =
      loc + s0 * normal_quantile(x0) when p <= 1 / (1 + skewness**2), and
      loc + s1 * normal_quantile(x1) when p > 1 / (1 + skewness**2)
  where
      s0 = scale / skewness
      s1 = scale * skewness
      x0 = (p * (1 + skewness**2)) / 2
      x1 = (p * (1 + skewness**2) - 1 + skewness**2) / (2 * skewness**2)
      y = (x - loc) / scale
  ```

  where `normal_quantile(x; 0, 1)` is the quantile function of the Normal
  distribution with zero mean and unit variance.

  The mean and variance are, respectively,

  ```none
  mean(loc, scale, skewness) = loc + scale * E(Y)
  variance(loc, scale, skewness) = scale**2 * (
      skewness**2 + 1 / skewness**2 - 1 - E(Y)**2)
  where
      E(Y) = sqrt(2) / sqrt(pi) * (skewness - 1 / skewness)
  ```

  The Two-Piece Normal distribution is a member of the [location-scale family](
  https://en.wikipedia.org/wiki/Location-scale_family): it can be constructed
  as,

  ```none
  Z ~ Normal(loc=0, scale=1)
  W ~ Bernoulli(probs=1 / (1 + skewness**2))
  Y = (1 - W) * |Z| * skewness - W * |Z| / skewness
  X = loc + scale * Y
  ```

  #### Examples

  Example of initialization of one distribution.

  ```python
  import tensorflow_probability as tfp
  tfd = tfp.distributions

  # Define a single scalar Two-Piece Normal distribution.
  dist = tfd.TwoPieceNormal(loc=3., scale=10., skewness=0.75)

  # Evaluate the cdf at 1, returning a scalar.
  dist.cdf(1.)
  ```

  Example of initialization of a batch of distributions. Arguments are
  broadcast when possible.

  ```python
  # Define a batch of three scalar valued Two-Piece Normals.
  # They have mean 3, scale 10, but different skewnesses.
  dist = tfd.TwoPieceNormal(loc=3., scale=10., skewness=[0.75, 1., 1.33])

  # Get 2 samples, returning a 2 x 3 tensor.
  value = dist.sample(2)

  # Evaluate the pdf of the distributions on the same points, value,
  # returning a 2 x 3 tensor.
  dist.prob(value)
  ```

  #### References

  [1]: Nabor O. Castillo et al. On the Fernández-Steel distribution: Inference
       and application. _Computational Statistics & Data Analysis_, 55(11),
       2951-2961, 2011.

  [2]: Carmen Fernández and Mark F. J. Steel. On Bayesian modeling of fat tails
       and skewness. _Journal of the American Statistical Association_, 93(441),
       359-371, 1998.

  [3]: Robert A. Rigby et al. _Distributions for modeling location, scale, and
       shape: Using GAMLSS in R_. Chapman and Hall/CRC, 2019.

  [4]: https://en.wikipedia.org/wiki/Split_normal_distribution

  """

  def __init__(self,
               loc,
               scale,
               skewness,
               validate_args=False,
               allow_nan_stats=True,
               name='TwoPieceNormal'):
    """Construct Two-Piece Normal distributions.

    The Two-Piece Normal is parametrized with location `loc`, scale `scale`,
    and shape parameter `skewness`. The parameters must be shaped in a way that
    supports broadcasting (e.g. `loc + scale` is a valid operation).

    Args:
      loc: Floating point tensor; the location(s) of the distribution(s).
      scale: Floating point tensor; the scale(s) of the distribution(s). Must
        contain only positive values.
      skewness: Floating point tensor; the skewness(es) of the distribution(s).
        Must contain only positive values.
      validate_args: Python `bool`, default `False`. When `True`, distribution
        parameters are checked for validity despite possibly degrading runtime
        performance. When `False`, invalid inputs may silently render incorrect
        outputs.
      allow_nan_stats: Python `bool`, default `True`. When `True`, statistics
        (e.g., mean, mode, variance) use the value "`NaN`" to indicate the
        result is undefined. When `False`, an exception is raised if one or
        more of the statistic's batch members are undefined.
      name: Python `str` name prefixed to Ops created by this class.

    Raises:
      TypeError: if `loc`, `scale`, and `skewness` have different `dtype`.
    """
    parameters = dict(locals())
    with tf.name_scope(name) as name:
      dtype = dtype_util.common_dtype(
          [loc, scale, skewness], dtype_hint=tf.float32)
      self._loc = tensor_util.convert_nonref_to_tensor(
          loc, dtype=dtype, name='loc')
      self._scale = tensor_util.convert_nonref_to_tensor(
          scale, dtype=dtype, name='scale')
      self._skewness = tensor_util.convert_nonref_to_tensor(
          skewness, dtype=dtype, name='skewness')
      super().__init__(
          dtype=dtype,
          reparameterization_type=reparameterization.FULLY_REPARAMETERIZED,
          validate_args=validate_args,
          allow_nan_stats=allow_nan_stats,
          parameters=parameters,
          name=name)

  @classmethod
  def _parameter_properties(cls, dtype, num_classes=None):
    return dict(
        loc=parameter_properties.ParameterProperties(),
        scale=parameter_properties.ParameterProperties(
            default_constraining_bijector_fn=(
                lambda: softplus_bijector.Softplus(low=dtype_util.eps(dtype)))),
        skewness=parameter_properties.ParameterProperties(
            default_constraining_bijector_fn=(
                lambda: softplus_bijector.Softplus(low=dtype_util.eps(dtype)))))

  @property
  def loc(self):
    """Distribution parameter for the location."""
    return self._loc

  @property
  def scale(self):
    """Distribution parameter for the scale."""
    return self._scale

  @property
  def skewness(self):
    """Distribution parameter for the skewness."""
    return self._skewness

  def _event_shape_tensor(self):
    return tf.constant([], dtype=tf.int32)

  def _event_shape(self):
    return tf.TensorShape([])

  def _sample_n(self, n, seed=None):
    loc = tf.convert_to_tensor(self.loc)
    scale = tf.convert_to_tensor(self.scale)
    skewness = tf.convert_to_tensor(self.skewness)

    batch_shape = self._batch_shape_tensor(
        loc=loc, scale=scale, skewness=skewness)
    sample_shape = ps.concat([[n], batch_shape], axis=0)

    samples = random_two_piece_normal(
        sample_shape, skewness=skewness, seed=seed)

    return loc + scale * samples

  def _log_prob(self, value):
    value = tf.convert_to_tensor(value, dtype_hint=self.dtype)
    loc = tf.convert_to_tensor(self.loc)
    scale = tf.convert_to_tensor(self.scale)
    skewness = tf.convert_to_tensor(self.skewness)

    half = tf.constant(0.5, dtype=self.dtype)
    two = tf.constant(2., dtype=self.dtype)
    pi = tf.constant(np.pi, dtype=self.dtype)

    z = standardize(value, loc=loc, scale=scale, skewness=skewness)

    log_unnormalized = -half * tf.math.square(z)
    log_normalization = (
        _numpy_cast(log1psquare(skewness), loc.dtype) -
        tf.math.log(two * skewness) +
        tf.math.log(scale) +
        half * tf.math.log(two * pi))

    return log_unnormalized - log_normalization

  def _cdf(self, value):
    value = tf.convert_to_tensor(value, dtype_hint=self.dtype)
    loc = tf.convert_to_tensor(self.loc)
    scale = tf.convert_to_tensor(self.scale)
    skewness = tf.convert_to_tensor(self.skewness)

    return cdf(value, loc=loc, scale=scale, skewness=skewness)

  def _survival_function(self, value):
    value = tf.convert_to_tensor(value, dtype_hint=self.dtype)
    loc = tf.convert_to_tensor(self.loc)
    scale = tf.convert_to_tensor(self.scale)
    skewness = tf.convert_to_tensor(self.skewness)

    # Here we use the following property of this distribution:
    # sf = 1. - cdf(value; loc, scale, skewness)
    #    = cdf(-value; -loc, scale, 1. / skewness)
    return cdf(-value, loc=-loc, scale=scale,
               skewness=tf.math.reciprocal(skewness))

  def _quantile(self, value):
    value = tf.convert_to_tensor(value, dtype_hint=self.dtype)
    loc = tf.convert_to_tensor(self.loc)
    scale = tf.convert_to_tensor(self.scale)
    skewness = tf.convert_to_tensor(self.skewness)

    return quantile(value, loc=loc, scale=scale, skewness=skewness)

  def _mean(self):
    loc = tf.convert_to_tensor(self.loc)
    scale = tf.convert_to_tensor(self.scale)
    skewness = tf.convert_to_tensor(self.skewness)

    two = tf.constant(2., dtype=self.dtype)
    pi = tf.constant(np.pi, dtype=self.dtype)

    m = tf.math.sqrt(two / pi) * (skewness - tf.math.reciprocal(skewness))
    mean = loc + scale * m

    batch_shape = self._batch_shape_tensor(
        loc=loc, scale=scale, skewness=skewness)

    return tf.broadcast_to(mean, shape=batch_shape)

  def _variance(self):
    scale = tf.convert_to_tensor(self.scale)
    skewness = tf.convert_to_tensor(self.skewness)

    one = tf.constant(1., dtype=self.dtype)
    two = tf.constant(2., dtype=self.dtype)
    pi = tf.constant(np.pi, dtype=self.dtype)

    m = tf.math.sqrt(two / pi) * (skewness - tf.math.reciprocal(skewness))
    squared_skewness = tf.math.square(skewness)
    v = (squared_skewness + tf.math.reciprocal(squared_skewness) - one -
         tf.math.square(m))
    variance = tf.square(scale) * v

    batch_shape = self._batch_shape_tensor(scale=scale, skewness=skewness)

    return tf.broadcast_to(variance, shape=batch_shape)

  def _mode(self):
    loc = tf.convert_to_tensor(self.loc)
    return tf.broadcast_to(loc, shape=self._batch_shape_tensor(loc=loc))

  def _default_event_space_bijector(self):
    return identity_bijector.Identity(validate_args=self.validate_args)

  def _parameter_control_dependencies(self, is_init):
    assertions = []
    if is_init:
      # _batch_shape() will raise error if it can statically prove that `loc`,
      # `scale`, and `skewness` have incompatible shapes.
      try:
        self._batch_shape()
      except ValueError as e:
        raise ValueError('Arguments `loc`, `scale` and `skewness` '
                         'must have compatible shapes; '
                         f'loc.shape={self.loc.shape}, '
                         f'scale.shape={self.scale.shape}, '
                         f'skewness.shape={self.skewness.shape}.') from e
      # We don't bother checking the shapes in the dynamic case because
      # all member functions access the three arguments anyway.

    if not self.validate_args:
      assert not assertions  # Should never happen.
      return []

    if is_init != tensor_util.is_ref(self.scale):
      assertions.append(
          assert_util.assert_positive(
              self.scale, message='Argument `scale` must be positive.'))
    if is_init != tensor_util.is_ref(self.skewness):
      assertions.append(
          assert_util.assert_positive(
              self.skewness, message='Argument `skewness` must be positive.'))

    return assertions

def _numpy_cast(x, dtype):
  # TODO(b/223684173): Many special math routines don't respect the input dtype.
  if NUMPY_MODE:
    return tf.cast(x, dtype)
  else:
    return x

def standardize(value, loc, scale, skewness):
  """Apply mean-variance-skewness standardization to input `value`.

  Note that scale and skewness can be negative.

  Args:
    value: Floating-point tensor; the value(s) to be standardized.
    loc: Floating-point tensor; the location(s) of the distribution(s).
    scale: Floating-point tensor; the scale(s) of the distribution(s).
    skewness: Floating-point tensor; the skewness(es) of the distribution(s).

  Returns:
    A tensor with shape broadcast according to the arguments.
  """
  value = tf.convert_to_tensor(value)
  loc = tf.convert_to_tensor(loc)
  scale = tf.convert_to_tensor(scale)
  skewness = tf.convert_to_tensor(skewness)

  return (value - loc) / tf.math.abs(scale) * tf.math.abs(
      tf.where(value < loc, skewness, tf.math.reciprocal(skewness)))

def cdf(value, loc, scale, skewness):
  """Compute cumulative distribution function of Two-Piece Normal distribution.

  Note that scale and skewness can be negative.

  Args:
    value: Floating-point tensor; where to compute the cdf.
    loc: Floating-point tensor; the location(s) of the distribution(s).
    scale: Floating-point tensor; the scale(s) of the distribution(s).
    skewness: Floating-point tensor; the skewness(es) of the distribution(s).

  Returns:
    A tensor with shape broadcast according to the arguments.
  """
  value = tf.convert_to_tensor(value)
  loc = tf.convert_to_tensor(loc)
  scale = tf.convert_to_tensor(scale)
  skewness = tf.convert_to_tensor(skewness)
  dtype = value.dtype

  one = tf.constant(1., dtype=dtype)
  two = tf.constant(2., dtype=dtype)

  z = standardize(value, loc=loc, scale=scale, skewness=skewness)
  normal_cdf = _numpy_cast(special_math.ndtr(z), dtype)

  squared_skewness = tf.math.square(skewness)
  return tf.math.reciprocal(one + squared_skewness) * tf.where(
      z < 0.,
      two * normal_cdf,
      one - squared_skewness + two * squared_skewness * normal_cdf)

def quantile(value, loc, scale, skewness):
  """Compute quantile function (inverse cdf) of Two-Piece Normal distribution.

  Note that scale and skewness can be negative.

  Args:
    value: Floating-point tensor; where to compute the quantile function.
    loc: Floating-point tensor; the location(s) of the distribution(s).
    scale: Floating-point tensor; the scale(s) of the distribution(s).
    skewness: Floating-point tensor; the skewness(es) of the distribution(s).

  Returns:
    A tensor with shape broadcast according to the arguments.
  """
  value = tf.convert_to_tensor(value)
  loc = tf.convert_to_tensor(loc)
  scale = tf.convert_to_tensor(scale)
  skewness = tf.convert_to_tensor(skewness)
  dtype = value.dtype

  half = tf.constant(0.5, dtype=dtype)
  one = tf.constant(1., dtype=dtype)
  two = tf.constant(2., dtype=dtype)

  squared_skewness = tf.math.square(skewness)
  cond = value < tf.math.reciprocal(one + squared_skewness)

  # Here we use the following fact:
  # X ~ Normal(loc=0, scale=1) => 2 * X**2 ~ Gamma(alpha=0.5, beta=1)
  probs = (one - value * (one + squared_skewness)) * tf.where(
      cond, one, -tf.math.reciprocal(squared_skewness))
  gamma_quantile = _numpy_cast(tfp_math.igammainv(half, p=probs), dtype)

  abs_skewness = tf.math.abs(skewness)
  adj_scale = tf.math.abs(scale) * tf.where(
      cond, -tf.math.reciprocal(abs_skewness), abs_skewness)

  return loc + adj_scale * tf.math.sqrt(two * gamma_quantile)

def _two_piece_normal_sample_no_gradient(shape, skewness, seed):
  """Generate samples from Two-Piece Normal distribution.

  The distribution is the Two-Piece Normal distribution with location zero,
  scale one, and skewness `skewness`. To change the location and scale, use:

  ```none
  loc + scale * samples
  ```

  Args:
    shape: 0D or 1D int32 Tensor. Shape of the generated samples.
    skewness: Floating-point tensor; the skewness(es) of the distribution(s).
    seed: PRNG seed; see `tfp.random.sanitize_seed` for details.

  Returns:
    A tensor with prepended dimensions `shape`.
  """
  uniform_seed, normal_seed = samplers.split_seed(
      seed, salt='two_piece_normal_split')
  uniform_samples = samplers.uniform(
      shape, maxval=1., dtype=skewness.dtype, seed=uniform_seed)
  normal_samples = samplers.normal(
      shape, dtype=skewness.dtype, seed=normal_seed)

  return tf.abs(normal_samples) * tf.where(
      uniform_samples < tf.math.reciprocal(1. + skewness**2),
      -tf.math.reciprocal(skewness),
      skewness)

def _two_piece_normal_sample_gradient(skewness, samples):
  """Compute the gradients of Two-Piece Normal samples w.r.t. skewness.

  This function computes the implicit reparameterization gradients [1]:

  ```none
  dz / dskewness = -(dF(z; skewness) / dskewness) / p(z; skewness)
  ```

  where `F(z; skewness)` and `p(z; skewness)` are the cdf and the pdf of the
  Two-Piece Normal distribution with location zero, scale one, and skewness
  `skewness`.

  Args:
    skewness: Floating-point tensor; the skewness(es) of the distribution(s).
    samples: Floating-point tensor; the samples of the distribution(s).

  Returns:
    A tensor with shape broadcast according to the arguments.

  Reference:
    [1]: Michael Figurnov, Shakir Mohamed, and Andriy Mnih.
         Implicit Reparameterization Gradients. In _Advances in Neural
         Information Processing Systems_, 31, 2018.
         https://arxiv.org/abs/1805.08498
  """
  one = tf.constant(1., dtype=skewness.dtype)
  two = tf.constant(2., dtype=skewness.dtype)
  sqrt_2 = tf.constant(np.sqrt(2.), dtype=skewness.dtype)
  sqrt_pi_over_two = tf.constant(np.sqrt(np.pi / 2.), dtype=skewness.dtype)

  left_piece = samples < 0.
  z = samples * tf.where(left_piece, skewness, tf.math.reciprocal(skewness))

  scale = tf.math.reciprocal(one + tf.math.square(skewness))

  safe_left_z = tf.where(left_piece, z, -tf.ones_like(z))
  safe_right_z = tf.where(left_piece, tf.ones_like(z), z)

  grad_left_piece = (
      -samples / skewness + scale * two * sqrt_pi_over_two *
      tfp_math.erfcx(-safe_left_z / sqrt_2))
  grad_right_piece = (
      samples / skewness + scale * two * sqrt_pi_over_two *
      tfp_math.erfcx(safe_right_z / sqrt_2))

  return tf.where(left_piece, grad_left_piece, grad_right_piece)

def _two_piece_normal_sample_fwd(shape, skewness, seed):
  """Compute output, aux (collaborates with _two_piece_normal_sample_bwd)."""
  samples = _two_piece_normal_sample_no_gradient(shape, skewness, seed)
  return samples, (skewness, samples)

def _two_piece_normal_sample_bwd(_, aux, dy):
  """The gradients of Two Piece Normal samples w.r.t. skewness."""
  skewness, samples = aux
  broadcast_skewness = tf.broadcast_to(skewness, ps.shape(samples))

  grad = dy * _two_piece_normal_sample_gradient(broadcast_skewness, samples)
  # Sum over the sample dimensions. Assume that they are always the first
  # ones.
  num_sample_dimensions = (ps.rank(broadcast_skewness) -
                           ps.rank(skewness))

  # None gradients for seed
  return tf.reduce_sum(grad, axis=ps.range(num_sample_dimensions)), None

def _two_piece_normal_sample_jvp(shape, primals, tangents):
  """Compute primals and tangents using implicit derivative."""
  skewness, seed = primals
  dskewness, dseed = tangents
  del dseed

  broadcast_skewness = tf.broadcast_to(skewness, shape)
  broadcast_dskewness = tf.broadcast_to(dskewness, shape)

  samples = _two_piece_normal_sample_no_gradient(shape, skewness, seed)
  dsamples = broadcast_dskewness * _two_piece_normal_sample_gradient(
      broadcast_skewness, samples)

  return samples, dsamples

@tfp_custom_gradient.custom_gradient(
    vjp_fwd=_two_piece_normal_sample_fwd,
    vjp_bwd=_two_piece_normal_sample_bwd,
    jvp_fn=_two_piece_normal_sample_jvp,
    nondiff_argnums=(0,))
def _two_piece_normal_sample_with_gradient(shape, skewness, seed):
  """Generate samples from Two-Piece Normal distribution.

  The distribution is the Two-Piece Normal distribution with location zero,
  scale one, and skewness `skewness`. To change the location and scale, use:

  ```none
  loc + scale * samples
  ```

  The samples are pathwise differentiable using the approach of [1].

  Args:
    shape: 0D or 1D int32 Tensor. Shape of the generated samples.
    skewness: Floating-point tensor; the skewness(es) of the distribution(s).
    seed: PRNG seed; see `tfp.random.sanitize_seed` for details.

  Returns:
    A tensor with prepended dimensions `shape`.

  References:
    [1]: Michael Figurnov, Shakir Mohamed, and Andriy Mnih.
         Implicit Reparameterization Gradients. In _Advances in Neural
         Information Processing Systems_, 31, 2018.
         https://arxiv.org/abs/1805.08498
  """
  return _two_piece_normal_sample_no_gradient(shape, skewness, seed)

def random_two_piece_normal(shape, skewness, seed=None):
  """Generate samples from Two-Piece Normal distribution.

  The distribution is the Two-Piece Normal distribution with location zero,
  scale one, and skewness `skewness`. To change the location and scale, use:

  ```none
  loc + scale * samples
  ```

  The samples are pathwise differentiable using the approach of [1].

  Note that skewness can be negative.

  Args:
    shape: The output sample shape.
    skewness: The skewness(es) of the distribution(s).
    seed: PRNG seed; see `tfp.random.sanitize_seed` for details.

  Returns:
    A tensor with prepended dimensions `shape`.

  References:
    [1]: Michael Figurnov, Shakir Mohamed, and Andriy Mnih.
         Implicit Reparameterization Gradients. In _Advances in Neural
         Information Processing Systems_, 31, 2018.
         https://arxiv.org/abs/1805.08498
  """
  shape = ps.convert_to_shape_tensor(shape, dtype_hint=tf.int32)
  skewness = tf.convert_to_tensor(skewness)
  seed = samplers.sanitize_seed(seed, salt='two_piece_normal')

  return _two_piece_normal_sample_with_gradient(shape, tf.abs(skewness), seed)

max_stars_repo_name: mederrata/probability


text:
# Copyright 2016 The Flexbox Authors. All rights reserved.
# Licensed under the open source MIT License, which is in the LICENSE file.
import pandas as pd
from twilio.rest import TwilioRestClient
import os
from pandas.tools.plotting import scatter_matrix
from datetime import date, datetime, timedelta
import copy
import numpy as np
import os.path
import requests
import re
import yaml
from flexbox import psql
from flexbox import psql_server
from flexbox import analysis_tools
from sqlalchemy import cast,Date,text
from flexbox import psql_server

TESTING=True
from_twilio_number = '+12223334444'
outlet_2 = []
receive_texts = []
#Datetime in UTC to match those in flexbox table
this_datetime = datetime.utcnow()

print datetime.now()
#Preparation for Texting
#This gets the phone numbers, a list of available flexboxes in the program,
#available hours, and hardcoded which outlets they are supposed to be on.
metadata = psql_server.get_metadata()
table_dict = psql_server.setup_tables(metadata)

avaiable_hours = {}
phone_dictionary = {}
phone_dictionary_reversed = {}
with open('twilio_auth.yaml') as f:
    cf = yaml.safe_load(f)
client = TwilioRestClient(cf['ACCOUNT_SID'],cf['AUTH_TOKEN'])
with open('../web/available_hours.yaml') as f:
    available_hours = yaml.safe_load(f)
with open('max_fridge_power.yaml') as f:
    max_fridge_power = yaml.safe_load(f)
with open('phonebook.yaml') as f:
    phone_dictionary = yaml.safe_load(f)
    phone_dictionary_reversed = dict((value,key)
                for key,value in phone_dictionary.items())
available_flexboxes = []
for val in available_hours.keys():
    if 'flxbx' in val:
        available_flexboxes.append(val)
correct_outlet = {}
for flxbx in available_flexboxes:
    correct_outlet[flxbx] = 3
    if flxbx in outlet_2:
        correct_outlet[flxbx] = 2
    else:
        correct_outlet[flxbx] = 3
def send_alert_message(message):
    print message
    if not TESTING:
        client.messages.create(
            to=phone_dictionary_reversed['Admin'],
            from_=from_twilio_number,
            body=message,
        )

def get_dataframe_from_sql(table_ref,this_datetime,look_back_hours,in_utc=True):
    look_back_datetime = this_datetime-timedelta(hours=look_back_hours)
    column_names = table_ref.columns.keys()
    df = pd.DataFrame(table_ref.select().\
                                  where(table_ref.c.datetime>=look_back_datetime).\
                                  where(table_ref.c.datetime<=this_datetime).\
    order_by(table_ref.c.datetime.asc()).execute().fetchall(),columns=column_names)
    if in_utc:
        df.index = df['datetime'] - timedelta(hours=6)
    else:
        df.index = df['datetime']
    return df

def get_temp_sensor_failure_rate(this_datetime,look_back_hours):
    inside_temperature = get_dataframe_from_sql(table_dict['inside_table'],\
                                                this_datetime,look_back_hours)
    temp_fails1 = {}
    temp_fails2 = {}
    for flxbx in available_flexboxes:
        temp_check = inside_temperature[inside_temperature['hostname']==flxbx]
        if len(temp_check)>0:
            temp_fails1[flxbx] = float(len(temp_check[(temp_check['inside_temp1']>50000) |\
                                           (temp_check['inside_temp1']<-25000)])/\
                                            float(len(temp_check)))
            temp_fails2[flxbx]= float(len(temp_check[(temp_check['inside_temp2']>50000) |\
                                           (temp_check['inside_temp2']<-25000)])/\
                                            float(len(temp_check)))
    return temp_fails1,temp_fails2

def send_temp_message(temp_fails1,temp_fails2):
    for flxbx in temp_fails1:
        if flxbx in temp_fails1 and temp_fails1[flxbx]>0.05 and \
                flxbx in temp_fails2 and temp_fails2[flxbx]>0.05:
            temp_message = flxbx + ':  AMBOS sensores TEMPERATURA estan dando valores que no son correctos. '+\
            'temp1:  ' +\
            str(round(temp_fails1[flxbx]*100,2))+'% y temp2: ' +\
            str(round(temp_fails2[flxbx]*100,2))+'% del tiempo'+\
            '. Checar inmediatamente en esta casa -'+\
            'telefono: '+ phone_dictionary_reversed[flxbx]

            ' del tiempo'
            send_alert_message(temp_message)
        elif flxbx in temp_fails1 and temp_fails1[flxbx]>0.05:
            temp_message = flxbx + ': sensor TEMPERATURA esta dando valores que no son correctos. '+\
            'temp1:  ' +str(round(temp_fails1[flxbx]*100,2))+'% del tiempo'+\
            '. Checar inmediatamente en esta casa - telefono '+phone_dictionary_reversed[flxbx]

            send_alert_message(temp_message)
        elif flxbx in temp_fails2 and temp_fails2[flxbx]>0.05:
            temp_message = flxbx + ': sensor TEMPERATURA esta dando valores que no son correctos. '+\
            'temp2:  ' +str(round(temp_fails2[flxbx]*100,2))+'% del tiempo'+\
            '. Checar inmediatamente en esta casa - telefono '+phone_dictionary_reversed[flxbx]
            send_alert_message(temp_message)

def send_mfi_message(this_datetime,look_back_hours,minutes_since_last_heartbeat):
    mfi_df = get_dataframe_from_sql(table_dict['mfi_table'],this_datetime,
                                    look_back_hours,in_utc=False)
    for flxbx in available_flexboxes:
        if flxbx!='flxbxD6':
            if len(mfi_df[mfi_df['hostname']==flxbx])>0:
                if mfi_df[mfi_df['hostname']==flxbx].iloc[-1].relay3 == True:
                    fridge_state = 'ON'
                elif mfi_df[mfi_df['hostname']==flxbx].iloc[-1].relay3 == False:
                    fridge_state = 'OFF'
                else:
                    fridge_state = 'text_error'
                if mfi_df[mfi_df['hostname']==flxbx].iloc[-1].\
                    datetime<this_datetime-timedelta(minutes=minutes_since_last_heartbeat):
                    minutes_since = int(float((this_datetime-mfi_df[mfi_df['hostname']==\
                                                        flxbx].iloc[-1].datetime).seconds)/60.0)
                    message = flxbx+': El MFI no esta funcionado para ' + str(minutes_since)+\
                    ' minutos. Es todavia '+ fridge_state +'.  '+\
                    'Checar inmediatamente en esta casa - telefono: '+phone_dictionary_reversed[flxbx]
                    send_alert_message(message)
            else:
                message = flxbx +': El MFI no esta funcionado para mas que '+str(look_back_hours)+\
                    ' horas. '+\
                    'Checar inmediatamente en esta casa - telefono: '+phone_dictionary_reversed[flxbx]
                send_alert_message(message)

def get_timedelta_info(td):
    days = td.days
    hours = td.seconds//3600
    minutes = (td.seconds//60)%60
    seconds = (td.seconds)%3600%60
    return td.days, td.seconds//3600, (td.seconds//60)%60, seconds

def send_network_message(this_datetime,look_back_hours,minutes_since_last_network):
    network_tests = get_dataframe_from_sql(table_dict['network_tests'],this_datetime,look_back_hours)
    for flxbx in available_flexboxes:
        if len(network_tests[network_tests['hostname']==flxbx])>0:
            network_gaps = (network_tests[network_tests['hostname']==flxbx]['datetime']-\
            network_tests[network_tests['hostname']==flxbx]['datetime'].shift())
            average_gap = network_gaps.mean()
            largest_gap = network_gaps.max()
            last_gap = this_datetime-timedelta(hours=6)-\
                network_tests[network_tests['hostname']==flxbx].index[-1]

            days,hours,minutes,seconds = get_timedelta_info(largest_gap)
            last_days,last_hours,last_minutes,last_seconds = get_timedelta_info(last_gap)
            gap_string = network_gaps.shift(-1).idxmax().strftime('%m-%d %H:%M')+" to "+\
            network_gaps.idxmax().strftime('%m-%d %H:%M')
            '''
            if largest_gap.seconds>5400 || last_gap>:
                network_message = flxbx + ': lost connection for ' + str(days)+' days, '+\
                    str(hours)+' hours, '+str(minutes)+' minutes '+ ' from ' + gap_string+\
                    ', have not had a connection for ' +\
                    str(last_hours)+' hours, '+str(last_minutes)+' minutes '\
                    +str(last_seconds)+' seconds '
                send_alert_message(network_message)
            '''
            if last_gap.seconds>60*minutes_since_last_network:
                network_message = flxbx + ': El MODEM no esta funcionando para '+\
                str(last_hours)+' hours y ' + str(last_minutes)+' minutos. '+\
                'Checar inmediatamente en esta casa - telefono: '+phone_dictionary_reversed[flxbx]
                send_alert_message(network_message)

def send_system_network_message(this_datetime,look_back_hours):
    network_tests = get_dataframe_from_sql(table_dict['network_tests'],this_datetime,look_back_hours)
    network_tests==network_tests[network_tests.index<this_datetime]
    if len(network_tests)==0:
        send_alert_message("The network test script is down!")

def send_system_heartbeat_message(this_datetime,look_back_hours):
    dr_table = get_dataframe_from_sql(table_dict['demand_response'],this_datetime,look_back_hours)
    dr_table==dr_table[dr_table.index<this_datetime]
    if len(dr_table)==0:
        send_alert_message("THE HEARTBEAT SCRIPT IS DOWN!!!!")

def send_zwave_message():
    metadata = psql_server.get_metadata()
    table_dict = psql_server.setup_tables(metadata)

    dfs = {}
    for flxbx in available_flexboxes:
        # The next two lines are SQL Alchemy code, ordering each house energy data by date and then
        last_row = table_dict['twilio_received'].select("hostname='"+flxbx+"'").\
             order_by(table_dict['twilio_received'].\
             c.datetime.desc()).execute().fetchone()

        if last_row:
             last_date = last_row[3]
             limit = last_row[6]
             last_text_sent = None

             # The next two lines are SQL Alchemy code, ordering each house energy data by date and t
             energia = analysis_tools.get_energy_since_date(flxbx,last_date)
             print flxbx+': '+str(last_date)+' : '+str(energia)
             if (datetime.now()-last_date).days>31:
                 if flxbx in receive_texts:
                    zwave_message = flxbx+': Hay uno mes desde un limite en el database.'\
                        +' El ultimo ves es '+str(last_date)
                    send_alert_message(zwave_message)
                 else:
                    print flxbx +' hasn\'t set a limit but does not receive messages.'
             elif (datetime.now()-last_date).days<0:
                zwave_message = flxbx+': El limite es incorrecta. El limite en el server es '\
                    +str(last_date)+' pero este en el futuro!'
                send_alert_message(zwave_message)
             elif energia == 0 and (datetime.now()-last_date).days>2:
                 zwave_message = flxbx+': Es possible el zwave no es funcianado. Checar'\
                 + ' immediatamente en esta casa por favor.'
                 send_alert_message(zwave_message)

def send_outlet_message(this_datetime,look_back_hours):
    metadata = psql_server.get_metadata()
    table_dict = psql_server.setup_tables(metadata)

    peak_shifting_df = get_dataframe_from_sql(table_dict\
                        ['peak_shifting_dr_table'],
                        this_datetime-timedelta(hours=6),look_back_hours+3,in_utc=False)
    peak_event = False
    if len(peak_shifting_df)>0:
        start_peak_event = peak_shifting_df.iloc[-1]['datetime']
        end_peak_event = start_peak_event +\
            timedelta(minutes=peak_shifting_df.iloc[-1]['duration_minutes'])
        print 'Ommitting Outlet checks for when peak events have recently occurred'
        print start_peak_event
        print end_peak_event
        for hours_back in range(look_back_hours):
            time_check = this_datetime -timedelta(hours=6+hours_back)
            peak_event = (time_check>=start_peak_event) and \
                        (time_check<=end_peak_event)
            print time_check
    print peak_event
    mfi_df = get_dataframe_from_sql(table_dict['mfi_table'],this_datetime,look_back_hours)
    hour = (this_datetime-timedelta(hours=(look_back_hours+6))).hour
    for flxbx in available_flexboxes:
        if flxbx not in ['flxbxD21']:
            is_available = True
            for val in range(hour,hour+look_back_hours):
                if val not in available_hours[flxbx]:
                    is_available = False
            if not peak_event and is_available and\
                    len(mfi_df[mfi_df['hostname']==flxbx])>0:
                energy_sum3 = sum(mfi_df[mfi_df['hostname']==flxbx]['active_pwr3'])
                energy_sum2 = sum(mfi_df[(mfi_df['hostname']==flxbx)&\
                                        (mfi_df['active_pwr2']<\
                                        max_fridge_power[flxbx])]['active_pwr2'])
                if not peak_event and energy_sum3<energy_sum2 and \
                        correct_outlet[flxbx]==3:
                    outlet_message = flxbx + ': El freezer esta conectado en'+\
                        'el outlet equivocado (outlet 2 (roja)).'+\
                        ' Checar inmediatamente en esta casa - telefono: '+\
                        phone_dictionary_reversed[flxbx]
                    send_alert_message(outlet_message)
                elif energy_sum3>energy_sum2 and correct_outlet[flxbx]==2:
                    outlet_message = flxbx + ': El freezer esta conectado en'+\
                        'el outlet equivocado (outlet 3 (negro)).'+\
                        ' Checar inmediatamente en esta casa - telefono: '+\
                        phone_dictionary_reversed[flxbx]
                    send_alert_message(outlet_message)
                elif energy_sum3==0 and energy_sum2==0 and not peak_event:
                    outlet_message = flxbx + ': El freezer esta conectado en'+\
                        ' NO outlet.'+\
                        ' Checar inmediatamente en esta casa - telefono: '+\
                        phone_dictionary_reversed[flxbx]
                    send_alert_message(outlet_message)
                    print hour
                    print available_hours[flxbx]

def send_ambient_message(this_datetime,look_back_hours):
    ambient_df = get_dataframe_from_sql(table_dict['ambient_table'],this_datetime,look_back_hours)
    for flxbx in available_flexboxes:
        if len(ambient_df[ambient_df['hostname']==flxbx])==0:
            send_alert_message(flxbx+": Problema con sensora ambiente")
        else:
            print flxbx+':'+str(ambient_df[ambient_df['hostname']==flxbx].iloc[-1].name)

def send_relay_message(this_datetime,look_back_hours):
    mfi_df = get_dataframe_from_sql(table_dict['mfi_table'],this_datetime,look_back_hours)
    dr_df = get_dataframe_from_sql(table_dict['demand_response'],this_datetime,look_back_hours)
    for flxbx in available_flexboxes:
        if len(dr_df[dr_df['hostname']==flxbx])>0 and len(mfi_df[mfi_df['hostname']==flxbx])>0:
            df_combine = pd.DataFrame([dr_df[dr_df['hostname']==flxbx]['mfi_state'].\
                                    resample('60S').mean().dropna(),\
                        mfi_df[mfi_df['hostname']==flxbx]['relay3'].resample('60S').\
                        mean().dropna()]).T.dropna()
            df_combine['matching_relay'] = df_combine['mfi_state']-df_combine['relay3']
            if len(df_combine[df_combine['matching_relay']!=0])>5:
                relay_message = flxbx+\
                ': This is a test: The relay on this flexbox is not matching the dr signal.'
                print relay_message
                #send_alert_message(relay_message)

try:
    look_back_minutes = 30
    send_system_heartbeat_message(this_datetime,look_back_minutes/60.0)
except:
    send_alert_message("Exception Accessing PSQL. "+\
                       "Server requires immediate attention.")

if requests.get('http://yourdomainhere.com/signal_time').status_code!=200:
    send_alert_message("The nginx server is down, no events will be sent!")
#Runs every 10 minutes
#Checks that heartbeat is running
look_back_minutes = 30
send_system_heartbeat_message(this_datetime,look_back_minutes/61.0)

if TESTING:
    send_ambient_message(this_datetime,24)

#Runs every hour
#Checkst that network testing script is running
if TESTING or (datetime.now().minute==10):
    look_back_minutes = 10
    send_system_network_message(this_datetime,look_back_minutes/60.0)

#Runs every 10 minutes
#Checks that relay is correct
look_back_minutes = 10
send_relay_message(this_datetime,look_back_minutes/60.0)

#Run each hour at :10
#Checks that each flexbox is connected over the modem
if TESTING or (datetime.now().minute==10 and datetime.now().hour>=7):
    look_back_hours = 12
    minutes_since_last_network=90
    send_network_message(this_datetime,look_back_hours,minutes_since_last_network)

#Run each hour at :10,
#Checks that each flexbox's MFI values are being sent through the heartbeat
if TESTING or (datetime.now().minute==10 and datetime.now().hour>=7):
    look_back_hours = 5
    minutes_since_last_heartbeat = 180
    send_mfi_message(this_datetime,look_back_hours,minutes_since_last_heartbeat)
    #flxbxD6 is exceptioned currently!!!!!

#Runs once a day at 9AM
#Checks that each flexbox's temperature sensors are functioning
look_back_hours = 24
if TESTING or (datetime.now().hour == 9 and datetime.now().minute==10):
    temp_fails1,temp_fails2 = get_temp_sensor_failure_rate(this_datetime,look_back_hours)
    send_temp_message(temp_fails1,temp_fails2)

#Run each hour at :00,
#Checks that each flexbox's MFI is on the right outlet
if TESTING or (datetime.now().minute==0 and datetime.now().hour>=7):
    look_back_hours = 3
    send_outlet_message(this_datetime,look_back_hours)
    send_outlet_message_flxbxD21(this_datetime,7)

if TESTING or datetime.now().minute==0 and datetime.now().hour == 10:
    send_zwave_message()

max_stars_repo_name: flexbox-nicaragua/flexbox-code

max_issues_repo_name: flexbox-nicaragua/flexbox-code

max_forks_repo_name: flexbox-nicaragua/flexbox-code