Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Inplace #47

Merged
merged 6 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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: 2 additions & 4 deletions brainio_collection/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import boto3
from tqdm import tqdm
from xarray import DataArray

import brainio_base.assemblies
from brainio_base.assemblies import get_levels
from brainio_collection import lookup, list_stimulus_sets
from brainio_collection.lookup import TYPE_ASSEMBLY, TYPE_STIMULUS_SET, sha1_hash

Expand Down Expand Up @@ -111,9 +111,7 @@ def package_stimulus_set(proto_stimulus_set, stimulus_set_identifier, bucket_nam

def write_netcdf(assembly, target_netcdf_file):
_logger.debug(f"Writing assembly to {target_netcdf_file}")
assembly = DataArray(assembly) # if we're passed a BrainIO DataAssembly, it will automatically re-index otherwise
for index in assembly.indexes.keys():
assembly.reset_index(index, inplace=True)
assembly = assembly.reset_index(list(assembly.indexes))
assembly.to_netcdf(target_netcdf_file)
sha1 = sha1_hash(target_netcdf_file)
return sha1
Expand Down
7 changes: 7 additions & 0 deletions tests/test_assemblies.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ def test_aperture(self, identifier, image_id, expected_amount_gray, ratio_gray):
assert amount_gray == expected_amount_gray


def test_inplace():
d = xr.DataArray(0, None, None, None, None, None, False)
with pytest.raises(TypeError) as te:
d = d.reset_index(None, inplace=True)
assert "inplace" in str(te.value)


class TestSeibert:
@pytest.mark.private_access
def test_dims(self):
Expand Down
57 changes: 57 additions & 0 deletions tests/test_packaging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest
from pathlib import Path

from brainio_base.assemblies import DataAssembly, get_levels
from brainio_collection.packaging import write_netcdf


def test_write_netcdf():
assy = DataAssembly(
data=[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]],
coords={
'up': ("a", ['alpha', 'alpha', 'beta', 'beta', 'beta', 'beta']),
'down': ("a", [1, 1, 1, 1, 2, 2]),
'sideways': ('b', ['x', 'y', 'z'])
},
dims=['a', 'b']
)
netcdf_path = Path("test.nc")
netcdf_sha1 = write_netcdf(assy, str(netcdf_path))
assert netcdf_path.exists()


def test_reset_index():
assy = DataAssembly(
data=[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]],
coords={
'up': ("a", ['alpha', 'alpha', 'beta', 'beta', 'beta', 'beta']),
'down': ("a", [1, 1, 1, 1, 2, 2]),
'sideways': ('b', ['x', 'y', 'z'])
},
dims=['a', 'b']
)
assert assy["a"].variable.level_names == ["up", "down"]
assert list(assy.indexes) == ["a", "b"]
assy = assy.reset_index(list(assy.indexes))
assert assy["a"].variable.level_names is None
assert get_levels(assy) == []
assert list(assy.indexes) == []



def test_reset_index_levels():
assy = DataAssembly(
data=[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]],
coords={
'up': ("a", ['alpha', 'alpha', 'beta', 'beta', 'beta', 'beta']),
'down': ("a", [1, 1, 1, 1, 2, 2]),
'sideways': ('b', ['x', 'y', 'z'])
},
dims=['a', 'b']
)
assert assy["a"].variable.level_names == ["up", "down"]
assy = assy.reset_index(["up", "down"])
assert get_levels(assy) == []