Ejemplo n.º 1
0
    def __init__(self, id, string, layout, input_set, input_id, outputs):
        Datum.__init__(self)
        self.id = id
        self.string = string
        self.layout = layout
        self.input_set = input_set
        self.input_id = input_id
        self.outputs = outputs

        self.input_path = IMAGE_FILE % (self.input_set, self.input_set, self.input_id)
        self.image_path = RAW_IMAGE_FILE % (self.input_set, self.input_set, self.input_id)

        if not os.path.exists(self.input_path):
            raise IOError("No such processed image: " + self.input_path)
        if not os.path.exists(self.input_path):
            raise IOError("No such source image: " + self.image_paht)
Ejemplo n.º 2
0
    def __init__(self, id, string, layout, input_set, input_id, outputs):
        Datum.__init__(self)
        self.id = id
        self.string = string
        self.layout = layout
        self.input_set = input_set
        self.input_id = input_id
        self.outputs = outputs

        self.input_path = IMAGE_FILE % (self.input_set, self.input_set,
                                        self.input_id)
        self.image_path = RAW_IMAGE_FILE % (self.input_set, self.input_set,
                                            self.input_id)

        if not os.path.exists(self.input_path):
            raise IOError("No such processed image: " + self.input_path)
        if not os.path.exists(self.input_path):
            raise IOError("No such source image: " + self.image_paht)
Ejemplo n.º 3
0
def read_data(iterable):
    # #1 @ 596,731: 11x27
    data_re = re.compile(r'^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)')

    data = []
    for l in iterable:
        m = re.match(data_re, l.strip())
        assert m, 'Error with the regular expression'
        data.append(Datum(*map(int, m.group(1, 2, 3, 4, 5))))

    return data
Ejemplo n.º 4
0
 def data_retrival(self, x, y, dtype):
     if self.data_construct:
         datum = self.data_construct.pop()
         datum.x = float(x)
         datum.y = float(y)
         datum.dtype = dtype
         datum.image = datum.images['hibit']
         datum.current_image = 'hibit'
         datum.rect.x = int(datum.x) - (datum.image.get_width() // 2)
         datum.rect.y = int(datum.y) - (datum.image.get_height() // 2)
         datum.state_changed = False
         datum.direction = 'd'
         datum.node_previous = None
         return datum
     else:
         return Datum(self, x, y, dtype)
Ejemplo n.º 5
0
def loadDataFile(filename, n, width, height):
    """
    Reads n data images from a file and returns a list of Datum objects.
    (Return less then n items if the end of file is encountered).
    """
    DATUM_WIDTH = width
    DATUM_HEIGHT = height
    fin = readlines(filename)
    fin.reverse()
    items = []
    for i in range(n):
        data = []
        for j in range(height):
            data.append(list(fin.pop()))
        if len(data[0]) < DATUM_WIDTH - 1:
            # we encountered end of file...
            print("Truncating at %d examples (maximum)" % i)
            break
        items.append(Datum(data, DATUM_WIDTH, DATUM_HEIGHT))
    return items
Ejemplo n.º 6
0
import pickle
import numpy as np
import pandas as pd
from datum import Datum
import networkx as nx
import graph
from gensim.models import Word2Vec

data_dir = '../data/'
output_name = 'graph'

data = Datum()

data.data_prepare()
weight_matrix = np.copy(data.weight_matrix)
weight_matrix_total = np.sum(weight_matrix, axis=0)

arr_tmp = []
for stock_index in range(len(data.list_stocks)):
    a_edge_index = np.where(weight_matrix_total[stock_index] != 0)[0]
    for edge_index in a_edge_index:
        arr_tmp.append([
            stock_index,
            len(data.list_stocks) + edge_index,
            weight_matrix_total[stock_index, edge_index]
        ])
arr_tmp = np.array(arr_tmp)
pd_tmp = pd.DataFrame(arr_tmp)
pd_tmp[0] = pd_tmp[0].astype(int)
pd_tmp[1] = pd_tmp[1].astype(int)
path = data_dir + 'graph/{}.csv'.format(output_name)
Ejemplo n.º 7
0
class EnglishPluralizationSimple(LearningSimulation):

    # Lexical
    DOG_NODE = SyntacticNode(SyntacticNode.TYPE_ROOT, 'DOG', 1000, 'dog')
    TABLE_NODE = SyntacticNode(SyntacticNode.TYPE_ROOT, 'TABLE', 1001, 'teibl')
    TREE_NODE = SyntacticNode(SyntacticNode.TYPE_ROOT, 'TREE', 1010, 'tri')
    CAR_NODE = SyntacticNode(SyntacticNode.TYPE_ROOT, 'CAR', 1011, 'car')
    DEER_NODE = SyntacticNode(SyntacticNode.TYPE_ROOT, 'DEER', 1100, 'dir')
    FISH_NODE = SyntacticNode(SyntacticNode.TYPE_ROOT, 'FISH', 1101, 'fish')

    # Functional
    PLURAL_NODE = SyntacticNode(SyntacticNode.TYPE_FEATURE, '+PL', 1100)

    NODES = [
        DOG_NODE, TABLE_NODE, TREE_NODE, CAR_NODE, DEER_NODE, FISH_NODE,
        PLURAL_NODE
    ]
    VOCABULARY = ['z']
    RULES = [
        Rule([PLURAL_NODE], 'z', []),
        Rule([PLURAL_NODE], '', [DEER_NODE, FISH_NODE])
    ]

    TARGET_GRAMMAR = Grammar(NODES, VOCABULARY, RULES)

    DATA = [
        Datum(DOG_NODE, [], 'dog'),
        Datum(DOG_NODE, [PLURAL_NODE], 'dogz'),
        Datum(TABLE_NODE, [], 'teibl'),
        Datum(TABLE_NODE, [PLURAL_NODE], 'teiblz'),
        Datum(TREE_NODE, [], 'tri'),
        Datum(TREE_NODE, [PLURAL_NODE], 'triz'),
        Datum(CAR_NODE, [], 'car'),
        Datum(CAR_NODE, [PLURAL_NODE], 'carz'),
        Datum(DEER_NODE, [], 'dir'),
        Datum(DEER_NODE, [PLURAL_NODE], 'dir'),
        Datum(FISH_NODE, [], 'fish'),
        Datum(FISH_NODE, [PLURAL_NODE], 'fish'),
    ]

    def __init__(self):
        super().__init__(self.NODES, self.VOCABULARY, self.RULES, self.DATA)
Ejemplo n.º 8
0
 def __init__(self):
     with open(CONFIG_FILE) as f:
         config = json.loads(f.read())
     self.open_weather = OpenWeather(config["open_weather"])
     self.tagesschau = ts.TagesschauService(3, 1, 1)
     self.datum = Datum(config["birthdays"], config["events"])
Ejemplo n.º 9
0
class Drawer:
    def __init__(self):
        with open(CONFIG_FILE) as f:
            config = json.loads(f.read())
        self.open_weather = OpenWeather(config["open_weather"])
        self.tagesschau = ts.TagesschauService(3, 1, 1)
        self.datum = Datum(config["birthdays"], config["events"])

    def render(self):
        self.image = Image.new("P", IMAGE_SIZE, 0)
        self.image.putpalette(PALETTE)
        self.draw = ImageDraw.ImageDraw(self.image)

        _, y_date = self.date()
        y_weather = self.weather()
        y = max(y_date, y_weather) + MARGIN_Y
        y = self.birthday_wishes(y)
        self.news(y)

        return self.image

    def birthday_wishes(self, y):
        names = self.datum.birthdays()
        if len(names) == 0:
            return y
        wishes = self.datum.birthday_wishes(names)
        _, y_range = self.draw_texts(
            [wishes],
            [40],
            colors=[BLACK],
            y_range=(y + 10, IMAGE_SIZE[1] - MARGIN_Y),
            x_aligns=[1],
        )
        return y_range[1]

    def date(self):
        wochentag, today = self.datum.get_wochentag_datum()
        texts = [wochentag, today]
        fonts = [40, 25]
        x_range, y_range = self.draw_texts(texts, fonts, colors=[BLACK, BLACK])
        return x_range[1], y_range[1]

    def weather(self):
        y_weather = MARGIN_Y
        temp, icon = self.open_weather.get_weather()
        if temp != "":
            x_range, y_range = self.draw_texts([temp + "°C"], [50],
                                               x_aligns=[2])
        else:
            return 0
        y_weather = y_range[1]
        dy = round(y_range[1] - y_range[0])
        icon_size = (dy, dy)
        if icon:
            self.draw_icon(icon, (x_range[0] - icon_size[0], MARGIN_Y),
                           icon_size)
        return y_weather

    def news(self, y):
        titles, descriptions = self.tagesschau.get_news()
        texts = []
        fonts = []
        x_aligns = []
        colors = []
        for t, d in zip(titles, descriptions):
            texts.extend([t, d])
            fonts.extend([25, 21])
            x_aligns.extend([1, 0])
            colors.extend([RED, BLACK])
        self.draw_texts(
            texts,
            fonts,
            y_align=2,
            x_aligns=x_aligns,
            y_range=(y, IMAGE_SIZE[1] - MARGIN_Y),
            spacing=10,
            colors=colors,
        )

    def draw_icon(self, icon, pos, size, color=BLACK):
        pos = (int(pos[0]), int(pos[1]))
        raw_icon = Image.open(os.path.join(PNG_DIR,
                                           icon + ".png")).convert("RGBA")
        icon = Image.new("P", raw_icon.size, 0)
        for x in range(raw_icon.size[0]):
            for y in range(raw_icon.size[1]):
                pixel = raw_icon.getpixel((x, y))
                new_color = color
                if pixel[3] < 125:
                    new_color = WHITE
                icon.putpixel((x, y), new_color)
        icon_image = icon.resize(size)
        self.image.paste(icon_image, pos)

    def draw_texts(
        self,
        texts,
        fonts,
        colors=[],
        x_range=(MARGIN_X, IMAGE_SIZE[0] - MARGIN_X),
        y_range=(MARGIN_Y, IMAGE_SIZE[1] - MARGIN_Y),
        y_align=0,  # 0: top, 1: mid, 2: bot
        x_aligns=[],  # 0: left, 1: mid, 2: right
        spacing=0,
    ):
        if y_range[0] > y_range[1]:
            print("Warning: negative space for text")
            return x_range, y_range
        if colors == []:
            colors = [BLACK] * len(texts)
        if x_aligns == []:
            x_aligns = [0] * len(texts)
        all_lines = []
        all_dys = []
        all_dxs = []
        # split into lines and get dxs and dys
        for i in range(len(texts)):
            split = texts[i].split()
            font = self.get_font(fonts[i])
            lines = []
            dxs = []
            dys = []
            while len(split) > 0:
                line = ""
                while (len(split) > 0 and
                       self.draw.textsize(line + " " + split[0], font=font)[0]
                       < x_range[1] - x_range[0]):
                    line = line + " " + split[0]
                    del split[0]
                dx, dy = self.draw.textsize(line, font=font)
                lines.append(line)
                dxs.append(dx)
                dys.append(dy)
            all_lines.append(lines)
            all_dxs.append(dxs)
            dys = [max(dys)] * len(dys)  # equal spacing
            all_dys.append(dys)
        for dys in all_dys[:-1]:
            dys[-1] += spacing
        # shorten to fit y_range
        dy_total = sum([a for b in all_dys for a in b])
        while dy_total > y_range[1] - y_range[0]:
            del all_lines[-1]
            del all_dys[-1]
            del all_dxs[-1]
            dy_total = sum([a for b in all_dys for a in b])
        # draw
        y_emtpy = (y_range[1] - y_range[0]) - dy_total
        y_start = y_range[0] + y_emtpy * y_align / 2
        y = y_start
        x_min = x_range[1]
        x_max = x_range[0]
        for i, lines in enumerate(all_lines):
            font = self.get_font(fonts[i])
            for j, line in enumerate(lines):
                x_empty = x_range[1] - x_range[0] - all_dxs[i][j]
                x = x_range[0] + x_empty * x_aligns[i] / 2
                x_min = min(x_min, x)
                x_max = max(x_max, x + all_dxs[i][j])
                self.draw.text((x, y), line, fill=colors[i], font=font)
                y += all_dys[i][j]
        return ((x_min, x_max), (y_start, y))

    def get_font(self, font):
        if font not in fonts:
            fonts[font] = ImageFont.truetype(
                # "/usr/share/fonts/liberation-sans/LiberationSans-%s.ttf" % font[0],
                os.path.join(DIR, "fonts/DejaVuSansCondensed.ttf"),
                # "/usr/share/fonts/paratype-pt-sans/PTS55F.ttf",
                # "fonts/Nintendo-DS-BIOS.ttf",
                # "fonts/Roboto-Regular.ttf",
                size=font,
            )
        return fonts[font]
Ejemplo n.º 10
0
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os
from datum import Datum
from conf import *


def xavier_init(size):
    in_dim = size[0]
    xavier_stddev = 1. / tf.sqrt(in_dim / 2.)
    return tf.random_normal(shape=size, stddev=xavier_stddev)


dt = Datum()
dt.get_data(training_start, training_end)

X = tf.placeholder(tf.float32, shape=[None, dt.dim])

D_W1 = tf.Variable(xavier_init([dt.dim, 128]))
D_b1 = tf.Variable(tf.zeros(shape=[128]))

D_W2 = tf.Variable(xavier_init([128, 1]))
D_b2 = tf.Variable(tf.zeros(shape=[1]))

theta_D = [D_W1, D_W2, D_b1, D_b2]

Z = tf.placeholder(tf.float32, shape=[None, 100])

G_W1 = tf.Variable(xavier_init([100, 128]))
Ejemplo n.º 11
0
 def from_msgpack(self, m):
     from datum import Datum
     return Datum.from_msgpack(m)