コード例 #1
0
ファイル: load_data.py プロジェクト: mattparrilla/lake-brite
def load_all_csvs(directory='data/long-term-lake-monitoring'):
    """Takes a directory and loads all CSVs in the directory into memory
       as an array of objects. The objects from the 'data' dir look like:
       {
         'Symbol': '',
         'Stratum': 'E',
         'VisitDate': '7/8/93',
         'Depth': 'COM',
         'Lab': 'VT',
         'StationID': '16',
         'Station': 'Shelburne Bay',
         'Result': '11.5',
         'Time': '1640',
         'Test': 'Chloride',
         'FieldID': '9341134'
       }
    """

    all_data = []
    for filename in glob.glob('%s/*.csv' % safe_path(directory)):
        with open(filename, 'rU') as fin:
            f = csv.DictReader(fin)
            data = [l for l in f]
            all_data += data

    return all_data
コード例 #2
0
def normal_gif_to_lake_brite(normal_gif, duration):
    """Take a normal, animated GIF and allow it to display on lake brite"""

    def iter_frames(gif):
        """Frame generator"""
        im = Image.open(gif)
        try:
            i = 0
            while 1:
                im.seek(i)
                imframe = im.copy()
                if i == 0:
                    palette = imframe.getpalette()
                else:
                    imframe.putpalette(palette)
                yield imframe
                i += 1
        except EOFError:
            pass

    frames = []
    for i, frame in enumerate(iter_frames(normal_gif)):
        result = Image.new("RGB", (50, 150))
        for i in range(0, 10):
            img = frame.copy()
            img.thumbnail((400, 400), Image.ANTIALIAS)
            x = 0
            y = i * 15
            w, h = img.size
            result.paste(img, (x, y, x + w, y + h))
        frames.append(result)

    return generate_gif(frames, safe_path("gif/video"), duration)
コード例 #3
0
def generate_lake_brite_gif(metric, palette='winter_r', duration=0.125,
        clip_to_lake=True, tween_frames=1, empty_frames=0):
    """Generate 3D Lake GIF for consumption by LakeBrite"""

    print "Generating 3D Lake GIFs of %s" % metric

    a = generate_lake_array(metric, clip_to_lake, tween_frames)
    max_value = get_max_of_data(a)
    min_value = get_min_of_data(a)

    print "Rotating matrix"
    rotated = []
    for index, frame in enumerate(a):
        rotated.append(zip(*frame))

    print "Increasing dimensions"
    slices = [increase_dimensions(frame, max_value, min_value) for
        frame in rotated]

    print "Stackining frames"
    frames = [stack_frames(frame) for frame in slices]

    print "Normalizing values"
    normalized = [normalize_values(frame, max_value, min_value)
        for frame in frames]

    # TODO: this should live outside of `generate_lake_brite_gif()`
    # it lives here because I use `palette` from the arguments
    # and can't figure out how to bind it to `color_map` otherwise
    def color_map(x, palette=palette):
        """To be consumed by np.vectorize to transform nan values to black
        and provide the color map"""

        if np.isnan(x):
            return [0, 0, 0]
        elif x < 0:
            return list(cm.get_cmap(palette)(0, bytes=True)[:3])
        else:
            return list(cm.get_cmap(palette)(x, bytes=True)[:3])

    print "Mapping values to colors"
    a = map_value_to_color(normalized, color_map)

    print "Adding empty frames"
    with_empties = add_empty_frames(a, empty_frames)

    print "Converting to numpy array"
    arrays = [np.fliplr(np.asarray(with_empties[i], 'uint8'))
        for i, f in enumerate(with_empties)]

    print "Generating GIFs"
    path_to_gif = safe_path('gif/lake-animation')
    generate_gif(arrays, path_to_gif, duration)

    return '%s.gif' % path_to_gif
コード例 #4
0
ファイル: lake_clip.py プロジェクト: mattparrilla/lake-brite
def find_current_boundary():
    """Finds all non-white pixels in the low-res image of lake champlain
    for use in an image clipper"""
    im = Image.open('%s' % safe_path('champlain-low-res.png'))

    lake_coords = []
    arr = np.array(im)
    for x, row in enumerate(arr):
        for z, item in enumerate(row):
            if not np.array_equal(item, np.array([255, 255, 255, 255])):
                lake_coords.append([x, z])

    return lake_coords