ENH: make LabelArray.from_codes_and_metadata public

This commit is contained in:
Joe Jevnik
2017-02-28 23:00:54 -05:00
parent b4ed1fcff3
commit a8d6ecd545
2 changed files with 22 additions and 17 deletions
+3 -9
View File
@@ -1,7 +1,7 @@
"""
Factorization algorithms.
"""
from libc.math cimport floor
from libc.math cimport floor, log
cimport numpy as np
import numpy as np
@@ -10,14 +10,8 @@ from zipline.utils.numpy_utils import unsigned_int_dtype_with_size_in_bytes
np.import_array()
IF UNAME_SYSNAME == "Windows":
# msvc doesn't define log2
from libc.math cimport log
cdef double log2(double d):
return log(d) / log(2);
ELSE:
from libc.math cimport log2
cdef inline double log2(double d):
return log(d) / log(2);
ctypedef fused unsigned_integral:
+19 -8
View File
@@ -176,7 +176,7 @@ class LabelArray(ndarray):
)
categories.setflags(write=False)
return cls._from_codes_and_metadata(
return cls.from_codes_and_metadata(
codes=codes.reshape(values.shape),
categories=categories,
reverse_categories=reverse_categories,
@@ -184,13 +184,24 @@ class LabelArray(ndarray):
)
@classmethod
def _from_codes_and_metadata(cls,
codes,
categories,
reverse_categories,
missing_value):
def from_codes_and_metadata(cls,
codes,
categories,
reverse_categories,
missing_value):
"""
View codes as a LabelArray and set LabelArray metadata on the result.
Rehydrate a LabelArray from the codes and metadata.
Parameters
----------
codes : np.ndarray[integral]
The codes for the label array.
categories : np.ndarray[object]
The unique string categories.
reverse_categories : dict[str, int]
The mapping from category to its code-index.
missing_value : any
The value used to represent missing data.
"""
ret = codes.view(type=cls, dtype=np.void)
ret._categories = categories
@@ -517,7 +528,7 @@ class LabelArray(ndarray):
Make an empty LabelArray with the same categories as ``self``, filled
with ``self.missing_value``.
"""
return type(self)._from_codes_and_metadata(
return type(self).from_codes_and_metadata(
codes=np.full(
shape,
self.reverse_categories[self.missing_value],