Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port read and write functions #70

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/finch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@
iinfo,
can_cast,
)
from .io import (
read,
write,
)

__all__ = [
"Tensor",
Expand Down Expand Up @@ -252,6 +256,8 @@
"real",
"imag",
"conj",
"read",
"write",
]

__array_api_version__: str = "2023.12"
11 changes: 11 additions & 0 deletions src/finch/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .julia import jl
from .tensor import Tensor


def read(filename: str) -> Tensor:
julia_obj = jl.fread(filename)
hameerabbasi marked this conversation as resolved.
Show resolved Hide resolved
return Tensor(julia_obj)


def write(filename: str, tns: Tensor) -> None:
jl.fwrite(filename, tns._obj)
hameerabbasi marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/finch/julia.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@

jl.seval("using Finch")
jl.seval("using Random")
jl.seval("using TensorMarket")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this limit us to just this format?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's just a dependency. Here's a list of all supported formats: https://willowahrens.io/Finch.jl/stable/guides/fileio/#Finch.fread

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to pull in any other dependencies for other formats?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@willow-ahrens Are there other IO related dependencies that we should install during the setup to fully support fread and fwrite?

In Finch.jl in Project.toml I found three more:

[weakdeps]
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
NPZ = "15e1cf62-19b3-5cfa-8e77-841668bca605"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
TensorMarket = "8b7d4fe7-0b45-4d0d-9dd8-5cc9b23b4b77"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HDF5, NPZ, and TensorMarket are required for binsparse and ttx files.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be added with pkg.add when we add Finch, or loaded dynamically depending on whether we want to take a dependency on hdf5

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I load them dynamically.

17 changes: 17 additions & 0 deletions tests/data/matrix_1.ttx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%%MatrixMarket matrix coordinate integer general
3 5 15
1 1 0
2 1 1
3 1 0
1 2 0
2 2 0
3 2 5
1 3 3
2 3 0
3 3 0
1 4 2
2 4 1
3 4 0
1 5 0
2 5 0
3 5 0
25 changes: 25 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os

from numpy.testing import assert_equal

import finch

base_path = "tests/data"


def test_read(arr2d):
tns = finch.read(f"{base_path}/matrix_1.ttx")

assert_equal(tns.todense(), arr2d)


def test_write(arr2d):
tns = finch.asarray(arr2d)
finch.write(f"{base_path}/tmp.ttx", tns)

expected = open(f"{base_path}/matrix_1.ttx").read()
actual = open(f"{base_path}/tmp.ttx").read()
hameerabbasi marked this conversation as resolved.
Show resolved Hide resolved

assert actual == expected

os.remove(f"{base_path}/tmp.ttx")
Loading