Example #1
0
 def set_profiles(self,
                  target_profile: DatasetProfile = None,
                  reference_profile: DatasetProfile = None):
     self._target_profile = target_profile
     self._reference_profile = reference_profile
     if self._target_profile:
         self._target_profile_json = message_to_json(
             self._target_profile.to_summary())
         if self._reference_profile:
             self._reference_profile_json = message_to_json(
                 self._reference_profile.to_summary())
def test_all_zeros_returns_summary_with_stats():
    stats = ("min", "max", "stddev", "mean")
    array = np.zeros([100, 1])

    prof = array_profile(array)
    msg = prof.to_summary()
    d = message_to_dict(msg)
    d1 = json.loads(message_to_json(msg))
    number_summary = d["columns"]["0"]["numberSummary"]
    missing_stats = [k for k in stats if k not in number_summary]
    if len(missing_stats) > 0:
        raise RuntimeError(f"Stats missing from number summary: {missing_stats}")

    assert d == d1
Example #3
0
def test_message_to_dict_equals_message_to_json():
    msg = DoublesMessage(min=0, max=1.0, sum=2.0, count=10)
    d1 = protobuf.message_to_dict(msg)
    d2 = json.loads(protobuf.message_to_json(msg))
    assert d1 == d2
Example #4
0
 def to_json(self) -> str:
     return message_to_json(self.to_protobuf())
Example #5
0
def profile_viewer(profiles: List[DatasetProfile] = None,
                   reference_profiles: List[DatasetProfile] = None,
                   output_path=None) -> str:
    """
    open a profile viewer loader on your default browser
    """
    try:
        from pybars import Compiler
    except ImportError as e:
        Compiler = None
        logger.debug(str(e))
        logger.warning(
            "Unable to load pybars; install pybars3 to load profile directly from the current session "
        )
        index_path = os.path.abspath(
            os.path.join(_MY_DIR, os.pardir, "viewer/templates", "index.html"))
        webbrowser.open_new_tab(f"file:{index_path}#")
        return None

    # create json output from profiles
    if profiles:
        if len(profiles) > 1:
            logger.warning(
                "More than one profile not implemented yet, default to first profile in the list "
            )
        profile_jsons = [
            message_to_json(each_prof.to_summary()) for each_prof in profiles
        ]
        if reference_profiles:
            reference_profile_jsons = [
                message_to_json(each_prof.to_summary())
                for each_prof in reference_profiles
            ]

    else:
        index_path = os.path.abspath(
            os.path.join(_MY_DIR, os.pardir, "viewer/templates", "index.html"))
        webbrowser.open_new_tab(f"file:{index_path}#")
        return None

    index_path = os.path.abspath(
        os.path.join(_MY_DIR, os.pardir, "viewer/templates",
                     "index-hbs-cdn-all-in.html"))

    with open(index_path, "r") as file_with_template:
        source = file_with_template.read()

    # compile templated files
    compiler = Compiler()
    template = compiler.compile(source)
    # replace handlebars for json profiles
    if reference_profiles:
        output_index = template({
            "profile_from_whylogs": profile_jsons[0],
            "reference_profile": reference_profile_jsons[0]
        })
    else:
        output_index = template({"profile_from_whylogs": profile_jsons[0]})

    if not output_path:
        output_path = tempfile.mkstemp(suffix=".html")[1]
    else:
        output_path = os.path.abspath(os.path.expanduser(output_path))
    with open(output_path, "w") as f:
        f.write(output_index)

    webbrowser.open_new_tab(f"file:{output_path}#")
    return output_path