Coder Social home page Coder Social logo

hurray-py's Introduction

Tutorial

Build Status

hurray-py has an API very similar to that of the h5py library.

For this tutorial, make sure you have a hurray server running on localhost on port 2222.

Make sure you import numpy and hurraypy:
>>> import numpy as np
>>> import hurraypy as hp

Creating a database/file

Establish a server connection and create an hdf5 file:
>>> conn = hp.connect('localhost', '2222')
>>> conn.create_db('mydatabase.h5')
Connect to the created file:
>>> db = conn.connect_db('mydatabase.h5')
>>> db
<HDF5 Group (db=mydatabase, path=/)>

Note that the database is a Group (the root group /).

Working with groups and datasets

To create a subgroup /mygroup run:
>>> grp = db.create_group("mygrp")
>>> grp
<HDF5 Group (db=mydatabase, path=/mygrp)>
Now let's store a 2D array in that group:
>>> data = np.array([[1, 2, 3], [4, 5, 6]])
>>> dataset = grp.create_dataset('myarray', data=data)
>>> dataset
<HDF5 Dataset (db=mydatabase, path=/mygrp/myarray)>

Let's fetch the array from the server and read only its first dimension (note that only the requested portion of the array is transferred over the network):

>>> dataset = db['/mygrp/myarray']
>>> dataset[:]
array([[1, 2, 3],
       [4, 5, 6]])
>>> dataset[0, :]
array([1, 2, 3])
Numpy-like broadcasting allows us to overwrite only a portion of the array:
>>> x = np.array([8, 9, 10])
>>> dataset[0, :] = x
>>> dataset[:]
array([[ 8,  9, 10],
       [ 4,  5,  6]])

Node attributes (i.e., meta-data)

Every node (Group or Dataset) can have a number of so-called attributes. An attribute is a key/value pair, where the value can either be a single value (string or number) or itself an n-dimensional array.

It works very much like a dictionary:
>>> dataset.attrs['foo'] = "helloworld"
>>> dataset.attrs['foo']
"helloworld"
>>> 'foo' in dst.attrs
True
>>> dst.attrs.keys()
['foo']
Using array values is also straightforward:
>>> dst.attrs['num'] = np.array([0.1, 0.2, 0.5])
>>> dst.attrs['num']
array([0.1, 0.2, 0.5])

hurray-py's People

Contributors

aeby avatar weatherfrog avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.