text_plain_output_1.png
from colorama import Fore, Style, Back

from dataclasses import dataclass

from functools import reduce

from typing import Any

import math

import pandas as pd

import re

import numpy as np
import pandas as pd
import re
import math
from functools import reduce
from dataclasses import dataclass
data = pd.read_csv('../input/dogsofcambridge2/Dogs_of_Cambridge_2021.csv')
clean_data = pd.read_csv('../input/dogsofcambridge2/clean_dogs_reference.csv')
clean_data.drop('Unnamed: 0', axis=1, inplace=True)

def fix_lat_long(data):
    lat, long = zip(*[map(lambda a: float(a), re.findall('([-\\d\\.]+)', loc)) for loc in data.Location_masked])
    data.Latitude_masked = lat
    data.Longitude_masked = long
    data.drop('Location_masked', axis=1, inplace=True)

def impute_name(data):
    data.Dog_Name = ['Poopsy' if pd.isna(name) else name for name in data.Dog_Name]
impute_name(data)

@dataclass
class Location:
    name: str
    lat: float
    long: float
    def get_distance(self, lat, long):
        return math.sqrt((self.lat - lat) ** 2 + (self.long - long) ** 2)
nhs = [Location('East Cambridge', 42.369204, -71.079015), Location('Area 2/MIT', 42.359145, -71.094415), Location('Wellington-Harrington', 42.371264, -71.092608), Location('The Port', 42.365604, -71.09691), Location('Cambridgeport', 42.3586, -71.109293), Location('Mid-Cambridge', 42.372655, -71.108721), Location('Riverside', 42.36757, -71.1136), Location('Agassiz', 42.380667, -71.116386), Location('Neighborhood Nine', 42.386545, -71.127079), Location('West Cambridge', 42.376936, -71.136375), Location('North Cambridge', 42.394835, -71.132134), Location('Cambridge Highlands', 42.390774, -71.149859), Location('Strawberry Hill', 42.37938, -71.152475)]
def closest_neighborhood(lat, long):
    close = nhs[0]
    for nh in nhs:
        if nh.get_distance(lat, long) < close.get_distance(lat, long):
            close = nh
    return close.name
def impute_neighborhood(data):
    data.Neighborhood = [closest_neighborhood(data.loc[idx, 'Latitude_masked'], data.loc[idx, 'Longitude_masked']) if pd.isna(item) else item for idx, item in data.Neighborhood.items()]

def impute_breed(data):
    bn, i = ({}, {})
    for nh in data.Neighborhood.unique():
        i[nh] = []
        vc = data[data.Neighborhood == nh].Dog_Breed.value_counts()
        res = [[] for x in range(vc[0] + 1)]
        for idx, item in vc.items():
            res[item].append(idx)
        [a.sort() for a in res]
        res = reduce(lambda a, b: b + (a if a else []), res)
        bn[nh] = res
    data.Dog_Breed = [bn[data.loc[idx, 'Neighborhood']][len(i[data.loc[idx, 'Neighborhood']]) + (0 if i[data.loc[idx, 'Neighborhood']].append('') else 0)] if pd.isna(item) else item for idx, item in data.Dog_Breed.items()]

@dataclass
class Pair:
    one: Any
    two: Any
def ascii_histogram(data, width=50):
    res = []
    for k, v in data.items():
        res.append(Pair(k, v))
    res.sort(key=lambda a: a.two, reverse=True)

colors = [None, 'BLACK', 'RED', 'GREEN', 'YELLOW', 'BLUE', 'MAGENTA', 'CYAN']
def ascii_scatterplot(data, height=50):
    global colors
    if len(data[0]) == 2:
        data = list(map(lambda a: [a[0], a[1], 1], data))
    xs, ys, code = map(list, zip(*data))
    xmax, xmin, ymax, ymin = (max(xs), min(xs), max(ys), min(ys))
    height = height * 2
    scale = (height - 1) / (ymax - ymin)
    width = round((xmax - xmin) * scale + 0.5)
    scale_point = lambda x, y: ((x - xmin) * scale, (y - ymin) * scale)
    chars = [[0 for i in range(width)] for i in range(height)]
    for x, y, c in data:
        x, y = scale_point(x, y)
        chars[round(y - 0.5)][round(x - 0.5)] = c
    for i in reversed(range(0, height, 2)):
        res = ''
        bottom, top = chars[i:i + 2]
        for i in range(width):
            if top[i] and bottom[i]:
                if top[i] == bottom[i]:
                    res += f'{getattr(Fore, colors[top[i]])}█{Style.RESET_ALL}'
                else:
                    res += f'{getattr(Fore, colors[bottom[i]])}{getattr(Back, colors[top[i]])}▄{Style.RESET_ALL}'
            elif top[i]:
                res += f'{getattr(Fore, colors[top[i]])}▀{Style.RESET_ALL}'
            elif bottom[i]:
                res += f'{getattr(Fore, colors[bottom[i]])}▄{Style.RESET_ALL}'
            else:
                res += ' '
        print(res)
coords = list(zip(*[data.Longitude_masked, data.Latitude_masked]))
ascii_scatterplot(coords, 35)