Skip to content
Open
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
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ xfail_strict = true
# convert warnings to errors, except for ImportWarnings in numpy (needed for pp38)
# FIXME: eventually we should be able to remove the numpy exception
# TODO: need to also move away from `user_array.container`: https://github.com/alexlancaster/pypop/issues/358
filterwarnings = ["error", "default::ImportWarning", "ignore:.*Arlequin.*deprecated.*",
"ignore:The numpy\\.lib\\.user_array\\.container class is deprecated:DeprecationWarning"]
filterwarnings = ["error", "default::ImportWarning", "ignore:.*Arlequin.*deprecated.*"]
log_cli_level = "INFO"
testpaths = [
"website/docs",
Expand Down
24 changes: 18 additions & 6 deletions src/PyPop/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
import shutil
import stat
import sys
from collections.abc import Sequence
from pathlib import Path

import numpy as np
from numpy import asarray, take, zeros
from numpy.lib.user_array import container

from PyPop import logger

Expand Down Expand Up @@ -217,12 +217,12 @@ def tagContents(self, tagname, content, **kw): # noqa: D417
self.closetag(tagname)


class StringMatrix(container):
class StringMatrix(Sequence):
"""Matrix of strings and other metadata from input file to PyPop.

``StringMatrix`` is a subclass of NumPy's
:class:`numpy.lib.user_array` class, store the data in an
efficient array format, using NumPy-style access.
``StringMatrix`` is a subclass of
:class:`collections.abc.Sequence` class, which stores the data in
an efficient array format, using NumPy-style access.

Args:
rowCount (int): number of rows in matrix
Expand Down Expand Up @@ -264,8 +264,9 @@ def __init__(
self.array = zeros(
(self.rowCount, self.colCount * 2 + self.extraCount), dtype="O"
)
self.dtype = self.array.dtype
self.shape = self.array.shape
self._typecode = self.array.dtype # Numeric array.typecode()
self._typecode = self.array.dtype
self.name = str(self.__class__).split()[0]

def __repr__(self):
Expand All @@ -286,6 +287,17 @@ def __repr__(self):
return (self.__class__.__name__) + repr(self.array)[len("array") :]
return (self.__class__.__name__) + "(" + repr(self.array) + ")"

def __len__(self):
"""Get number of rows (individuals) in the matrix.

This allows StringMatrix instances to be used with `len()`,
iteration, and other Python sequence protocols.

Returns:
int: number of rows in the matrix
"""
return self.array.shape[0]

def dump(self, locus=None, stream=sys.stdout):
"""Write file to a stream in original format.

Expand Down
Loading