def test_colorize():
    coords = [np.array([0, 1]), np.array([2, 5])]
    cat_agg = xr.DataArray(np.array([[(0, 12, 0), (3, 0, 3)],
                                    [(12, 12, 12), (24, 0, 0)]]),
                           coords=(coords + [['a', 'b', 'c']]),
                           dims=(dims + ['cats']))

    colors = [(255, 0, 0), '#0000FF', 'orange']

    img = tf.colorize(cat_agg, colors, how='log')
    sol = np.array([[3137273856, 2449494783],
                    [4266997674, 3841982719]])
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    colors = dict(zip('abc', colors))
    img = tf.colorize(cat_agg, colors, how='cbrt')
    sol = np.array([[3070164992, 2499826431],
                    [4283774890, 3774873855]])
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    img = tf.colorize(cat_agg, colors, how='linear')
    sol = np.array([[1660878848, 989876991],
                    [4283774890, 2952790271]])
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    img = tf.colorize(cat_agg, colors, how=lambda x: x ** 2)
    sol = np.array([[788463616, 436228863],
                    [4283774890, 2080375039]])
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
def test_colorize():
    coords = [np.array([0, 1]), np.array([2, 5])]
    cat_agg = xr.DataArray(np.array([[(0, 12, 0), (3, 0, 3)],
                                     [(12, 12, 12), (24, 0, 0)]]),
                           coords=(coords + [['a', 'b', 'c']]),
                           dims=(dims + ['cats']))

    colors = [(255, 0, 0), '#0000FF', 'orange']

    img = tf.colorize(cat_agg, colors, how='log')
    sol = np.array([[2583625728, 335565567], [4283774890, 3707764991]],
                   dtype='u4')
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    colors = dict(zip('abc', colors))
    img = tf.colorize(cat_agg, colors, how='cbrt')
    sol = np.array([[2650734592, 335565567], [4283774890, 3657433343]],
                   dtype='u4')
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    img = tf.colorize(cat_agg, colors, how='linear')
    sol = np.array([[1140785152, 335565567], [4283774890, 2701132031]],
                   dtype='u4')
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    img = tf.colorize(cat_agg,
                      colors,
                      how=lambda x, m: np.where(m, np.nan, x)**2)
    sol = np.array([[503250944, 335565567], [4283774890, 1744830719]],
                   dtype='u4')
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
示例#3
0
def test_colorize():
    coords = [np.array([0, 1]), np.array([2, 5])]
    cat_agg = xr.DataArray(np.array([[(0, 12, 0), (3, 0, 3)],
                                     [(12, 12, 12), (24, 0, 0)]]),
                           coords=(coords + [['a', 'b', 'c']]),
                           dims=(dims + ['cats']))

    colors = [(255, 0, 0), '#0000FF', 'orange']

    img = tf.colorize(cat_agg, colors, how='log')
    sol = np.array([[3137273856, 2449494783], [4266997674, 3841982719]])
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    colors = dict(zip('abc', colors))
    img = tf.colorize(cat_agg, colors, how='cbrt')
    sol = np.array([[3070164992, 2499826431], [4283774890, 3774873855]])
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    img = tf.colorize(cat_agg, colors, how='linear')
    sol = np.array([[1660878848, 989876991], [4283774890, 2952790271]])
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    img = tf.colorize(cat_agg, colors, how=lambda x: x**2)
    sol = np.array([[788463616, 436228863], [4283774890, 2080375039]])
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
def test_colorize():
    coords = [np.array([0, 1]), np.array([2, 5])]
    cat_agg = xr.DataArray(np.array([[(0, 12, 0), (3, 0, 3)],
                                    [(12, 12, 12), (24, 0, 0)]]),
                           coords=(coords + [['a', 'b', 'c']]),
                           dims=(dims + ['cats']))

    colors = [(255, 0, 0), '#0000FF', 'orange']

    img = tf.colorize(cat_agg, colors, how='log', min_alpha=20)
    sol = np.array([[2583625728, 335565567],
                    [4283774890, 3707764991]], dtype='u4')
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    colors = dict(zip('abc', colors))
    img = tf.colorize(cat_agg, colors, how='cbrt', min_alpha=20)
    sol = np.array([[2650734592, 335565567],
                    [4283774890, 3657433343]], dtype='u4')
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    img = tf.colorize(cat_agg, colors, how='linear', min_alpha=20)
    sol = np.array([[1140785152, 335565567],
                    [4283774890, 2701132031]], dtype='u4')
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
    img = tf.colorize(cat_agg, colors,
                      how=lambda x, m: np.where(m, np.nan, x) ** 2,
                      min_alpha=20)
    sol = np.array([[503250944, 335565567],
                    [4283774890, 1744830719]], dtype='u4')
    sol = tf.Image(sol, coords=coords, dims=dims)
    assert img.equals(sol)
示例#5
0
def create_image3(x_range, y_range, w=plot_width, h=plot_height):
    cvs = dshader.Canvas(plot_width=w,
                         plot_height=h,
                         x_range=x_range,
                         y_range=y_range)
    agg = cvs.points(df, 'dens', 'temp', dshader.count_cat('phase'))
    img = tf.colorize(agg, phase_color_key, how='eq_hist')
    return img
示例#6
0
def create_image(x_range, y_range, w=plot_width, h=plot_height):
    cvs = dshader.Canvas(plot_width=w,
                         plot_height=h,
                         x_range=x_range,
                         y_range=y_range)
    agg = cvs.points(df, 'x', 'z', dshader.count_cat('phase'))
    img = tf.colorize(agg, phase_color_key, how='eq_hist')
    return tf.dynspread(img, threshold=0.3, max_px=4)
def test_colorize():
    colors = [(255, 0, 0), '#0000FF', 'orange']

    img = tf.colorize(cat_agg, colors, how='log').img
    sol = np.array([[3137273856, 2449494783],
                    [4266997674, 3841982719]])
    assert (img == sol).all()
    img = cat_agg.colorize(colors, how='log').img
    assert (img == sol).all()
    colors = dict(zip('abc', colors))
    img = tf.colorize(cat_agg, colors, how='cbrt').img
    sol = np.array([[3070164992, 2499826431],
                    [4283774890, 3774873855]])
    assert (img == sol).all()
    img = tf.colorize(cat_agg, colors, how='linear').img
    sol = np.array([[1660878848, 989876991],
                    [4283774890, 2952790271]])
    assert (img == sol).all()
    img = tf.colorize(cat_agg, colors, how=lambda x: x ** 2).img
    sol = np.array([[788463616, 436228863],
                    [4283774890, 2080375039]])
    assert (img == sol).all()
    def render_image(self):

        # handle categorical field
        if self.field in self.categorical_fields:
            pix = tf.colorize(self.agg, self.colormap, how=self.transfer_function)

        # handle ordinal field
        elif self.field in self.ordinal_fields:
            pix = tf.interpolate(self.agg, cmap=self.color_ramp, how=self.transfer_function)
        # handle no field
        else:
            pix = tf.interpolate(self.agg, cmap=self.color_ramp, how=self.transfer_function)

        if self.spread_size > 0:
            pix = tf.spread(pix, px=self.spread_size)

        return pix
示例#9
0
    def render_image(self):

        # handle categorical field
        if self.field in self.categorical_fields:
            pix = tf.colorize(self.agg,
                              self.colormap,
                              how=self.transfer_function)

        # handle ordinal field
        elif self.field in self.ordinal_fields:
            pix = tf.interpolate(self.agg,
                                 cmap=self.color_ramp,
                                 how=self.transfer_function)
        # handle no field
        else:
            pix = tf.interpolate(self.agg,
                                 cmap=self.color_ramp,
                                 how=self.transfer_function)

        if self.spread_size > 0:
            pix = tf.spread(pix, px=self.spread_size)

        return pix
示例#10
0
    for i in range(len(tdb)):
        events = list(tdb.trail(i, event_filter=query))
        if events:
            yield events[0].time, events


def get_dataframe():
    tdb = TrailDB('pydata-tutorial.tdb')
    base = tdb.min_timestamp()
    types = []
    xs = []
    ys = []
    # try this:
    # for y, (first_ts, events) in enumerate(sorted(get_events(tdb), reverse=True)):
    for y, (first_ts, events) in enumerate(get_events(tdb)):
        for event in events:
            xs.append(old_div(int(event.time - base), (24 * 3600)))
            ys.append(y)
            types.append('user' if event.user else 'anon')
    data = pd.DataFrame({'x': xs, 'y': ys})
    data['type'] = pd.Series(types, dtype='category')
    return data


cnv = ds.Canvas(400, 300)
agg = cnv.points(get_dataframe(), 'x', 'y', ds.count_cat('type'))
colors = {'anon': 'red', 'user': '******'}
img = tf.set_background(tf.colorize(agg, colors, how='eq_hist'), 'white')
with open('prince.png', 'w') as f:
    f.write(img.to_bytesio().getvalue())
def get_events(tdb):
    query = [('title', 'Prince (musician)')]
    for i in range(len(tdb)):
        events = list(tdb.trail(i, event_filter=query))
        if events:
            yield events[0].time, events

def get_dataframe():
    tdb = TrailDB('pydata-tutorial.tdb')
    base = tdb.min_timestamp()
    types = []
    xs = []
    ys = []
    #try this:
    #for y, (first_ts, events) in enumerate(sorted(get_events(tdb), reverse=True)):
    for y, (first_ts, events) in enumerate(get_events(tdb)):
        for event in events:
            xs.append(int(event.time - base) / (24 * 3600))
            ys.append(y)
            types.append('user' if event.user else 'anon')
    data = pd.DataFrame({'x': xs, 'y': ys})
    data['type'] = pd.Series(types, dtype='category')
    return data

cnv = ds.Canvas(400, 300)
agg = cnv.points(get_dataframe(), 'x', 'y', ds.count_cat('type'))
colors = {'anon': 'red', 'user': '******'}
img=tf.set_background(tf.colorize(agg, colors, how='eq_hist'), 'white')
with open('prince.png', 'w') as f:
    f.write(img.to_bytesio().getvalue())