text:
#This file was created by Julian Harrison
#from DataObject import DataObject #Data will be stored in this format throughout its processing
#from MockDataObject import DataObject #Used for testing purposes
import DataObject
from Output_GUI import OutputGUI
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from astropy.io import fits #Used to save fits file
import os
from datetime import datetime
import platform
import ImageIntensity
from GUIHandler import GUIHandler
import ZoomObject

#Method to be called after processing which will save the data into a text file at the correct place in the file directory

def outputDataToGUI(dataObject, cmin=6500, cmax=6700): #Default values subject to change
    #Create string for data output to GUI
    dataString = None

    #Flag specific behaviour
    flag = dataObject.getFlag()
    if flag == "CheckPixels":
        #Create pixel image as a figure
        figure = dataObject.getPixelImage()
    elif flag == "Publication":
        #Create publication image as a figure
        figure = dataObject.getAnnotatedImage()
    elif flag == "Orbits":
        #Create orbits image as a figure
        figure = dataObject.getOrbitPlot()
    elif flag == "Simulate":
        #Create simulate image as a figure
        simulateImage = dataObject.getSimulateImage()
        figure = newImage(simulateImage, cmin, cmax)
    elif flag == "Zoomed":
        #Create zoomed image as a figure
        zoomedObjects = dataObject.getZoomedImages()

        figures = []

        for zoomObject in zoomedObjects:
            figure = zoomObject.getImage()
            figures.append(figure)

        handler = GUIHandler.getInstance()
        handler.setWindow("OutputGUI")
        handler.setMultipleOutputGUIData(figures, dataObject, dataString)

    elif flag == "Photoplot":
        #Create and prepare figure
        figure = dataObject.getPhotoplotImage()

        #Set data string to correct values
        dataString = formatPhotometryData(dataObject)

    #Prepare the Output GUI
    if(flag != "Zoomed"):
        handler = GUIHandler.getInstance()
        handler.setWindow("OutputGUI")
        handler.setOutputGUIData(figure, dataObject, dataString)

#Replot the image with different cmin and cmax values
def getFilteredPlot(cmin, cmax, figure):
    #Change figure clim values
    plt.clim(cmin, cmax)

    return figure

#Prepares a figure object to be used by the GUI
def newImage(image, cmin, cmax):
    #Create figure
    newImage = plt.figure(figsize=(10,8))

    #Set image data into figure
    plt.imshow(image, clim=(cmin,cmax))

    #Set axes and a colourbar
    plt.xlabel("Pixel number")
    plt.ylabel("Pixel number")
    plt.colorbar()

    return newImage

def outputDataToFile(dataObject, inFileName = "", inFileLocation = ""):

    #Get processing flag
    flag = dataObject.getFlag()

    if((inFileName == "") and (inFileLocation == "")): #No file name or location

        #Create file name and path
        currentDateTime = datetime.now()
        dtString = currentDateTime.strftime("%d_%m_%Y__%H_%M_%S")
        fileName = os.path.join(getDefaultFileLocation(), dtString)

        #Check directory exists
        if(not(os.path.isdir(getDefaultFileLocation()))):
            os.makedirs(getDefaultFileLocation())

        #Save data to file
        #saveImageData(dataObject, fileName)
        saveData(dataObject, fileName, flag)

    elif ((inFileLocation == "") and not (inFileName == "")): #File name but no file location

        #Create file name and path
        fileName = os.path.join(getDefaultFileLocation(), inFileName)

        #Check directory exists
        if(not(os.path.isdir(getDefaultFileLocation()))):
            os.makedirs(getDefaultFileLocation())

        #Save data to file
        #saveImageData(dataObject, fileName)
        saveData(dataObject, fileName, flag)

    elif (not (inFileLocation == "") and not(inFileName == "")): #Both file name and location

        #Create file name and path
        fileName = os.path.join(inFileLocation, inFileName)

        #Check directory exists
        if(not(os.path.isdir(inFileLocation))):
            os.makedirs(inFileLocation)

        #Save data to file
        #saveImageData(dataObject, fileName)
        saveData(dataObject, fileName, flag)
    else:
        raise ValueError("Cannot save file with only a file path")

def saveData(dataObject, filename, flag):
    #Set ImageData file name
    imageFileName = os.path.join(filename, "image.fit")

    if not os.path.isdir(filename):
        os.mkdir(filename)

    if flag == "CheckPixels":
        #Set pixelData filename
        pixelDataFileName = os.path.join(filename, "checkPixels.png")

        #Retrieve and save pixel data
        savePixelData(dataObject, pixelDataFileName)
    elif flag == "Publication":
        #Set publication filename
        publicationDataFileName = os.path.join(filename, "publication.png")

        #Retrieve and save publication data
        savePublicationData(dataObject, publicationDataFileName)
    elif flag == "Orbits":
        #Set orbits filename
        #orbitsDataFileName = os.path.join(filename, "orbits.txt")
        orbitsDataFileName = os.path.join(filename, "orbits.png")

        #Retrieve and save orbits data
        saveOrbitsData(dataObject, orbitsDataFileName)
    elif flag == "Simulate":
        simulateDataFileName = os.path.join(filename, "simulate.png")

        #Retrieve and save simulate data
        saveSimulateData(dataObject, simulateDataFileName)
    elif flag == "Zoomed":
        zoomedDataFileName = os.path.join(filename, "zoomed")

        #Retrieve and save zoomed data
        saveZoomedImageData(dataObject, zoomedDataFileName)
    elif flag == "Photoplot":
        #Retrieve and save photoplot data
        savePhotoPlotData(dataObject, filename)

def savePixelData(dataObject, fileName):
    #TODO May need modifying to suit the completed process
    figure = dataObject.getPixelImage()

    saveImageData(figure, fileName)

def savePublicationData(dataObject, fileName):
    figure = dataObject.getAnnotatedImage()

    saveImageData(figure, fileName)

def saveOrbitsData(dataObject, fileName):
    #TODO May need modifying to suit the completed process
    #Get orbits image from dataObject
    figure = dataObject.getOrbitPlot()

    #Save image to file
    saveImageData(figure, fileName)

def saveSimulateData(dataObject, fileName):
    #TODO May need modifying to suit the completed process
    #Get simulate image from dataObject
    simulateArray = dataObject.getSimulateImage()

    #Create figure for saving image
    figure = newImage(simulateArray, 6500, 6700)

    #Save image to file
    saveImageData(figure, fileName)

def saveZoomedImageData(dataObject, fileName):
    #Get zoomed image from dataObject
    zoomedObjects = dataObject.getZoomedImages()

    ii = 1

    for zoomObject in zoomedObjects:
        #Get figure
        figure = zoomObject.getImage()

        tempFileName = fileName + str(ii)
        tempFileName = tempFileName + ".png"
        ii = ii + 1

        #Save image to file
        saveImageData(figure, tempFileName)

def savePhotoPlotData(dataObject, fileName):
    #Retrieve photometry data from the dataObject in a format suitable for human consumption
    outputString = formatPhotometryData(dataObject)

    figure = dataObject.getPhotoplotImage()

    dataFileName = os.path.join(fileName, "data.txt")
    imageFileName = os.path.join(fileName, "image.png")

    #Save text data to file
    saveTextToFile(outputString, dataFileName)

    #Save annotated image to file
    saveImageData(figure, imageFileName)

def saveTextToFile(inText, fileName):
    try:
        file = open(fileName, "w")
        file.write(inText)
        file.close()
    except IOError:
        try:
            file.close()
        except IOError:
            #Failed to close file
            pass

def saveImageData(imageData, fileName):
    #Ensure filename is correctly formatted
    fileName = os.path.abspath(fileName)

    #Save image data
    try:
        imageData.savefig(fileName)
    except OSError as err:
        print("Error with saving: " + fileName +" to file")

#Creates a default file location within the Pictures Folder
def getDefaultFileLocation(): #Warning: Can be quite slow. This function is intended as a backup

    #Find Pictures file
    filePath = ""

    #Get operation system
    if(platform.system() == 'Windows'):
        start = "\\Libraries\\" #Windows
    elif(platform.system() == 'Linux'):
        start = "\\home\\" #Linux

    for dirpath, dirnames, filenames in os.walk(start):
        for dirname in dirnames:
            if(dirname == "Pictures"):
                filePath = os.path.join(dirpath, "Pictures")

    today = datetime.today()
    outputFileLocation = os.path.join(filePath, "HighCadenceProcessing")
    outputFileLocation = os.path.join(outputFileLocation, today.strftime("%d_%m_%y"))
    return outputFileLocation

def formatPhotometryData(dataObject):
    #Get slice data
    sliceList = dataObject.getSliceList()

    #Get data from photometry into text format
    dataString = "Photometry:\n\n"
    dataString = dataString + "Background brightness: {}\n".format(ImageIntensity.meanBrightness(dataObject))
    #dataString = dataString + "Calibration factor: {}\n".format(dataObject.getMeanCalibrationFactor())

    #Set slice counter
    ii = 0

    #Get data from each string into text format
    dataString = dataString + "\n_________________________________________\nSlices: \n\n"

    for slice in sliceList:
        ii = ii + 1
        dataString = dataString + "Slice {}\n\n".format(ii)

        sliceDataString = "Peak brightness: {}\n".format(slice.getPeak())
        sliceDataString = sliceDataString + "X: {}\nY: {}".format(slice.getX(), slice.getY())
        sliceDataString = sliceDataString + "Star magnitude: {}\n".format(slice.getActualBrightness())
        sliceDataString = sliceDataString + "___________________________\n"
        dataString = dataString + sliceDataString
        #dataString = dataString + slice.toString() + "\n_________________________\n"

    return dataString

max_stars_repo_name: renshj/High-Cadence-Processing

max_issues_repo_name: renshj/High-Cadence-Processing

max_forks_repo_name: renshj/High-Cadence-Processing

reason: from astropy


text:
# -*- coding: utf-8 -*-
"""
Package to read a mesh in GR3 format.
"""

from schimpy.schism_mesh import BoundaryType
import schimpy.schism_mesh
import schimpy.base_io
import numpy as np
import os

class Gr3IO(schimpy.base_io.BaseIO):
    """ A class that manages I/O of GR3 files
    """

    def __init__(self, logger=None):
        """ Constructor
        """
        super(Gr3IO, self).__init__(logger=logger)
        self._mesh = None

    def read(self, gr3_fname="hgrid.gr3", mode=0):
        """ Read in a hgrid.gr3 file.
            If mode is 1, it does not read in boundary information.
        """
        if self._logger is not None:
            self._logger.info("Reading in a gr3 file: %s ..." % gr3_fname)
        # Create a new mesh
        self._mesh = schism_mesh.SchismMesh()
        try:
            f = open(gr3_fname)
        except:
            raise Exception("Grid file not found.")

        self._linecounter = 0
        # Mesh info
        self._read_header(f)
        if self._logger is not None:
            self._logger.info("Reading in nodes...")
        self._read_nodes(f)
        if self._logger is not None:
            self._logger.info("Reading in elements...")
        self._read_elems(f)

        # Build grid
        # if self._verbose > 1:
        #     print "Building a mesh..."
        # self._mesh.build_edges_from_elems()

        # Boundary info
        if not mode == 1:
            if self._logger is not None:
                self._logger.info("Reading in boundaries...")
            self._read_boundaries(f)

        ## Close
        f.close()
        if self._logger is not None:
            self._logger.info("Done reading a GR3 file.")
        return self._mesh

    def _read_header(self, f):
        ## Header
        # First line: ignored
        tokens, ok = self._read_and_parse_line(f, 0)
        if len(tokens) > 0:
            self._mesh.comment = tokens[0]
        # Second line: # of elements and # of nodes
        tokens, ok = self._read_and_parse_line(f, 2)
        if not ok:
            raise ValueError("The Header information of GR3 file is corrupted.")
        (n_elems, n_nodes) = list(map(int, tokens[:2]))
        if n_nodes <= 0 or n_elems <= 0:
            raise ValueError("The Header information of GR3 file is corrupted.")
        self._mesh.allocate(n_elems, n_nodes) # Allocate memory
        self._n_elems = n_elems
        self._n_nodes = n_nodes

    def _read_nodes(self, f):
        node_counter = 0
        for i in range(self._n_nodes):
            tokens, ok = self._read_and_parse_line(f, 4)
            if not ok:
                print("Line: {}".format(self.linecounter))
                raise ValueError("Node block is corrupt.")
            node_coords = list(map(float, tokens[1:4]))
            self._mesh.set_node(node_counter, node_coords)
            node_counter += 1

    def _read_elems(self, f):
        for elem_i in range(self._n_elems):
            tkn = f.readline().split()
            self.linecounter += 1
            if len(tkn) < 5:
                print("Line: {}".format(self.linecounter))
                raise ValueError("Element block is corrupt.")
            type_elem = int(tkn[1])
            if type_elem < 3 or type_elem > 4:
                raise ValueError("Currently only triangular and " \
                                 "quadrilateral are supported.")
            # Zero-based connectivities
            if type_elem == 3:
                connectivities = np.subtract(np.array(list(map(int, tkn[2:5]))), 1)
            elif type_elem == 4:
                connectivities = np.subtract(np.array(list(map(int, tkn[2:6]))), 1)
            else:
                raise ValueError("Not allowed element type is encountered.")
            self._mesh.set_elem(elem_i, connectivities)

    def _read_boundaries(self, f):
        """ Read boundary information
        """
        ## Assuming the order of the different boundary types are consitent
        ## Open boundaries
        # # of open boundaries
        tokens, ok = self._read_and_parse_line(f, 1)
        if not ok:
            self._logger.info("No boundary information is present?")
            return

        n_open_boundaries = int(tokens[0])
        # total # of open boundary nodes
        tokens, ok = self._read_and_parse_line(f, 1)
        if not ok:
            print("Line: {}".format(self.linecounter))
            raise Exception("The number of total open boundary nodes is not"\
                            " correctly provided.")
        n_open_boundary_nodes = int(tokens[0])

        # open boundaries
        for _ in range(n_open_boundaries):
            # # of nodes of this open boundary
            tokens, ok = self._read_and_parse_line(f, 1)
            if not ok:
                print("Line: {}".format(self.linecounter))
                raise Exception("The number of nodes for a boundary is not"\
                                " correctly provided.")
            # Comment
            comment = None
            if len(tokens) > 1:
                comment = tokens[1]

            n_nodes = int(tokens[0])
            nodes = []
            for _ in range(n_nodes):
                tokens, ok = self._read_and_parse_line(f, 1)
                if not ok:
                    print("Line: {}".format(self.linecounter))
                    raise ValueError("Node for boundary not"\
                                     " correctly provided.")

                node = int(tokens[0]) - 1 # Zero based
                nodes.append(node)
            self._mesh.add_boundary(nodes,
                                    BoundaryType.OPEN,
                                    comment)

        # land boundaries
        # I found out that there is no distinction between
        # land and island boundaries.
        tokens, ok = self._read_and_parse_line(f, 1)
        if not ok:
            self._logger.error("No land boundary presented?")
            return
        n_land_boundaries = int(tokens[0])
        # total # of land boundary nodes
        tokens, ok = self._read_and_parse_line(f, 1)
        if not ok:
            print("Line: {}".format(self.linecounter))
            raise Exception("The number of total land boundary nodes is " \
                            "not provided properly.")
        # n_land_boundary_nodes = int(tokens[0])
        for _ in range(n_land_boundaries):
            # # of nodes of this open boundary
            (tokens, ok) = self._read_and_parse_line(f,1)
            if not ok:
                print("Line: {}".format(self.linecounter))
                raise Exception("The number of nodes for a boundary is "\
                                "not provided properly.")
            # Comment
            comment = None
            if len(tokens) > 1:
                comment = tokens[1]

            n_nodes = int(tokens[0])
            nodes = []
            for _ in range(n_nodes):
                (tokens, ok) = self._read_and_parse_line(f, 1)
                if not ok:
                    print("Line: {}".format(self.linecounter))
                    raise Exception("Node for a boundary not correctly provided.")
                node = int(tokens[0]) - 1 # Zero based
                nodes.append(node)
            self._mesh.add_boundary(nodes, BoundaryType.LAND, comment)

    def write(self, mesh, fname, node_attr=None, boundary=False):
        """ Write a GR3 format grid.
            mesh = SCHISM mesh (schism_mesh) instance
            fname = output file name
            node_attr = a list of node attribute
            boundary = If true, boundary information will be added. Otherwise,
            it will not be appended.
        """
        if self._logger is not None:
            self._logger.info("Writing an gr3 file: %s" % fname)
        f = open(fname, 'w')
        # Header
#         if mesh.comment is None:
        buf = "%s\n" % os.path.basename(fname)
#         else:
#             buf = "%s !modified by the preprocessing tool\n" \
#                   % mesh.comment

        f.write(buf)
        n_elems = mesh.n_elems()
        n_nodes = mesh.n_nodes()
        buf = "%d %d ! # of elements and nodes \n" % (n_elems, n_nodes)
        f.write(buf)
        # Nodes
        for i in range(n_nodes):
            if not node_attr is None:
                buf = "%d %18.8f %18.8f %18.8f\n" % (i + 1, \
                                     mesh.nodes[i, 0], \
                                     mesh.nodes[i, 1], \
                                     node_attr[i])

            else:
                buf = "%d %18.8f %18.8f %18.8f\n" % (i + 1, \
                                     mesh.nodes[i, 0], \
                                     mesh.nodes[i, 1], \
                                     mesh.nodes[i, 2])
            f.write(buf)

        # Elements
        for elem_i in range(n_elems):
            elem = mesh.elem(elem_i) + 1
            n_nodes = len(elem)
            buf = '%d %d' % (elem_i + 1, n_nodes)
            fmt = ' %d' * n_nodes + '\n'
            buf += fmt % tuple(elem)
            f.write(buf)

        # Boundaries
        if boundary:
            # Open
            buf = "%d = Number of open boundaries\n" \
                  % mesh.n_boundaries(BoundaryType.OPEN)
            f.write(buf)
            buf = "%d = Total number of open boundary nodes\n" \
                  % mesh.n_boundary_nodes(BoundaryType.OPEN)
            f.write(buf)
            openbound_count = 0
            for boundary in mesh.boundaries:
                if boundary.btype == BoundaryType.OPEN:
                    openbound_count += 1
                    if boundary.comment is None:
                        buf = "%d = Number of nodes for open boundary %d\n" % \
                              (boundary.n_nodes(), openbound_count)
                    else:
                        buf = "%d %s\n" % (boundary.n_nodes(), boundary.comment)
                    f.write(buf)
                    buf = ""
                    for node_i in boundary.nodes:
                        buf += "%d\n" % (node_i + 1)
                    f.write(buf)
#             else:
#                 raise Exception("Unsupported boundary type.")

            # Land
            buf = "%d = Number of land boundaries\n" \
                  % (mesh.n_boundaries(BoundaryType.LAND))
            f.write(buf)
            buf = "%d = Total number of land boundary nodes\n" \
                  % (mesh.n_boundary_nodes(BoundaryType.LAND))
            f.write(buf)
            landbound_count = 0
            islandbound_count = 0
            for boundary in mesh.boundaries:
                if boundary.btype == BoundaryType.LAND:
                    landbound_count += 1
                    if boundary.comment is None:
                        buf = "%d = Number of nodes for land boundary %d\n" % \
                          (boundary.n_nodes(), landbound_count)
                    else:
                        buf = "%d %s\n" % (boundary.n_nodes(), boundary.comment)
                    f.write(buf)
                    buf = ""
                    for node_i in boundary.nodes:
                        buf += "%d\n" % (node_i + 1)
                    f.write(buf)
                # elif boundary.btype == BoundaryType.ISLAND:
                #     islandbound_count += 1
                #     if boundary.comment is None:
                #         buf = "%d = Number of nodes for island boundary %d\n" \
                #                % (boundary.n_nodes(), islandbound_count)
                #     else:
                #         buf = "%d %s\n" % (boundary.n_nodes(), boundary.comment)
                #     f.write(buf)
                #     buf = ""
                #     for node_i in boundary.nodes:
                #         buf += "%d\n" % (node_i + 1)
                #     f.write(buf)
#             else:
#                 raise Exception("Unsupported boundary type.")

        f.flush()
        f.close()

        if self._logger is not None:
            self._logger.info("Done writing a gr3 file.")

ext: py

max_stars_repo_name: CADWRDeltaModeling/schimpy

max_issues_repo_name: CADWRDeltaModeling/schimpy

max_forks_repo_name: CADWRDeltaModeling/schimpy

reason: import numpy


text:
import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

from utils.timer import Timer
from utils.blob import im_list_to_blob
from fast_rcnn.nms_wrapper import nms
from rpn_msr.proposal_layer import proposal_layer as proposal_layer_py
from rpn_msr.anchor_target_layer import anchor_target_layer as anchor_target_layer_py
from rpn_msr.proposal_target_layer import proposal_target_layer as proposal_target_layer_py
from fast_rcnn.bbox_transform import bbox_transform_inv, clip_boxes

import network
from network import Conv2d, FC
# from roi_pooling.modules.roi_pool_py import RoIPool
from roi_pooling.modules.roi_pool import RoIPool
from vgg16 import VGG16

'''
关键词：
1. anchor 候选窗口
2. IoU 交并比
3. GT （ground truth） 正确的标注
'''

def nms_detections(pred_boxes, scores, nms_thresh, inds=None):
    dets = np.hstack((pred_boxes,
                      scores[:, np.newaxis])).astype(np.float32)
    keep = nms(dets, nms_thresh)
    if inds is None:
        return pred_boxes[keep], scores[keep]
    return pred_boxes[keep], scores[keep], inds[keep]

class RPN(nn.Module):
    _feat_stride = [16, ]
    anchor_scales = [8, 16, 32]

    def __init__(self):
        super(RPN, self).__init__()
        # 获取特征
        self.features = VGG16(bn=False)
        # 一层卷积
        self.conv1 = Conv2d(512, 512, 3, same_padding=True)
        #卷积层得分 每个像素值以锚点为心，九种大小的得分
        self.score_conv = Conv2d(512, len(self.anchor_scales) * 3 * 2, 1, relu=False, same_padding=False)

        self.bbox_conv = Conv2d(512, len(self.anchor_scales) * 3 * 4, 1, relu=False, same_padding=False)

        # loss
        self.cross_entropy = None
        self.los_box = None

    @property
    def loss(self):
        return self.cross_entropy + self.loss_box * 10

    def forward(self, im_data, im_info, gt_boxes=None, gt_ishard=None, dontcare_areas=None):
        im_data = network.np_to_variable(im_data, is_cuda=True)
        im_data = im_data.permute(0, 3, 1, 2)
        features = self.features(im_data)

        # 区域生成网络
        rpn_conv1 = self.conv1(features)

        # rpn score 的分
        rpn_cls_score = self.score_conv(rpn_conv1)
        # 分类得分的形状
        rpn_cls_score_reshape = self.reshape_layer(rpn_cls_score, 2)
        # 通过得分获得经过softmax运算获取概率
        rpn_cls_prob = F.softmax(rpn_cls_score_reshape)
        # 转换形状
        rpn_cls_prob_reshape = self.reshape_layer(rpn_cls_prob, len(self.anchor_scales)*3*2)

        # rpn boxes
        rpn_bbox_pred = self.bbox_conv(rpn_conv1)

        # proposal layer
        cfg_key = 'TRAIN' if self.training else 'TEST'
        rois = self.proposal_layer(rpn_cls_prob_reshape, rpn_bbox_pred, im_info,
                                   cfg_key, self._feat_stride, self.anchor_scales)

        # generating training labels and build the rpn loss
        if self.training:
            assert gt_boxes is not None
            rpn_data = self.anchor_target_layer(rpn_cls_score, gt_boxes, gt_ishard, dontcare_areas,
                                                im_info, self._feat_stride, self.anchor_scales)
            self.cross_entropy, self.loss_box = self.build_loss(rpn_cls_score_reshape, rpn_bbox_pred, rpn_data)

        return features, rois

    def build_loss(self, rpn_cls_score_reshape, rpn_bbox_pred, rpn_data):
        # classification loss
        rpn_cls_score = rpn_cls_score_reshape.permute(0, 2, 3, 1).contiguous().view(-1, 2)
        rpn_label = rpn_data[0].view(-1)

        rpn_keep = Variable(rpn_label.data.ne(-1).nonzero().squeeze()).cuda()
        rpn_cls_score = torch.index_select(rpn_cls_score, 0, rpn_keep)
        rpn_label = torch.index_select(rpn_label, 0, rpn_keep)

        fg_cnt = torch.sum(rpn_label.data.ne(0))

        rpn_cross_entropy = F.cross_entropy(rpn_cls_score, rpn_label)

        # box loss
        rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = rpn_data[1:]
        rpn_bbox_targets = torch.mul(rpn_bbox_targets, rpn_bbox_inside_weights)
        rpn_bbox_pred = torch.mul(rpn_bbox_pred, rpn_bbox_inside_weights)

        rpn_loss_box = F.smooth_l1_loss(rpn_bbox_pred, rpn_bbox_targets, size_average=False) / (fg_cnt + 1e-4)

        return rpn_cross_entropy, rpn_loss_box

    @staticmethod
    def reshape_layer(x, d):
        input_shape = x.size()
        # 获取输入形状
        # x = x.permute(0, 3, 1, 2)
        # b c w h batch 通道 宽高
        x = x.view(
            input_shape[0],
            int(d),
            int(float(input_shape[1] * input_shape[2]) / float(d)),
            input_shape[3]
        )
        # C:\Users\77696\AppData\Local\Programs\Python\Python37
        # x = x.permute(0, 2, 3, 1)
        return x

    @staticmethod
    def proposal_layer(rpn_cls_prob_reshape, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchor_scales):
        rpn_cls_prob_reshape = rpn_cls_prob_reshape.data.cpu().numpy()
        rpn_bbox_pred = rpn_bbox_pred.data.cpu().numpy()
        x = proposal_layer_py(rpn_cls_prob_reshape, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchor_scales)
        x = network.np_to_variable(x, is_cuda=True)
        return x.view(-1, 5)

    @staticmethod
    def anchor_target_layer(rpn_cls_score, gt_boxes, gt_ishard, dontcare_areas, im_info, _feat_stride, anchor_scales):
        """
        rpn_cls_score: for pytorch (1, Ax2, H, W) bg/fg scores of previous conv layer
        gt_boxes: (G, 5) vstack of [x1, y1, x2, y2, class]
        gt_ishard: (G, 1), 1 or 0 indicates difficult or not
        dontcare_areas: (D, 4), some areas may contains small objs but no labelling. D may be 0
        im_info: a list of [image_height, image_width, scale_ratios]
        _feat_stride: the downsampling ratio of feature map to the original input image
        anchor_scales: the scales to the basic_anchor (basic anchor is [16, 16])
        ----------
        Returns
        ----------
        rpn_labels : (1, 1, HxA, W), for each anchor, 0 denotes bg, 1 fg, -1 dontcare
        rpn_bbox_targets: (1, 4xA, H, W), distances of the anchors to the gt_boxes(may contains some transform)
                        that are the regression objectives
        rpn_bbox_inside_weights: (1, 4xA, H, W) weights of each boxes, mainly accepts hyper param in cfg
        rpn_bbox_outside_weights: (1, 4xA, H, W) used to balance the fg/bg,
        beacuse the numbers of bgs and fgs mays significiantly different
        """
        rpn_cls_score = rpn_cls_score.data.cpu().numpy()
        rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = \
            anchor_target_layer_py(rpn_cls_score, gt_boxes, gt_ishard, dontcare_areas, im_info, _feat_stride, anchor_scales)

        rpn_labels = network.np_to_variable(rpn_labels, is_cuda=True, dtype=torch.LongTensor)
        rpn_bbox_targets = network.np_to_variable(rpn_bbox_targets, is_cuda=True)
        rpn_bbox_inside_weights = network.np_to_variable(rpn_bbox_inside_weights, is_cuda=True)
        rpn_bbox_outside_weights = network.np_to_variable(rpn_bbox_outside_weights, is_cuda=True)

        return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights

    def load_from_npz(self, params):
        # params = np.load(npz_file)
        self.features.load_from_npz(params)

        pairs = {'conv1.conv': 'rpn_conv/3x3', 'score_conv.conv': 'rpn_cls_score', 'bbox_conv.conv': 'rpn_bbox_pred'}
        own_dict = self.state_dict()
        for k, v in pairs.items():
            key = '{}.weight'.format(k)
            param = torch.from_numpy(params['{}/weights:0'.format(v)]).permute(3, 2, 0, 1)
            own_dict[key].copy_(param)

            key = '{}.bias'.format(k)
            param = torch.from_numpy(params['{}/biases:0'.format(v)])
            own_dict[key].copy_(param)

class FasterRCNN(nn.Module):
    n_classes = 21
    classes = np.asarray(['__background__',
                       'aeroplane', 'bicycle', 'bird', 'boat',
                       'bottle', 'bus', 'car', 'cat', 'chair',
                       'cow', 'diningtable', 'dog', 'horse',
                       'motorbike', 'person', 'pottedplant',
                       'sheep', 'sofa', 'train', 'tvmonitor'])
    PIXEL_MEANS = np.array([[[102.9801, 115.9465, 122.7717]]])
    SCALES = (600,)
    MAX_SIZE = 1000

    def __init__(self, classes=None, debug=False):
        super(FasterRCNN, self).__init__()

        if classes is not None:
            self.classes = classes
            self.n_classes = len(classes)

        self.rpn = RPN()
        self.roi_pool = RoIPool(7, 7, 1.0/16)
        self.fc6 = FC(512 * 7 * 7, 4096)
        self.fc7 = FC(4096, 4096)
        self.score_fc = FC(4096, self.n_classes, relu=False)
        self.bbox_fc = FC(4096, self.n_classes * 4, relu=False)

        # loss
        self.cross_entropy = None
        self.loss_box = None

        # for log
        self.debug = debug

    @property
    def loss(self):
        # print self.cross_entropy
        # print self.loss_box
        # print self.rpn.cross_entropy
        # print self.rpn.loss_box
        return self.cross_entropy + self.loss_box * 10

    def forward(self, im_data, im_info, gt_boxes=None, gt_ishard=None, dontcare_areas=None):
        features, rois = self.rpn(im_data, im_info, gt_boxes, gt_ishard, dontcare_areas)
        # roi 感兴趣区域
        if self.training:
            roi_data = self.proposal_target_layer(rois, gt_boxes, gt_ishard, dontcare_areas, self.n_classes)
            rois = roi_data[0]

        # roi pool
        pooled_features = self.roi_pool(features, rois)
        x = pooled_features.view(pooled_features.size()[0], -1)
        x = self.fc6(x)
        x = F.dropout(x, training=self.training)
        x = self.fc7(x)
        x = F.dropout(x, training=self.training)

        cls_score = self.score_fc(x)
        cls_prob = F.softmax(cls_score)
        bbox_pred = self.bbox_fc(x)

        if self.training:
            self.cross_entropy, self.loss_box = self.build_loss(cls_score, bbox_pred, roi_data)

        return cls_prob, bbox_pred, rois

    def build_loss(self, cls_score, bbox_pred, roi_data):
        # classification loss
        label = roi_data[1].squeeze()
        fg_cnt = torch.sum(label.data.ne(0))
        bg_cnt = label.data.numel() - fg_cnt

        # for log
        if self.debug:
            maxv, predict = cls_score.data.max(1)
            self.tp = torch.sum(predict[:fg_cnt].eq(label.data[:fg_cnt])) if fg_cnt > 0 else 0
            self.tf = torch.sum(predict[fg_cnt:].eq(label.data[fg_cnt:]))
            self.fg_cnt = fg_cnt
            self.bg_cnt = bg_cnt

        ce_weights = torch.ones(cls_score.size()[1])
        ce_weights[0] = float(fg_cnt) / bg_cnt
        ce_weights = ce_weights.cuda()
        cross_entropy = F.cross_entropy(cls_score, label, weight=ce_weights)

        # bounding box regression L1 loss
        bbox_targets, bbox_inside_weights, bbox_outside_weights = roi_data[2:]
        bbox_targets = torch.mul(bbox_targets, bbox_inside_weights)
        bbox_pred = torch.mul(bbox_pred, bbox_inside_weights)

        loss_box = F.smooth_l1_loss(bbox_pred, bbox_targets, size_average=False) / (fg_cnt + 1e-4)

        return cross_entropy, loss_box

    @staticmethod
    def proposal_target_layer(rpn_rois, gt_boxes, gt_ishard, dontcare_areas, num_classes):
        """
        ----------
        rpn_rois:  (1 x H x W x A, 5) [0, x1, y1, x2, y2]
        gt_boxes: (G, 5) [x1 ,y1 ,x2, y2, class] int
        # gt_ishard: (G, 1) {0 | 1} 1 indicates hard
        dontcare_areas: (D, 4) [ x1, y1, x2, y2]
        num_classes
        ----------
        Returns
        ----------
        rois: (1 x H x W x A, 5) [0, x1, y1, x2, y2]
        labels: (1 x H x W x A, 1) {0,1,...,_num_classes-1}
        bbox_targets: (1 x H x W x A, K x4) [dx1, dy1, dx2, dy2]
        bbox_inside_weights: (1 x H x W x A, Kx4) 0, 1 masks for the computing loss
        bbox_outside_weights: (1 x H x W x A, Kx4) 0, 1 masks for the computing loss
        """
        rpn_rois = rpn_rois.data.cpu().numpy()
        rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights = \
            proposal_target_layer_py(rpn_rois, gt_boxes, gt_ishard, dontcare_areas, num_classes)
        # print labels.shape, bbox_targets.shape, bbox_inside_weights.shape
        rois = network.np_to_variable(rois, is_cuda=True)
        labels = network.np_to_variable(labels, is_cuda=True, dtype=torch.LongTensor)
        bbox_targets = network.np_to_variable(bbox_targets, is_cuda=True)
        bbox_inside_weights = network.np_to_variable(bbox_inside_weights, is_cuda=True)
        bbox_outside_weights = network.np_to_variable(bbox_outside_weights, is_cuda=True)

        return rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights

    def interpret_faster_rcnn(self, cls_prob, bbox_pred, rois, im_info, im_shape, nms=True, clip=True, min_score=0.0):
        # find class
        scores, inds = cls_prob.data.max(1)
        scores, inds = scores.cpu().numpy(), inds.cpu().numpy()

        keep = np.where((inds > 0) & (scores >= min_score))
        scores, inds = scores[keep], inds[keep]

        # Apply bounding-box regression deltas
        keep = keep[0]
        box_deltas = bbox_pred.data.cpu().numpy()[keep]
        box_deltas = np.asarray([
            box_deltas[i, (inds[i] * 4): (inds[i] * 4 + 4)] for i in range(len(inds))
        ], dtype=np.float)
        boxes = rois.data.cpu().numpy()[keep, 1:5] / im_info[0][2]
        pred_boxes = bbox_transform_inv(boxes, box_deltas)
        if clip:
            pred_boxes = clip_boxes(pred_boxes, im_shape)

        # nms
        if nms and pred_boxes.shape[0] > 0:
            pred_boxes, scores, inds = nms_detections(pred_boxes, scores, 0.3, inds=inds)

        return pred_boxes, scores, self.classes[inds]

    def detect(self, image, thr=0.3):
        im_data, im_scales = self.get_image_blob(image)
        im_info = np.array(
            [[im_data.shape[1], im_data.shape[2], im_scales[0]]],
            dtype=np.float32)

        cls_prob, bbox_pred, rois = self(im_data, im_info)
        pred_boxes, scores, classes = \
            self.interpret_faster_rcnn(cls_prob, bbox_pred, rois, im_info, image.shape, min_score=thr)
        return pred_boxes, scores, classes

    def get_image_blob_noscale(self, im):
        im_orig = im.astype(np.float32, copy=True)
        im_orig -= self.PIXEL_MEANS

        processed_ims = [im]
        im_scale_factors = [1.0]

        blob = im_list_to_blob(processed_ims)

        return blob, np.array(im_scale_factors)

    def get_image_blob(self, im):
        """Converts an image into a network input.
        Arguments:
            im (ndarray): a color image in BGR order
        Returns:
            blob (ndarray): a data blob holding an image pyramid
            im_scale_factors (list): list of image scales (relative to im) used
                in the image pyramid
        """
        im_orig = im.astype(np.float32, copy=True)
        im_orig -= self.PIXEL_MEANS

        im_shape = im_orig.shape
        im_size_min = np.min(im_shape[0:2])
        im_size_max = np.max(im_shape[0:2])

        processed_ims = []
        im_scale_factors = []

        for target_size in self.SCALES:
            im_scale = float(target_size) / float(im_size_min)
            # Prevent the biggest axis from being more than MAX_SIZE
            if np.round(im_scale * im_size_max) > self.MAX_SIZE:
                im_scale = float(self.MAX_SIZE) / float(im_size_max)
            im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale,
                            interpolation=cv2.INTER_LINEAR)
            im_scale_factors.append(im_scale)
            processed_ims.append(im)

        # Create a blob to hold the input images
        blob = im_list_to_blob(processed_ims)

        return blob, np.array(im_scale_factors)

    def load_from_npz(self, params):
        self.rpn.load_from_npz(params)

        pairs = {'fc6.fc': 'fc6', 'fc7.fc': 'fc7', 'score_fc.fc': 'cls_score', 'bbox_fc.fc': 'bbox_pred'}
        own_dict = self.state_dict()
        for k, v in pairs.items():
            key = '{}.weight'.format(k)
            param = torch.from_numpy(params['{}/weights:0'.format(v)]).permute(1, 0)
            own_dict[key].copy_(param)

            key = '{}.bias'.format(k)
            param = torch.from_numpy(params['{}/biases:0'.format(v)])
            own_dict[key].copy_(param)


text:
import numpy as np

############################
# simulation helpers #
############################

def simulate_pulse(IF_freq, chi, k, Ts, Td, power):
    I = [0]
    Q = [0]
    # solve numerically a simplified version of the readout resonator
    for t in range(Ts):
        I.append(I[-1] + (power / 2 - k * I[-1] + Q[-1] * chi))
        Q.append(Q[-1] + (power / 2 - k * Q[-1] - I[-1] * chi))

    for t in range(Td - 1):
        I.append(I[-1] + (-k * I[-1] + Q[-1] * chi))
        Q.append(Q[-1] + (-k * Q[-1] - I[-1] * chi))

    I = np.array(I)
    Q = np.array(Q)
    t = np.arange(len(I))

    S = I * np.cos(2 * np.pi * IF_freq * t * 1e-9) + Q * np.sin(
        2 * np.pi * IF_freq * t * 1e-9
    )

    return t, I, Q, S

###########################
# config params #
###########################

lo_freq = 7.1e9
lo_freq_qb = 5.1e9

rr_num = 5

qb_freqs = [51.223e6, 63.635e6, 72.11e6, 81.784e6, 95.533e6]
freqs = [51.223e6, 63.635e6, 72.11e6, 81.784e6, 95.533e6]

time_of_flight = 100
readout_len = 480

i_port = 10
q_port = 5

############################
# generate waveforms #
############################

Ts = readout_len - 200
Td = 200
power = 0.2
k = 0.04
chi = 0.023 * np.array([-1, 1, 5])

t_, I_, Q_, S_ = [""] * rr_num, [""] * rr_num, [""] * rr_num, [""] * rr_num
state = [0, 1, 1, 2, 0]
num_of_states = 3
for i in range(num_of_states):
    t_[i], I_[i], Q_[i], S_[i] = simulate_pulse(freqs[1], chi[i], k, Ts, Td, power)

divide_signal_factor = 60  # scale to get the signal within -0.5,0.5 range
############################
############################
############################

config = {
    "version": 1,
    "controllers": {
        "con1": {
            "type": "opx1",
            "analog_outputs": {
                i_port: {"offset": 0},
                q_port: {"offset": 0},
            },
            "digital_outputs": {
                1: {},
            },
            "analog_inputs": {
                1: {"offset": 0},
                2: {"offset": 0},
            },
        },
        "con2": {
            "type": "opx1",
            "analog_outputs": {i: {"offset": 0} for i in range(1, 11)},
            "digital_outputs": {
                1: {},
            },
            "analog_inputs": {
                1: {"offset": 0},
                2: {"offset": 0},
            },
        },
    },
    "elements": {},
    "pulses": {
        "readout_pulse": {
            "operation": "measurement",
            "length": readout_len,
            "waveforms": {"I": "zero_wf", "Q": "const_wf"},
            "integration_weights": {
                "integW_cos": "integW_cos",
                "integW_sin": "integW_sin",
            },
            "digital_marker": "ON",
        },
    },
    "waveforms": {
        "zero_wf": {"type": "constant", "sample": 0.0},
        "const_wf": {"type": "constant", "sample": 0.1},
    },
    "digital_waveforms": {"ON": {"samples": [(1, 0)]}},
    "integration_weights": {},
    "mixers": {},
}

for i in range(rr_num):
    config["elements"]["rr" + str(i)] = {
        "mixInputs": {
            "I": ("con1", i_port),
            "Q": ("con1", q_port),
            "lo_frequency": lo_freq,
            "mixer": "mixer_rr" + str(i),
        },
        "intermediate_frequency": freqs[i],
        "operations": {
            "readout": "readout_pulse_" + str(i),
        },
        "outputs": {
            "out1": ("con1", 1),
            "out2": ("con1", 2),
        },
        "time_of_flight": time_of_flight,
        "smearing": 0,
    }

    config["pulses"]["readout_pulse_" + str(i)] = {
        "operation": "measurement",
        "length": readout_len,
        "waveforms": {"I": "const_wf", "Q": "const_wf"},
        "integration_weights": {},
        "digital_marker": "ON",
    }
    config["waveforms"]["const_wf"] = {"type": "constant", "sample": 0.2}
    config["elements"]["qb" + str(i)] = {
        "mixInputs": {
            "I": ("con2", 2 * i + 1),
            "Q": ("con2", 2 * i + 2),
            "lo_frequency": lo_freq_qb,
            "mixer": "mixer_qb" + str(i),
        },
        "intermediate_frequency": qb_freqs[i],
        "operations": {
            "prepare0": "prepare_pulse_0",
            "prepare1": "prepare_pulse_1",
            "prepare2": "prepare_pulse_2",
        },
        "time_of_flight": time_of_flight,
        "smearing": 0,
    }
    config["mixers"]["mixer_rr" + str(i)] = [
        {
            "intermediate_frequency": freqs[i],
            "lo_frequency": lo_freq,
            "correction": [1, 0, 0, 1],
        },
    ]
    config["mixers"]["mixer_qb" + str(i)] = [
        {
            "intermediate_frequency": qb_freqs[i],
            "lo_frequency": lo_freq_qb,
            "correction": [1, 0, 0, 1],
        },
    ]

for i in range(num_of_states):
    config["pulses"]["prepare_pulse_" + str(i)] = {
        "operation": "measurement",
        "length": readout_len,
        "waveforms": {"I": "I_wf_" + str(i), "Q": "Q_wf_" + str(i)},
        "integration_weights": {},
        "digital_marker": "ON",
    }
    # need to create the desired waveforms for the preparation pulses
    config["waveforms"]["I_wf_" + str(i)] = {
        "type": "arbitrary",
        "samples": [float(arg / divide_signal_factor) for arg in I_[i]],
    }
    config["waveforms"]["Q_wf_" + str(i)] = {
        "type": "arbitrary",
        "samples": [float(arg / divide_signal_factor) for arg in Q_[i]],
    }

max_stars_repo_name: qua-platform/qua-libs

max_issues_repo_name: qua-platform/qua-libs

max_forks_repo_name: qua-platform/qua-libs


text:
#-*- coding: utf-8 -*-
#-*- coding: cp950 -*-

# 工具載入
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt

def printx(string, obj) :
    print "\n" + string + " \n" + str(obj)

arr = np.array([1,2,3])
printx("np.array([1,2,3])", arr)

arr = np.zeros(5)
printx("np.zeros(5)", arr)

arr = np.arange(5)
printx("np.arange(5)", arr)

arr = np.arange(6,10)
printx("np.arange(6,10)", arr)

arr = np.linspace(0,10,6)
printx("np.linspace(0,10,6)", arr)

#例 : 印出 10^0 ~ 10^1 分100個 np.logspace(0,1,100)
arr = np.logspace(0,8,9, base = 2)
printx("np.logspace(0,8,9, base = 2)", arr)

image = np.zeros((2,3))
printx("np.zeros((2,3))", image)

cube = np.zeros((2,3,4)).astype(int) + 1
printx("np.zeros((2,3,4)).astype(int) + 1", cube)
#http://stackoverflow.com/questions/28010860/slicing-3d-numpy-arrays

cube = np.zeros((2,3,4)).astype(np.float16) + 0.1
printx("np.zeros((2,3,4)).astype(np.float16) + 0.1", cube)

reason: import numpy,import scipy


text:
import logging
import time
import os
import numpy as np
import tensorflow as tf
from runner import Runner
import configs
import bsdes

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('tensorboard_dir', './train/',
                           """Directory for tensorboard log data.""")
tf.app.flags.DEFINE_boolean(
    'clone_deep_bsde_weights', False,
    """Initialize dual nn with the weights of the primal stage.""")

def dim_test():
    '''
    Stage 1 of analysing impact of dimensions of runtime.

    Only tests training speeds, and saves models to disk.
    '''
    dims = np.array([1, 25, 50, 75, 100, 250, 500, 750, 1000])
    speeds = {}
    for dimCount in dims:
        logging.info("Starting run for %4u dimensions." % dimCount)
        problem_config = configs.problemConfigBase._replace(dim=dimCount)
        runner = Runner(bsdes.Squared, problem_config, "Dim_%04u" % dimCount)

        start_time = time.time()
        runner.build_deep_nn(configs.deepConfigBase)
        deep_y0 = runner.train_deep_nn()
        elapsed_deep = time.time() - start_time
        runner.save_deep_nn("./saves/dimTest_%04u_dims" % dimCount)

        # start_time = time.time()
        # primal_y0 = runner.run_primal(configs.primalConfigBase)
        # elapsed_primal = time.time() - start_time

        start_time = time.time()
        runner.build_dual_nn(configs.dualConfigBase, configs.deepConfigBase)
        runner.train_dual_nn()
        elapsed_dual_train = time.time() - start_time
        runner.save_dual_nn("./saves/dimTest_%04u_dims" % dimCount)

        # start_time = time.time()
        # dual_y0 = runner.run_dual()
        # elapsed_dual_run = time.time() - start_time

        #logging.info("Primal: %.5e Deep: %.5e Dual: %.5e" % (primal_y0, deep_y0, dual_y0))
        speeds[dimCount] = (elapsed_deep, elapsed_dual_train)
    np.save("speedtest_half.dump", speeds)

def dim_test2():
    '''
    Stage 2 of analysing impact of dimensions of runtime.

    Loads previously generated models and tests Primal and Dual sampling.
    '''

    dims = np.array([1, 25, 50, 75, 100, 250, 500, 750, 1000])
    speeds = {}
    for dimCount in dims:
        logging.info("Starting run for %4u dimensions." % dimCount)
        problem_config = configs.problemConfigBase._replace(dim=dimCount)
        runner = Runner(bsdes.Squared, problem_config, "Dim_%04u" % dimCount)

        runner.build_deep_nn(configs.deepConfigBase)
        runner.restore_deep_nn("./saves/dimTest_%04u_dims_deep" % dimCount)

        start_time = time.time()
        primal_y0 = runner.run_primal(configs.primalConfigBase)
        elapsed_primal = time.time() - start_time

        runner.build_dual_nn(configs.dualConfigBase, configs.deepConfigBase)
        runner.restore_dual_nn("./saves/dimTest_%04u_dims_dual" % dimCount)

        start_time = time.time()
        dual_y0 = runner.run_dual()
        elapsed_dual_run = time.time() - start_time

        #logging.info("Primal: %.5e Deep: %.5e Dual: %.5e" % (primal_y0, deep_y0, dual_y0))
        speeds[dimCount] = (elapsed_primal, elapsed_dual_run)
    np.save("speedtest_other_half.dump", speeds)

def tiv_test():
    '''
    Test impact of number of time intervals on convergence.

    Analysis is done using tensorboard.
    Also records speeds to verify linear dependence.
    '''
    stepping = np.array([5, 10, 15, 20, 25, 30])
    speeds = {}
    for steps in stepping:
        tf.set_random_seed(4242424242)
        np.random.seed(4242424242)
        logging.info("Starting run for %02u steps." % steps)
        problem_config = configs.problemConfigBase._replace(
            num_time_interval=steps)
        runner = Runner(bsdes.Squared, problem_config, "Step_%04u" % steps)

        start_time = time.time()
        runner.build_deep_nn(configs.deepConfigBase)
        deep_y0 = runner.train_deep_nn()
        elapsed_deep = time.time() - start_time
        runner.save_deep_nn("./saves/tivTest_%04u_steps" % steps)

        start_time = time.time()
        primal_y0 = runner.run_primal(configs.primalConfigBase)
        elapsed_primal = time.time() - start_time

        start_time = time.time()
        runner.build_dual_nn(configs.dualConfigBase, configs.deepConfigBase)
        runner.train_dual_nn()
        elapsed_dual_train = time.time() - start_time
        runner.save_dual_nn("./saves/tivTest_%04u_steps" % steps)

        start_time = time.time()
        dual_y0 = runner.run_dual()
        elapsed_dual = time.time() - start_time

        #logging.info("Primal: %.5e Deep: %.5e Dual: %.5e" % (primal_y0, deep_y0, dual_y0))
        speeds[steps] = (elapsed_deep, elapsed_primal, elapsed_dual_train,
                         elapsed_dual)
    np.save("tivtest.dump", speeds)

def y0_impact_test():
    '''
    Test impact of inital value of y_0 on convergence of the deep BSDE solver.

    Analysis is done using tensorboard.
    '''
    y0_guesses = np.array([0, 0.1, 0.2, 0.26, 0.3, 0.4])
    for guess in y0_guesses:
        tf.set_random_seed(4242424242)
        np.random.seed(4242424242)
        logging.info("Starting run for y_0 = %.2e" % guess)
        runner = Runner(bsdes.Squared, configs.problemConfigBase,
                        "guess_%u" % (guess * 100))

        start_time = time.time()
        deep_config = configs.deepConfigBase._replace(
            y_init_range=[guess, guess])
        runner.build_deep_nn(deep_config)
        deep_y0 = runner.train_deep_nn()

def depth_test():
    '''
    Test impact of different subnetwork structures on convergence.

    Analysis is done using tensorboard.
    Also records speeds to verify linear dependence.
    '''
    depths = np.array([3, 3, 4, 4, 5, 5])
    structures = np.array([[0, 0, 0], [0, 10, 0], [0, 0, 0, 0], [0, 10, 10, 0],
                           [0, 0, 0, 0, 0], [0, 10, 10, 10, 0]])

    speeds = {}
    for (depth, structure) in zip(depths, structures):
        tf.set_random_seed(4242424242)
        np.random.seed(4242424242)
        logging.info("Starting run for" + str(structure))
        runner = Runner(bsdes.Squared, configs.problemConfigBase,
                        "Depth_%s" % str(structure))

        deep_config = configs.deepConfigBase._replace(
            n_layer=depth, num_hiddens=structure)

        start_time = time.time()
        runner.build_deep_nn(deep_config)
        deep_y0 = runner.train_deep_nn()
        elapsed_deep = time.time() - start_time
        runner.save_deep_nn("./saves/depthTest_%s_steps" % str(structure))

        start_time = time.time()
        primal_y0 = runner.run_primal(configs.primalConfigBase)
        elapsed_primal = time.time() - start_time

        start_time = time.time()
        runner.build_dual_nn(configs.dualConfigBase, deep_config)
        runner.train_dual_nn()
        elapsed_dual_train = time.time() - start_time
        runner.save_dual_nn("./saves/depthTest_%s_steps" % str(structure))

        start_time = time.time()
        dual_y0 = runner.run_dual()
        elapsed_dual = time.time() - start_time

        speeds[str(structure)] = (elapsed_deep, elapsed_primal,
                                  elapsed_dual_train, elapsed_dual)
    np.save("depthtest.dump", speeds)

def linear():
    '''
    Do a full run for f(u) = u
    '''
    runner = Runner(bsdes.Linear, configs.problemConfigBase)
    runner.build_deep_nn(configs.deepConfigBase)
    deep_y0 = runner.train_deep_nn()
    runner.save_deep_nn("./saves/T1")
    #runner.restore_nn("./saves/SHACV2.ckpt")
    primal_y0 = runner.run_primal(configs.primalConfigBase)
    runner.build_dual_nn(configs.dualConfigBase, configs.deepConfigBase)
    #runner.restore_nn("./saves/SHACV2.ckpt")
    runner.train_dual_nn()
    runner.save_dual_nn("./saves/T1")
    dual_y0 = runner.run_dual()
    logging.info("Primal: %.5e Deep: %.5e Dual: %.5e" % (primal_y0, deep_y0,
                                                         dual_y0))

def square_learn():
    '''
    Do a full run for f(u) = u^2
    '''
    runner = Runner(bsdes.Squared, configs.problemConfigBase)
    runner.build_deep_nn(configs.deepConfigBase)
    deep_y0 = runner.train_deep_nn()
    #runner.restore_nn("./saves/SHACV2.ckpt")
    primal_y0 = runner.run_primal(configs.primalConfigBase)
    runner.save_deep_nn("./saves/T2")
    runner.build_dual_nn(configs.dualConfigBase, configs.deepConfigBase)
    #runner.restore_nn("./saves/SHACV2.ckpt")
    runner.train_dual_nn()
    dual_y0 = runner.run_dual()
    runner.save_dual_nn("./saves/T2")
    logging.info("Primal: %.5e Deep: %.5e Dual: %.5e" % (primal_y0, deep_y0,
                                                         dual_y0))

def square_restore():
    '''
    Do only Dual sampling, using networks restored from disk.
    '''
    runner = Runner(bsdes.Squared, configs.problemConfigBase)
    runner.build_deep_nn(configs.deepConfigBase)
    #deep_y0 = runner.train_deep_nn()
    runner.restore_deep_nn("./saves/T2_deep")
    #deep_y0 = runner.run_deep_nn()

    #primal_y0 = runner.run_primal(configs.primalConfigBase)

    runner.build_dual_nn(configs.dualConfigBase, configs.deepConfigBase)
    #runner.train_dual_nn()
    runner.restore_dual_nn("./saves/T2_dual")
    dual_y0 = runner.run_dual()
    #logging.info("Primal: %.5e Deep: %.5e Dual: %.5e" % (primal_y0, deep_y0, dual_y0))

def main():
    logging.basicConfig(
        level=logging.INFO, format='%(levelname)-6s %(message)s')
    if (not os.path.exists('./saves')):
        os.mkdir('./saves')
    if (not os.path.exists(FLAGS.tensorboard_dir)):
        os.mkdir(FLAGS.tensorboard_dir)
    square_learn()

if __name__ == '__main__':
    main()

max_stars_repo_name: karlschrader/deepPD

max_issues_repo_name: karlschrader/deepPD

max_forks_repo_name: karlschrader/deepPD


text:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist

# Define hyper parameters
numClasses = 10
numFeatures = 28*28
learningRate = 0.01
numSteps = 1000
batchSize = 256
displayStep = 50

(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Reshape the dataset to linear array
x_train = np.array(x_train.reshape([-1,numFeatures]),np.float32)
x_test = np.array(x_test.reshape([-1,numFeatures]),np.float32)
x_train = x_train/255.
x_test = x_test/255.
print("Shape of x_train",x_train.shape)
print("Shape of y_train",y_train.shape)

train_data = tf.data.Dataset.from_tensor_slices((x_train,y_train))
train_data = train_data.repeat().shuffle(60000).batch(batchSize).prefetch(1)

# Define the variables
W = tf.Variable(tf.ones([numFeatures,numClasses]))
b = tf.Variable(tf.zeros([numClasses]))

# Define necessary functions
def logReg(x):
    return tf.nn.softmax(tf.matmul(x,W)+b)

def crossEntropy(y_pred,y_true):
    y_true = tf.one_hot(y_true,depth = numClasses,dtype = tf.float32)
    return tf.reduce_mean(-tf.math.log(tf.reduce_sum(tf.math.multiply(y_true,y_pred),1)))

def accuracy(y_pred,y_true):
    correctPredictions = tf.equal(tf.argmax(y_pred,1),tf.cast(y_true,tf.int64))
    return tf.reduce_mean(tf.cast(correctPredictions,tf.float32))

optimizer = tf.optimizers.SGD(learningRate)

def runOptimization(x,y):
    with tf.GradientTape() as g:
        pred = logReg(x)
        loss = crossEntropy(pred,y)

    # Compute gradients
    gradients = g.gradient(loss,[W,b])
    optimizer.apply_gradients(zip(gradients,[W,b]))

for step,(batchX,batchY) in enumerate(train_data.take(numSteps),1):
    runOptimization(batchX,batchY)

    if step % displayStep == 0:
        pred = logReg(batchX)
        loss = crossEntropy(pred,batchY)
        acc = accuracy(pred,batchY)
        print("Step: %i, loss: %f, accuracy: %f"%(step,loss,acc))

pred = logReg(x_test)
acc = accuracy(pred,y_test)
print("Your test data set accuracy is %f"%(acc))

nImages = 5
testImages = x_test[:nImages]

for i in range(nImages):
    plt.imshow(np.reshape(testImages[i],[28,28]),cmap = 'gray')
    plt.show()
    print("Model prediction: %i" %np.argmax(pred.numpy()[i]))


text:
import numpy as np

def epsilon_greedy_policy(Qs, nA, i_episode, epsilon=None):
    """epsilon greedy policy
        env: Gym environment
        s: state
        i_episode: index of current episode -> epsilon should reduce when i increase
        eps: customized epsilon
    """
    epsilon = 1 / i_episode if epsilon is None else epsilon
    policy = np.ones(nA) * epsilon / nA
    policy[np.argmax(Qs)] = 1 - epsilon + (epsilon /nA) #np.argmax(s) means action a maximizes Q(s,a)

    return policy

def update_helper(Qsa, Qsa_next, alpha, reward, gamma):
    """helper function to update Q table
    Args:
        Qsa: Q table value at State s, Action a
        Qsa_next: Q table value at State s_next, Action a_next

    Return:
        Updated Qsa
    """
    return Qsa + alpha * (reward + gamma * Qsa_next - Qsa)

max_stars_repo_name: mirocody/Deep-Reinforcement-Learning

max_issues_repo_name: mirocody/Deep-Reinforcement-Learning

max_forks_repo_name: mirocody/Deep-Reinforcement-Learning


text:
import numpy as np
import h5py
import json
sys.path.append('F:\Linux')
import illustris_python as il
import matplotlib.pyplot as plt

def Flatness(MassTensor):
    #c / a = (M3)**0.5 / (M1)**0.5
    return np.sqrt(MassTensor[0]) / np.sqrt(MassTensor[2])

def BtoA(MassTensor):
    #b / a = (M2)**0.5 / (M1)**0.5
    return np.sqrt(MassTensor[0]) / np.sqrt(MassTensor[1])

def LoadMergHist(simu, subhaloID):
    '''
    return subhalo's main progenitor and merger history with snapshot
    '''
    if simu == 'TNG':
        ldir = 'f:/Linux/localRUN/tng_DiskMerTree/%d.json' % subhaloID
    else:
        ldir = 'f:/Linux/localRUN/il1_DiskMerTree/%d.json' % subhaloID

    with open(ldir) as f:
        data = json.load(f)

    Main = np.array(data['Main'])
    return dict(zip(Main[:, 0], Main[:, 1])), np.array(data['Mergers'])

def MassTensorEigenVals(coor, mas, half_r):
    '''
    Return eigenvalues of the mass tensor, sorted by M1 < M2 < M3
    '''
    r = coor - coor[0]
    r[r > 37500] -= 75000
    r[r < -37500] += 75000
    dis = np.linalg.norm(r, axis=1)
    inside = dis < (half_r * 2)
    r = r[inside]
    mas = mas[inside]

    M_x = ((mas * (r[:, 0]/0.6774)**2).sum())**0.5 / mas.sum()**0.5
    M_y = ((mas * (r[:, 1]/0.6774)**2).sum())**0.5 / mas.sum()**0.5
    M_z = ((mas * (r[:, 2]/0.6774)**2).sum())**0.5 / mas.sum()**0.5
    M = np.array([M_x, M_y, M_z])
    M.sort()
    return M

diskID = np.load('f:/Linux/localRUN/diskID_il1.npy')
barredID = np.load('f:/Linux/localRUN/barredID_il1.npy')

'''
Illustris-1 Snapshot-Redshift:
snap_127 z=0.1
snap_120 z=0.2
snap_113 z=0.3
snap_108 z=0.4
snap_103 z=0.5
snap_95 z=0.7
snap_85 z=1.0
snap_75 z=1.5
snap_68 z=2.0
'''
'''
SnapList = [135, 127, 120, 113, 108, 103, 95, 85, 75, 68]
RedShift = [0, 0.1 , 0.2, 0.3, 0.4, 0.5, 0.7, 1.0, 1.5, 2.0]
'''

SnapList = [127, 120, 113, 108, 103, 95, 85, 75, 68]
RedShift = [0.1, 0.2, 0.3, 0.4, 0.5, 0.7, 1.0, 1.5, 2.0]

# move = np.array([0, 1, -1, 2, -2])

for haloID in barredID[::3]:
    proj = LoadMergHist('il1', haloID)[0]
    ca = []
    ba = []
    for snap in SnapList:
        try:
            hid = proj[snap]
        except KeyError:
            apdata = 0
        else:
            print('hid ', hid, 'snap ', snap)
            if hid == -1:
                apdata = 0
            else:
                with h5py.File('g:/il1_cutoff/snap_%d/cutout_%d.hdf5' % (snap, hid)) as f:
                    coor = np.array(f['PartType1']['Coordinates'])
                    DMhalf_r = il.func.loadSubhalos('il1', snap, 'SubhaloHalfmassRadType')[hid, 1]
                MTE = MassTensorEigenVals(coor, np.ones(len(coor)), DMhalf_r)

                ba.append(BtoA(MTE))
                ca.append(Flatness(MTE))
                apdata = 1

        if apdata == 0:
            ba.append(1)
            ca.append(1)
    CA = []
    x = []
    for i in range(9):
        if ca[i] != 1:
            CA.append(ca[i])
            x.append(RedShift[i])

    plt.ylim(0.5,1)
    plt.plot(x, CA, color='black', linewidth=0.2)

    plt.plot(RedShift, ba)

import numpy as np
import h5py
import illustris_python as il
def MassTensorEigenVals(coor, mas, half_r):
    '''
    Return eigenvalues of the mass tensor, sorted by M1 < M2 < M3
    '''
    r = coor - coor[0]
    r[r > 37500] -= 75000
    r[r < -37500] += 75000
    dis = np.linalg.norm(r, axis=1)
    inside = dis < (half_r * 2)
    r = r[inside]
    mas = mas[inside]

    M_x = ((mas * (r[:, 0]/0.6774)**2).sum())**0.5 / mas.sum()**0.5
    M_y = ((mas * (r[:, 1]/0.6774)**2).sum())**0.5 / mas.sum()**0.5
    M_z = ((mas * (r[:, 2]/0.6774)**2).sum())**0.5 / mas.sum()**0.5
    M = np.array([M_x, M_y, M_z])
    M.sort()
    return M

data = {}
barredID = np.load('/Raid0/zhouzb/TNG/barredID_4WP_TNG.npy')
for haloID in barredID:
    coor = il.func.loadgalaxy('TNG', 99, haloID, 1, 'Coordinates')
    r = il.func.loadSubhalos('TNG', 99, 'SubhaloHalfmassRadType')[haloID, 1]
    data[haloID] = MassTensorEigenVals(coor, np.ones(len(coor)), r)

np.save('/Raid0/zhouzb/barMTE_TNGsnap99.npy', data)


text:
import os

import numpy as np

from .file import File
from .input import InpFile
from .vals import ValueSubSection

class OutFile(File):
    """
    Parent class for (nearly all) DSSAT output files.
    """

    filename = None  # type: str

    def __init__(self, folder):
        if os.path.isdir(folder):
            file = os.path.join(folder, self.filename)
        else:
            file = folder

        super().__init__(file)

    def _process_section_header(self, lines):

        section_name = lines[0][1:]
        self._values.add_section(section_name)
        i = 0
        for l in lines:
            if 'TREATMENT' in l:
                trt_no = l.split(' : ')[0].split()[1].strip()
                subsect = ValueSubSection(['TREATMENT'], l_vals=[np.array([trt_no], dtype=int)])

                self._values[section_name].set_header_vars(subsect)

            elif '@' in l:
                break
            i += 1

        return section_name, lines[i:]

    def _get_var_info(self):
        raise NotImplementedError

    @classmethod
    def matches_file(cls, file):
        """
        Checks whether a given file can be read by this class.

        Parameters
        ----------
        file: str
           The filename or full path to be read.

        Returns
        -------
        bool
           ``True`` if the file matches; ``False`` otherwise.
        """

        fname = os.path.split(file)[1]
        return fname.lower() == cls.filename.lower()

class FinalOutFile(InpFile):
    filename = None  # type: str

    def __init__(self, folder):
        if os.path.isdir(folder):
            file = os.path.join(folder, self.filename)
        else:
            file = folder

        super().__init__(file)

    @classmethod
    def matches_file(cls, file):
        fname = os.path.split(file)[1]
        return fname.lower() == cls.filename.lower()

    def _get_var_info(self):
        raise NotImplementedError

max_stars_repo_name: shreyayadav/traDSSAT

max_issues_repo_name: shreyayadav/traDSSAT

max_forks_repo_name: shreyayadav/traDSSAT


text:
#!/usr/bin/env python

# stdlib imports
import urllib.request as request
import tempfile
import os.path
import sys

# third party imports
import numpy as np

# local imports
from losspager.models.growth import PopulationGrowth, adjust_pop

POPURL = 'http://esa.un.org/unpd/wpp/DVD/Files/1_Indicators%20(Standard)/EXCEL_FILES/1_Population/WPP2015_POP_F02_POPULATION_GROWTH_RATE.XLS'

def test_adjust_pop():
    print('Testing simple positive population growth...')
    tpop = 2015
    tevent = 2016
    ccode = 841  # US
    pop = 1e6
    rate = 0.01  # 1% growth rate
    newpop = adjust_pop(pop, tpop, tevent, rate)
    assert newpop == pop + pop * rate
    print('Passed simple positive population growth.')

    print('Testing simple negative population growth...')
    tpop = 2016
    tevent = 2015
    pop = 1e6
    rate = 0.01  # 1% growth rate
    newpop = adjust_pop(pop, tpop, tevent, rate)
    assert newpop == 990099.0  # not (pop - pop*rate) : how come?
    print('Passed simple negative population growth.')

    print('Testing simple zero population growth...')
    tpop = 2016
    tevent = 2016
    pop = 1e6
    rate = 0.01  # 1% growth rate
    newpop = adjust_pop(pop, tpop, tevent, rate)
    assert newpop == pop
    print('Passed simple zero population growth.')

def test_pop_growth():
    homedir = os.path.dirname(os.path.abspath(
        __file__))  # where is this script?
    print('Testing loading Population Growth from UN spreadsheet...')
    pg = PopulationGrowth.fromDefault()
    print('Passed loading Population Growth from UN spreadsheet...')

    print('Testing getting growth rates for the US...')
    rate = pg.getRate(840, 1963)
    assert rate == 1.373 / 100.0
    allrates = np.array([1.581, 1.724, 1.373, 0.987, 0.885, 0.948,
                         0.945, 0.985, 1.035, 1.211, 0.915, 0.907, 0.754]) / 100.0
    starts, usrates = pg.getRates(840)
    np.testing.assert_almost_equal(usrates, allrates)
    print('Passed getting growth rate for the US...')

    # three scenarios to test with regards to population growth rates
    # 1: time population data was "collected" is before the event time
    tpop = 2015
    tevent = 2016
    ccode = 840  # US
    pop = 1e6
    newpop = pg.adjustPopulation(pop, ccode, tpop, tevent)

    tpop = 2007
    tevent = 2016
    ccode = 840
    pop = 1e6
    # TODO:  hand-verify this result!
    newpop = pg.adjustPopulation(pop, ccode, tpop, tevent)

    # #2: time population data was "collected" is after the event time
    tpop = 2016
    tevent = 2012
    ccode = 840  # US
    pop = 1e6
    newpop = pg.adjustPopulation(pop, ccode, tpop, tevent)
    # #3 time population data was collected is equal to event time
    # tpop = 2012
    # tevent = 2012
    # ccode = 841 #US
    # pop = 1e6
    # newpop = pg.adjustPopulation(pop,ccode,tpop,tevent)

if __name__ == '__main__':
    test_adjust_pop()
    test_pop_growth()

max_stars_repo_name: emthompson-usgs/pager

max_issues_repo_name: emthompson-usgs/pager

max_forks_repo_name: emthompson-usgs/pager


text:
import os
import pickle

import numpy
import matplotlib.pyplot as plt
import scipy.stats

import _mypath
import chroma

def encode_obstypeid(filter_name, zenith, seed):
    filter_number = {'u':'0','g':'1','r':'2','i':'3','z':'4','Y':'5'}
    filter_digit = filter_number[filter_name]
    zenith_digit = str(int(round((zenith / 10.0))))
    seed_digit = str(seed - 1000)
    return filter_digit + zenith_digit + seed_digit

def plot_relative_moments(filter_name, zenith, seed):
    obstypeid = encode_obstypeid(filter_name, zenith, seed)
    m_star_diff, m_gal_diff = pickle.load(open('relative_moments.{}.pik'.format(obstypeid), 'rb'))

    # Rbar plot (x)
    ###############
    M1_key = 'ALPHAWIN_SKY'

    f = plt.figure(figsize=(8,6), dpi=100)
    ax1 = f.add_subplot(111)
    ax1.set_xlim(-0.1, 4.0)
    ax1.set_xlabel('Redshift')
    ax1.set_ylabel('$\Delta \overline{\mathrm{R}_x}$ (arcsec)')
    ax1.set_title('field angle = 1.6447 degrees, filter = {}'.format(filter_name))
    if not os.path.exists('plots/'):
        os.mkdir('plots/')

    star_names = ['O5V', 'B5III', 'A5V', 'F5V', 'G5V', 'K5V', 'M5V']
    stars = ['uko5v', 'ukb5iii', 'uka5v', 'ukf5v', 'ukg5v', 'ukk5v', 'ukm5v']
    star_colors = ['Blue', 'Cyan', 'Green', 'Gold', 'Orange', 'Red', 'Violet']

    yvals = numpy.empty(0)
    #plot stars
    for star, star_name, star_color in zip(stars, star_names, star_colors):
        measured = m_star_diff[star][M1_key] * 3600
        ax1.scatter(0.0, measured, marker='*', c=star_color, s=80, label=star_name)
        yvals = numpy.append(yvals, measured)

    gal_names = ['E', 'Sa', 'Sb', 'Sbc', 'Scd', 'Im', 'SB1', 'SB6']
    gals = ['CWW_E_ext', 'KIN_Sa_ext', 'KIN_Sb_ext', 'CWW_Sbc_ext', 'CWW_Scd_ext',
            'CWW_Im_ext', 'KIN_SB1_ext', 'KIN_SB6_ext']
    gal_colors = ['Violet', 'Red', 'Orange', 'Gold', 'Green', 'Cyan', 'Blue']

    #plot gals
    zs = numpy.arange(0.0, 3.0, 0.03)
    for gal, gal_name, gal_color in zip(gals, gal_names, gal_colors):
        measured = numpy.array(m_gal_diff[gal][M1_key]) * 3600
        ax1.scatter(zs, measured, marker='.', c=gal_color, s=50, label=gal_name)
        yvals = numpy.append(yvals, measured)
    ax1.legend(prop={"size":9})

    yrange = numpy.array([scipy.stats.scoreatpercentile(yvals, 1),
                          scipy.stats.scoreatpercentile(yvals, 99)])

    yspan = yrange[1] - yrange[0]
    yrange = yrange + yspan * numpy.array([-0.3, 0.3])

    ax1.set_ylim(yrange[0], yrange[1])

    f.savefig('plots/Rbar_x.{}.png'.format(obstypeid))

    # Rbar plot (y)
    ###############
    M1_key = 'DELTAWIN_SKY'

    f = plt.figure(figsize=(8,6), dpi=100)
    ax1 = f.add_subplot(111)
    ax1.set_xlim(-0.1, 4.0)
    ax1.set_xlabel('Redshift')
    ax1.set_ylabel('$\Delta \overline{\mathrm{R}_y}$ (arcsec)')
    ax1.set_title('field angle = 1.6447 degrees, filter = {}'.format(filter_name))
    if not os.path.exists('plots/'):
        os.mkdir('plots/')

    star_names = ['O5V', 'B5III', 'A5V', 'F5V', 'G5V', 'K5V', 'M5V']
    stars = ['uko5v', 'ukb5iii', 'uka5v', 'ukf5v', 'ukg5v', 'ukk5v', 'ukm5v']
    star_colors = ['Blue', 'Cyan', 'Green', 'Gold', 'Orange', 'Red', 'Violet']

    yvals = numpy.empty(0)
    #plot stars
    for star, star_name, star_color in zip(stars, star_names, star_colors):
        measured = m_star_diff[star][M1_key] * 3600
        ax1.scatter(0.0, measured, marker='*', c=star_color, s=80, label=star_name)
        yvals = numpy.append(yvals, measured)

    gal_names = ['E', 'Sa', 'Sb', 'Sbc', 'Scd', 'Im', 'SB1', 'SB6']
    gals = ['CWW_E_ext', 'KIN_Sa_ext', 'KIN_Sb_ext', 'CWW_Sbc_ext', 'CWW_Scd_ext',
            'CWW_Im_ext', 'KIN_SB1_ext', 'KIN_SB6_ext']
    gal_colors = ['Violet', 'Red', 'Orange', 'Gold', 'Green', 'Cyan', 'Blue']

    #plot gals
    zs = numpy.arange(0.0, 3.0, 0.03)
    for gal, gal_name, gal_color in zip(gals, gal_names, gal_colors):
        measured = numpy.array(m_gal_diff[gal][M1_key]) * 3600
        ax1.scatter(zs, measured, marker='.', c=gal_color, s=50, label=gal_name)
        yvals = numpy.append(yvals, measured)
    ax1.legend(prop={"size":9})

    yrange = numpy.array([scipy.stats.scoreatpercentile(yvals, 1),
                          scipy.stats.scoreatpercentile(yvals, 99)])

    yspan = yrange[1] - yrange[0]
    yrange = yrange + yspan * numpy.array([-0.3, 0.3])

    ax1.set_ylim(yrange[0], yrange[1])

    f.savefig('plots/Rbar_y.{}.png'.format(obstypeid))

    # V plot (x), unweighted
    ########################

    M2_key = 'VX40'

    f = plt.figure(figsize=(8,6), dpi=100)
    ax1 = f.add_subplot(111)
    ax1.set_xlim(-0.1, 4.0)
    ax1.set_xlabel('Redshift')
    ax1.set_ylabel('$\Delta \mathrm{V}_x$ (arcsec$^2$)')
    ax1.set_title('field angle = 1.6447 degrees, filter = {}'.format(filter_name))
    if not os.path.exists('plots/'):
        os.mkdir('plots/')

    star_names = ['O5V', 'B5III', 'A5V', 'F5V', 'G5V', 'K5V', 'M5V']
    stars = ['uko5v', 'ukb5iii', 'uka5v', 'ukf5v', 'ukg5v', 'ukk5v', 'ukm5v']
    star_colors = ['Blue', 'Cyan', 'Green', 'Gold', 'Orange', 'Red', 'Violet']

    yvals = numpy.empty(0)
    #plot stars
    for star, star_name, star_color in zip(stars, star_names, star_colors):
        measured = m_star_diff[star][M2_key] * 3600**2
        ax1.scatter(0.0, measured, marker='*', c=star_color, s=80, label=star_name)
        yvals = numpy.append(yvals, measured)

    gal_names = ['E', 'Sa', 'Sb', 'Sbc', 'Scd', 'Im', 'SB1', 'SB6']
    gals = ['CWW_E_ext', 'KIN_Sa_ext', 'KIN_Sb_ext', 'CWW_Sbc_ext', 'CWW_Scd_ext',
            'CWW_Im_ext', 'KIN_SB1_ext', 'KIN_SB6_ext']
    gal_colors = ['Violet', 'Red', 'Orange', 'Gold', 'Green', 'Cyan', 'Blue']

    #plot gals
    zs = numpy.arange(0.0, 3.0, 0.03)
    for gal, gal_name, gal_color in zip(gals, gal_names, gal_colors):
        measured = numpy.array(m_gal_diff[gal][M2_key]) * 3600**2
        ax1.scatter(zs, measured, marker='.', c=gal_color, s=50, label=gal_name)
        yvals = numpy.append(yvals, measured)
    ax1.legend(prop={"size":9})

    yrange = numpy.array([scipy.stats.scoreatpercentile(yvals, 1),
                           scipy.stats.scoreatpercentile(yvals, 99)])

    yspan = yrange[1] - yrange[0]
    yrange = yrange + yspan * numpy.array([-0.3, 0.3])

    ax1.set_ylim(yrange[0], yrange[1])

    f.savefig('plots/V_x.{}.png'.format(obstypeid))

    # V plot (y), unweighted
    ########################

    M2_key = 'VY40'

    f = plt.figure(figsize=(8,6), dpi=100)
    ax1 = f.add_subplot(111)
    ax1.set_xlim(-0.1, 4.0)
    ax1.set_xlabel('Redshift')
    ax1.set_ylabel('$\Delta \mathrm{V}_y$ (arcsec$^2$)')
    ax1.set_title('field angle = 1.6447 degrees, filter = {}'.format(filter_name))
    if not os.path.exists('plots/'):
        os.mkdir('plots/')

    star_names = ['O5V', 'B5III', 'A5V', 'F5V', 'G5V', 'K5V', 'M5V']
    stars = ['uko5v', 'ukb5iii', 'uka5v', 'ukf5v', 'ukg5v', 'ukk5v', 'ukm5v']
    star_colors = ['Blue', 'Cyan', 'Green', 'Gold', 'Orange', 'Red', 'Violet']

    yvals = numpy.empty(0)
    #plot stars
    for star, star_name, star_color in zip(stars, star_names, star_colors):
        measured = m_star_diff[star][M2_key] * 3600**2
        ax1.scatter(0.0, measured, marker='*', c=star_color, s=80, label=star_name)
        yvals = numpy.append(yvals, measured)

    gal_names = ['E', 'Sa', 'Sb', 'Sbc', 'Scd', 'Im', 'SB1', 'SB6']
    gals = ['CWW_E_ext', 'KIN_Sa_ext', 'KIN_Sb_ext', 'CWW_Sbc_ext', 'CWW_Scd_ext',
            'CWW_Im_ext', 'KIN_SB1_ext', 'KIN_SB6_ext']
    gal_colors = ['Violet', 'Red', 'Orange', 'Gold', 'Green', 'Cyan', 'Blue']

    #plot gals
    zs = numpy.arange(0.0, 3.0, 0.03)
    for gal, gal_name, gal_color in zip(gals, gal_names, gal_colors):
        measured = numpy.array(m_gal_diff[gal][M2_key]) * 3600**2
        ax1.scatter(zs, measured, marker='.', c=gal_color, s=50, label=gal_name)
        yvals = numpy.append(yvals, measured)
    ax1.legend(prop={"size":9})

    yrange = numpy.array([scipy.stats.scoreatpercentile(yvals, 1),
                           scipy.stats.scoreatpercentile(yvals, 99)])

    yspan = yrange[1] - yrange[0]
    yrange = yrange + yspan * numpy.array([-0.3, 0.3])

    ax1.set_ylim(yrange[0], yrange[1])

    f.savefig('plots/V_y.{}.png'.format(obstypeid))

    # V plot (x), weighted
    ######################

    M2_key = 'WVX40'

    f = plt.figure(figsize=(8,6), dpi=100)
    ax1 = f.add_subplot(111)
    ax1.set_xlim(-0.1, 4.0)
    ax1.set_xlabel('Redshift')
    ax1.set_ylabel('$\Delta \mathrm{wV}_x$ (arcsec$^2$)')
    ax1.set_title('zenith = 30, filter = {}'.format(filter_name))
    if not os.path.exists('plots/'):
        os.mkdir('plots/')

    star_names = ['O5V', 'B5III', 'A5V', 'F5V', 'G5V', 'K5V', 'M5V']
    stars = ['uko5v', 'ukb5iii', 'uka5v', 'ukf5v', 'ukg5v', 'ukk5v', 'ukm5v']
    star_colors = ['Blue', 'Cyan', 'Green', 'Gold', 'Orange', 'Red', 'Violet']

    yvals = numpy.empty(0)
    #plot stars
    for star, star_name, star_color in zip(stars, star_names, star_colors):
        measured = m_star_diff[star][M2_key] * 3600**2
        ax1.scatter(0.0, measured, marker='*', c=star_color, s=80, label=star_name)
        yvals = numpy.append(yvals, measured)

    gal_names = ['E', 'Sa', 'Sb', 'Sbc', 'Scd', 'Im', 'SB1', 'SB6']
    gals = ['CWW_E_ext', 'KIN_Sa_ext', 'KIN_Sb_ext', 'CWW_Sbc_ext', 'CWW_Scd_ext',
            'CWW_Im_ext', 'KIN_SB1_ext', 'KIN_SB6_ext']
    gal_colors = ['Violet', 'Red', 'Orange', 'Gold', 'Green', 'Cyan', 'Blue']

    #plot gals
    zs = numpy.arange(0.0, 3.0, 0.03)
    for gal, gal_name, gal_color in zip(gals, gal_names, gal_colors):
        measured = numpy.array(m_gal_diff[gal][M2_key]) * 3600**2
        ax1.scatter(zs, measured, marker='.', c=gal_color, s=50, label=gal_name)
        yvals = numpy.append(yvals, measured)
    ax1.legend(prop={"size":9})

    yrange = numpy.array([scipy.stats.scoreatpercentile(yvals, 1),
                           scipy.stats.scoreatpercentile(yvals, 99)])

    yspan = yrange[1] - yrange[0]
    yrange = yrange + yspan * numpy.array([-0.3, 0.3])

    ax1.set_ylim(yrange[0], yrange[1])

    f.savefig('plots/wV_x.{}.png'.format(obstypeid))

    # V plot (y), weighted
    ######################

    M2_key = 'WVY40'

    f = plt.figure(figsize=(8,6), dpi=100)
    ax1 = f.add_subplot(111)
    ax1.set_xlim(-0.1, 4.0)
    ax1.set_xlabel('Redshift')
    ax1.set_ylabel('$\Delta \mathrm{wV}_y$ (arcsec$^2$)')
    ax1.set_title('zenith = 30, filter = {}'.format(filter_name))
    if not os.path.exists('plots/'):
        os.mkdir('plots/')

    star_names = ['O5V', 'B5III', 'A5V', 'F5V', 'G5V', 'K5V', 'M5V']
    stars = ['uko5v', 'ukb5iii', 'uka5v', 'ukf5v', 'ukg5v', 'ukk5v', 'ukm5v']
    star_colors = ['Blue', 'Cyan', 'Green', 'Gold', 'Orange', 'Red', 'Violet']

    yvals = numpy.empty(0)
    #plot stars
    for star, star_name, star_color in zip(stars, star_names, star_colors):
        measured = m_star_diff[star][M2_key] * 3600**2
        ax1.scatter(0.0, measured, marker='*', c=star_color, s=80, label=star_name)
        yvals = numpy.append(yvals, measured)

    gal_names = ['E', 'Sa', 'Sb', 'Sbc', 'Scd', 'Im', 'SB1', 'SB6']
    gals = ['CWW_E_ext', 'KIN_Sa_ext', 'KIN_Sb_ext', 'CWW_Sbc_ext', 'CWW_Scd_ext',
            'CWW_Im_ext', 'KIN_SB1_ext', 'KIN_SB6_ext']
    gal_colors = ['Violet', 'Red', 'Orange', 'Gold', 'Green', 'Cyan', 'Blue']

    #plot gals
    zs = numpy.arange(0.0, 3.0, 0.03)
    for gal, gal_name, gal_color in zip(gals, gal_names, gal_colors):
        measured = numpy.array(m_gal_diff[gal][M2_key]) * 3600**2
        ax1.scatter(zs, measured, marker='.', c=gal_color, s=50, label=gal_name)
        yvals = numpy.append(yvals, measured)
    ax1.legend(prop={"size":9})

    yrange = numpy.array([scipy.stats.scoreatpercentile(yvals, 1),
                           scipy.stats.scoreatpercentile(yvals, 99)])

    yspan = yrange[1] - yrange[0]
    yrange = yrange + yspan * numpy.array([-0.3, 0.3])

    ax1.set_ylim(yrange[0], yrange[1])

    f.savefig('plots/wV_y.{}.png'.format(obstypeid))

if __name__ == '__main__':
    plot_relative_moments('r', 0, 1000)
    plot_relative_moments('i', 0, 1000)

max_stars_repo_name: DarkEnergyScienceCollaboration/chroma

max_issues_repo_name: DarkEnergyScienceCollaboration/chroma

max_forks_repo_name: DarkEnergyScienceCollaboration/chroma


text:
from xml.dom import NOT_SUPPORTED_ERR
import numpy as np
import pandas as pd
from sklearn.preprocessing import KBinsDiscretizer
from sklearn.utils.random import sample_without_replacement
from storage_strategy.storage_strategy import ID, StorageStrategy
from .context_item_selection_strategy import ContextItemSelectionStrategy

# the idea for doi based context strategy is we use the computed doi values to find good candidates
# to include as context. To this end, we divide the data in storage into bins from which we then
# sample the context. There are many suitable distributions we may use, based on dataset and the
# task at hand:
#  * normal distribution: most context items in the 0.4-0.6 range
#  * min/max: oversample high and low doi values, undersample the intermediate values
#  * even cardinality: the same number of items get sampled per bin
#  * uniform: items are sampled randomly acros bins, such that the doi distribution in the
#    context is the same as in storage

class DoiBasedContext(ContextItemSelectionStrategy):
  def __init__(self, n_dims: int, storage: StorageStrategy, n_bins: int,
               strategy: str = "min_max") -> None:
    super().__init__(n_dims, storage)
    self.n_bins = n_bins
    self.strategy = strategy

  def _get_doi_bins_from_storage(self) -> pd.DataFrame:
    id_dois = self.storage.get_available_dois(with_ids=True)

    if len(id_dois) == 0:
      return pd.DataFrame()

    binning = KBinsDiscretizer(n_bins=self.n_bins, strategy="uniform", encode="ordinal")
    bin_per_id = binning.fit_transform(id_dois[:, 1].reshape(-1, 1))

    id_dois_df = pd.DataFrame(id_dois, columns=[ID, "doi"])
    id_dois_df["bin"] = bin_per_id
    return id_dois_df

  def _get_samples_per_bin(self, total_samples: int):
    # from every bin, we draw the same number of ids
    if self.strategy == "uniform":
      samples_per_bin = np.ones((self.n_bins, )) * (total_samples // self.n_bins)
    # otherwise, use a normal distribution to decide on the sample frequency per bin
    elif self.strategy == "normal" or self.strategy == "min_max":
      rng = np.random.default_rng()
      draw = rng.normal(size=total_samples)
      samples_per_bin = np.histogram(draw, bins=self.n_bins)[0]
    else:
      raise Exception("unknown sampling strategy in doi based context")

    if self.strategy == "min_max":
      # the mean of the normal distribution above now sits at the min and max bins
      samples_per_bin = np.roll(samples_per_bin, len(samples_per_bin) // 2)

    return samples_per_bin

  def get_context_ids(self, n: int, current_chunk: int) -> np.ndarray:
    bin_per_id_df = self._get_doi_bins_from_storage()

    if len(bin_per_id_df) == 0:
      return np.empty((0, self.n_dims))

    outdated_ids = np.array([])
    samples_per_bin = self._get_samples_per_bin(total_samples=n)
    # from every bin, get a number of ids from the stored data, according to samples_per_bin
    for i in range(self.n_bins):
      ids_in_bin = bin_per_id_df[bin_per_id_df["bin"] == i]
      n_population = len(ids_in_bin)
      n_samples = min(samples_per_bin[i], n_population)
      picks = sample_without_replacement(n_population=n_population, n_samples=n_samples)
      outdated_ids_bin = ids_in_bin.iloc[picks][ID].to_numpy()
      outdated_ids = np.append(outdated_ids, outdated_ids_bin)

    # some bins may be smaller than the number of samples we want to draw from them, so there can be
    # "open spots" that we can fill randomly
    n_open_spots = n - len(outdated_ids)
    if n_open_spots > 0:
      remaining_ids = bin_per_id_df[~bin_per_id_df[ID].isin(outdated_ids)][ID]
      if len(remaining_ids) > 0:
        n_samples = min(n_open_spots, len(remaining_ids))
        filler_picks = sample_without_replacement(len(remaining_ids), n_samples)
        filler_ids = remaining_ids.iloc[filler_picks]
        outdated_ids = np.append(outdated_ids, filler_ids)

    return outdated_ids

max_stars_repo_name: vis-au/prointerest

max_issues_repo_name: vis-au/prointerest

max_forks_repo_name: vis-au/prointerest


text:
"""Calculation section from density_point.

Functions
---------
    - calc_section_from_density_point
"""
import numpy

from cryspy.A_functions_base.function_1_matrices import \
    tri_linear_interpolation

from cryspy.A_functions_base.function_2_mem import transfer_to_density_3d

from cryspy.C_item_loop_classes.cl_1_atom_site import AtomSiteL
from cryspy.C_item_loop_classes.cl_1_atom_site_susceptibility import \
    AtomSiteSusceptibilityL
from cryspy.C_item_loop_classes.cl_1_cell import Cell
from cryspy.C_item_loop_classes.cl_1_mem_parameters import MEMParameters
from cryspy.C_item_loop_classes.cl_1_space_group_symop import \
    SpaceGroupSymop, SpaceGroupSymopL

from cryspy.C_item_loop_classes.cl_2_section import Section

from cryspy.C_item_loop_classes.cl_3_density_point import DensityPoint, \
    DensityPointL

def read_file_den(file_name: str):
    """Read density file.

    Arguments
    ---------
        -   file_name is a file name

    Output
    ------
        - cell
        - space_group_symop
        - density_point
    """
    with open(file_name, "r") as fid:
        l_content = fid.readlines()

    number_lines = int(l_content[1])

    hh = l_content[number_lines+2].strip().split()
    cell = Cell(length_a=float(hh[0]), length_b=float(hh[1]),
                length_c=float(hh[2]), angle_alpha=float(hh[3]),
                angle_beta=float(hh[4]), angle_gamma=float(hh[5]))

    number_points = [int(hh) for hh in
                     l_content[number_lines+3][:-1].split()[:3]]
    [n_el_symm, centr, n_orig] = [int(hh) for hh in
                                  l_content[number_lines+3][:-1].split()[3:6]]

    l_item_dp = []
    for line in l_content[2:number_lines+2]:
        hh = line.strip().split()
        ind_x, ind_y, ind_z = int(hh[0]), int(hh[1]), int(hh[2])
        den, den_f, den_a = 0., 0., 0.
        if len(hh) == 6:
            den_f = float(hh[4])
            den_a = float(hh[5])
        else:
            den_u = float(hh[3])
            if den_u >= 0.:
                den_f = den_u
            else:
                den_a = den_u
        item = DensityPoint(
            index_x=ind_x, index_y=ind_y, index_z=ind_z,
            fract_x=float(ind_x)/float(number_points[0]),
            fract_y=float(ind_y)/float(number_points[1]),
            fract_z=float(ind_z)/float(number_points[2]),
            density=den, density_ferro=den_f, density_antiferro=den_a)
        l_item_dp.append(item)
    density_point = DensityPointL()
    density_point.items = l_item_dp

    l_el_symm = []
    for line in l_content[(number_lines+4):(number_lines+4+n_el_symm)]:
        hh = line[:-1].strip().split()
        l_el_symm.append([float(hh[9]), int(hh[0]), int(hh[1]), int(hh[2]),
                          float(hh[10]), int(hh[3]), int(hh[4]), int(hh[5]),
                          float(hh[11]), int(hh[6]), int(hh[7]), int(hh[8])])
    l_item_sg = []
    for line in l_content[(number_lines+4+n_el_symm):
                          (number_lines+4+n_el_symm+n_orig)]:
        orig = [float(hh) for hh in line[:-1].split()[:3]]
        for el_symm in l_el_symm:
            el_symm_full = [
                (el_symm[0]+orig[0])%1., el_symm[1], el_symm[2], el_symm[3],
                (el_symm[4]+orig[1])%1., el_symm[5], el_symm[6], el_symm[7],
                (el_symm[8]+orig[2])%1., el_symm[9], el_symm[10], el_symm[11]]
            item = SpaceGroupSymop.define_by_el_symm(el_symm_full)
            l_item_sg.append(item)
    space_group_symop = SpaceGroupSymopL()
    space_group_symop.items = l_item_sg
    return cell, space_group_symop, density_point, number_points

def calc_section_from_density_point(
        section: Section, density_point: DensityPointL,
        mem_parameters: MEMParameters, cell: Cell,
        space_group_symop: SpaceGroupSymopL, atom_site: AtomSiteL,
        atom_site_susceptibility: AtomSiteSusceptibilityL):
    """Calculate section in density point.

    Arguments
    ---------
        - section
        - density_point
        - cell
        - space_group_symop

    Output:
        - matrix_chi, matrix_b
    """
    r_11 = numpy.array(space_group_symop.r_11, dtype=int)
    r_12 = numpy.array(space_group_symop.r_12, dtype=int)
    r_13 = numpy.array(space_group_symop.r_13, dtype=int)
    r_21 = numpy.array(space_group_symop.r_21, dtype=int)
    r_22 = numpy.array(space_group_symop.r_22, dtype=int)
    r_23 = numpy.array(space_group_symop.r_23, dtype=int)
    r_31 = numpy.array(space_group_symop.r_31, dtype=int)
    r_32 = numpy.array(space_group_symop.r_32, dtype=int)
    r_33 = numpy.array(space_group_symop.r_33, dtype=int)

    b_1 = numpy.array(space_group_symop.b_1, dtype=float)
    b_2 = numpy.array(space_group_symop.b_2, dtype=float)
    b_3 = numpy.array(space_group_symop.b_3, dtype=float)

    r_ij = (r_11, r_12, r_13, r_21, r_22, r_23, r_31, r_32, r_33)
    b_i = (b_1, b_2, b_3)

    np_ind_x = density_point.numpy_index_x
    np_ind_y = density_point.numpy_index_y
    np_ind_z = density_point.numpy_index_z
    np_indexes = (np_ind_x, np_ind_y, np_ind_z)

    den_chi = density_point.calc_density_chi(cell, atom_site_susceptibility)
    den_f = numpy.array(density_point.density_ferro, dtype=float)
    den_a = numpy.array(density_point.density_antiferro, dtype=float)

    points_a = mem_parameters.points_a
    points_b = mem_parameters.points_b
    points_c = mem_parameters.points_c
    chi_f = mem_parameters.chi_ferro
    chi_a = mem_parameters.chi_antiferro

    n_xyz = (points_a, points_b, points_c)
    den_chi_3d = transfer_to_density_3d(np_indexes, den_chi, n_xyz, r_ij, b_i)

    den_b = chi_f*den_f + chi_a*den_a
    den_b_3d = transfer_to_density_3d(np_indexes, den_b, n_xyz, r_ij, b_i)

    fract_x, fract_y, fract_z = section.calc_fractions(cell, atom_site)
    fract_x_uc, fract_y_uc = fract_x % 1., fract_y % 1.
    fract_z_uc = fract_z % 1.

    ind_xyz = numpy.array([fract_x_uc*points_a, fract_y_uc*points_b,
                           fract_z_uc*points_c], dtype=float).transpose()

    den_chi_section = tri_linear_interpolation(ind_xyz, den_chi_3d)
    den_b_section = tri_linear_interpolation(ind_xyz, den_b_3d)

    points_x = section.points_x
    points_y = section.points_y
    den_chi_section = den_chi_section.reshape((points_x, points_y))
    den_b_section = den_b_section.reshape((points_x, points_y))
    return den_chi_section, den_b_section

max_stars_repo_name: ikibalin/rhochi

max_issues_repo_name: ikibalin/rhochi

max_forks_repo_name: ikibalin/rhochi


text:
import pickle
import tkinter as tk
import numpy as np

def sigmoid(z):
    return 1.0/(1.0+np.exp(-z))

#ucitavanje podataka
pickle_off = open('weights', 'rb')
weights = pickle.load(pickle_off)

pickle_off = open('biases', 'rb')
biases = pickle.load(pickle_off)

#tkinter
window = tk.Tk()

okvir = tk.Canvas(window)
okvir.config(bg='purple', height=280, width=280)
okvir.pack()

for i in range(28):
    ii = i + 1
    for j in range(28):
        jj = j + 1
        okvir.create_rectangle(i * 10, j * 10, ii * 10, jj * 10)

circles = {}
k = 0

lista = []
radius = 10

def move(event):
    global k, circles
    print(event.x, event.y)
    k += 1
    circles[k] = okvir.create_oval(event.x - 5, event.y - 5, event.x + 5, event.y + 5, fill='black')

    lista.append([event.x, event.y])

okvir.bind('<B1-Motion>', move)

def clean(event):
    global circles, k
    while k != 0:
        okvir.delete(circles[k])
        del circles[k]
        k -= 1
    #brisanje elemenata liste
    global lista
    lista = []

cleanButton = tk.Button(window)
cleanButton.config(text='Clean')
cleanButton.pack()

cleanButton.bind('<Button-1>', clean)

def recognise(event):

    matrica = []
    #bojanje malih pikselića
    for i in lista:
        for j in range(-5, 6):
            for z in range(-5, 6):
                if i[0] + j >= 0 and i[1] + z >= 0 and (i[0] + j, i[1] + z) not in matrica and i[0] + j < 280 and i[1] + z < 280:
                    matrica.append((i[0] + j, i[1] + z))

    data = np.zeros((784, 1))
    #popunjavanje zavrsne matrice
    for i, j in matrica:
        i2 = i//10
        j2 = j//10
        position = j2 * 28 + i2
        data[position][0] += 1

    a = data/100
    #računanje broja
    for i in range(2):
        a = sigmoid(np.dot(weights[i],a)+biases[i])
    print(a)
    print(np.argmax(a))

recogniseButton = tk.Button(window)
recogniseButton.config(text='Recognise')
recogniseButton.pack(side=tk.RIGHT)

recogniseButton.bind('<Button-1>', recognise)

window.mainloop()

max_stars_repo_path: Radna verzija.py

max_stars_repo_name: MatejVe/Digit-Recognition-in-Python

max_issues_repo_path: Radna verzija.py

max_issues_repo_name: MatejVe/Digit-Recognition-in-Python

max_forks_repo_path: Radna verzija.py

max_forks_repo_name: MatejVe/Digit-Recognition-in-Python


text:
#!/usr/bin/python3

import numpy as np
import pandas as pd
from predictor.utility import msg2log

def aux_deleteRowWithZero(ar:np.array):
    (n,m)=ar.shape
    l_del=[]
    for i in range(n-1,-1,-1):
        if ar[i,m-1]==0:
            l_del.append(i)
        else:
            break

    ar=np.delete(ar,l_del,0)

    return ar

def getRowLongestSequence(ar:np.ndarray)->tuple:
    (n,m)=ar.shape
    max_long=ar[0,1]
    max_long_index=ar[0,0]
    for i in range(n):
        if ar[i,1]>max_long:
            max_long=ar[i,1]
            max_long_index=ar[i,0]
    return (max_long_index, max_long)

def createStatesSequencу(ds:pd.DataFrame, item:str,dt_col_name:str,minValue:float, maxValue:float,enum:tuple,abrupt_type:str='all',
                     abrupt_level:float =0.9, f:object=None)->(list,dict,dict):

    (LOWER_ABRUPT, NO_ABRUPT, UPPER_ABRUPT) = enum
    upper_dict = {}
    lower_dict = {}
    amplitude = maxValue - minValue
    delta = amplitude *(1.0 -abrupt_level) if abrupt_type=='all' else amplitude *(1.0 -abrupt_level)/2.0
    highLevel = maxValue - delta
    lowLevel  = minValue + delta

    msg2log("DelAfterDebug",
            "abrupt level={} max={} min={} amplitude={}  delta={} highLevel={} low={}".format(abrupt_level, maxValue,
             minValue, amplitude, delta, highLevel, lowLevel),f)

    states = []
    for i in range(len(ds[item])):
        stateValue = NO_ABRUPT
        if ds[item].values[i]>=highLevel and abrupt_type!="lower":
            upper_dict[ds[dt_col_name].values[i]]=ds[item].values[i]
            stateValue= UPPER_ABRUPT
        if ds[item].values[i] <= lowLevel and abrupt_type!="upper":
            lower_dict[ds[dt_col_name].values[i]] = ds[item].values[i]
            stateValue = LOWER_ABRUPT

        states.append(stateValue)
    return states, upper_dict,lower_dict

def createAbruptMatrixes(states:list, enum:tuple, abrupt_type:str='all',f:object=None)->(np.ndarray,np.ndarray,np.ndarray):

    (LOWER_ABRUPT, NO_ABRUPT, UPPER_ABRUPT) = enum
    sorted_array, count_array = np.unique(states, return_counts=True)
    msg2log(None, "\nSorted states array: {} Count array: {}\n".format(sorted_array, count_array), f)

    stPrev = states[0]
    indStart = 0
    IND_LOWER_ABRUPT = LOWER_ABRUPT
    IND_NO_ABRUPT    = NO_ABRUPT
    IND_UPPER_ABRUPT = UPPER_ABRUPT

    if abrupt_type=='lower':
        lower_abrupts = np.zeros((count_array[LOWER_ABRUPT], 2), dtype=int)
        no_abrupts    = np.zeros((count_array[NO_ABRUPT],    2), dtype=int)
        upper_abrupts = np.zeros((0,                         0), dtype=int)

    elif abrupt_type=='upper':
        lower_abrupts = np.zeros((0,              0),            dtype=int)
        no_abrupts    = np.zeros((count_array[0], 2),            dtype=int)
        upper_abrupts = np.zeros((count_array[1], 2),            dtype=int)
        # IND_NO_ABRUPT    = IND_NO_ABRUPT    - 1
        # IND_UPPER_ABRUPT = IND_UPPER_ABRUPT - 1
    else:
        lower_abrupts = np.zeros((count_array[LOWER_ABRUPT],  2), dtype=int)
        no_abrupts    = np.zeros((count_array[NO_ABRUPT],     2), dtype=int)
        upper_abrupts = np.zeros((count_array[UPPER_ABRUPT],  2), dtype=int)

    k = np.zeros((UPPER_ABRUPT + 1), dtype=int)

    for i in range(len(states)):
        if states[i] == LOWER_ABRUPT and stPrev == LOWER_ABRUPT and abrupt_type!='upper':
            lower_abrupts[k[IND_LOWER_ABRUPT], 0] = indStart
            lower_abrupts[k[IND_LOWER_ABRUPT], 1] += 1
            stPrev = LOWER_ABRUPT
        elif states[i] == NO_ABRUPT and stPrev == NO_ABRUPT:
            no_abrupts[k[IND_NO_ABRUPT], 0] = indStart
            no_abrupts[k[IND_NO_ABRUPT], 1] += 1
            stPrev = NO_ABRUPT
        elif states[i] == UPPER_ABRUPT and stPrev == UPPER_ABRUPT and abrupt_type!='lower':
            upper_abrupts[k[IND_UPPER_ABRUPT], 0] = indStart
            upper_abrupts[k[IND_UPPER_ABRUPT], 1] += 1
            stPrev = UPPER_ABRUPT
        else:
            k[stPrev] += 1
            indStart = i
            stPrev = states[i]
            if states[i] == LOWER_ABRUPT and abrupt_type!='upper':
                # k[LOWER_ABRUPT] += 1
                lower_abrupts[k[IND_LOWER_ABRUPT], 0] = indStart
                lower_abrupts[k[IND_LOWER_ABRUPT], 1] += 1
                stPrev = LOWER_ABRUPT
            elif states[i] == NO_ABRUPT:
                # k[NO_ABRUPT] += 1
                no_abrupts[k[IND_NO_ABRUPT], 0] = indStart
                no_abrupts[k[IND_NO_ABRUPT], 1] += 1
                stPrev = NO_ABRUPT
            elif states[i] == UPPER_ABRUPT and abrupt_type!='lower':
                # k[UPPER_ABRUPT] += 1
                upper_abrupts[k[IND_UPPER_ABRUPT], 0] = indStart
                upper_abrupts[k[IND_UPPER_ABRUPT], 1] += 1
                stPrev = UPPER_ABRUPT
            else:
                pass

    if abrupt_type!='upper': lower_abrupts =aux_deleteRowWithZero(lower_abrupts)
    no_abrupts = aux_deleteRowWithZero(no_abrupts)
    if abrupt_type!='lower': upper_abrupts = aux_deleteRowWithZero(upper_abrupts)

    return upper_abrupts, no_abrupts, lower_abrupts

def getAbruptChanges(ds:pd.DataFrame, item:str,dt_col_name:str,minValue:float, maxValue:float, enum:tuple,
                     abrupt_type:str='all',abrupt_level:float =0.9, f:object=None)->(list, dict,dict, tuple,tuple,tuple):

    (LOWER_ABRUPT, NO_ABRUPT,UPPER_ABRUPT)=enum
    states,upper_dict,lower_dict =  createStatesSequencу(ds, item, dt_col_name, minValue, maxValue, enum,abrupt_type,
                                                         abrupt_level, f)

    upper_abrupts, no_abrupts, lower_abrupts = createAbruptMatrixes(states, enum, abrupt_type, f)

    res_abrupt_dict={}
    if abrupt_type!='upper': res_abrupt_dict["LOWER_ABRUPT"]  = lower_dict
    if abrupt_type!='lower': res_abrupt_dict["UPPER_ABRUPT"] = upper_dict
    period_abrupt_dict={}
    if abrupt_type!='upper':period_abrupt_dict["LOWER_ABRUPT"] = lower_abrupts
    period_abrupt_dict["NO_ABRUPT"]    = no_abrupts
    if abrupt_type!='lower': period_abrupt_dict["UPPER_ABRUPT"] = upper_abrupts

    tuple_lower=getRowLongestSequence(period_abrupt_dict["LOWER_ABRUPT"]) if abrupt_type!='upper' else ('--','--')
    tuple_no_abrupt = getRowLongestSequence(period_abrupt_dict["NO_ABRUPT"])
    tuple_upper = getRowLongestSequence(period_abrupt_dict["UPPER_ABRUPT"]) if abrupt_type!='lower' else ('--','--')

    return states, res_abrupt_dict, period_abrupt_dict, tuple_lower, tuple_no_abrupt, tuple_upper

if __name__ == "__main__":
    pass