text:
import numpy as np

class window():
    # Dealing with the viewing window defined by user
    # Dependencies: numpy

    # METHODS:
    ## pane: finds the corners of a window around a point
    ## box_corner: finds the corner of a box for a point
    ## square_corner: finds the corner of the square "
    ## square_number: finds the square number "
    ## in_square_coords: finds the northing and easting wihtin a square
    ## spanner: finds the corners for all squares in the pane
    ## filename: finds the file in which a point resides
    ## pane_files: gets filenames for all squares in the pane

    def __init__(self, point, side_len=500):
        # INPUT:
        ## point (easting, northing) both in feet
        ## side length of window (optional), default: 500

        self.point = point

        # define the desired side length
        self.side_len = side_len

        # basis defined for finding box and square corners
        # necessary in case plots are not defined from true 0
        self.easting_basis = 2000000
        self.northing_basis = 640000
        pass

    def pane(self):
        # find the viewing window pane defined by user
        # INPUT:
        ## self.point (easting, northing)
        ### easting of center self.point (feet)
        ### northing of center self.point (feet)
        ## side length of the window (feet)
        # OUTPUT:
        ## numpy array (4x2)
        ## 4 coordinates of the corners of the pane
        ## (easting, northing) x4 left to right top to bottom

        easting, northing = self.point  # split coords of self.point

        # each corner is 1/2 a side length in the easting
        # and 1/2 a side lenth in the northing from eachother
        north_west = [easting - self.side_len / 2, northing + self.side_len / 2]
        north_east = [easting + self.side_len / 2, northing + self.side_len / 2]
        south_west = [easting - self.side_len / 2, northing - self.side_len / 2]
        south_east = [easting + self.side_len / 2, northing - self.side_len / 2]

        return np.array([north_west, north_east, south_west,
                         south_east]).astype('int')

    def box_corner(self):
        # find the south-west corner of the box
        # in which the self.point resides
        # INPUT:
        ## self.point (easting, northing) both in feet
        # OUTPUT:
        ## numpy array (2x1)
        ## box corner coordinates (easting, northing) in feet

        easting, northing = self.point  # split coords out of self.point

        # round down to the nearest 10,000 feet
        # from basis of 2,000,000
        down_to = 10000
        corner_easting = np.floor((easting - self.easting_basis) /
                                  down_to) * down_to + self.easting_basis

        # round down to the nearest 1,000 feet
        # from basis of 66,000
        down_to = 10000
        corner_northing = np.floor((northing - self.northing_basis) /
                                   down_to) * down_to + self.northing_basis

        return np.array([corner_easting, corner_northing]).astype('int')

    def square_corner(self):
        # find the south-west corner of the square in the box
        # in which the self.point resides
        # INPUT:
        ## self.point (easting, northing) both in feet
        # OUTPUT:
        ## numpy array (2x1)
        ## square corner coordinates (easting, northing) in feet

        easting, northing = self.point  # split coords of self.point

        # round down to the nearest 2,500 feet
        down_to = 2500
        corner_easting = np.floor((easting - self.easting_basis) /
                                  down_to) * down_to + self.easting_basis
        corner_northing = np.floor((northing - self.northing_basis) /
                                   down_to) * down_to + self.northing_basis

        return np.array([corner_easting, corner_northing]).astype('int')

    def square_number(self):
        # find the square number in which the self.point resides
        # INPUT:
        ## self.point (easting, northing)
        # OUTPUT:
        ## square number based on weird convention

        corner_square = self.square_corner()  # find corner of the square
        corner_box = self.box_corner()  # find corner of the box

        # set up array for weird naming convention
        num_conv = np.array([['17', '18', '19', '20'], ['13', '14', '15', '16'],
                             ['09', '10', '11', '12'], ['05', '06', '07',
                                                        '08']])

        # find how many squares over the square corner is from box corner
        squares_loc = (corner_square - corner_box) / 2500

        easting_squares, northing_squares = squares_loc.astype('int')

        # backwards because indexes rows then cols
        square_number = num_conv[northing_squares, easting_squares]

        return square_number

    def in_square_coords(self):
        # finds the easting and northing from the square corner
        # INPUT:
        ## self.point (easting, norhting)
        # OUTPUT:
        ## (easting, norhting) from square corner

        corner_square = self.square_corner()  # get square corner

        return np.array([self.point - corner_square]).astype('int')

    def spanner(self):
        # finds all squares in the pane
        # INPUT:
        ## pane()
        # OUTPUT:
        ## numpy array of all square/box corners in the window

        corners = self.pane()  # find the corners of the pane

        # find square corners for pane corner points
        pane_square_corns = np.array(
            [window(corner).square_corner() for corner in corners])

        # finding the bounds of the pane
        nw_northing = pane_square_corns[0][1]
        sw_northing = pane_square_corns[2][1]
        sw_easting = pane_square_corns[2][0]
        se_easting = pane_square_corns[3][0]

        # find the range of square corners (+1 so its inclusive)
        step = 2500
        easting_square_range = np.arange(sw_easting, se_easting + 1, step)
        northing_square_range = np.arange(sw_northing, nw_northing + 1, step)

        # create coordinates from the ranges of square corners
        mesh = np.meshgrid(easting_square_range, northing_square_range)
        coords = np.array([mesh[0], mesh[1]])
        coord_len = coords.shape[1] * coords.shape[2]
        square_coords = coords.reshape(2, coord_len).T

        return square_coords

    def filename(self):
        # INPUT:
        ## self.point (easting, norhting)
        # OUTPUT:
        ## string filename where the point resides

        box_easting, box_northing = self.box_corner()  # find corner of the box

        w, y = str(box_easting)[1:3]
        x, z = str(box_northing)[0:2]

        #string together name
        file = 'c2005_' + w + x + y + z + '_0' + self.square_number() + '.png'

        #return tuple
        #file = (box_easting, box_northing, self.square_number())
        return file

    def pane_files(self):
        # find file names for points identified in pane()
        # INPUT:
        ## spanner()
        # OUTPUT:
        ## numpy array of file names of all the squares in the pane

        square_coords = self.spanner()  # find all square corners in pane

        # find all file names for squares in pane
        files = np.array(
            [[window(coord).filename() for coord in square_coords]]).T

        return files

    pass

# testing
easting = 2050000 + 2500  # easting coordinate (feet)
northing = 670000 + 7500  # northing coordinate (feet)
side_len = 1000  # side length of pane (feet)
point = np.array([easting, northing])
wind = window(point, side_len)
files = wind.pane_files()
files

max_stars_repo_name: tylerashoff/mapping

max_issues_repo_name: tylerashoff/mapping

max_forks_repo_name: tylerashoff/mapping


text:
import pytest
import math
import numpy as np
from scipy.stats import norm
from opendp.whitenoise.mechanisms.laplace import Laplace

#
#   Unit tests
#
class TestLaplace:
    def test_simple_lap(self):
        g = Laplace(0.1) # epsilon of 0.1
        x = range(10000)
        y = g.release(x).values
        assert(round(np.sum(x) / 10E+6) == round(np.sum(y) / 10E+6))

    def test_bounds1_lap(self):
        # check that analytic and bootstrap bounds work
        g = Laplace(0.5) # epsilon of 0.5
        lower, upper = g.bounds(False)[0] # analytic bounds
        lower2, upper2 = g.bounds(True)[0]  # bootstrap bounds
        assert(lower < upper)
        assert(lower2 < upper2)

    def test_bounds1b_lap(self):
        # check that analytic and bootstrap bounds work
        g = Laplace(0.05) # epsilon of 0.05, very wide bounds
        lower, upper = g.bounds(False)[0] # analytic bounds
        lower2, upper2 = g.bounds(True)[0]  # bootstrap bounds
        assert(lower < upper)
        assert(lower2 < upper2)

    def test_bounds1c_lap(self):
        # check that analytic and bootstrap bounds work
        # use very small bounds to make sure order doesn't swap
        g = Laplace(1.0, interval_widths=[0.1]) # epsilon of 1.0
        lower, upper = g.bounds(False)[0] # analytic bounds
        lower2, upper2 = g.bounds(True)[0]  # bootstrap bounds
        assert(lower <= upper)
        assert(lower2 <= upper2)
    def test_bounds2_lap(self):
        # check that outer bounds enclose inner bounds
        g = Laplace(4.0, interval_widths=[0.95, 0.97]) # epsilon of 4.0, tighter bounds
        lower1, upper1 = g.bounds(False)[0]
        lower1b, upper1b = g.bounds(True)[0]
        lower2, upper2 = g.bounds(False)[1]
        lower2b, upper2b = g.bounds(True)[1]
        assert(lower2 < lower1)
        assert(upper2 > upper1)
        assert(lower2b < lower1b)
        assert(upper2b > upper1b)
    def test_intervals_lap(self):
        l = Laplace(4.0, interval_widths=[0.95, 0.5])
        vals = [100, 3333, 99999]
        r_n = l.release(vals, False)
        r = l.release(vals, True)
        assert(r_n.accuracy is None)
        assert(r.accuracy is not None)
        r0, r1 = r.intervals
        assert(r0.contains(r1))
        assert(not r0.inside(r1))

ext: py

reason: import numpy,from scipy


text:
print('Setting up dependencies for data processing...', end='\r')
import csv
import numpy as np

from sklearn.preprocessing import MinMaxScaler
from sklearn.utils import shuffle

raw = []
train_samples = []
train_labels = []
# test_samples = []
# test_labels = []
scaled_train_samples = []

print('Collecting data...                            ', end='\r')
with open('Data/MNIST.csv') as file:
    csv_data = csv.reader(file, delimiter=',')

    for row in csv_data:
        raw.append(row)

print('Parsing data...   ', end='\r')
raw.pop(0)  # Remove headers from the dataset

for image in raw:                     # Iterate through each image
    train_labels.append(image[0])     # Add label to label list
    image.pop(0)                      # Remove from list of the image

for image in raw:
    train_samples.append(image)       # Add each image's data to the samples

for image in range(len(train_samples)):
    for pixel in range(len(train_samples[image])):
        train_samples[image][pixel] = int(train_samples[image][pixel]) # Turn all samples into integers

# Turn all data and labels into integers
for label in range(len(train_labels)):

    if type(train_labels[label]) == list:

        for i in range(len(train_labels[label])):
            train_labels[label][i] = int(train_labels[label][i])

    elif type(train_labels[label]) == int or type(train_labels[label]) == str:
        train_labels[label] = int(train_labels[label])

    else: raise TypeError(f'Label data type {str(type(train_labels[label]))[8 : len(str(type(train_labels[label])))-2]} is not allowed.')

print('Shaping data...', end='\r')

del train_labels[len(train_labels) - 335:]    # Get right size
del train_samples[len(train_samples) - 335:]  # Get right size

assert len(train_samples) == 19600
assert len(train_labels) == 19600

# assert len(test_samples) == 19600
# assert len(test_labels) == 19600

train_samples = np.array(train_samples)
train_labels = np.array(train_labels)
train_labels, train_samples = shuffle(train_labels, train_samples)

scaler = MinMaxScaler(feature_range=(0, 1))
scaled_train_samples = scaler.fit_transform(train_samples.reshape(-1, 784))

print('Data preprocessed!')
if __name__ == '__main__': print(train_samples)

max_stars_repo_name: Password-Classified/MNIST-Learner

max_issues_repo_name: Password-Classified/MNIST-Learner

max_forks_repo_name: Password-Classified/MNIST-Learner

reason: import numpy