Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Sep 14, 2022
1 parent 9817c09 commit 6a4561e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 34 deletions.
33 changes: 18 additions & 15 deletions apis/python/src/tiledbsoma/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _from_h5ad_common(
experiment: SOMAExperiment,
input_path: Path,
measurement_name: str,
handler_func: Callable[[SOMAExperiment, ad.AnnData], None],
handler_func: Callable[[SOMAExperiment, ad.AnnData, str, tiledb.Ctx], None],
ctx: Optional[tiledb.Ctx] = None,
) -> None:
"""
Expand All @@ -64,7 +64,7 @@ def _from_h5ad_common(
util.format_elapsed(s, f"{experiment._indent}FINISH READING {input_path}"),
)

handler_func(experiment, anndata, measurement_name, ctx=ctx)
handler_func(experiment, anndata, measurement_name, ctx)

logging.log_io(
None,
Expand Down Expand Up @@ -143,11 +143,14 @@ def from_anndata(

# TODO: more types to check?
if isinstance(anndata.X, np.ndarray):
Xdata = SOMADenseNdArray(uri=f"{measurement.X.get_uri()}/data", ctx=ctx)
ddata = SOMADenseNdArray(uri=f"{measurement.X.get_uri()}/data", ctx=ctx)
# Code here and in else-block duplicated for linter appeasement
ddata.from_matrix(anndata.X)
measurement.X.set(ddata)
else:
Xdata = SOMASparseNdArray(uri=f"{measurement.X.get_uri()}/data", ctx=ctx)
Xdata.from_matrix(anndata.X)
measurement.X.set(Xdata)
sdata = SOMASparseNdArray(uri=f"{measurement.X.get_uri()}/data", ctx=ctx)
sdata.from_matrix(anndata.X)
measurement.X.set(sdata)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# TODO: port from v0
Expand All @@ -163,25 +166,25 @@ def from_anndata(
if len(anndata.varm.keys()) > 0: # do not create an empty collection
measurement.varm.create()
for key in anndata.varm.keys():
arr = SOMADenseNdArray(uri=f"{measurement.varm.get_uri()}/{key}", ctx=ctx)
arr.from_matrix(anndata.varm[key])
measurement.varm.set(arr)
darr = SOMADenseNdArray(uri=f"{measurement.varm.get_uri()}/{key}", ctx=ctx)
darr.from_matrix(anndata.varm[key])
measurement.varm.set(darr)
measurement.set(measurement.varm)

if len(anndata.obsp.keys()) > 0: # do not create an empty collection
measurement.obsp.create()
for key in anndata.obsp.keys():
arr = SOMASparseNdArray(uri=f"{measurement.obsp.get_uri()}/{key}", ctx=ctx)
arr.from_matrix(anndata.obsp[key])
measurement.obsp.set(arr)
sarr = SOMASparseNdArray(uri=f"{measurement.obsp.get_uri()}/{key}", ctx=ctx)
sarr.from_matrix(anndata.obsp[key])
measurement.obsp.set(sarr)
measurement.set(measurement.obsp)

if len(anndata.varp.keys()) > 0: # do not create an empty collection
measurement.varp.create()
for key in anndata.varp.keys():
arr = SOMASparseNdArray(uri=f"{measurement.varp.get_uri()}/{key}", ctx=ctx)
arr.from_matrix(anndata.varp[key])
measurement.varp.set(arr)
sarr = SOMASparseNdArray(uri=f"{measurement.varp.get_uri()}/{key}", ctx=ctx)
sarr.from_matrix(anndata.varp[key])
measurement.varp.set(sarr)
measurement.set(measurement.varp)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down
4 changes: 2 additions & 2 deletions apis/python/src/tiledbsoma/soma_collection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, Dict, Iterator, List, Optional, Sequence
from typing import Any, Dict, Iterator, Optional, Sequence

import tiledb

Expand Down Expand Up @@ -148,7 +148,7 @@ def __repr__(self) -> str:
"""
return "\n".join(self._repr_aux())

def _repr_aux(self) -> List[str]:
def _repr_aux(self) -> Sequence[str]:
"""
Internal helper function for ``__repr__`` which is nesting-aware.
"""
Expand Down
7 changes: 4 additions & 3 deletions apis/python/src/tiledbsoma/soma_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SOMADataFrame(TileDBArray):

_shape: Optional[NTuple] = None
_is_sparse: Optional[bool]
_index_column_names: List[str]

def __init__(
self,
Expand Down Expand Up @@ -126,7 +127,7 @@ def __repr__(self) -> str:
"""
return "\n".join(self._repr_aux())

def _repr_aux(self) -> List[str]:
def _repr_aux(self) -> Sequence[str]:
if not self.exists():
return ["Unpopulated"]
lines = [
Expand All @@ -147,7 +148,7 @@ def __getattr__(self, name: str) -> Any:
elif name == "ndims":
return self._get_ndims()

def keys(self) -> List[str]:
def keys(self) -> Sequence[str]:
"""
Returns the names of the columns when read back as a dataframe. TODO: make it clear whether or not this will read back ``soma_rowid`` / ``soma_joinid``.
"""
Expand All @@ -171,7 +172,7 @@ def _get_ndims(self) -> int:
def get_indexed(self) -> bool:
return False

def get_index_column_names(self) -> List[str]:
def get_index_column_names(self) -> Sequence[str]:
return []

def read(
Expand Down
2 changes: 1 addition & 1 deletion apis/python/src/tiledbsoma/soma_dense_nd_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __repr__(self) -> str:
"""
return "\n".join(self._repr_aux())

def _repr_aux(self) -> List[str]:
def _repr_aux(self) -> Sequence[str]:
if not self.exists():
return ["Unpopulated"]
lines = [
Expand Down
10 changes: 5 additions & 5 deletions apis/python/src/tiledbsoma/soma_indexed_dataframe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Generator, Iterator, List, Optional, Sequence, TypeVar, Union
from typing import Any, Iterator, List, Optional, Sequence, TypeVar, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -151,7 +151,7 @@ def __repr__(self) -> str:
"""
return "\n".join(self._repr_aux())

def _repr_aux(self) -> List[str]:
def _repr_aux(self) -> Sequence[str]:
if not self.exists():
return ["Unpopulated"]
lines = [
Expand All @@ -172,7 +172,7 @@ def __getattr__(self, name: str) -> Any:
elif name == "ndims":
return self._get_ndims()

def keys(self) -> List[str]:
def keys(self) -> Sequence[str]:
"""
Returns the names of the columns when read back as a dataframe. TODO: make it clear whether or not this will read back ``soma_rowid`` / ``soma_joinid``.
"""
Expand All @@ -196,7 +196,7 @@ def _get_ndims(self) -> int:
def get_indexed(self) -> bool:
return False

def get_index_column_names(self) -> List[str]:
def get_index_column_names(self) -> Sequence[str]:
"""
Return index (dimension) column names.
"""
Expand Down Expand Up @@ -337,7 +337,7 @@ def read_as_pandas(
attrs: Optional[Sequence[str]] = None,
# to rename index to 'obs_id' or 'var_id', if desired, for anndata
id_column_name: Optional[str] = None,
) -> Generator:
) -> Iterator[pd.DataFrame]:
"""
For ``to_anndata``, as well as for any interactive use where the user wants a Pandas dataframe.
"""
Expand Down
4 changes: 2 additions & 2 deletions apis/python/src/tiledbsoma/soma_metadata_mapping.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Iterator, List
from typing import Any, Dict, Iterator, Sequence

# importing tiledbsoma.TileDBObject leads to a circular reference as TileDBObject imports us. This
# is, in turn, because this class requires a back-link to the underlying object -- hence,
Expand All @@ -12,7 +12,7 @@ class SOMAMetadataMapping:
def __init__(self, underlying: "tiledbsoma.TileDBObject"):
self._underlying = underlying

def keys(self) -> List[str]:
def keys(self) -> Sequence[str]:
"""
Returns the object's metadata keys as a list.
"""
Expand Down
4 changes: 2 additions & 2 deletions apis/python/src/tiledbsoma/soma_sparse_nd_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __repr__(self) -> str:
"""
return "\n".join(self._repr_aux())

def _repr_aux(self) -> List[str]:
def _repr_aux(self) -> Sequence[str]:
if not self.exists():
return ["Unpopulated"]
lines = [
Expand Down Expand Up @@ -527,7 +527,7 @@ def _ingest_data_cols_chunked(self, matrix: sp.csc_matrix) -> None:
while j < ncol:
t1 = time.time()
# Find a number of CSC columns which will result in a desired nnz for the chunk.
chunk_size = util.find_csc_chunk_size(
chunk_size = util_scipy.find_csc_chunk_size(
matrix, j, self._tiledb_platform_config.goal_chunk_nnz
)
j2 = j + chunk_size
Expand Down
5 changes: 4 additions & 1 deletion apis/python/src/tiledbsoma/tiledb_object.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from abc import ABC, abstractmethod
from typing import Optional, Union
from typing import Optional, Sequence, Union

import tiledb

Expand Down Expand Up @@ -66,6 +66,9 @@ def __repr__(self) -> str:
"""
return f"name={self._name},uri={self._uri}"

def _repr_aux(self) -> Sequence[str]:
raise Exception("Must be overridden by inherting classes.")

def get_name(self) -> str:
return self._name

Expand Down
6 changes: 3 additions & 3 deletions apis/python/src/tiledbsoma/util_arrow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Generator, Optional, Union
from typing import Any, Iterator, Optional, Union

import numpy as np
import pyarrow as pa
Expand All @@ -17,7 +17,7 @@ def tiledb_type_from_arrow_type(t: pa.DataType) -> Union[type, np.dtype]:
else:
# mypy says:
# Returning Any from function declared to return "type" [no-any-return]
return t.to_pandas_dtype() # type: ignore
return t.to_pandas_dtype()


def get_arrow_type_from_tiledb_dtype(tiledb_dtype: np.dtype) -> pa.DataType:
Expand Down Expand Up @@ -76,7 +76,7 @@ def ascii_to_unicode_pyarrow_readback(record_batch: pa.RecordBatch) -> pa.Record
return pa.RecordBatch.from_arrays(new_fields, names=names)


def concat_batches(batch_generator: Generator) -> pa.RecordBatch:
def concat_batches(batch_generator: Iterator[Any]) -> pa.RecordBatch:
"""
Iterates a generator of ``pyarrow.RecordBatch`` (e.g. ``SOMADataFrame.read``) and returns a concatenation of all the record batches found. The nominal use is to simply unit-test cases.
"""
Expand Down

0 comments on commit 6a4561e

Please sign in to comment.