(func, ids, *args, N=10, id_dtype=np.int32, score_dtype=np.float32, **kwargs)
| 104 | |
| 105 | |
| 106 | def _batch_call(func, ids, *args, N=10, id_dtype=np.int32, score_dtype=np.float32, **kwargs): |
| 107 | # we're running in batch mode, just loop over each item and call the scalar version of the |
| 108 | # function |
| 109 | output_ids = np.zeros((len(ids), N), dtype=id_dtype) |
| 110 | output_scores = np.zeros((len(ids), N), dtype=score_dtype) |
| 111 | |
| 112 | user_items = kwargs.pop("user_items") if "user_items" in kwargs else None |
| 113 | item_users = kwargs.pop("item_users") if "item_users" in kwargs else None |
| 114 | |
| 115 | # pylint: disable=unsubscriptable-object |
| 116 | for i, idx in enumerate(ids): |
| 117 | current_kwargs = kwargs |
| 118 | if user_items is not None: |
| 119 | current_kwargs = {"user_items": user_items[i], **kwargs} |
| 120 | elif item_users is not None: |
| 121 | current_kwargs = {"item_users": item_users[i], **kwargs} |
| 122 | |
| 123 | batch_ids, batch_scores = func(idx, *args, N=N, **current_kwargs) |
| 124 | |
| 125 | # pad out to N items if we're returned fewer |
| 126 | missing_items = N - len(batch_ids) |
| 127 | if missing_items > 0: |
| 128 | batch_ids = np.append(batch_ids, np.full(missing_items, -1)) |
| 129 | batch_scores = np.append( |
| 130 | batch_scores, np.full(missing_items, -np.finfo(np.float32).max) |
| 131 | ) |
| 132 | |
| 133 | output_ids[i] = batch_ids[:N] |
| 134 | output_scores[i] = batch_scores[:N] |
| 135 | |
| 136 | return output_ids, output_scores |
| 137 | |
| 138 | |
| 139 | def _filter_items_from_results(queryid, ids, scores, filter_items, N): |
no outgoing calls
no test coverage detected