Much faster FormatStringEncoding
* Much faster iteration over a DataFrame's rows
* Remove unused import
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see
* Update napari/layers/utils/string_encoding.py
from string import Formatter
-from typing import (
- Any,
- Dict,
- Literal,
- Protocol,
- Sequence,
- Union,
- runtime_checkable,
-)
+from typing import Any, Literal, Protocol, Sequence, Union, runtime_checkable
import numpy as np
from pydantic import parse_obj_as
@@ -173,18 +165,14 @@ class FormatStringEncoding(_DerivedStyleEncoding[StringValue, StringArray]):
encoding_type: Literal['FormatStringEncoding'] = 'FormatStringEncoding'
def __call__(self, features: Any) -> StringArray:
+ feature_names = features.columns.to_list()
values = [
- self.format.format(**_get_feature_row(features, i))
- for i in range(len(features))
+ self.format.format(**dict(zip(feature_names, row)))
+ for row in features.itertuples(index=False, name=None)
]
return np.array(values, dtype=str)
-def _get_feature_row(features: Any, index: int) -> Dict[str, Any]:
- """Returns one row of the features table as a dictionary."""
- return {name: values.iloc[index] for name, values in features.items()}
-
-
def _is_format_string(string: str) -> bool:
"""Returns True if a string is a valid format string with at least one field, False otherwise."""
try:
