class_docstr: Compiler for Duplication Orphan Check
  
  Param:
      package (str, Optional): component package name
      module (str, Optional): component module name
      class (str): component class name
  
  Attr:
      data (str): path to dataframe file
      duplication_rule (dict): a dict which represents the rule, of which there are following keys:
                              - keys (list of str): the list of columns that the duplication is checked on.
                                               Default is None and duplication is checking on all columns.
                              - to_file (str): the filepath that duplicate-removed dataframe dumps to.
                                               Default is None and no save back,
      orphan_rules (dict): a dict which represents the rule, of which there are following keys:
                              - rules: a list of dict object, which of each object is a rule and has following keys:
                                      - local_key (str): column name in current data frame
                                      - foreign_data (str): foreign file path
                                      - foreign_key (str): column name in the foreign file
                              - to_file (str): filepath to dump the orphan-droped data frame.
                                               Default is None and no save back.
  
  Example:
      "component": {
          "package": "xai",
          "module": "compiler",
          "class": "DuplicationOrphanCheck",
          "attr": {
            "data": "./student.csv",
            "duplication_rule": {
              "keys": [
                "STUDENT_ID"
              ],
              "to_file": "./data/drop_student.csv"
            },
            "orphan_rules": {
                "rules":[
                  {
                    "local_key": "STUDENT_ID",
                    "foreign_data": "./course.csv",
                    "foreign_key": "STUDENT_ID"
                  }
                ],
                "to_file": "./data/drop_student_2.csv"
            }
          }
        }
class_docstr_tok: ["Compiler","for","Duplication","Orphan","Check","Param",":","package","(","str",",","Optional",")",":","component","package","name","module","(","str",",","Optional",")",":","component","module","name","class","(","str",")",":","component","class","name","Attr",":","data","(","str",")",":","path","to","dataframe","file","duplication_rule","(","dict",")",":","a","dict","which","represents","the","rule",",","of","which","there","are","following","keys",":","-","keys","(","list","of","str",")",":","the","list","of","columns","that","the","duplication","is","checked","on",".","Default","is","None","and","duplication","is","checking","on","all","columns",".","-","to_file","(","str",")",":","the","filepath","that","duplicate-removed","dataframe","dumps","to",".","Default","is","None","and","no","save","back",",","orphan_rules","(","dict",")",":","a","dict","which","represents","the","rule",",","of","which","there","are","following","keys",":","-","rules",":","a","list","of","dict","object",",","which","of","each","object","is","a","rule","and","has","following","keys",":","-","local_key","(","str",")",":","column","name","in","current","data","frame","-","foreign_data","(","str",")",":","foreign","file","path","-","foreign_key","(","str",")",":","column","name","in","the","foreign","file","-","to_file","(","str",")",":","filepath","to","dump","the","orphan-droped","data","frame",".","Default","is","None","and","no","save","back",".","Example",":","``","component","''",":","{","``","package","''",":","``","xai","''",",","``","module","''",":","``","compiler","''",",","``","class","''",":","``","DuplicationOrphanCheck","''",",","``","attr","''",":","{","``","data","''",":","``",".\/student.csv","''",",","``","duplication_rule","''",":","{","``","keys","''",":","[","``","STUDENT_ID","''","]",",","``","to_file","''",":","``",".\/data\/drop_student.csv","''","}",",","``","orphan_rules","''",":","{","``","rules","''",":","[","{","``","local_key","''",":","``","STUDENT_ID","''",",","``","foreign_data","''",":","``",".\/course.csv","''",",","``","foreign_key","''",":","``","STUDENT_ID","''","}","]",",","``","to_file","''",":","``",".\/data\/drop_student_2.csv","''","}","}","}"]
docstr: Execution
  
  Args:
      report (Report): report object
      level (int): content level
docstr_tok: ["Execution","Args",":","report","(","Report",")",":","report","object","level","(","int",")",":","content","level"]
code: def __call__(self, report: Report, level: int):
      """
      Execution
  
      Args:
          report (Report): report object
          level (int): content level
      """
      super(DuplicationOrphanCheck, self).__call__(
          report=report, level=level
      )
      # -- Load Parameters --
      data_var = self.assert_attr(key="data")
      duplication_rule = self.assert_attr(
          key="duplication_rule", default=None
      )
      orphan_rules = self.assert_attr(key="orphan_rules")
  
      df = None
      # -- Load Data --
      if not (data_var is None):
          df = self.load_data(data_var, header=True)
  
      # -- Information about Raw Dataframe --
      self.report.detail.add_key_value_pairs(
          info_list=[("Total number of raw samples", df.shape[0])],
          notes="Raw Data Quantity",
      )
  
      # -- Initialize DataframeValidator --
      dv_processor = DataframeValidator()
  
      # -- Duplication Check about Raw Dataframe --
      if "keys" in duplication_rule:
          duplication_keys = duplication_rule["keys"]
      else:
          duplication_keys = None
      if "to_file" in duplication_rule:
          duplication_to_file = duplication_rule["to_file"]
      else:
          duplication_to_file = None
  
      duplicated_indices = dv_processor.duplication_check(
          df, duplication_keys
      )
      drop_indices = list(
          itertools.chain.from_iterable(
              [l[1:] for l in duplicated_indices]
          )
      )
  
      self.report.detail.add_key_value_pairs(
          info_list=[
              (
                  "Total number of samples after duplication check on %s"
                  % (
                      duplication_keys
                      if duplication_keys is not None
                      else "all columns"
                  ),
                  df.shape[0] - len(drop_indices),
              )
          ],
          notes="Duplication Check",
      )
      duplicate_dropped_df = df.drop(index=drop_indices)
  
      if duplication_to_file is not None:
          duplicate_dropped_df.to_csv(duplication_to_file)
  
      # -- Orphan Relation Check about Duplicated Data --
      orphan_check_info = []
      if "rules" in orphan_rules:
          rules = orphan_rules["rules"]
      else:
          rules = []
  
      if "to_file" in orphan_rules:
          orphan_to_filepath = orphan_rules["to_file"]
      else:
          orphan_to_filepath = None
  
      all_orphaned_index = set()
  
      for rule in rules:
          local_key = rule["local_key"]
          foreign_var = rule["foreign_data"]
          foreign_key = rule["foreign_key"]
  
          foreign_df = None
          if not (foreign_var is None):
              foreign_df = self.load_data(foreign_var, header=True)
  
          orphan_indices = dv_processor.orphaned_relation_check(
              df_a=duplicate_dropped_df,
              df_b=foreign_df,
              col_a=local_key,
              col_b=foreign_key,
          )
  
          all_orphaned_index.update(set(orphan_indices))
  
          orphan_check_info.append(
              (
                  "[%s] not in [%s of %s]"
                  % (local_key, foreign_key, foreign_var),
                  len(orphan_indices),
              )
          )
  
      if len(orphan_check_info) > 0:
          self.report.detail.add_key_value_pairs(
              info_list=orphan_check_info,
              notes="Orphaned Relation Check",
          )
  
      orphan_dropped_df = duplicate_dropped_df.drop(
          index=list(all_orphaned_index)
      )
  
      if orphan_to_filepath is not None:
          orphan_dropped_df.to_csv(orphan_to_filepath)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import itertools
  from collections import defaultdict
  from copy import deepcopy
  from xai.compiler.base import Dict2Obj
  from xai.data.constants import DATATYPE
  from xai.data.exceptions import AnalyzerDataTypeNotSupported
  from xai.data.explorer import CategoricalDataAnalyzer, NumericDataAnalyzer, TextDataAnalyzer, DatetimeDataAnalyzer
  from xai.data.explorer import CategoricalStats, NumericalStats, TextStats, DatetimeStats
  from xai.data.validator.dataframe_validator import DataframeValidator
  from xai.formatter import Report
label_desc: Use image node_id 2 for calling the DuplicationOrphanCheck obj's underlying member method code with example usage: obj.__call__(report, level) without return types