def timerange_filter(df: pd.DataFrame, timeranges: np.ndarray) -> pd.DataFrame: """\ Returns the filter that can be used to get all the rows of dataframe df where timestamp column falls in timeranges, a 2D NumPy array with start time in the first column and end time in the second column; both in miliseconds """ timeranges = pd.DataFrame(timeranges, columns=['from', 'to']) inds = timeranges.apply(lambda x: in_range(x, df), axis=1) return inds.T.any(axis=1)
def lr_pandas( spot_lr1: np.ndarray, spot_lr2: np.ndarray, neighbours: list, ) -> np.ndarray: """Calculate the lr scores for each spot. Parameters ---------- spot_lr1: pd.DataFrame Cells*Ligands spot_lr2: pd.DataFrame Cells*Receptors neighbours: list List of neighbours by indices for each spot. Returns ------- lr_scores: numpy.ndarray Cells*LR-scores. """ # function to calculate mean of lr2 expression between neighbours or within spot (distance==0) for each spot def mean_lr2(x): # get lr2 expressions from the neighbour(s) n_spots = neighbours[spot_lr2.index.tolist().index(x.name)] nbs = spot_lr2.loc[n_spots, :] if nbs.shape[0] > 0: # if neighbour exists return nbs.sum() / nbs.shape[0] else: return 0 # mean of lr2 expressions from neighbours of each spot nb_lr2 = spot_lr2.apply(mean_lr2, axis=1) # check whether neighbours exist try: nb_lr2.shape[1] except: raise ValueError("No neighbours found within given distance.") # keep value of nb_lr2 only when lr1 is also expressed on the spots spot_lr = pd.DataFrame( spot_lr1.values * (nb_lr2.values > 0) + (spot_lr1.values > 0) * nb_lr2.values, ).sum(axis=1) return spot_lr.values / 2