Code Monkey home page Code Monkey logo

pyjdata's Introduction

JData for Python - lightweight and serializable data annotations for Python

Build Status

The JData Specification defines a lightweight language-independent data annotation interface targetted at storing and sharing complex data structures across different programming languages such as MATLAB, JavaScript, Python etc. Using JData formats, a complex Python data structure can be encoded as a dict object that is easily serialized as a JSON/binary JSON file and share such data between programs of different languages.

How to install

This package can also be installed on Ubuntu 21.04 or Debian Bullseye via

sudo apt-get install python3-jdata

On older Ubuntu or Debian releases, you may install jdata via the below PPA:

sudo add-apt-repository ppa:fangq/ppa
sudo apt-get update
sudo apt-get install python3-jdata

Dependencies:

  • numpy: PIP: run pip install numpy or sudo apt-get install python3-numpy
  • (optional) bjdata: PIP: run pip install bjdata or sudo apt-get install python3-bjdata, see https://pypi.org/project/bjdata/, only needed to read/write BJData/UBJSON files
  • (optional) lz4: PIP: run pip install lz4, only needed when encoding/decoding lz4-compressed data
  • (optional) backports.lzma: PIP: run sudo apt-get install liblzma-dev and pip install backports.lzma (needed for Python 2.7), only needed when encoding/decoding lzma-compressed data
  • (optional) blosc2: PIP: run pip install blosc2, only needed when encoding/decoding blosc2-compressed data

Replacing pip by pip3 if you are using Python 3.x. If either pip or pip3 does not exist on your system, please run

    sudo apt-get install python3-pip

Please note that in some OS releases (such as Ubuntu 20.04), python2.x and python-pip are no longer supported.

One can also install this module from the source code. To do this, you first check out a copy of the latest code from Github by

    git clone https://github.com/NeuroJSON/pyjdata.git
    cd pyjdata

then install the module to your local user folder by

    python3 setup.py install --user

or, if you prefer, install to the system folder for all users by

    sudo python3 setup.py install

Please replace python by python3 if you want to install it for Python 3.x instead of 2.x.

Instead of installing the module, you can also import the jdata module directly from your local copy by cd the root folder of the unzipped pyjdata package, and run

   import jdata as jd

How to use

The PyJData module is easy to use. You can use the encode()/decode() functions to encode Python data into JData annotation format, or decode JData structures into native Python data, for example

import jdata as jd
import numpy as np

a={'str':'test','num':1.2,'list':[1.1,[2.1]],'nan':float('nan'),'np':np.arange(1,5,dtype=np.uint8)}
jd.encode(a)
jd.decode(jd.encode(a))
d1=jd.encode(a,{'compression':'zlib','base64':1})
d1
jd.decode(d1,{'base64':1})

One can further save the JData annotated data into JSON or binary JSON (UBJSON) files using the jdata.save function, or loading JData-formatted data to Python using jdata.load

import jdata as jd
import numpy as np

a={'str':'test','num':1.2,'list':[1.1,[2.1]],'nan':float('nan'),'np':np.arange(1,5,dtype=np.uint8)}
jd.save(a,'test.json')
newdata=jd.load('test.json')
newdata

PyJData supports multiple N-D array data compression/decompression methods (i.e. codecs), similar to HDF5 filters. Currently supported codecs include zlib, gzip, lz4, lzma, base64 and various blosc2 compression methods, including blosc2blosclz, blosc2lz4, blosc2lz4hc, blosc2zlib, blosc2zstd. To apply a selected compression method, one simply set {'compression':'method'} as the option to jdata.encode or jdata.save function; jdata.load or jdata.decode automatically decompress the data based on the _ArrayZipType_ annotation present in the data. Only blosc2 compression methods support multi-threading. To set the thread number, one should define an nthread value in the option (opt) for both encoding and decoding.

Utility

One can convert from JSON based data files (.json, .jdt, .jnii, .jmsh, .jnirs) to binary-JData based binary files (.bjd, .jdb, .bnii, .bmsh, .bnirs) and vice versa using command

python3 -mjdata /path/to/text/json/file.json # convert to /path/to/text/json/file.jdb
python3 -mjdata /path/to/text/json/file.jdb  # convert to /path/to/text/json/file.json
python3 -mjdata -h                           # show help info

Test

To see additional data type support, please run the built-in test using below command

python3 -m unittest discover -v test

pyjdata's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

openjdata fangq

pyjdata's Issues

jdata.encode not working for np.complex64 scalar

For scalar values of np.complex64 type, encoder does not process it correctly.

Example:

import numpy as np
import jdata as jd

x = {"a": np.complex64(1+1j)}
enc_x = jd.encode(x)
print(enc_x)

Expected behavior:

{'a': {'_ArrayType_': 'single',
  '_ArraySize_': 1,
  '_ArrayIsComplex_': True,
  '_ArrayData_': [1.0, 1.0]}}

Actual behavior:

{'a': (1+1j)}

Suggested change:

Add condition isinstance(d, np.complex64): to jdata.py

assignment destination is read-only for numpy array

Hi,

I have noticed that after migrating from jdata==0.3.7 to jdata==0.5.1 I am getting the error ValueError: assignment destination is read-only after trying to modify the values that I load with jdata.load. Is this behaviour intended or is there something happening that we are unaware of?

np.stack error on complex numpy array

to encode complex array, the np.stack method is used, but it results in the following error :

File "C:\XXX\Lib\site-packages\jdata\jdata.py", line 136, in encode
newobj["_ArrayData_"] = np.stack(d.ravel().real, d.ravel().imag)
TypeError: only integer scalar arrays can be converted to a scalar index

imaginary part is seen as the "axis" parameter of np.stack function. adding 2 parenthesis should fix the bug
newobj["_ArrayData_"] = np.stack((d.ravel().real, d.ravel().imag))

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.