Skip to content

Commit

Permalink
Merge branch 'master' into backed-sparse
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Jun 19, 2019
2 parents 8bed6fa + e9ccfc3 commit 6ee7e3e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 144 deletions.
121 changes: 14 additions & 107 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,116 +1,23 @@
*~

# tests
*.pytest_cache/*

# anndata
docs/_build
docs/anndata.*

# Mac
# Temp files
.DS_Store
*~

# Byte-compiled / optimized / DLL files
# Compiled files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject
/build/
/dist/
/*.egg-info/

# Rope project settings
.ropeproject
# Tests and coverage
/.pytest_cache/
/.cache/

# PyCharm project settings
.idea/
# docs
/docs/anndata.*
/docs/_build/

# mkdocs documentation
/site
# IDEs
/.idea/

# mypy
.mypy_cache/
3 changes: 1 addition & 2 deletions anndata/core/anndata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Main class and helper functions.
"""
import os
from enum import Enum
from collections import OrderedDict
from collections.abc import MutableMapping
Expand Down Expand Up @@ -1152,7 +1151,7 @@ def filename(self, filename: Optional[PathLike]):
# write the content of self to the old file
# and close the file
self.write()
os.rename(self.filename, filename)
self.filename.rename(filename)
else:
# do nothing
return
Expand Down
64 changes: 29 additions & 35 deletions anndata/tests/test_h5py.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# Had to rename the test module to h5py_ so that it wouldn't conflict with the
# h5py import upon testing.

import os
from tempfile import mkstemp

import numpy as np
import scipy.sparse as ss

from anndata import h5py


def test_create_and_read_dataset():
h5_path = mkstemp(suffix=".h5")[1]
sparse_matrix = ss.csr_matrix([[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
[1, 1, 0]],
dtype=np.float64)
def test_create_and_read_dataset(tmp_path):
h5_path = tmp_path / 'test.h5'
sparse_matrix = ss.csr_matrix([
[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
[1, 1, 0],
], dtype=np.float64)
with h5py.File(h5_path) as h5f:
h5f.create_dataset('sparse/matrix', data=sparse_matrix)
with h5py.File(h5_path) as h5f:
Expand All @@ -27,43 +25,39 @@ def test_create_and_read_dataset():
assert (h5f['sparse']['matrix'][:-2] != sparse_matrix[:-2]).size == 0
assert (h5f['sparse']['matrix'].value != sparse_matrix).size == 0

os.remove(h5_path)


def test_create_dataset_from_dataset():
from_h5_path = mkstemp(suffix=".h5")[1]
to_h5_path = mkstemp(suffix=".h5")[1]
sparse_matrix = ss.csr_matrix([[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
[1, 1, 0]],
dtype=np.float64)
def test_create_dataset_from_dataset(tmp_path):
from_h5_path = tmp_path / 'from.h5'
to_h5_path = tmp_path / 'to.h5'
sparse_matrix = ss.csr_matrix([
[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
[1, 1, 0],
], dtype=np.float64)
with h5py.File(from_h5_path) as from_h5f:
from_dset = from_h5f.create_dataset('sparse/matrix', data=sparse_matrix)

with h5py.File(to_h5_path) as to_h5f:
to_h5f.create_dataset('sparse/matrix', data=from_dset)
assert (to_h5f['sparse/matrix'].value != sparse_matrix).size == 0

os.remove(from_h5_path)
os.remove(to_h5_path)


def test_dataset_append():
h5_path = mkstemp(suffix=".h5")[1]
sparse_matrix = ss.csr_matrix([[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
[1, 1, 0]],
dtype=np.float64)
to_append = ss.csr_matrix([[0, 1, 1],
[1, 0, 0]],
dtype=np.float64)
def test_dataset_append(tmp_path):
h5_path = tmp_path / 'test.h5'
sparse_matrix = ss.csr_matrix([
[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
[1, 1, 0],
], dtype=np.float64)
to_append = ss.csr_matrix([
[0, 1, 1],
[1, 0, 0],
], dtype=np.float64)
appended_matrix = ss.vstack((sparse_matrix, to_append))

with h5py.File(h5_path) as h5f:
h5f.create_dataset('matrix', data=sparse_matrix, chunks=(100000,))
h5f['matrix'].append(to_append)
assert (h5f['matrix'].value != appended_matrix).size == 0

os.remove(h5_path)

0 comments on commit 6ee7e3e

Please sign in to comment.