Addons/convert/numpy

From J Wiki
Jump to navigation Jump to search

Overview

This addon has functions for reading and writing numpy npy and npz files.

The npy files should contain a single unnested array. The result of reading an npy file is a J noun.

The npz files are zips of one or more npy files. Data for reading and writing npz files should be a 2 column boxed matrix of names and values.

See the demo.ijs script in the addon.

Numpy docs are at Npy Format .

Notes:

1. npy files may contain python object arrays, stored as pickles. This is an internal python format that is not well documented for use by external programs. The J addon does not support reading or writing pickles. However, in some cases it should be possible to at least read such data. For example, the object.ijs script in the source was used to read pickles with a mixed matrix of numbers and characters from npy files in the scipy benchmarks suite.

2. So far only boolean, integer and float types are supported for reading. It should be straightforward to add other python datatypes except for objects.

Examples

From the demo.ijs script:

   load 'convert/numpy'
   
   D=. jpath '~temp/'
   
   wint=. i.3 4
   wbool=. 0 = 3 | wint
   wfloat=. 1.1 * p: i.2 5
   
   writenpy_pnumpy_ (D,'wbool.npy');wbool
140
   writenpy_pnumpy_ (D,'wfloat.npy');wfloat
208
   writenpy_pnumpy_ (D,'wint.npy');wint
224
   
   wbool -: readnpy_pnumpy_ D,'wbool.npy'
1
   wfloat -: readnpy_pnumpy_ D,'wfloat.npy'
1
   wint -: readnpy_pnumpy_ D,'wint.npy'
1
   
   nms=. ;: 'wbool wfloat wint'
   dat=. nms,.".each nms
   
   writenpz_pnumpy_ (D,'wdata.npz');<dat
1
   
   dat -: readnpz_pnumpy_ D,'wdata.npz'
1