code: def _clean_data(X, return_idx=False):
      """
      Clean dataset **X** by removing unseen entities and relations from valid and test sets.
  
      Parameters
      ----------
      X: dict
          Dictionary containing the following keys: `train`, `valid`, `test`.
          Each key should contain a ndarray of shape [n, m] with m >= 3
          (> if weights are included in the array).
  
      return_idx: bool
          Whether to return the indices of the remaining rows in valid and test respectively.
  
      Returns
      -------
      filtered_X: dict
          Dictionary containing the following keys: `train`, `valid`, `test`.
          Each key contains a ndarray of shape [n, m].
          Valid and test do not contain entities or relations that are not present in train.
  
      valid_idx: ndarray
          Indices of the remaining rows of the `valid` dataset (with respect to the original `valid` ndarray).
  
      test_idx: ndarray
          Indices of the remaining rows of the `test` dataset (with respect to the original `test` ndarray).
  
      """
      filtered_X = {}
      train = pd.DataFrame(X["train"][:, :3], columns=["s", "p", "o"])
      filtered_X["train"] = X["train"]
  
      valid = pd.DataFrame(X["valid"][:, :3], columns=["s", "p", "o"])
      test = pd.DataFrame(X["test"][:, :3], columns=["s", "p", "o"])
  
      train_ent = np.unique(np.concatenate((train.s, train.o)))
      train_rel = train.p.unique()
  
      if "valid_negatives" in X:
          valid_negatives = pd.DataFrame(
              X["valid_negatives"][:, :3], columns=["s", "p", "o"]
          )
          valid_negatives_idx = (
              valid_negatives.s.isin(train_ent)
              & valid_negatives.o.isin(train_ent)
              & valid_negatives.p.isin(train_rel)
          )
          filtered_valid_negatives = valid_negatives[
              valid_negatives_idx
          ].values
          filtered_X["valid_negatives"] = filtered_valid_negatives
      if "test_negatives" in X:
          test_negatives = pd.DataFrame(
              X["test_negatives"][:, :3], columns=["s", "p", "o"]
          )
          test_negatives_idx = (
              test_negatives.s.isin(train_ent)
              & test_negatives.o.isin(train_ent)
              & test_negatives.p.isin(train_rel)
          )
          filtered_test_negatives = test[test_negatives_idx].values
          filtered_X["test_negatives"] = filtered_test_negatives
  
      valid_idx = (
          valid.s.isin(train_ent)
          & valid.o.isin(train_ent)
          & valid.p.isin(train_rel)
      )
      test_idx = (
          test.s.isin(train_ent)
          & test.o.isin(train_ent)
          & test.p.isin(train_rel)
      )
  
      # filtered_valid = valid[valid_idx].values
      # filtered_test = test[test_idx].values
      filtered_valid = X["valid"][valid_idx]
      filtered_test = X["test"][test_idx]
  
      filtered_X["valid"] = filtered_valid
      filtered_X["test"] = filtered_test
  
      if "mapper" in X:
          filtered_X["mapper"] = X["mapper"]
      if "test-human" in X and "test-human-ids" in X:
          filtered_X["test-human"] = X["test-human"]
          filtered_X["test-human-ids"] = X["test-human-ids"]
  
      if return_idx:
          return filtered_X, valid_idx, test_idx
      else:
          return filtered_X
  
imports: import hashlib
  import json
  import logging
  import os
  import urllib
  import zipfile
  from collections import namedtuple
  from pathlib import Path
  import numpy
  import pandas
label_desc: Use image node_id 1 for calling a global function with example usage: _clean_data(X, return_idx) and returns: filtered_X,  valid_idx,  test_idx, filtered_X