Skip to content

Commit 2775996

Browse files
committed
fixed some errors
1 parent b25f06b commit 2775996

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

src/cell2sentence4longevity/explore.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def extract_fields_from_h5ad(
142142
requested_fields = fields if fields is not None else obs_columns
143143
preloaded_fields = preload_complex_obs_fields(
144144
obs_group=obs_group,
145-
fields=[field for field in requested_fields if field in obs_columns]
145+
fields=[field for field in requested_fields if field in obs_columns],
146+
total_rows=n_cells
146147
)
147148
if preloaded_fields:
148149
action.log(

src/cell2sentence4longevity/preprocessing/obs_stream.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,40 @@ def _fill_string_array(arr: np.ndarray, fill_value: str) -> np.ndarray:
108108
return arr
109109

110110

111-
def _normalize_preloaded_column(values: Any) -> np.ndarray | list[Any]:
111+
def _to_string_list(values: np.ndarray) -> list[Any]:
112+
result: list[Any] = []
113+
for value in values.tolist():
114+
if value is None:
115+
result.append(None)
116+
else:
117+
result.append(str(value))
118+
return result
119+
120+
121+
def _normalize_preloaded_column(values: Any, total_rows: int) -> np.ndarray:
112122
if isinstance(values, np.ndarray):
123+
if values.ndim == 0:
124+
filled = np.full(total_rows, _decode_scalar(values.item()), dtype=object)
125+
return filled
113126
return values
114127
if hasattr(values, "to_numpy"):
115-
return values.to_numpy()
128+
arr = values.to_numpy()
129+
arr = np.asarray(arr, dtype=object)
130+
if arr.ndim == 0:
131+
arr = np.full(total_rows, _decode_scalar(arr.item()), dtype=object)
132+
return arr
116133
if hasattr(values, "to_list"):
117-
return values.to_list()
118-
if hasattr(values, "tolist"):
119-
return values.tolist()
120-
return np.asarray(values, dtype=object)
134+
arr = np.asarray(values.to_list(), dtype=object)
135+
elif hasattr(values, "tolist"):
136+
arr = np.asarray(values.tolist(), dtype=object)
137+
else:
138+
try:
139+
arr = np.asarray(values, dtype=object)
140+
except Exception:
141+
arr = np.array([values], dtype=object)
142+
if arr.ndim == 0:
143+
arr = np.full(total_rows, _decode_scalar(arr.item()), dtype=object)
144+
return arr
121145

122146

123147
def _slice_preloaded_column(
@@ -171,8 +195,8 @@ def read_obs_chunk_dict(
171195
if is_string_field:
172196
if string_fill_value is not None:
173197
values = _fill_string_array(values, string_fill_value)
174-
values_list = values.tolist()
175-
values = pl.Series(field, values_list, dtype=pl.String)
198+
values_list = _to_string_list(values)
199+
values = pl.Series(field, values_list, dtype=pl.String, strict=False)
176200
if as_lists:
177201
if isinstance(values, np.ndarray):
178202
values = values.tolist()
@@ -208,9 +232,10 @@ def build_obs_chunk_dataframe(
208232

209233
def preload_complex_obs_fields(
210234
obs_group: h5py.Group,
211-
fields: list[str]
212-
) -> dict[str, np.ndarray | list[Any]]:
213-
preloaded: dict[str, np.ndarray | list[Any]] = {}
235+
fields: list[str],
236+
total_rows: int
237+
) -> dict[str, np.ndarray]:
238+
preloaded: dict[str, np.ndarray] = {}
214239
for field in fields:
215240
node = obs_group.get(field)
216241
if node is None:
@@ -224,7 +249,7 @@ def preload_complex_obs_fields(
224249
except Exception as exc:
225250
msg = f"Failed to preload obs field '{field}': {exc}"
226251
raise RuntimeError(msg) from exc
227-
preloaded[field] = _normalize_preloaded_column(values)
252+
preloaded[field] = _normalize_preloaded_column(values, total_rows)
228253
return preloaded
229254

230255

0 commit comments

Comments
 (0)