Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a JLD file in Python

Tags:

python

julia

I have a database created in Julia with the JLD Package

This database contains 2 elements: input and output

In julia, I can load it with the following code:

using JLD

data_in = load("file_path")["input"]
1×5 Array{Int64,2}:
 1  2  3  4  5

data_out = load("file_path")["output"]
1×5 Array{Int64,2}:
 3  6  9  12  15

I would like to load these arrays in Python. I tried the following (in Python):

filename = "file_path"

data = open(filename, r)

data returns the following:

data
<_io.TextIOWrapper name='file_path' mode='r' encoding='UTF-8'>

After that, I read a document that explained how to read a file. Nevertheless, if I run the following:

print(data.readlines())

I have only this output:

[]

Is it possible to load these arrays in Python ?

EDIT

I tried to do an equivalent of Julia:

data = open("file_path")["input"]

But there is this error:

TypeError: '_io.TextIOWrapper' object is not subscriptable

Maybe there is an other function to read a file ?

like image 899
Julien Avatar asked Jun 26 '18 16:06

Julien


People also ask

What is Jld file?

JLD is a specific "dialect" of HDF5, a cross-platform, multi-language data storage format most frequently used for scientific data. By comparison with "plain" HDF5, JLD files automatically add attributes and naming conventions to preserve type information for each object.

What is JLD2?

JLD2 saves and loads Julia data structures in a format comprising a subset of HDF5, without any dependency on the HDF5 C library. It typically outperforms the JLD package (sometimes by multiple orders of magnitude) and often outperforms Julia's built-in serializer.


2 Answers

Julia JLD files have HDF5 format so you can read them using e.g. h5py like this:

import h5py
f = h5py.File("filename", "r")
f["input"].value, f["output"].value

The file will also contain an entry _creator with metadata saved by Julia.

Note that Julia stores data in column major order, as opposed to row major used by numpy, so if you would read matrices this way they would be transposed.

like image 54
Bogumił Kamiński Avatar answered Sep 22 '22 23:09

Bogumił Kamiński


I had a similar issue to solve and I did the following:

  1. I read the JLD file in julia:

    data = load("filename_julia.jld")["data"]

  2. Converted into an array by numpy.asarray using PyCall to invoke numpy,

    using PyCall:

    np = pyimport("numpy")

    data = np.asarray(data)

  3. I then saved the array as .npy file using numpy.save:

    np.save("filename_python.npy",data)

You can then open this file in python as a regular .npy file

like image 33
Nishan Avatar answered Sep 21 '22 23:09

Nishan