Julien
Julien

Reputation: 781

Load a JLD file in Python

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 ?

Upvotes: 9

Views: 5715

Answers (2)

Nishan
Nishan

Reputation: 21

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

Upvotes: 2

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69919

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.

Upvotes: 12

Related Questions