You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In create_mapping, there's a couple numpy calls which would be faster (it won't impact total running time much, matter of principle...):
defcreate_mapping(
src_idx: Any, tgt_idx: Any, nsrc: int, ntgt: int, operator: str
) ->Tuple[csr_matrix, NDArray[np.int_]]:
""" Create a mapping from source indexes to target indexes by constructing a sparse matrix of size (ntgt x nsrc) and creates a mask array with 0 for mapped entries and 1 otherwise. The mask allows to update the target array without overwriting the unmapped entries with zeroes: target = mask * target + mapping * source Parameters ---------- src_idx : int The indexes in the source array, zero-based tgt_idx : int The indexes in the target array, zero-based nsrc : int The number of entries in the source array ntgt : int The number of entries in the target array operator : str Indicating how n-1 mappings should be dealt with: "avg" for average, "sum" for sum. Operator does not affect 1-n couplings. Returns ------- Tuple containing the mapping (csr_matrix) and a mask (numpy array) """ifoperator=="avg":
cnt=np.zeros(max(tgt_idx) +1)
foriinrange(len(tgt_idx)):
cnt[tgt_idx[i]] +=1dat=np.array([1.0/cnt[xx] forxxintgt_idx])
elifoperator=="sum":
dat=np.ones(tgt_idx.shape)
else:
raiseValueError("`operator` should be either 'sum' or 'avg'")
map_out=csr_matrix((dat, (tgt_idx, src_idx)), shape=(ntgt, nsrc))
mask=np.array([0ifi>0else1foriinmap_out.getnnz(axis=1)])
returnmap_out, mask
In create_mapping, there's a couple numpy calls which would be faster (it won't impact total running time much, matter of principle...):
This can be simplified:
into:
The mask can be constructed without a loop:
mask = np.array([0 if i > 0 else 1 for i in map_out.getnnz(axis=1)])
I think this is likely the most efficient:
mask = np.minimum(map_out.getnnz(axis=1), 1)
.The text was updated successfully, but these errors were encountered: