text:
import numpy as np

def stress_radiation(ra_24):
    r"""
    Computes the stress for plants when there is not sufficient radiation

    .. math ::
        S_{r}=\frac{S^{\downarrow}}{\left(S^{\downarrow}+60.\right)}
        \left(1+\frac{60}{500}\right)

    Parameters
    ----------
    ra_24 : float
        daily solar radiation
        :math:`S^{\downarrow}`
        [Wm-2]

    Returns
    -------
    stress_rad : float
        stress factor for radiation
        :math:`S_{r}`
        [-]

    Examples
    --------
    >>> import ETLook.stress as stress
    >>> stress.stress_radiation()
    0.0
    >>> stress.stress_radiation(500)
    1.0
    >>> stress.stress_radiation(700)
    1.0
    >>> stress.stress_radiation(250)
    0.90322580645161288
    """
    stress = ra_24/(ra_24 + 60.)*(1 + 60./500.)
    stress = np.clip(stress, 0, 1)

    return stress

def stress_moisture(se_root, tenacity=1.5):
    r"""
    Computes the stress for plants when there is not sufficient soil
    moisture in the root zone

    .. math ::
        S_{m}=K_{sf}S_{e,root}-\frac{\sin\left(2\pi S_{e,root}\right)}{2\pi}

    The tenacity factor :math:`K_{sf}` ranges from 1 for sensitive plants to
    1.5 for moderately sensitive plants to 3 for insensitive
    (tenacious plants).

    Parameters
    ----------
    se_root : float
        effective saturation root zone moisture
        `S_{e,root}`
        [-]
    tenacity : float
        tenacity factor
        `K_{sf}`
        [-]

    Returns
    -------
    stress_moist : float
        stress factor for root zone moisture
        :math:`S_{m}`
        [-]

    Examples
    --------
    >>> import ETLook.stress as stress
    >>> stress.stress_moisture(0.5)
    0.75
    >>> stress.stress_moisture(0.5, tenacity = 1)
    0.5
    >>> stress.stress_moisture(0.5, tenacity = 3)
    1.0
    """
    stress = tenacity*se_root - (np.sin(2*np.pi*se_root))/(2*np.pi)
    stress = np.clip(stress, 0, 1)

    return stress

def stress_temperature(t_air_24, t_opt=25.0, t_min=0.0, t_max=50.0):
    r"""
    Computes the stress for plants when it is too cold or hot

    .. math ::
        f=\frac{T_{max}-T_{opt}}{T_{opt}-T_{min}}

    .. math ::
        s_{T}=\frac{\left(T_{a}-T_{min}\right)\left(T_{max}-T_{a}\right)^{f}}
            {\left(T_{opt}-T_{min}\right)\left(T_{max}-T_{opt}\right)^{f}}

    Parameters
    ----------
    t_air_24 : float
        daily air temperature
        :math:`T_{a}`
        [C]
    t_opt : float
        optimum air temperature for plant growth
        :math:`T_{opt}`
        [C]
    t_min : float
        minimum air temperature for plant growth
        :math:`T_{min}`
        [C]
    t_max : float
        maximum air temperature for plant growth
        :math:`T_{max}`
        [C]

    Returns
    -------
    stress_temp : float
        stress factor for air temperature
        :math:`S_{T}`
        [-]

    Examples
    --------
    >>> import ETLook.stress as stress
    >>> stress.stress_temperature(15)
    0.83999999999999997
    >>> stress.stress_temperature(15, t_opt =20)
    0.9451080185178129
    >>> stress.stress_temperature(15, t_opt =20, t_min=10)
    0.79398148148148151
    >>> stress.stress_temperature(15, t_opt =20, t_min=10, t_max=30)
    0.75

    """
    # f = float((t_max - t_opt))/float((t_opt - t_min))
    f = (t_max - t_opt)/(t_opt - t_min)
    x = (t_air_24 - t_min) * (t_max - t_air_24)**f
    y = (t_opt - t_min) * (t_max - t_opt)**f

    stress = x/y
    stress = np.clip(stress, 0, 1)

    return stress

def stress_vpd(vpd_24, vpd_slope=-0.3):
    r"""
    Computes the stress for plants if the vpd increases too much. With
    lower slopes the stress increases faster. The slope of the curve
    is between -0.3 and -0.7

    .. math ::
        S_{v}=m\ln(0.1\Delta_{e}+\frac{1}{2})+1

    Parameters
    ----------
    vpd_24 : float
        daily vapour pressure deficit
        :math:`\Delta_{e}`
        [mbar]
    vpd_slope : float
        vapour pressure stress curve slope
        :math:`m`
        [mbar-1]

    Returns
    -------
    stress_vpd : float
        stress factor for vapour pressure deficit
        :math:`S_{v}`
        [-]

    Examples
    --------
    >>> import ETLook.stress as stress
    >>> stress.stress_vpd(15)
    0.79205584583201638
    >>> stress.stress_vpd(15, vpd_slope=-0.7)
    0.51479697360803833
    >>> stress.stress_vpd(15, vpd_slope=-0.3)
    0.79205584583201638

    """
    stress = vpd_slope * np.log(vpd_24/10. + 0.5) + 1
    stress = np.clip(stress, 0, 1)

    return stress

max_stars_repo_name: hectornieto/wapor-et-look

max_issues_repo_name: hectornieto/wapor-et-look

max_forks_repo_name: DHI-GRAS/wapor-et-look


text:
# Zooplankton community model II
# One stage-structured consumer species feeding on two resources
# For units and references, see Appendix S2, Table 2
# Created by Wojciech Uszko (2020)

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

# Body masses (ng dry weight):

B_J = 100     # juvenile
B_A = 1000    # adult

# Temperature- or body mass-independent parameters:

deltaRS = 0.1  # resource 1 supply rate
deltaRL = 0.1  # resource 2 supply rate

q       = 0    # functional response (Hill) exponent; if =0 then type II

p       = 0.85 # diet preference
pJRS    = p
pJRL    = 1-pJRS
pARS    = 1-pJRS
pARL    = pJRS

betaJ  = 0.6  # juvenile conversion efficiency
betaA  = 0.6  # adult conversion efficiency

HJRS   = 0.2  # half-saturation constant
HJRL   = 0.2  # half-saturation constant
HARS   = 0.2  # half-saturation constant
HARL   = 0.2  # half-saturation constant

zJA     = 0.1 # juvenile-to-adult mass ratio

muJ    = 0.01 # juvenile background mortality rate
muA    = 0.01 # adult background mortality rate

# Ambient temperature (Kelvin):

T = 273.15 + 20

"""
# Temperature- or body mass-dependent parameters
# Without size-temperature interaction:

# Resource supply density:
RSmax    = 0.0042 * np.exp( 0.151/(0.00008617*T) )
RLmax    = RSmax

# Consumer maximum ingestion rate:
IJRSmax = (19 * (B_J**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_J
IJRLmax = (19 * (B_J**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_J
IARSmax = (19 * (B_A**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_A
IARLmax = (19 * (B_A**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_A

# Consumer metabolic rate:
mJ      = (850000000 * (B_J**0.7) * np.exp( -0.56/(0.00008617*T) )) / B_J
mA      = (850000000 * (B_A**0.7) * np.exp( -0.56/(0.00008617*T) )) / B_A
"""

# Temperature- or body mass-dependent parameters
# With size-temperature interaction in Rmax and in temperature optimum of Imax:

# Resource supply density:
RSmax    = 0.0042 * np.exp( 0.151/(0.00008617*T) )
RLmax    = (5.88* 10**(-7)) * np.exp( 0.37564/(0.00008617*T) )

# Consumer maximum ingestion rate:
IJRSmax = (19 * (B_J**(0.7)) * np.exp(-((T-(273.15+24))**2)/(2*(8**2)))) / B_J
IJRLmax = (19 * (B_J**(0.7)) * np.exp(-((T-(273.15+24))**2)/(2*(8**2)))) / B_J
IARSmax = (19 * (B_A**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_A
IARLmax = (19 * (B_A**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_A

# Consumer metabolic rate:
mJ      = (850000000 * (B_J**0.7) * np.exp( -0.56/(0.00008617*T) )) / B_J
mA      = (850000000 * (B_A**0.7) * np.exp( -0.56/(0.00008617*T) )) / B_A

"""
# Temperature- or body mass-dependent parameters
# With size-temperature interaction in Rmax and in metabolic rate:

# Resource supply density:
RSmax    = 0.0042 * np.exp( 0.151/(0.00008617*T) )
RLmax    = (5.88* 10**(-7)) * np.exp( 0.37564/(0.00008617*T) )

# Consumer maximum ingestion rate:
IJRSmax = (19 * (B_J**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_J
IJRLmax = (19 * (B_J**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_J
IARSmax = (19 * (B_A**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_A
IARLmax = (19 * (B_A**(0.7)) * np.exp(-((T-(273.15+20))**2)/(2*(8**2)))) / B_A

# Consumer metabolic rate:
mJ      = (850000000 * (B_J**(0.7 + 0.0005*T)) * np.exp( -0.56/(0.00008617*T) )) / B_J
mA      = (850000000 * (B_A**(0.7 + 0.0005*T)) * np.exp( -0.56/(0.00008617*T) )) / B_A
"""

# Specify the model:
def model(X,t):
    # Variables:
    RS = X[0]   # small resource biomass density
    RL = X[1]   # large resource biomass density
    J = X[2]    # juvenile biomass density
    A = X[3]    # adult biomass density
    # Ingestion rates:
    IJRS = ( ( pJRS * (IJRSmax/(HJRS**(1+q))) * RS**(1+q) + 0 * (IJRLmax/(HJRL**(1+q))) * RL**(1+q) ) /
    ( 1 + (pJRS/(HJRS**(1+q))) * RS**(1+q) + (pJRL/(HJRL**(1+q))) * RL**(1+q) ) )
    IJRL = ( ( 0 * (IJRSmax/(HJRS**(1+q))) * RS**(1+q) + pJRL * (IJRLmax/(HJRL**(1+q))) * RL**(1+q) ) /
    ( 1 + (pJRS/(HJRS**(1+q))) * RS**(1+q) + (pJRL/(HJRL**(1+q))) * RL**(1+q) ) )
    IARS = ( ( pARS * (IARSmax/(HARS**(1+q))) * RS**(1+q) + 0 * (IARLmax/(HARL**(1+q))) * RL**(1+q) ) /
    ( 1 + (pARS/(HARS**(1+q))) * RS**(1+q) + (pARL/(HARL**(1+q))) * RL**(1+q) ) )
    IARL = ( ( 0 * (IARSmax/(HARS**(1+q))) * RS**(1+q) + pARL * (IARLmax/(HARL**(1+q))) * RL**(1+q) ) /
    ( 1 + (pARS/(HARS**(1+q))) * RS**(1+q) + (pARL/(HARL**(1+q))) * RL**(1+q) ) )
    # Stage-specific production rates:
    vJ     = betaJ*(IJRS+IJRL) - mJ                 # juvenile production rate
    gammaJ = (vJ - muJ) / (1 - zJA**(1-(muJ/vJ)))   # maturation rate
    vA     = betaA*(IARS+IARL) - mA                 # reproduction rate
    # ODE system:
    dRSdt = deltaRS*(RSmax - RS) - IJRS*J - IARS*A
    dRLdt = deltaRL*(RLmax - RL) - IJRL*J - IARL*A
    dJdt = max(vA,0)*A + vJ*J - max(gammaJ,0)*J - muJ*J
    dAdt = max(gammaJ,0)*J + min(vA,0)*A - muA*A
    return np.array([dRSdt, dRLdt, dJdt, dAdt])

# Initial densities for RS, RL, J, A
X0 = np.array([0.01, 0.01, 0.01, 0.01])

# Time range
t = np.linspace(0,300,1000)

# Solve ODE
X = odeint(model,X0,t)

# Plot results
RS,RL,J,A = np.transpose(X)

plt.figure()
plt.plot(t, RS, 'g-', label='RS', linewidth=1.0)
plt.plot(t, RL, 'g-', label='RL', linewidth=2.5)
plt.legend(loc='upper right')
plt.xlabel('Time (day)')
plt.ylabel('Density (mg/L)')
plt.show()

plt.figure()
plt.plot(t, J, 'k-', label='J', linewidth=1.0)
plt.plot(t, A, 'k-', label='A', linewidth=2.5)
plt.legend(loc='upper right')
plt.xlabel('Time (day)')
plt.ylabel('Density (mg/L)')
plt.show()

ext: py

max_stars_repo_name: wojciechuszko/UszkoHussGardmark

max_issues_repo_name: wojciechuszko/UszkoHussGardmark

max_forks_repo_name: wojciechuszko/UszkoHussGardmark

reason: import numpy,from scipy


text:
from setuptools import setup
from Cython.Build import cythonize
import numpy
setup(
    ext_modules = cythonize("QuickSearch_cython.pyx"),
    include_dirs=[numpy.get_include()]
)

#python setup.py build_ext --inplace

reason: import numpy


text:
# coding=utf-8
import cv2
import numpy as np
from face_train_use_keras import Model

if __name__ == '__main__':

    modelPath = 'C:/Users/Administrator/Desktop/BossSensor-master/modelsss/man.face.model2.h5'

    # 加载模型
    model = Model()
    model.load_model(file_path=modelPath)

    # 捕获指定摄像头的实时视频流
    cap = cv2.VideoCapture(0)
    classifierPath = "C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml"
    color = (134, 126, 255)  # 设置人脸框的颜色

    while True:
        _, frame = cap.read()  # 读取一帧视频

        # 图像灰化，降低计算复杂度
        frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # 使用人脸识别分类器，读入分类器
        cascade = cv2.CascadeClassifier(classifierPath)

        faceRects = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))

        if len(faceRects) > 0:  # 如果检测到人脸，则将人脸进行标记
            for faceRect in faceRects:  # 对每一个人脸画圆形标记
                x, y, w, h = faceRect

                faceImage = frame[y:y + h, x:x + w]

                faceID = model.face_predict(faceImage)
                # 如果是“辣个男人”
                if faceID == 0:
                    # cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, thickness=2)
                    cv2.circle(frame, (x + w // 2, y + h // 2), w // 2, color, 2, 8, 0)
                else:
                    pass

        cv2.imshow("test", frame)  # 显示图像

        key = cv2.waitKey(10)
        c = chr(key & 255)
        if c in ['q', 'Q', chr(27)]:
            break

    cap.release()
    cv2.destroyAllWindows()

max_stars_repo_name: horacework/FaceIdentification

max_issues_repo_name: horacework/FaceIdentification

max_forks_repo_name: horacework/FaceIdentification


text:
import matplotlib.pyplot as pl
import numpy as np
import scipy.spatial
import math
import pickle

from PIL import Image

input_folder = './input/'
output_folder = './output/'

def memoize_edges_for_region(f):
    memo = {}

    def helper(self, point):
        key = point['idx']
        if key not in memo:
            memo[key] = f(self, point)
        return memo[key]
    return helper

class NaturalNeighbourInterpolation:

    def __init__(self, file_name=None, number_of_sites=None):
        if file_name is not None and number_of_sites is None:
            self.read_from_pickle(file_name)
            self.add_clipping()
            self.create_voronoi()
        else:
            self.number_of_sites = number_of_sites
            self.sample_from_image(file_name)

    def add_clipping(self):
        bounding_box = np.array([0., self.width, 0., self.height])
        points_center = self.sites
        points_left = np.copy(points_center)
        points_left[:, 0] = bounding_box[0] - (points_left[:, 0] - bounding_box[0])
        points_right = np.copy(points_center)
        points_right[:, 0] = bounding_box[1] + (bounding_box[1] - points_right[:, 0])
        points_down = np.copy(points_center)
        points_down[:, 1] = bounding_box[2] - (points_down[:, 1] - bounding_box[2])
        points_up = np.copy(points_center)
        points_up[:, 1] = bounding_box[3] + (bounding_box[3] - points_up[:, 1])
        points = np.append(points_center,
                           np.append(np.append(points_left,
                                               points_right,
                                               axis=0),
                                     np.append(points_down,
                                               points_up,
                                               axis=0),
                                     axis=0),
                           axis=0)

        point_colors = dict()
        n = len(self.sites)
        for idx in range(n):
            color = self.site_colors[idx]
            left_idx = n + idx
            right_idx = n * 2 + idx
            down_idx = n * 3 + idx
            up_idx = n * 4 + idx
            point_colors[idx] = color
            point_colors[left_idx] = color
            point_colors[right_idx] = color
            point_colors[down_idx] = color
            point_colors[up_idx] = color

        self.sites = points
        self.site_colors = point_colors

    def create_voronoi(self):
        self.voronoi = scipy.spatial.Voronoi(self.sites)

        # swap key/value in dict
        old_ridge_dict = dict(self.voronoi.ridge_dict)
        self.voronoi.ridge_dict.clear()
        for key, value in old_ridge_dict.items():
            self.voronoi.ridge_dict[tuple(value)] = key

        # create kdtree
        self.kdtree = scipy.spatial.cKDTree(self.sites)

        # create site dict
        self.sites_dict = {}
        #for idx, site in enumerate(self.sites):
        for idx in range(len(self.site_colors)):
            site = self.sites[idx]
            self.sites_dict[(int(site[0]), int(site[1]))] = self.site_colors[idx]

        #self.draw()

    def add_point(self, point):
        #print('Started processing point: ', point)
        int_point = (int(point[0]), int(point[1]))
        if int_point in self.sites_dict:
            color = self.sites_dict[int_point]
            return color
        #new_edges = []
        list_of_area_and_color = []
        closest_point = self.get_closest_point(point)
        perpendicular_bisector = self.get_perpendicular_bisector(closest_point['point'], point)
        e1, e2 = self.get_edges_crossing_bisector(closest_point, perpendicular_bisector, point)
        #new_edges.extend([e1['intersection'], e2['intersection']])
        list_of_area_and_color.append(self.calculate_area_taken((e1, e2), closest_point))

        final_edge = e1
        prev_point = closest_point
        loop_counter = 0
        while not self.edge_equals(e2, final_edge):
            if loop_counter > 20: return 0
            other_point = self.get_other_point(e2, prev_point)
            perpendicular_bisector = self.get_perpendicular_bisector(other_point['point'], point)
            e1, e2 = self.get_edges_crossing_bisector(other_point, perpendicular_bisector, point)
            #new_edges.extend([e1['intersection'], e2['intersection']])
            list_of_area_and_color.append(self.calculate_area_taken((e1, e2), other_point))
            prev_point = other_point
            loop_counter += 1

        return self.calculate_weighed_color(list_of_area_and_color)

    def get_closest_point(self, point):
        query_res = self.kdtree.query(point)
        return {
            'idx': query_res[1],
            'point': self.kdtree.data[query_res[1]]
        }

    # Cython code
    def get_perpendicular_bisector(self, a, b):
        return optimized.get_perpendicular_bisector(a[0], a[1], b[0], b[1])

    def get_edges_crossing_bisector(self, point, bisector, sort_point):
        edges = self.get_edges_for_region_with_bisector(point, bisector)

        if len(edges) != 2:
            raise Exception('Edges must be 2, were ' + str(len(edges)) + '!')

        ccw_value = self.ccw(edges[0]['intersection'][0], edges[0]['intersection'][1], edges[1]['intersection'][0], edges[1]['intersection'][1], sort_point[0], sort_point[1])
        if ccw_value >= 1:# is_ccw
            return edges[0], edges[1]
        return edges[1], edges[0]

    def calculate_area_taken(self, new_edge, point):
        stolen_area = self.calculate_area_for_stolen_polygon(new_edge, point)
        return [stolen_area, self.site_colors[point['idx']]]

    def get_vertices_for_region(self, point):
        region_idx = self.voronoi.point_region[point['idx']]
        region = self.voronoi.regions[region_idx]
        if -1 in region:
            raise Exception('-1 as vertexIdx!')
        vertices = [
            {
                'idx': vertex_idx,
                'vertex': self.voronoi.vertices[vertex_idx]
            } for vertex_idx in region
        ]

        return self.sort_vertices(vertices)

    def sort_vertices(self, vertices):
        # calculate centroid of the polygon
        n = len(vertices)  # of corners
        cx = float(sum(vertex['vertex'][0] for vertex in vertices)) / n
        cy = float(sum(vertex['vertex'][1] for vertex in vertices)) / n
        # create a new list of corners which includes angles
        corners_with_angles = []
        for vertex in vertices:
            x, y = vertex['vertex']
            an = (math.atan2(y - cy, x - cx) + 2.0 * math.pi) % (2.0 * math.pi)
            corners_with_angles.append((vertex, an))
        # sort it using the angles
        corners_with_angles.sort(key=lambda tup: tup[1])
        # return the sorted corners w/ angles removed
        return list(map(lambda x: x[0], corners_with_angles))

    # Cython code
    def calculate_area_for_stolen_polygon(self, new_edge, stolen_from_point):
        return optimized.calculate_area_for_stolen_polygon(self, new_edge, stolen_from_point)

    def get_other_point(self, edge, point):
        try:
            ridge = self.voronoi.ridge_dict[(edge['start']['idx'], edge['end']['idx'])]
        except KeyError:
            ridge = self.voronoi.ridge_dict[(edge['end']['idx'], edge['start']['idx'])]

        ridge_point_idx = ridge[1] if ridge[0] == point['idx'] else ridge[0]
        return {
            'idx': ridge_point_idx,
            'point': self.voronoi.points[ridge_point_idx]
        }

    def sample_random_sites(self):
        return np.c_[
            np.random.uniform(0, self.width, self.number_of_sites),
            np.random.uniform(0, self.height, self.number_of_sites)
        ], np.random.randint(0xFFFFFFFF, size=self.number_of_sites, dtype=np.uint32)

    def sample_from_image(self, image_name):
        image = Image.open(input_folder + image_name)
        pixels = np.array(image)
        self.width, self.height = image.size
        self.sites, _ = self.sample_random_sites()
        self.site_colors = [pixels[int(point[1])][int(point[0])] for point in self.sites]
        self.add_clipping()
        self.create_voronoi()

    def generate_image(self):
        pixels = []
        for y in np.linspace(0, self.height, self.height):
            print('Processing line: ', int(y), ' of ', self.height)
            x_row = []
            for x in np.linspace(0, self.width, self.width):
                try:
                    color = self.add_point((x, y))
                except Exception as e:
                    #print(e)
                    color = (0, 0, 0)
                x_row.append(color)
            pixels.append(x_row)

        print('Saving image.')
        # Convert the pixels into an array using numpy
        pixels = np.array(pixels, dtype=np.uint8)

        # Use PIL to create an image from the new array of pixels
        new_image = Image.fromarray(pixels)
        new_image.save(output_folder + 'output.png')

        print('Done!')

    def get_edges_for_region(self, point):
        vertices = self.get_vertices_for_region(point)
        n = len(vertices)
        edges = []
        for i in range(n):
            first = vertices[i]
            second = vertices[(i + 1) % n]

            edges.append({
                'start': first,
                'end': second
            })
        return edges

    def get_edges_for_region_with_bisector(self, point, bisector):
        edges = self.get_edges_for_region(point)
        new_edges = []
        for edge in edges:
            intersection = None
            if bisector is not None:
                first = edge['start']
                second = edge['end']
                intersection = self.get_line_intersection(
                    first['vertex'][0], first['vertex'][1], second['vertex'][0], second['vertex'][1],
                    bisector[0][0], bisector[0][1], bisector[1][0], bisector[1][1]
                )
            edge['intersection'] = intersection

            if edge['intersection'] is not None:
                new_edges.append(edge)
        return new_edges

    def draw(self, **kw):
        #sp.spatial.voronoi_plot_2d(self.voronoi, show_vertices=False, point_size=2)
        #
        vor = self.voronoi
        fig = pl.figure(figsize=(16, 16))
        ax = fig.gca()
        from matplotlib.collections import LineCollection

        if vor.points.shape[1] != 2:
            raise ValueError("Voronoi diagram is not 2-D")

        if kw.get('show_points', True):
            point_size = kw.get('point_size', None)
            ax.plot(vor.points[:, 0], vor.points[:, 1], '.', markersize=point_size)
        if kw.get('show_vertices', True):
            ax.plot(vor.vertices[:, 0], vor.vertices[:, 1], 'o')

        line_colors = kw.get('line_colors', 'k')
        line_width = kw.get('line_width', 1.0)
        line_alpha = kw.get('line_alpha', 1.0)

        center = vor.points.mean(axis=0)
        ptp_bound = vor.points.ptp(axis=0)

        finite_segments = []
        infinite_segments = []
        for pointidx, simplex in zip(vor.ridge_points, vor.ridge_vertices):
            simplex = np.asarray(simplex)
            if np.all(simplex >= 0):
                finite_segments.append(vor.vertices[simplex])
            else:
                i = simplex[simplex >= 0][0]  # finite end Voronoi vertex

                t = vor.points[pointidx[1]] - vor.points[pointidx[0]]  # tangent
                t /= np.linalg.norm(t)
                n = np.array([-t[1], t[0]])  # normal

                midpoint = vor.points[pointidx].mean(axis=0)
                direction = np.sign(np.dot(midpoint - center, n)) * n
                far_point = vor.vertices[i] + direction * ptp_bound.max()

                infinite_segments.append([vor.vertices[i], far_point])

        ax.add_collection(LineCollection(finite_segments,
                                         colors=line_colors,
                                         lw=line_width,
                                         alpha=line_alpha,
                                         linestyle='solid'))
        ax.add_collection(LineCollection(infinite_segments,
                                         colors=line_colors,
                                         lw=line_width,
                                         alpha=line_alpha,
                                         linestyle='dashed'))

        margin = 0.1 * vor.points.ptp(axis=0)
        xy_min = vor.points.min(axis=0) - margin
        xy_max = vor.points.max(axis=0) + margin
        #ax.set_xlim(xy_min[0], xy_max[0])
        #ax.set_ylim(xy_min[1], xy_max[1])

        pl.show()
        #pl.savefig(output_folder + 'voronoi.png')

    def debug_draw(self, point, edges):
        fig = pl.figure(figsize=(16, 16))
        ax = fig.gca()
        """
        # Plot initial points
        ax.plot(self.voronoi.points[:, 0], self.voronoi.points[:, 1], 'b.')
        # Plot ridges points
        for region in self.voronoi.regions:
            vertices = self.voronoi.vertices[region, :]
            ax.plot(vertices[:, 0], vertices[:, 1], 'go')
        # Plot ridges
        for region in self.voronoi.regions:
            if len(region) == 0: continue;
            vertices = self.voronoi.vertices[region + [region[0]], :]
            ax.plot(vertices[:, 0], vertices[:, 1], 'k-')
        """
        from matplotlib.collections import LineCollection

        line_colors = 'k'
        line_width = 1.0
        line_alpha = 1.0

        center = self.voronoi.points.mean(axis=0)
        ptp_bound = self.voronoi.points.ptp(axis=0)

        ax.plot([point[0]], [point[1]], 'yo')

        ax.plot(self.voronoi.points[:, 0], self.voronoi.points[:, 1], '.', markersize=None)# points
        ax.plot(self.voronoi.vertices[:, 0], self.voronoi.vertices[:, 1], '.')# vertices

        finite_segments = []
        infinite_segments = []
        for pointidx, simplex in zip(self.voronoi.ridge_points, self.voronoi.ridge_vertices):
            simplex = np.asarray(simplex)
            if np.all(simplex >= 0):
                finite_segments.append(self.voronoi.vertices[simplex])
            else:
                i = simplex[simplex >= 0][0]  # finite end Voronoi vertex

                t = self.voronoi.points[pointidx[1]] - self.voronoi.points[pointidx[0]]  # tangent
                t /= np.linalg.norm(t)
                n = np.array([-t[1], t[0]])  # normal

                midpoint = self.voronoi.points[pointidx].mean(axis=0)
                direction = np.sign(np.dot(midpoint - center, n)) * n
                far_point = self.voronoi.vertices[i] + direction * ptp_bound.max()

                infinite_segments.append([self.voronoi.vertices[i], far_point])

        ax.add_collection(LineCollection(finite_segments,
                                         colors=line_colors,
                                         lw=line_width,
                                         alpha=line_alpha,
                                         linestyle='solid'))
        ax.add_collection(LineCollection(infinite_segments,
                                         colors=line_colors,
                                         lw=line_width,
                                         alpha=line_alpha,
                                         linestyle='dashed'))

        x_coords = []
        y_coords = []
        for idx, edge in enumerate(edges):
            if idx == 0:
                x_coords.append(edge[0][0])
                y_coords.append(edge[0][1])
            x_coords.append(edge[1][0])
            y_coords.append(edge[1][1])
            """
            if idx == 0:
                x_coords.append(edge['start']['vertex'][0])
                y_coords.append(edge['start']['vertex'][1])
            x_coords.append(edge['end']['vertex'][0])
            y_coords.append(edge['end']['vertex'][1])
            """
            ax.arrow(edge[0][0], edge[0][1], edge[1][0] - edge[0][0], edge[1][1] - edge[0][1], color='red', head_width=0.5)#2

        #ax.plot(x_coords, y_coords, 'r-')

        margin = 0.1 * self.voronoi.points.ptp(axis=0)
        xy_min = self.voronoi.points.min(axis=0) - margin
        xy_max = self.voronoi.points.max(axis=0) + margin
        ax.set_xlim(xy_min[0], xy_max[0])
        ax.set_ylim(xy_min[1], xy_max[1])

        pl.show()
        pl.savefig(output_folder + 'debug.png')

    # Cython code
    @staticmethod
    def calculate_weighed_color(list_area_and_color):
        return optimized.calculate_weighed_color(list_area_and_color, len(list_area_and_color))

    @staticmethod
    def edge_equals(a, b):
        dx = a['intersection'][0] - b['intersection'][0]
        dy = a['intersection'][1] - b['intersection'][1]
        return - 0.000001 < dx < 0.000001 and - 0.000001 < dy < 0.000001

    # Cython code
    def ccw(self, Ax, Ay, Bx, By, Cx, Cy):
        return optimized.ccw(Ax, Ay, Bx, By, Cx, Cy)

    # Cython code
    def get_line_intersection(self, Ax, Ay, Bx, By, Cx, Cy, Dx, Dy):
        return optimized.get_line_intersection(Ax, Ay, Bx, By, Cx, Cy, Dx, Dy)

    def write_to_pickle(self):
        print('Started saving to pickle!')
        obj = {
            'size': {
                'width': self.width,
                'height': self.height
            },
            'sites': self.sites,
            'site_colors': self.site_colors

        }
        pickle.dump(obj, open(output_folder + 'dump.pickle', 'wb'))
        print('Finished saving to pickle!')

    def read_from_pickle(self, file_name):
        print('Started loading from pickle!')
        obj = pickle.load(open(input_folder + file_name, 'rb'))
        self.width = obj['size']['width']
        self.height = obj['size']['height']
        self.sites = obj['sites']
        self.site_colors = obj['site_colors']
        print('Finished loading from pickle!')

if __name__ == '__main__':
    NaturalNeighbourInterpolation.get_edges_for_region = memoize_edges_for_region(NaturalNeighbourInterpolation.get_edges_for_region)

    import pyximport

    pyximport.install(setup_args={
        'include_dirs': np.get_include()
    })

    import optimized

    # Run random points on 300x300 image with 1000 samples
    #nni = NaturalNeighbourInterpolation('300x300.jpg', 1000)
    #nni.generate_image()

    # Run random points on 300x300 image with 10000 samples
    #nni = NaturalNeighbourInterpolation('300x300.jpg', 10000)
    #nni.generate_image()

    # Run random points on 1024x1024 image with 100000 samples
    nni = NaturalNeighbourInterpolation('1024x1024.jpg', 100000)
    nni.generate_image()

    # Save dump of sampled data
    #nni.write_to_pickle()

    # Read from dump of sampled data
    #nni = NaturalNeighbourInterpolation('dump.pickle')

reason: import numpy,import scipy


text:
from scipy.optimize import minimize
import numpy as np
import os
import sys

def get_dist(cords):

        def distance(a, b):
                return ((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2)**0.5

        D = []
        for i in cords:
                D.append([])
                for j in cords:
                        D[-1].append(distance(i, j))

        return np.array(D)

def job(arr, coords, atoms = None):

	if atoms:

		g = open('temp_mov.xyz', 'w')

	def loss(x0):
		crd = x0.reshape((-1, 3))
		if atoms:
			g.write(str(len(crd)) + '\nComment\n')
			for j in range (len(atoms)):
				g.write(atoms[j] + ' ' + ' '.join(list(map(str, crd[j]))) + '\n')
		D = get_dist(crd)

		res = sum((D.flatten() - arr.flatten())**2)

		return res

	x0 = np.copy(coords).flatten()

	res = minimize(loss, x0).x

	g.close()

	res = res.reshape((-1, 3))

	#print (res)

	return res

max_stars_repo_name: ekraka/TS-GAN

max_issues_repo_name: ekraka/TS-GAN

max_forks_repo_name: ekraka/TS-GAN


text:
import argparse
from rouge_score import rouge_scorer
import spacy
import numpy as np
import nltk
from concurrent.futures import ProcessPoolExecutor

nlp = spacy.load('en_core_web_sm', disable=['ner'])

scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeLsum'], use_stemmer=True)

def eval_one(prediction, target):
    prediction_qs = prediction.split("||||")
    prediction_q = " ".join(prediction_qs)

    target_qs = target.split("||||")
    target_q = " ".join(target_qs)

    pred_q_doc = nlp(prediction_q)
    target_q_doc = nlp(target_q)

    score = scorer.score("\n".join([sent.text for sent in target_q_doc.sents]), "\n".join([sent.text for sent in pred_q_doc.sents]))

    smooth_method = nltk.translate.bleu_score.SmoothingFunction()
    question_b4 = nltk.translate.bleu_score.sentence_bleu([[t.text.lower() for t in target_q_doc]],
                                                          [t.text.lower() for t in pred_q_doc],
                                                          smoothing_function=smooth_method.method2,
                                                          weights=(0.25, 0.25, 0.25, 0.25))

    return (score['rouge1'].fmeasure, score['rouge2'].fmeasure, score['rougeLsum'].fmeasure), question_b4

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--prediction')
    parser.add_argument('--target')
    args = parser.parse_args()

    with open(args.prediction) as f:
        predictions = [line.strip() for line in f]

    with open(args.target) as f:
        targets = [line.strip() for line in f]

    assert len(predictions) == len(targets)
    print(len(predictions), len(targets))

    scores = []
    bleu4_scores = []
    with ProcessPoolExecutor() as executor:
        futures = []
        for prediction, target in zip(predictions, targets):
            futures.append(executor.submit(eval_one, prediction, target))
        for future in futures:
            rs, b4 = future.result()

            scores.append(rs)
            bleu4_scores.append(b4)

    print('R1', np.mean([score[0] for score in scores]))
    print('R2', np.mean([score[1] for score in scores]))
    print('RL', np.mean([score[2] for score in scores]))
    print('B4', np.mean(bleu4_scores))

if __name__ == '__main__':
    main()


text:
#!/usr/bin/env python
"""
Insert a shapelet coefficient file into the visibilities of a measurement set
"""

import sys
import numpy as np
import scipy.constants
#from matplotlib import pyplot as plt
import shapelets
import shapelets.phs
import casacore.tables as tbls
import distutils.dir_util

if __name__ == '__main__':
    from optparse import OptionParser
    o = OptionParser()
    o.set_usage('%prog [options] -c COEFF_FILE MS')
    o.set_description(__doc__)
    o.add_option('-c', '--coeff', dest='cfn', default=None,
        help='Shapelet coefficient file, default: None')
    o.add_option('-m', '--mode', dest='mode', default='add',
        help='\'replace\': Replace the visibilities, \'add\': add to the visibilites, \'subtract\': subtract from visibilites, default: add')
    o.add_option('-d', '--data_column', dest='data_column', default='DATA',
        help='Data column to take visibilities from/write to default: DATA')
    o.add_option('-o', '--outfile', dest='ofn', default=None,
        help='Output measurement set name, default: input file name + \'.shape\'')
    o.add_option('-X','--over', dest='overwrite', action="store_true", default=False,
        help='Over write original visibilities')
    o.add_option('-S','--scale',dest='rescale', default=None,
        help='A flux scaling factor. This is useful if the model was derived from an averaged wideband image and the coefficients need to be rescaled to reverse the averaging, in this situation the rescaling value should be the number of channels imaged. A value of 1 will cause no rescaling Default: Number of channels in the MS')

    #rephasing options
    o.add_option('-p','--phs',dest='phsMode', default='centre',
        help='Placement of source by phase rotating the visibilities, options are centre: the model is inserted at the phase centre no phase rotation is performed ; source: visibilities are rotated to make the centroid of the model the phase centre, then rotated back to the original phase centre ; manual: a manually input (RA,dec) is rotated to and the source inserted. default: centre')
    o.add_option('--ra', dest='ra', default=None,
        help='RA (in radians unless flag set) to phase data to')
    o.add_option('--dec', dest='dec', default=None,
        help='DEC (in radians unless flag set) to phase data to')
    o.add_option('--deg',dest='deg_flag',action='store_true',
        help='Use degrees instead of radians')
    o.add_option('--str',dest='str_flag', action='store_true',
        help='Use hh:mm:ss.sss format for RA and dd:mm:ss.sss format for DEC')

    o.add_option('-b','--beta',dest='beta',default=None,
        help='Override beta (in pixels), can be a 1, 2, or 3 comma seperated values')
    opts, args = o.parse_args(sys.argv[1:])

    #load coefficient file
    if opts.cfn is None:
        sys.exit('error:missing shapelet coefficient file')
    d=shapelets.fileio.readCoeffs(opts.cfn)

    #override beta if input as option
    if opts.beta is None:
        beta=d['beta']
        phi=d['phi']
    else:
        betaList=map(float,opts.beta.split(','))
        if len(betaList)==1:
            beta=[betaList[0],betaList[0]]
            phi=d['phi']
        elif len(betaList)==2:
            beta=betaList
            phi=d['phi']
        elif len(betaList)==3:
            beta=[betaList[0],betaList[1]]
            phi=betaList[2]

    ##scale beta to radians
    #rotM=np.matrix([[np.cos(d['phi']),-1.*np.sin(d['phi'])],[np.sin(d['phi']),np.cos(d['phi'])]])
    #temp0=np.dot(rotM,np.array([(np.pi/180.)*d['dra'],0.]))
    #temp1=np.dot(rotM,np.array([0.,(np.pi/180.)*d['ddec']]))
    #rotDeltas=np.array([np.sqrt(temp0[0,0]**2.+temp0[0,1]**2.), np.sqrt(temp1[0,0]**2.+temp1[0,1]**2.)])
    #betaRad=(2.*np.pi)*rotDeltas*np.array(d['beta']) #there is some mathematical convention issue, something about wavenumber and wavelength...should sort out a clear answer
    #phi=d['phi']

    #scale beta to radians
    rotM=np.matrix([[np.cos(phi),-1.*np.sin(phi)],[np.sin(phi),np.cos(phi)]])
    temp0=np.dot(rotM,np.array([(np.pi/180.)*d['dra'],0.]))
    temp1=np.dot(rotM,np.array([0.,(np.pi/180.)*d['ddec']]))
    rotDeltas=np.array([np.sqrt(temp0[0,0]**2.+temp0[0,1]**2.), np.sqrt(temp1[0,0]**2.+temp1[0,1]**2.)])
    betaRad=(2.*np.pi)*rotDeltas*np.array(beta) #there is some mathematical convention issue, something about wavenumber and wavelength...should sort out a clear answer

    #generate basis functions
    print 'generating UV Basis Functions...',
    if d['mode'].startswith('herm'):
        bfs=shapelets.decomp.genBasisFuncs([betaRad[0],betaRad[1]],d['norder'],phi,fourier=True)
    elif d['mode'].startswith('lag'):
        bfs=shapelets.decomp.genPolarBasisFuncs([betaRad[0],betaRad[1]],d['norder'],phi,fourier=True)
    print len(bfs), 'done'

    #parse where to insert the shapelet model
    if opts.phsMode.startswith('manual'):
        if opts.ra is None or opts.dec is None:
            print 'ERROR: RA or DEC not set'
            exit(1)

        if opts.deg_flag:
            ra=np.pi*float(opts.ra)/180.
            dec=np.pi*float(opts.dec)/180.
        elif opts.str_flag:
            raArr=map(float,opts.ra.split(':'))
            ra=15.*(raArr[0]+raArr[1]/60.+raArr[2]/3600.)
            ra=np.pi*ra/180.
            decArr=map(float,opts.dec.split(':'))
            dec=np.abs(decArr[0])+decArr[1]/60.+decArr[2]/3600.
            if decArr[0] < 0.: dec*=-1.
            dec=np.pi*dec/180.
        else:
            ra=float(opts.ra)
            dec=float(opts.dec)
        phsRotate=True
    elif opts.phsMode.startswith('source'):
        ra=np.pi*d['ra']/180.
        dec=np.pi*d['dec']/180.
        phsRotate=True
    else:
        phsRotate=False

    if phsRotate:
        print 'Inserting shapelet model at RA=%f Dec=%f (mode=%s)'%(ra,dec,opts.phsMode)
    else:
        print 'Inserting shapelet model at phase centre'

    #load MS, get visibilites and u,v,w positions
    data_column=opts.data_column.upper()
    for fid,fn in enumerate(args):

        if opts.overwrite:
            ofn=fn
        else:
            ofn=fn+'.shape'
            distutils.dir_util.copy_tree(fn,ofn)
        print 'working on: %s (%i of %i)'%(ofn,fid+1,len(args))

        #get the initial phase centre
        field=pt.table(ofn+'/FIELD')
        phaseCentre=field.col('PHASE_DIR').getcol()[0,0]
        field.close()
        if phsRotate:
            print 'Phasing to RA: %1.10f, DEC: %1.10f'%(ra,dec)
            MS=shapelets.phs.ClassMS.ClassMS(ofn,Col=data_column)
            MS.RotateMS((ra,dec))
            MS.SaveVis(Col=data_column)
            print 'done'

        MS=tbls.table(ofn,readonly=True)
        uvw=MS.col('UVW').getcol() # [vis id, (u,v,w)]
        vis=MS.col(data_column).getcol() #[vis id, freq id, stokes id]
        MS.close()

        #gather channel frequency information
        SW=tbls.table(ofn+'/SPECTRAL_WINDOW')
        freqs=SW.col('CHAN_FREQ').getcol()
        cc=scipy.constants.c
        uu=uvw[:,0]
        vv=uvw[:,1]
        uu=np.reshape(uu,(uvw.shape[0],1))
        vv=np.reshape(vv,(uvw.shape[0],1))
        #convert u,v to units of wavelengths
        uu=(uu*freqs)/cc
        vv=(vv*freqs)/cc
        SW.close()

        #rescaling value
        #flux scaling is too low if the model was derived form a wideband averaged image, in that case the correct rescaling is the number of channels
        if opts.rescale is None:
            rescale=float(freqs.shape[1])
        else: rescale=float(opts.rescale)
        print 'Rescale factor: %f'%rescale

        #evaulate basis functions at each u,v postion
        print 'Evaluating shapelet basis functions for all (u,v) positions'
        shapeVis=np.zeros_like(vis[:,:,0]).flatten() #only doing this for Stokes I
        for bfid,bf in enumerate(bfs):
            #TODO: visibilites seem to be rotated by 90 degrees
            #TODO: i think this line is correct, but the rotation and mirroring leads me to use the line below -> shapeVis+=d['coeffs'][bfid]*shapelets.shapelet.computeBasis2d(bf,uu.flatten(),vv.flatten())
            #TODO: this could be at least partially due to the sign of the FITS ddec and dra
            shapeVis+=d['coeffs'][bfid]*shapelets.shapelet.computeBasis2d(bf,-1.*vv.flatten(),-1.*uu.flatten())
        #shapeVis+=1.*shapelets.shapelet.computeBasis2d(bfs[1],uu.flatten(),vv.flatten())
        shapeVis=rescale*np.reshape(shapeVis,uu.shape)
        print 'done'

        #update visibilites
        #TODO: assuming unpolarized source for the moment, this needs to be fixed properly
        print 'Updating visibilities (mode:%s)'%opts.mode
        if opts.mode.startswith('replace'):
            newVis=np.zeros_like(vis)
            newVis[:,:,0]=shapeVis/2. #XX
            newVis[:,:,3]=shapeVis/2. #YY
        elif opts.mode.startswith('add'):
            newVis=vis
            newVis[:,:,0]+=shapeVis/2. #XX
            newVis[:,:,3]+=shapeVis/2. #YY
        elif opts.mode.startswith('sub'):
            newVis=vis
            newVis[:,:,0]-=shapeVis/2. #XX
            newVis[:,:,3]-=shapeVis/2. #YY
        else:
            print 'Unknown MS update mode, the MS will be left unchanged'
        print 'done'

        print 'writing to:',ofn
        MS=tbls.table(ofn,readonly=False)
        MS.putcol(data_column, newVis)
        MS.close()
        print 'done'

        if phsRotate:
            print 'Phase rotating back to original phase centre...'
            print 'Phasing to RA: %1.10f, DEC: %1.10f'%(phaseCentre[0],phaseCentre[1])
            MS=shapelets.phs.ClassMS.ClassMS(ofn,Col=data_column)
            MS.RotateMS((phaseCentre[0],phaseCentre[1]))
            MS.SaveVis(Col=data_column)
            print 'done'

max_stars_repo_name: richarms/shapelets

max_issues_repo_name: richarms/shapelets

max_forks_repo_name: richarms/shapelets


text:
# -*- coding: utf-8 -*-
"""
==============================================================================
@author: Nikolaos Giakoumoglou
@date: Sat May  8 12:14:26 2021
==============================================================================
"""

import numpy as np

def shape_parameters(f, mask, perimeter, pixels_per_mm2=1):
    '''
    Parameters
    ----------
    f : numpy ndarray
        Image of dimensions N1 x N2.
    mask : numpy ndarray
        Mask image N1 x N2 with 1 if pixels belongs to ROI, 0 else. Give None
        if you want to consider ROI the whole image.
    perimeter : numpy ndarray
         Image N1 x N2 with 1 if pixels belongs to perimeter of ROI, 0 else.
    pixels_per_mm2 : int, optional
        Density of image f. The default is 1.

    Returns
    -------
    features : numpy ndarray
        1,2)X, Y coordinate max length, 3)area, 4)perimeter, 5)perimeter2/area.
    labels : list
        Labels of features.
    '''

    if mask is None:
        mask = np.ones(f.shape)

    # 1) Labels
    labels = ["SHAPE_XcoordMax", "SHAPE_YcoordMax", "SHAPE_area",
              "SHAPE_perimeter", "SHAPE_perimeter2perArea"]

    # 2) Parameters
    mask = np.array(mask, np.int32)
    perimeter = np.array(perimeter, np.int32)
    N1, N2 = mask.shape

    # 3) Find X max coordinate
    start, end = 0, N1
    for x in range(N1):
        if (sum(mask[x,:])>0) & (start==None):
            start = x
        if (sum(mask[x,:])==0) & (start != None):
            end = x-1
            break
    Xcoord_max = end - start + 1

    # 4) Find Y max coordinate
    start, end = 0, N2
    for x in range(N2):
        if (sum(mask[:,x])>0) & (start==None):
            start = x
        if (sum(mask[:,x])==0) & (start != None):
            end = x-1
            break
    Ycoord_max = end - start + 1

    # 5) Calculate Features
    features = np.zeros(5,np.double)
    features[0] = Xcoord_max / pixels_per_mm2
    features[1] = Ycoord_max / pixels_per_mm2
    features[2] = mask.sum() / (pixels_per_mm2 ** 2)
    features[3] = perimeter.sum() / pixels_per_mm2
    features[4] = (features[3] ** 2)/features[2]

    return features, labels


text:
def dataCleaning(marriageNum='Full', classNum=2, dropColumns=True):

    '''
    This is the data cleaning function for the CE264 final project: Marriage Analysis
    Return a pandas data frame and a numpy array: data (X) and observation (y)
    The name of each column refers to the JGSS data codebook
    For some combined categories in a particular data feature, please refer to the source code

    Parameters:
    marriageNum: integer or 'Full'(as default); the number of sampled 'Marriage' class,
                 should be smaller than its actual number
    classNum: 2(as default), 3 or 'Full'; the class number of the observation;
              if 2, class 1 for 'Currently Marriage' and 2 for other
              if 3, class 1 for 'Currently Marriage', 2 for 'Divorced', and 3 for other
              if 'Full', check the code book for the meaning of each class
    dropColumns: whether drop the columns that may have correlation with the marriage status (default is True)
    '''

    # parameter check
    if classNum not in [2, 3, 'Full']:
        raise ValueError('Invalid class number: should be 2, 3, or "Full".')

    # import libraries and raw data
    import numpy as np
    import pandas as pd
    rawData = pd.read_stata('34623-0001-Data.dta')

    # data selected primarily by genetic algorithm
    selectedCol = ['SIZE', 'SEXA', 'TP5UNEMP', 'AGESTPWK', 'SZSJBHWK', 'TPJOBP', 'TPJBDP', 'SZCMTHR', 'XXJOB',
                   'XJOBDWK', 'SZTTLSTA', 'ST5JOB', 'SSJB1WK', 'SSTPUNEM', 'SSSJBHWK', 'SSTPJOB','SSTPJBDP',
                   'SSTPJBSE', 'SSXJBSCH', 'SSSZWKYR', 'SSSZSTFA', 'SPAGEX', 'SPLVTG', 'PPLVTG', 'MMLVTG', 'PPAGE',
                   'MMAGE', 'MMJOB', 'CCNUMTTL', 'CC01SEX', 'CC01AGE', 'CC02LVTG', 'CC02AGE', 'CC02MG', 'CC03MG',
                   'CC03JOB', 'CC04LVTG', 'CC04MG', 'CC05SEX', 'CC05JOB', 'CC06SEX', 'CC06AGE', 'CC07SEX',
                   'CC07LVTG', 'CC07AGE', 'CC07JOB', 'CC08MG', 'CC08JOB', 'SZFFOTHR', 'FFH01REL', 'FFH03SEX',
                   'FFH04REL', 'FFH04SEX', 'FFH05REL', 'FFH05SEX', 'FFH07REL', 'FFH07SEX', 'FFH07AGE', 'SZFFONLY',
                   'SZFFTTL', 'FFHEAD', 'SZFFOUT', 'FFO01REL', 'FFO01WHY', 'FFO02REL', 'FFO02WHY', 'FFO03REL',
                   'FFO03WHY', 'FFO05REL', 'FFO05WHY', 'FFO06REL', 'FFO06WHY', 'INCSELF', 'INCSP', 'INCPEN',
                   'INCUEB', 'INCIRR', 'INCRENT', 'INCMAIN', 'SZINCOMA', 'XNUMSISE', 'XNUMBROY', 'XSSNBROY',
                   'XSSNSISY', 'PREF15', 'TP5LOC15', 'PPJBXX15', 'PPJBSZ15', 'MMJBTP15', 'XXLSTSCH','SSLSTSCH',
                   'PPLSTSCH', 'DOLSTSCH', 'XGRADE', 'XSPSCH', 'MARC']
    selectedData = rawData[selectedCol]

    # filter data with more than 1500 'NA' values
    NAfilter = []
    for i in selectedCol:
        if len(selectedData[selectedData[i] == 'Not applicable']) < 1500:
            NAfilter += [i]
    selectedData = selectedData[NAfilter]

    # sample the marriage class
    if marriageNum != 'Full':
        marriageClass = selectedData[selectedData['MARC'] == 'Currently married']
        dropNum = len(marriageClass) - marriageNum
        if not dropNum > 0:
            raise ValueError('The number of sampled "Marriage" class should be smaller than its actual number. Try another value.')
        drop_id = np.random.choice(marriageClass.index, dropNum, replace=False)
        selectedData = selectedData.drop(drop_id)

    # data cleaning...
    selectedData['SIZE'].replace('Largest cities', 1, inplace=True)
    selectedData['SIZE'].replace('Cities with population of 200000 or more', 2, inplace=True)
    selectedData['SIZE'].replace('Cities with population of less than 200000', 3, inplace=True)
    selectedData['SIZE'].replace('Town/village', 4, inplace=True)
    selectedData['SEXA'].replace('Male', 1, inplace=True)
    selectedData['SEXA'].replace('Female', 2, inplace=True)
    selectedData['SSJB1WK'].replace('He/she worked last week.', 1, inplace=True)
    selectedData['SSJB1WK'].replace('He/she was going to work last week, but did not work.', 2, inplace=True)
    selectedData['SSJB1WK'].replace('He/she did not work.', 3, inplace=True)
    selectedData['SPLVTG'].replace('Living together', 1, inplace=True)
    selectedData['SPLVTG'].replace('Not living together (because of work circumstances)', 2, inplace=True)
    selectedData['SPLVTG'].replace('Not living together (for other reasons)', 3, inplace=True)
    selectedData['PPLVTG'].replace('Living together', 1, inplace=True)
    selectedData['PPLVTG'].replace('Not living together', 2, inplace=True)
    selectedData['PPLVTG'].replace('Deceased', 3, inplace=True)
    selectedData['MMLVTG'].replace('Living together', 1, inplace=True)
    selectedData['MMLVTG'].replace('Not living together', 2, inplace=True)
    selectedData['MMLVTG'].replace('Deceased', 3, inplace=True)
    selectedData['CC01SEX'].replace('Male', 1, inplace=True)
    selectedData['CC01SEX'].replace('Female', 2, inplace=True)
    for i in np.unique(selectedData['FFHEAD']):
        if i not in ['Respondent himself/herself', 'Husband', 'Wife']:
            selectedData['FFHEAD'].replace(i, 4, inplace=True)
    selectedData['FFHEAD'].replace('Respondent himself/herself', 1, inplace=True)
    selectedData['FFHEAD'].replace('Husband', 2, inplace=True)
    selectedData['FFHEAD'].replace('Wife', 3, inplace=True)
    selectedData['INCSELF'].replace('Chosen', 0, inplace=True)
    selectedData['INCSELF'].replace('Not chosen', 1, inplace=True)
    selectedData['INCSP'].replace('Chosen', 1, inplace=True)
    selectedData['INCSP'].replace('Not chosen', 1, inplace=True)
    selectedData['INCPEN'].replace('Chosen', 0, inplace=True)
    selectedData['INCPEN'].replace('Not chosen', 1, inplace=True)
    selectedData['INCUEB'].replace('Chosen', 0, inplace=True)
    selectedData['INCUEB'].replace('Not chosen', 1, inplace=True)
    selectedData['INCIRR'].replace('Chosen', 0, inplace=True)
    selectedData['INCIRR'].replace('Not chosen', 1, inplace=True)
    selectedData['INCRENT'].replace('Chosen', 0, inplace=True)
    selectedData['INCRENT'].replace('Not chosen', 1, inplace=True)
    selectedData.drop('INCMAIN', axis=1, inplace=True)
    for i in np.unique(selectedData['SZINCOMA']):
        if i == 'None':
            selectedData['SZINCOMA'].replace(i, 1, inplace=True)
        elif i in ['Less than 700,000 yen', '700,000 yen - 1 million yen']:
            selectedData['SZINCOMA'].replace(i, 2, inplace=True)
        elif i in ['1 million yen - 1.3 million yen',
                   '1.3 million yen - 1.5 million yen',
                   '1.5 million yen - 2.5 million yen']:
            selectedData['SZINCOMA'].replace(i, 3, inplace=True)
        elif i in ['2.5 million yen - 3.5 million yen',
                   '3.5 million yen - 4.5 million yen',
                   '4.5 million yen - 5.5 million yen',]:
            selectedData['SZINCOMA'].replace(i, 4, inplace=True)
        elif i in ['5.5 million yen - 6.5 million yen',
                   '6.5 million yen - 7.5 million yen',
                   '7.5 million yen - 8.5 million yen',
                   '8.5 million yen - 10 million yen',
                   '10 million yen - 12 million yen',
                   '12 million yen - 14 million yen',
                   '14 million yen - 16 million yen',
                   '16 million yen - 18.5 million yen',
                   '18.5 million yen - 23 million yen',]:
            selectedData['SZINCOMA'].replace(i, 5, inplace=True)
        else:
            selectedData['SZINCOMA'].replace(i, 0, inplace=True)
    selectedData.drop('PREF15', axis=1, inplace=True)
    selectedData['TP5LOC15'].replace('Large city', 1, inplace=True)
    selectedData['TP5LOC15'].replace('Small to medium sized city', 2, inplace=True)
    selectedData['TP5LOC15'].replace('Town', 3, inplace=True)
    selectedData['TP5LOC15'].replace('Village', 4, inplace=True)
    for i in np.unique(selectedData['PPJBXX15']):
        if i == 'managers in companies/organizations':
            selectedData['PPJBXX15'].replace(i, 1, inplace=True)
        elif i == 'Not applicable':
            selectedData['PPJBXX15'].replace(i, 0, inplace=True)
        else:
            selectedData['PPJBXX15'].replace(i, 2, inplace=True)
    selectedData['PPJBSZ15'].replace('1', 1, inplace=True)
    selectedData['PPJBSZ15'].replace('Small company (2-29 employees)', 2, inplace=True)
    selectedData['PPJBSZ15'].replace('Medium-sized company (30-299 employees)', 3, inplace=True)
    selectedData['PPJBSZ15'].replace('Large company (300-999 employees)', 4, inplace=True)
    selectedData['PPJBSZ15'].replace('Major company (1000 or more employees', 5, inplace=True)
    selectedData['PPJBSZ15'].replace('Government agency', 6, inplace=True)
    selectedData['PPJBSZ15'].replace("Don't know", 0, inplace=True)
    for i in np.unique(selectedData['MMJBTP15']):
        if i == 'She was not working.':
            selectedData['MMJBTP15'].replace(i, 1, inplace=True)
        elif i == 'Temporary worker, Daily worker, Part-time temporary worker':
            selectedData['MMJBTP15'].replace(i, 2, inplace=True)
        elif i in ["Regular employee - don't know about occupation",
                   'Regular employee - managerial position',
                   'Regular employee - non-management',
                   'Regular employee - professional (nurse, teacher, etc.)']:
            selectedData['MMJBTP15'].replace(i, 3, inplace=True)
        elif i in ["Don't know", 'No answer']:
            selectedData['MMJBTP15'].replace(i, 0, inplace=True)
        else:
            selectedData['MMJBTP15'].replace(i, 4, inplace=True)
    for i in np.unique(selectedData['XXLSTSCH']):
        if i in ['Ordinary elementary school in the old system',
                 'Higher elementary school in the old system']:
            selectedData['XXLSTSCH'].replace(i, 1, inplace=True)
        elif i in ["Junior high school/Girls' high school in the old system",
                 'Vocational school/Commerce school in the old system',
                 'Normal school in the old system',
                 'Higher school or vocational school in the old system',
                 'Junior high school',
                 'High school']:
            selectedData['XXLSTSCH'].replace(i, 2, inplace=True)
        elif i in ['No answer', "Don't know"]:
            selectedData['XXLSTSCH'].replace(i, 0, inplace=True)
        else:
            selectedData['XXLSTSCH'].replace(i, 3, inplace=True)
    for i in np.unique(selectedData['SSLSTSCH']):
        if i in ['Ordinary elementary school in the old system',
                 'Higher elementary school in the old system']:
            selectedData['SSLSTSCH'].replace(i, 1, inplace=True)
        elif i in ["Junior high school/Girls' high school in the old system",
                 'Vocational school/Commerce school in the old system',
                 'Normal school in the old system',
                 'Higher school or vocational school in the old system',
                 'Junior high school',
                 'High school']:
            selectedData['SSLSTSCH'].replace(i, 2, inplace=True)
        elif i in ['No answer', "Don't know", 'Never-married/Divorced']:
            selectedData['SSLSTSCH'].replace(i, 0, inplace=True)
        else:
            selectedData['SSLSTSCH'].replace(i, 3, inplace=True)
    for i in np.unique(selectedData['PPLSTSCH']):
        if i in ['Ordinary elementary school in the old system',
                 'Higher elementary school in the old system']:
            selectedData['PPLSTSCH'].replace(i, 1, inplace=True)
        elif i in ["Junior high school/Girls' high school in the old system",
                 'Vocational school/Commerce school in the old system',
                 'Normal school in the old system',
                 'Higher school or vocational school in the old system',
                 'Junior high school',
                 'High school']:
            selectedData['PPLSTSCH'].replace(i, 2, inplace=True)
        elif i in ['No answer', "Don't know"]:
            selectedData['PPLSTSCH'].replace(i, 0, inplace=True)
        else:
            selectedData['PPLSTSCH'].replace(i, 3, inplace=True)
    selectedData['DOLSTSCH'].replace('Graduated', 1, inplace=True)
    selectedData['DOLSTSCH'].replace('Quit', 2, inplace=True)
    selectedData['DOLSTSCH'].replace('Still a student', 3, inplace=True)
    selectedData['XSPSCH'].replace('Yes', 1, inplace=True)
    selectedData['XSPSCH'].replace('No', 2, inplace=True)

    # drop those terms correlated to marriage status
    selectedData.drop('SSJB1WK', axis=1, inplace=True)
    selectedData.drop('SPAGEX', axis=1, inplace=True)
    selectedData.drop('SPLVTG', axis=1, inplace=True)
    selectedData.drop('INCSP', axis=1, inplace=True)
    selectedData.drop('SSLSTSCH', axis=1, inplace=True)
    selectedData.drop('XSSNBROY', axis=1, inplace=True)
    selectedData.drop('XSSNSISY', axis=1, inplace=True)

    if dropColumns == True:
        selectedData.drop('CCNUMTTL', axis=1, inplace=True)
        selectedData.drop('CC01SEX', axis=1, inplace=True)
        selectedData.drop('CC01AGE', axis=1, inplace=True)
        selectedData.drop('SZFFOTHR', axis=1, inplace=True)
        selectedData.drop('SZFFONLY', axis=1, inplace=True)
        selectedData.drop('SZFFTTL', axis=1, inplace=True)
        selectedData.drop('FFHEAD', axis=1, inplace=True)

    selectedData.replace('No answer', 0, inplace=True)
    selectedData.replace('Not applicable', 0, inplace=True)

    # observation cleaning
    observation = np.array(selectedData['MARC'])
    cleanedObservation = np.zeros(len(observation))

    if classNum == 2:
        for i in range(len(observation)):
            if observation[i] == 'Currently married':
                cleanedObservation[i] = 1
            else:
                cleanedObservation[i] = 2
    elif classNum == 3:
        for i in range(len(observation)):
            if observation[i] == 'Currently married':
                cleanedObservation[i] = 1
            elif observation[i] == 'Divorced':
                cleanedObservation[i] = 2
            else:
                cleanedObservation[i] = 3
    else:
         for i in range(len(observation)):
            if observation[i] == 'Currently married':
                cleanedObservation[i] = 1
            elif observation[i] == 'Divorced':
                cleanedObservation[i] = 2
            elif observation[i] == 'Widowed':
                cleanedObservation[i] = 3
            elif observation[i] == 'Never-married':
                cleanedObservation[i] = 4
            elif observation[i] == 'Separated':
                cleanedObservation[i] = 5
            elif observation[i] == 'Cohabiting':
                cleanedObservation[i] = 6
            else:
                cleanedObservation[i] = 9

    selectedData.drop('MARC', axis=1, inplace=True)
    cleanedData = selectedData.copy()
    cleanedObservation = cleanedObservation.astype(int)

    return cleanedData, cleanedObservation

def svm_test():
    #you need to import pandas, numpy, sklearn.svm.SVC, sklearn to use this function
    data = pd.read_csv('cleanedData.csv',header = None)
    data = np.asarray(data)[1:,1:]
    result = pd.read_csv('cleanedObservation.csv',header = None)
    result = np.asarray(result)[1:,1]
    trainSample = np.random.choice(range(5000), 4000, replace=False)
    trainData = data[trainSample,:]
    trainObservation = result[trainSample]
    testSample = np.setdiff1d(np.arange(5003), trainSample)
    testData = data[testSample,:]
    testObservation = result[testSample]
    prediction = clf.predict(testData)
    accuracy = np.sum(np.abs(prediction.astype(int) - testObservation.astype(int)))
    score = sklearn.metrics.accuracy_score(testObservation, prediction)
    return accuracy, score

def decision_tree():
    df1 = pd.read_csv('cleanedData.csv')
    df2 = pd.read_csv('cleanedObservation.csv')
    x = df1[df1.columns[1:]]
    y = df2['MARC']
    x_new = SelectKBest(chi2, k=10).fit_transform(x, y)
    x_train,x_test,y_train,y_test = cross_validation.train_test_split(x_new,y,test_size=0.4,random_state=0)
    score = []
    i_range = range(3,40)
    for i in range(3,40):
        clf = tree.DecisionTreeClassifier(max_depth=i)
        clf = clf.fit(x_train,y_train)
        score.append((clf.score(x_test,y_test)))
    ##plt.plot(i_range, score)
    depth = score.index(max(score)) + 5
    clf = tree.DecisionTreeClassifier(max_depth= depth)
    clf = clf.fit(x_train,y_train)
    score = max(score)
    accuracy = np.sum(np.abs(clf.predict(x_test).astype(int) - y_test.astype(int)))
    return score, accuracy,metrics.confusion_matrix(y_test, clf.predict(x_test))

max_issues_repo_name: QinganZhao/Marriage-Analysis

max_forks_repo_name: QinganZhao/Marriage-Analysis


text:
from pathlib import PosixPath

import uproot
import numpy as np

from tdub.config import AVOID_IN_CLF
from tdub.frames import iterative_selection, drop_avoid
from tdub.frames import apply_weight_trrw, apply_weight_tptrw, apply_weight_inverse
from tdub.data import selection_branches

test_file_root = PosixPath(__file__).parent / "test_data"

def test_exclude_avoids():
    files = [
        str(test_file_root / "testfile1.root"),
        str(test_file_root / "testfile2.root"),
        str(test_file_root / "testfile3.root"),
    ]
    df = iterative_selection(files, "(reg1j1b == True)", exclude_avoids=True)
    cols = set(df.columns)
    avoid = set(AVOID_IN_CLF)
    assert len(cols & avoid) == 0

    df = iterative_selection(
        files,
        "(reg1j1b == True)",
        exclude_avoids=True,
        keep_category="kinematics",
    )
    cols = set(df.columns)
    avoid = set(AVOID_IN_CLF)
    assert len(cols & avoid) == 0

def test_drop_avoid():
    files = [
        str(test_file_root / "testfile1.root"),
        str(test_file_root / "testfile2.root"),
        str(test_file_root / "testfile3.root"),
    ]
    df = iterative_selection(files, "(reg1j1b == True)")
    df.drop_avoid()
    avoid = set(AVOID_IN_CLF)
    cols = set(df.columns)
    assert len(cols & avoid) == 0

def test_drop_jet2():
    files = [
        str(test_file_root / "testfile1.root"),
        str(test_file_root / "testfile3.root"),
    ]
    df = iterative_selection(files, "(OS == True)")
    j2s = [col for col in df.columns if "jet2" in col]
    df.drop_jet2()
    for j in j2s:
        assert j not in df.columns

def test_selection_augmented():
    files = [
        str(test_file_root / "testfile1.root"),
        str(test_file_root / "testfile3.root"),
    ]
    df = iterative_selection(
        files, "(OS == True) & (reg1j1b == True) & (mass_lep1jet1 < 155)"
    )
    sel_vars = set(selection_branches(df.selection_used))
    manual = {"OS", "reg1j1b", "mass_lep1jet1"}
    assert sel_vars == manual
    assert (
        df.selection_used == "(OS == True) & (reg1j1b == True) & (mass_lep1jet1 < 155)"
    )

def test_selection_strings():
    files = [
        str(test_file_root / "testfile1.root"),
        str(test_file_root / "testfile3.root"),
    ]
    root_sel1 = "OS == 1 && reg2j2b == 1 && mass_lep1jet1 < 155"
    nume_sel1 = "(OS == 1) & (reg2j2b == 1) & (mass_lep1jet1 < 155)"
    root_sel2 = "OS == true && reg2j2b == true && mass_lep1jet1 < 155"
    nume_sel2 = "(OS == True) & (reg2j2b == True) & (mass_lep1jet1 < 155)"
    df_r_sel1 = iterative_selection(files, root_sel1)
    df_r_sel2 = iterative_selection(files, root_sel2)
    df_n_sel1 = iterative_selection(files, nume_sel1)
    df_n_sel2 = iterative_selection(files, nume_sel2)
    assert df_r_sel1.equals(df_r_sel2)
    assert df_r_sel1.equals(df_n_sel1)
    assert df_r_sel1.equals(df_n_sel2)

def test_apply_weight_tptrw():
    f = test_file_root / "testfile4.root"
    t = uproot.open(f).get("WtLoop_nominal")
    w1 = t.arrays(["weight_nominal"], library="np")["weight_nominal"]
    w2 = t.arrays(["weight_sys_pileup_DOWN"], library="np")["weight_sys_pileup_DOWN"]
    wr = t.arrays(["weight_tptrw_tool"], library="np")["weight_tptrw_tool"]
    r1 = w1 * wr
    r2 = w2 * wr
    df = t.arrays(library="pd")
    apply_weight_tptrw(df)
    rr1 = df["weight_nominal"].to_numpy()
    rr2 = df["weight_sys_pileup_DOWN"].to_numpy()
    assert np.allclose(r1, rr1)
    assert np.allclose(r2, rr2)

def test_apply_weight_trrw():
    f = test_file_root / "testfile4.root"
    t = uproot.open(f).get("WtLoop_nominal")
    w1 = t.arrays(["weight_nominal"], library="np")["weight_nominal"]
    w2 = t.arrays(["weight_sys_pileup_DOWN"], library="np")["weight_sys_pileup_DOWN"]
    wr = t.arrays(["weight_trrw_tool"], library="np")["weight_trrw_tool"]
    r1 = w1 * wr
    r2 = w2 * wr
    df = t.arrays(library="pd")
    apply_weight_trrw(df)
    rr1 = df["weight_nominal"].to_numpy()
    rr2 = df["weight_sys_pileup_DOWN"].to_numpy()
    assert np.allclose(r1, rr1)
    assert np.allclose(r2, rr2)

max_stars_repo_name: douglasdavis/tdub

max_issues_repo_name: douglasdavis/tdub

max_forks_repo_name: douglasdavis/tdub


text:
import sys
import pickle
import numpy as np
import subprocess
import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 6})
import sys
sys.path.append('../code')
from tools import general_tools as gt
from tools import post_processing_tools as ppt
import warnings

def main():
	""" Open the plot of a Forward systematic search analysis. """
	resultsFolder = "../../results/"
	toLoad = "2018_09_24_static_reflex_recording_min0.8_max1.5batchSRR_seedn_0__SS_RORa_w1_0.0_w2_-0.01_w3_0.022_sr.p"
	meanFr = load_results(toLoad, resultsFolder)

def load_results(pattern,resultsFolder):
	fileToLoad = gt.find(pattern, resultsFolder)
	if not fileToLoad:
		warnings.warn('\tfile with pattern: '+pattern+' not found')
		return None
	else:
		print "\tloading file: "+fileToLoad[0]
		with open(fileToLoad[0], 'r') as pickle_file:
			_ = pickle.load(pickle_file)
			meanFr = pickle.load(pickle_file)
		return meanFr

if __name__ == '__main__':
	main()

max_stars_repo_name: neurorestore/neuraldynamicplatform

max_issues_repo_name: neurorestore/neuraldynamicplatform

max_forks_repo_name: neurorestore/neuraldynamicplatform


text:
from unittest import TestCase
from teafacto.blocks.basic import IdxToOneHot, VectorEmbed
import theano, numpy as np

class TestFloat32(TestCase):
    def setUp(self):
        theano.config.floatX = "float32"

    def test_idxtoonehot(self):
        m = IdxToOneHot(100)
        self.assertEqual(m.W.dtype, "float32")

    def test_vectorembed_init(self):
        m = VectorEmbed(indim=100, dim=100)
        self.assertEqual(m.W.dtype, "float32")

    def test_vectorembed_cast(self):
        md = np.random.random((100, 100))
        m = VectorEmbed(indim=100, dim=100, value=md)
        self.assertEqual(m.W.dtype, "float32")

max_stars_repo_name: lukovnikov/teafacto

max_issues_repo_name: lukovnikov/teafacto

max_forks_repo_name: lukovnikov/teafacto

reason: import theano


text:
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

tiempoMuestreo = 0.0001 #Tiempo de muestreo
umbral = 0.5 #Umbral de amplitud de 0.5

tiempo = np.arange(0.0,0.05,tiempoMuestreo) #Se crea un arreglo que contiene todos los pasos discretos de tiempo de la señal que se va a generar.

#Se crea la señal que consiste de una onda senoidal con frecuencia de 440 Hz (un La en la cuarta octava del piano) y amplitud de 5 y otra con una frecuencia de 523.25 Hz (un Do en la quinta octava del piano) y amplitud de 3.
#También se le añade ruido usando la función random.normal de NumPy, este con una media de 0, una desviación estandar de 0.5 y una ampitud de 2.
señal = 5*np.sin(2*np.pi*440*tiempo) + 3*np.sin(2*scipy.pi*523.25*tiempo) + 2*np.random.normal(0, 0.5, tiempo.shape)

transformada = scipy.fftpack.rfft(señal) #Se calcula la transformada rápida de Fourier usando la función de SciPy.

transformadaMagnitud = np.abs(transformada) #Se calcula la magnitud de la transformada de Fourier de la señal de entrada.

#Se calculan las frecuencias respectivas de la señal de entrada con la frecuencia de muestreo
frec_señal = np.linspace(0.0, 1.0/(2.0*tiempoMuestreo), señal.size)

#Se define la función para filtrar la señal y obtener la señal filtrada
def Filtrar_Señal(señal, umbral):
    #Se genera una copia de la transformada obtenida para filtrar
    señalFiltrada = señal.copy()

    #Se filtran la señal usando la fft en cada una de las frecuencias
    #Si la amplitud en el punto es menor al umbral, se deja en 0.
    for i in range(0, señalFiltrada.shape[0]):
        if (2.0/señal.size)*np.abs(señalFiltrada[i]) <= umbral:
            señalFiltrada[i] = 0

    #Se retorna el resultado del filtrado.
    return señalFiltrada

#Se obtiene la fft de la señal filtrada.
#Se filtra con el umbral definido, ya que las señales deseadas tienen una amplitud mayor a esta y no van a ser filtradas.
transformadaFiltrada = Filtrar_Señal(transformada, umbral)

#Se calcula la magnitud de la transformada de Fourier de la señal filtrada.
transformadaFiltradaMagnitud = np.abs(transformadaFiltrada)

#Se calcula la señal filtrada de la transformada de Fourier filtrada.
señalFiltrada = scipy.fftpack.irfft(transformadaFiltrada)

#Se grafica la señal de entrada.
fig1, ax1 = plt.subplots(1, 1)
ax1.plot(tiempo,señal)
ax1.set_title('Señal de entrada')
ax1.set_xlabel('Tiempo (s)')
ax1.set_ylabel('Amplitud (u.l.)')

#Se grafica la transformada de Fourier de la señal de entrada.
fig2, ax2 = plt.subplots(1, 1)
ax2.plot(frec_señal, (2.0/señal.size)*transformadaMagnitud)
ax2.set_title('Transformada de Fourier de la señal de entrada (Magnitud)')
ax2.set_xlabel('Frecuencia (Hz)')
ax2.set_ylabel('Amplitud (u.l.)')

#Se grafica la señal filtrada.
fig3, ax3 = plt.subplots(1, 1)
ax3.plot(tiempo,señalFiltrada)
ax3.set_title('Señal filtrada')
ax3.set_xlabel('Tiempo (s)')
ax3.set_ylabel('Amplitud (u.l.)')

#Se grafica la transformada de Fourier de la señal filtrada.
fig4, ax4 = plt.subplots(1, 1)
ax4.plot(frec_señal, (2.0/señal.size)*transformadaFiltradaMagnitud)
ax4.set_title('Transformada de Fourier de la señal filtrada (Magnitud)')
ax4.set_xlabel('Frecuencia (Hz)')
ax4.set_ylabel('Amplitud (u.l.)')

#Se grafican ambas señales para compararlas.
fig5, ax5 = plt.subplots(1, 1)
ax5.plot(tiempo,señal, 'b', label="Señal original")
ax5.plot(tiempo,señalFiltrada, 'r', label="Señal filtrada")
ax5.set_title('Señal original y filtrada')
ax5.set_xlabel('Tiempo (s)')
ax5.set_ylabel('Amplitud (u.l.)')
ax5.legend(loc='upper right')

plt.show()

max_stars_repo_path: Tarea 3.py

max_issues_repo_path: Tarea 3.py

max_forks_repo_path: Tarea 3.py


text:
from numba import jit
import numpy as np
import time

def mult_abs_basic(n, x, y):
    r = []
    for i in range(n):
        r.append(abs(x[i] * y[i]))
    return r

def mult_abs_numpy(x, y):
    return np.abs(x*y)

# @jitデコレータで高速化を図る
@jit('f8[:](i8, c16[:], c16[:])', nopython=True)
def mult_abs_numba(n, x, y):
    r = np.zeros(n)
    for i in range(n):
        r[i] = abs(x[i] * y[i])
    return r

if __name__ == "__main__":
    # %% 処理するデータを生成
    N = 1000000
    x_np = (np.random.rand(N) - 0.5) + 1J * (np.random.rand(N) - 0.5)
    y_np = (np.random.rand(N) - 0.5) + 1J * (np.random.rand(N) - 0.5)
    x = list(x_np)
    y = list(y_np)

    # %% 処理時間比較
    ts = time.clock()
    b1 = mult_abs_basic(N, x, y)
    print('Pythonの計算時間 : {0:0.3f}[s]'.format(time.clock() - ts))
    ts = time.clock()
    b1 = mult_abs_numpy(x_np, y_np)
    print('NumPyの計算時間 : {0:0.3f}[s]'.format(time.clock() - ts))
    ts = time.clock()
    b1 = mult_abs_numba(N, x_np, y_np)
    print('Numbaの計算時間 : {0:0.3f}[s]'.format(time.clock() - ts))

max_stars_repo_name: deepkick/PyRockSim

max_issues_repo_name: deepkick/PyRockSim

max_forks_repo_name: deepkick/PyRockSim

reason: import numpy,from numba


text:
"""Personal Identifiable Information Anonymizer."""

import importlib
import inspect
import warnings
from copy import deepcopy

import faker
import numpy as np

from rdt.errors import Error
from rdt.transformers.base import BaseTransformer
from rdt.transformers.null import NullTransformer

class AnonymizedFaker(BaseTransformer):
    """Personal Identifiable Information Anonymizer using Faker.

    This transformer will drop a column and regenerate it with the previously specified
    ``Faker`` provider and ``function``. The transformer will also be able to handle nulls
    and regenerate null values if specified.

    Args:
        provider_name (str):
            The name of the provider in ``Faker``. If ``None`` the ``BaseProvider`` is used.
            Defaults to ``None``.
        function_name (str):
            The name of the function to use within the ``faker.provider``. Defaults to
            ``lexify``.
        function_kwargs (dict):
            Keyword args to pass into the ``function_name`` when being called.
        locales (list):
            List of localized providers to use instead of the global provider.
        missing_value_replacement (object or None):
            Indicate what to do with the null values. If an integer or float is given,
            replace them with the given value. If the strings ``'mean'`` or ``'mode'`` are
            given, replace them with the corresponding aggregation. If ``None`` is given,
            do not replace them. Defaults to ``None``.
        model_missing_values (bool):
            Whether to create a new column to indicate which values were null or not. The column
            will be created only if there are null values. If ``True``, create the new column if
            there are null values. If ``False``, do not create the new column even if there
            are null values. Defaults to ``False``.
    """

    DETERMINISTIC_TRANSFORM = False
    DETERMINISTIC_REVERSE = False
    INPUT_SDTYPE = 'pii'
    null_transformer = None

    @staticmethod
    def check_provider_function(provider_name, function_name):
        """Check that the provider and the function exist.

        Attempt to get the provider from ``faker.providers`` and then get the ``function``
        from the provider object. If one of them fails, it will raise an ``AttributeError``.

        Raises:
            ``AttributeError`` if the provider or the function is not found.
        """
        try:
            module = getattr(faker.providers, provider_name)
            if provider_name == 'BaseProvider':
                getattr(module, function_name)

            else:
                provider = getattr(module, 'Provider')
                getattr(provider, function_name)

        except AttributeError as exception:
            raise Error(
                f"The '{provider_name}' module does not contain a function named "
                f"'{function_name}'.\nRefer to the Faker docs to find the correct function: "
                'https://faker.readthedocs.io/en/master/providers.html'
            ) from exception

    def _check_locales(self):
        """Check if the locales exist for the provided provider."""
        locales = self.locales if isinstance(self.locales, list) else [self.locales]
        missed_locales = []
        for locale in locales:
            spec = importlib.util.find_spec(f'faker.providers.{self.provider_name}.{locale}')
            if spec is None:
                missed_locales.append(locale)

        if missed_locales:
            warnings.warn(
                f"Locales {missed_locales} do not support provider '{self.provider_name}' "
                f"and function '{self.function_name}'.\nIn place of these locales, 'en_US' will "
                'be used instead. Please refer to the localized provider docs for more '
                'information: https://faker.readthedocs.io/en/master/locales.html'
            )

    def __init__(self, provider_name=None, function_name=None, function_kwargs=None,
                 locales=None, missing_value_replacement=None, model_missing_values=False):
        self.data_length = None
        self.provider_name = provider_name if provider_name else 'BaseProvider'
        if self.provider_name != 'BaseProvider' and function_name is None:
            raise Error(
                'Please specify the function name to use from the '
                f"'{self.provider_name}' provider."
            )

        self.function_name = function_name if function_name else 'lexify'
        self.function_kwargs = deepcopy(function_kwargs) if function_kwargs else {}
        self.check_provider_function(self.provider_name, self.function_name)

        self.missing_value_replacement = missing_value_replacement
        self.model_missing_values = model_missing_values

        self.locales = locales
        self.faker = faker.Faker(locales)
        if self.locales:
            self._check_locales()

    def _function(self):
        """Return a callable ``faker`` function."""
        return getattr(self.faker, self.function_name)(**self.function_kwargs)

    def get_output_sdtypes(self):
        """Return the output sdtypes supported by the transformer.

        Returns:
            dict:
                Mapping from the transformed column names to supported sdtypes.
        """
        output_sdtypes = {}
        if self.null_transformer and self.null_transformer.models_missing_values():
            output_sdtypes['is_null'] = 'float'

        return self._add_prefix(output_sdtypes)

    def _fit(self, data):
        """Fit the transformer to the data.

        Args:
            data (pandas.Series):
                Data to fit to.
        """
        self.null_transformer = NullTransformer(
            self.missing_value_replacement,
            self.model_missing_values
        )
        self.null_transformer.fit(data)
        self.data_length = len(data)

    def _transform(self, data):
        """Drop the column and return ``null`` column if ``models_missing_values``.

        Args:
            data (pandas.Series):
                Data to transform.

        Returns:
            (numpy.ndarray or None):
                If ``self.model_missing_values`` is ``True`` then will return a ``numpy.ndarray``
                indicating which values should be ``nan``, else will return ``None``. In both
                scenarios the original column is being dropped.
        """
        if self.null_transformer and self.null_transformer.models_missing_values():
            return self.null_transformer.transform(data)[:, 1].astype(float)

        return None

    def _reverse_transform(self, data):
        """Generate new anonymized data using a ``faker.provider.function``.

        Args:
            data (pd.Series or numpy.ndarray):
                Data to transform.

        Returns:
            pandas.Series
        """
        reverse_transformed = np.array([
            self._function()
            for _ in range(self.data_length)
        ], dtype=object)

        if self.null_transformer.models_missing_values():
            reverse_transformed = np.column_stack((reverse_transformed, data))

        return self.null_transformer.reverse_transform(reverse_transformed)

    def __repr__(self):
        """Represent initialization of transformer as text.

        Returns:
            str:
                The name of the transformer followed by any non-default parameters.
        """
        class_name = self.__class__.__name__
        custom_args = []
        args = inspect.getfullargspec(self.__init__)
        keys = args.args[1:]
        defaults = dict(zip(keys, args.defaults))
        instanced = {key: getattr(self, key) for key in keys}

        defaults['function_name'] = None
        for arg, value in instanced.items():
            if value and defaults[arg] != value and value != 'BaseProvider':
                value = f"'{value}'" if isinstance(value, str) else value
                custom_args.append(f'{arg}={value}')

        args_string = ', '.join(custom_args)
        return f'{class_name}({args_string})'

max_stars_repo_name: HDI-Project/RDT

max_issues_repo_name: HDI-Project/RDT

max_forks_repo_name: HDI-Project/RDT


text:
from __future__ import division, print_function

import os
import shutil
import theano
import theano.tensor as T

from blocks.extensions.saveload import Checkpoint

from sample import generate_samples

class SampleCheckpoint(Checkpoint):
    def __init__(self, image_size, channels, save_subdir, **kwargs):
        super(SampleCheckpoint, self).__init__(path=None, **kwargs)
        self.image_size = image_size
        self.channels = channels
        self.save_subdir = save_subdir
        self.iteration = 0
        self.epoch_src = "{0}/sample.png".format(save_subdir)

    def do(self, callback_name, *args):
        """Sample the model and save images to disk
        """
        generate_samples(self.main_loop.model, self.save_subdir, self.image_size, self.channels)
        if os.path.exists(self.epoch_src):
            epoch_dst = "{0}/epoch-{1:03d}.png".format(self.save_subdir, self.iteration)
            self.iteration = self.iteration + 1
            shutil.copy2(self.epoch_src, epoch_dst)
            os.system("convert -delay 5 -loop 1 {0}/epoch-*.png {0}/training.gif".format(self.save_subdir))


text:
"""
This tutorial uses a simple implementation of Deep Deterministic Policy Gradient (DDPG),
derived from keras examples, refactored to work with this tutorial.

This implementation uses tensorflow, and keras wrapers for the neural networks to do
the heavy lifting of the implementation for us.
For more examles visit: https://keras.io/examples/rl/ddpg_pendulum/
"""

import gym
import tensorflow as tf
from tensorflow.keras import layers
import numpy as np
import matplotlib.pyplot as plt

from exploration import OUActionNoise
from rpm import Buffer, update_target

class DDPG:
    def __init__(self,
                 problem_name,
                 num_states,
                 num_actions,
                 lower_bound,
                 upper_bound,
                 total_episodes=100):

        self.problem_name = problem_name
        self.num_states = num_states
        self.num_actions = num_actions
        self.lower_bound = lower_bound
        self.upper_bound = upper_bound

        self.std_dev = 0.2
        self.ou_noise = OUActionNoise(mean=np.zeros(1),
                                      std_deviation=float(self.std_dev) *
                                      np.ones(1))

        self.actor_model = self.get_actor()
        self.critic_model = self.get_critic()

        self.target_actor = self.get_actor()
        self.target_critic = self.get_critic()

        # Making the weights equal initially
        self.target_actor.set_weights(self.actor_model.get_weights())
        self.target_critic.set_weights(self.critic_model.get_weights())

        # Learning rate for actor-critic models
        self.critic_lr = 0.002
        self.actor_lr = 0.001

        self.critic_optimizer = tf.keras.optimizers.Adam(self.critic_lr)
        self.actor_optimizer = tf.keras.optimizers.Adam(self.actor_lr)

        self.total_episodes = total_episodes
        # Discount factor for future rewards
        self.gamma = 0.99
        # Used to update target networks
        self.tau = 0.005

        self.buffer = Buffer(50000,
                             64,
                             gamma=self.gamma,
                             num_states=self.num_states,
                             num_actions=self.num_actions)

    def get_actor(self):
        # Initialize weights between -3e-3 and 3-e3
        last_init = tf.random_uniform_initializer(minval=-0.003, maxval=0.003)

        inputs = layers.Input(shape=(self.num_states, ))
        out = layers.Dense(256, activation="relu")(inputs)
        out = layers.Dense(256, activation="relu")(out)
        outputs = layers.Dense(self.num_actions,
                               activation="tanh",
                               kernel_initializer=last_init)(out)

        # Our upper bound is 2.0 for Pendulum.
        outputs = outputs * self.upper_bound
        model = tf.keras.Model(inputs, outputs)
        return model

    def get_critic(self):
        # State as input
        state_input = layers.Input(shape=(self.num_states))
        state_out = layers.Dense(16, activation="relu")(state_input)
        state_out = layers.Dense(32, activation="relu")(state_out)

        # Action as input
        action_input = layers.Input(shape=(self.num_actions))
        action_out = layers.Dense(32, activation="relu")(action_input)

        # Both are passed through seperate layer before concatenating
        concat = layers.Concatenate()([state_out, action_out])

        out = layers.Dense(256, activation="relu")(concat)
        out = layers.Dense(256, activation="relu")(out)
        outputs = layers.Dense(self.num_actions)(out)

        # Outputs single value for give state-action
        model = tf.keras.Model([state_input, action_input], outputs)

        return model

    def policy(self, state, noise_object):
        sampled_actions = tf.squeeze(self.actor_model(state))
        noise = noise_object()
        # Adding noise to action
        sampled_actions = sampled_actions.numpy() + noise

        # We make sure action is within bounds
        legal_action = np.clip(sampled_actions, self.lower_bound,
                               self.upper_bound)

        return [np.squeeze(legal_action)]

    def save_parameters(self):

        # Save the weights
        actor_model.save_weights("pendulum_actor.h5")
        critic_model.save_weights("pendulum_critic.h5")

        target_actor.save_weights("pendulum_target_actor.h5")
        target_critic.save_weights("pendulum_target_critic.h5")

    def load_parameters(self):

        # Save the weights
        actor_model.load_parameters("pendulum_actor.h5")
        critic_model.load_parameters("pendulum_critic.h5")

        target_actor.load_parameters("pendulum_target_actor.h5")
        target_critic.load_parameters("pendulum_target_critic.h5")

if __name__ == '__main__':

    # problem = "Pendulum-v0"
    # env = gym.make(problem)

    # num_states = env.observation_space.shape[0]
    # print("Size of State Space ->  {}".format(num_states))
    # num_actions = env.action_space.shape[0]
    # print("Size of Action Space ->  {}".format(num_actions))

    # upper_bound = env.action_space.high[0]
    # lower_bound = env.action_space.low[0]

    # print("Max Value of Action ->  {}".format(upper_bound))
    # print("Min Value of Action ->  {}".format(lower_bound))

    problem = "Pendulum-v0"
    env = gym.make(problem)

    num_states = env.observation_space.shape[0]
    print("Size of State Space ->  {}".format(num_states))
    num_actions = env.action_space.shape[0]
    print("Size of Action Space ->  {}".format(num_actions))

    upper_bound = env.action_space.high[0]
    lower_bound = env.action_space.low[0]

    print("Max Value of Action ->  {}".format(upper_bound))
    print("Min Value of Action ->  {}".format(lower_bound))

    ddpg_agent = DDPG(problem_name=problem,
                      num_states=num_states,
                      num_actions=num_actions,
                      lower_bound=lower_bound,
                      upper_bound=upper_bound,
                      total_episodes=100)

    # To store reward history of each episode
    ep_reward_list = []
    # To store average reward history of last few episodes
    avg_reward_list = []

    # Takes about 4 min to train
    for ep in range(ddpg_agent.total_episodes):

        prev_state = env.reset()
        episodic_reward = 0
        render_episodes = 10
        render = not (ep % render_episodes)

        while True:
            # Uncomment this to see the Actor in action
            # But not in a python notebook.
            # env.render()

            tf_prev_state = tf.expand_dims(tf.convert_to_tensor(prev_state), 0)
            action = ddpg_agent.policy(tf_prev_state, ddpg_agent.ou_noise)
            # Recieve state and reward from environment.
            state, reward, done, info = env.step(action)
            if render: env.render()

            ddpg_agent.buffer.record((prev_state, action, reward, state))
            episodic_reward += reward

            ddpg_agent.buffer.learn(ddpg_agent.target_actor,
                                    ddpg_agent.target_critic,
                                    ddpg_agent.actor_model,
                                    ddpg_agent.critic_model,
                                    ddpg_agent.actor_optimizer,
                                    ddpg_agent.critic_optimizer)

            update_target(ddpg_agent.target_actor.variables,
                          ddpg_agent.actor_model.variables, ddpg_agent.tau)
            update_target(ddpg_agent.target_critic.variables,
                          ddpg_agent.critic_model.variables, ddpg_agent.tau)

            # End this episode when `done` is True
            if done:
                break

            prev_state = state

        ep_reward_list.append(episodic_reward)

        # Mean of last 40 episodes
        avg_reward = np.mean(ep_reward_list[-40:])
        print("Episode * {} * Avg Reward is ==> {}".format(ep, avg_reward))
        avg_reward_list.append(avg_reward)

    # Plotting graph
    # Episodes versus Avg. Rewards
    plt.plot(avg_reward_list)
    plt.xlabel("Episode")
    plt.ylabel("Avg. Epsiodic Reward")
    plt.show()

max_stars_repo_name: DanielLSM/safe-rl-tutorial

max_issues_repo_name: DanielLSM/safe-rl-tutorial

max_forks_repo_name: DanielLSM/safe-rl-tutorial


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

# Lint as python3
"""Tests for Python New Game."""

import difflib
import os
import pickle

from absl.testing import absltest
import numpy as np
from open_spiel.python.algorithms.get_all_states import get_all_states
from open_spiel.python.games import new_game
from open_spiel.python.observation import make_observation
import pyspiel

_DATA_DIR = "open_spiel/integration_tests/playthroughs/"

class NewGameTest(absltest.TestCase):

  def test_can_create_game_and_state(self):
    """Checks we can create the game and a state."""
    game = new_game.NewGameGame()
    state = game.new_initial_state()
    self.assertEqual(str(state), "...\n...\n...")

  def test_random_game(self):
    """Tests basic API functions."""
    # This is here mostly to show the API by example.
    # More serious simulation tests are done in python/tests/games_sim_test.py
    # and in test_game_from_cc (below), both of which test the conformance to
    # the API thoroughly.
    game = new_game.NewGameGame()
    state = game.new_initial_state()
    while not state.is_terminal():
      print(state)
      cur_player = state.current_player()
      legal_actions = state.legal_actions()
      action = np.random.choice(legal_actions)
      print("Player {} chooses action {}".format(cur_player, action))
      state.apply_action(action)
    print(state)
    print("Returns: {}".format(state.returns()))

  def test_game_from_cc(self):
    """Runs our standard game tests, checking API consistency."""
    game = pyspiel.load_game("python_new_game")
    pyspiel.random_sim_test(game, num_sims=10, serialize=False, verbose=True)

  def test_playthoughs_consistent(self):
    """Checks the saved C++ and Python playthroughs are the same."""
    """
    test_srcdir = os.environ.get("TEST_SRCDIR", "")
    path = os.path.join(test_srcdir, _DATA_DIR)
    cc_playthrough = os.path.join(path, "new_game.txt")
    py_playthrough = os.path.join(path, "python_new_game.txt")
    with open(cc_playthrough, encoding="utf-8") as cc:
      with open(py_playthrough, encoding="utf-8") as py:
        diffs = difflib.ndiff(list(cc), list(py))
    diffs = {d for d in diffs if d and d[0] in {"+", "-"}}
    self.assertEqual(
        diffs, {
            "- game: new_game\n",
            "+ game: python_new_game\n",
            '- GameType.long_name = "New Game"\n',
            '+ GameType.long_name = "Python New Game"\n',
            '- GameType.short_name = "new_game"\n',
            '+ GameType.short_name = "python_new_game"\n',
            '- ToString() = "new_game()"\n',
            '+ ToString() = "python_new_game()"\n',
            "- CurrentPlayer() = -4\n",
            "+ CurrentPlayer() = PlayerId.TERMINAL\n",
            "- Returns() = [0.0, 0.0]\n",
            "+ Returns() = [0.0, -0.0]\n",
        })
    """

  def test_observation_tensors_same(self):
    """Checks observation tensor is the same from C++ and from Python."""
    game = pyspiel.load_game("python_new_game")
    state = game.new_initial_state()
    for a in [4, 5, 2, 3]:
      state.apply_action(a)
    py_obs = make_observation(game)
    py_obs.set_from(state, state.current_player())
    cc_obs = state.observation_tensor()
    np.testing.assert_array_equal(py_obs.tensor, cc_obs)

  def test_pickle(self):
    """Checks pickling and unpickling of game and state."""
    game = pyspiel.load_game("python_new_game")
    pickled_game = pickle.dumps(game)
    unpickled_game = pickle.loads(pickled_game)
    self.assertEqual(str(game), str(unpickled_game))
    state = game.new_initial_state()
    for a in [4, 2, 3, 7]:
      state.apply_action(a)
    ser_str = pyspiel.serialize_game_and_state(game, state)
    new_game, new_state = pyspiel.deserialize_game_and_state(ser_str)
    self.assertEqual(str(game), str(new_game))
    self.assertEqual(str(state), str(new_state))
    pickled_state = pickle.dumps(state)
    unpickled_state = pickle.loads(pickled_state)
    self.assertEqual(str(state), str(unpickled_state))

  def test_cloned_state_matches_original_state(self):
    """Check we can clone states successfully."""
    game = new_game.NewGameGame()
    state = game.new_initial_state()
    state.apply_action(1)
    state.apply_action(2)
    clone = state.clone()

    self.assertEqual(state.history(), clone.history())
    self.assertEqual(state.num_players(), clone.num_players())
    self.assertEqual(state.move_number(), clone.move_number())
    self.assertEqual(state.num_distinct_actions(), clone.num_distinct_actions())

    self.assertEqual(state._cur_player, clone._cur_player)
    self.assertEqual(state._player0_score, clone._player0_score)
    self.assertEqual(state._is_terminal, clone._is_terminal)
    np.testing.assert_array_equal(state.board, clone.board)

  def test_consistent(self):
    """Checks the Python and C++ game implementations are the same."""
    """
    py_game = pyspiel.load_game("python_new_game")
    cc_game = pyspiel.load_game("python_new_game")
    py_obs = make_observation(py_game)
    cc_obs = make_observation(cc_game)
    py_states = get_all_states(py_game, to_string=str)
    cc_states = get_all_states(cc_game, to_string=str)
    self.assertCountEqual(list(cc_states), list(py_states))
    for key, cc_state in cc_states.items():
      py_state = py_states[key]
      np.testing.assert_array_equal(py_state.history(), cc_state.history())
      np.testing.assert_array_equal(py_state.returns(), cc_state.returns())
      py_obs.set_from(py_state, 0)
      cc_obs.set_from(cc_state, 0)
      np.testing.assert_array_equal(py_obs.tensor, cc_obs.tensor)
    """

if __name__ == "__main__":
  absltest.main()


text:
import numpy as np
import utils
import os

config = utils.read_config()
path_dataset = config['path_dataset']
path_aux = config['path_aux']
images = os.path.join(path_dataset, 'intersection', 'images')
sketches = os.path.join(path_dataset, 'intersection', 'sketches')

clss = []
npys = [ '/ml_data/sem_pcyc/aux/Semantic/intersection/plus_words.npy', '/ml_data/sem_pcyc/aux/Semantic/intersection/word2vec-google-news.npy']

for npy in npys:
    print(npy)
    load_dict = np.load(npy, allow_pickle=True)
    for key, value in load_dict.tolist().items():
        clss.append(key)
        if key == 'plunger':
            print('----------------plunger---------------------')
            print(value)
print(len(clss))
print(clss)
print(path_dataset)
image_list =  os.listdir(images)
print('image list : {}'.format(image_list))
print ('-------image diff --------------')
diff = set(image_list) - set(clss)
print(diff)

sketches_list =  os.listdir(sketches)
print('sketches list : {}'.format(sketches_list))
print ('-------sketches diff --------------')
diff = set(sketches_list) - set(clss)
print(diff)

max_stars_repo_name: toonsquare/sem-pcyc

max_issues_repo_name: toonsquare/sem-pcyc

max_forks_repo_name: toonsquare/sem-pcyc