Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions dataprofiler/profilers/profile_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import copy
import json
import logging
import os
import pickle
import random
import re
Expand All @@ -18,7 +19,7 @@
import numpy as np
import pandas as pd

from .. import data_readers, dp_logging
from .. import data_readers, dp_logging, settings
from ..data_readers.data import Data
from ..labelers.base_data_labeler import BaseDataLabeler
from ..labelers.data_labelers import DataLabeler
Expand Down Expand Up @@ -647,11 +648,22 @@ def clean_data_and_get_base_stats(
df_series = df_series.loc[true_sample_list]
total_na = total_sample_size - len(true_sample_list)

rng = np.random.default_rng(settings._seed)

if "DATAPROFILER_SEED" in os.environ and settings._seed is None:
seed = os.environ.get("DATAPROFILER_SEED")
if isinstance(seed, int):
rng = np.random.default_rng(int(seed))
else:
warnings.warn("Seed should be an integer", RuntimeWarning)

base_stats = {
"sample_size": total_sample_size,
"null_count": total_na,
"null_types": na_columns,
"sample": random.sample(list(df_series.values), min(len(df_series), 5)),
"sample": rng.choice(
list(df_series.values), (min(len(df_series), 5),), replace=False
).tolist(),
Comment on lines +651 to +666
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the big fix is here for making sampling consistent

"min_id": min_id,
"max_id": max_id,
}
Expand Down
116 changes: 59 additions & 57 deletions dataprofiler/tests/profilers/test_profile_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ def setUpClass(cls):
)

@mock.patch(
"dataprofiler.profilers.profile_builder." "ColumnPrimitiveTypeProfileCompiler"
"dataprofiler.profilers.profile_builder.ColumnPrimitiveTypeProfileCompiler"
)
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnStatsProfileCompiler")
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnDataLabelerCompiler")
@mock.patch("dataprofiler.profilers.profile_builder.ColumnStatsProfileCompiler")
@mock.patch("dataprofiler.profilers.profile_builder.ColumnDataLabelerCompiler")
@mock.patch(
"dataprofiler.profilers.profile_builder.DataLabeler", spec=StructuredDataLabeler
)
Expand All @@ -103,16 +103,15 @@ def test_bad_input_data(self, *mocks):
StructuredProfiler(data)

@mock.patch(
"dataprofiler.profilers.profile_builder." "ColumnPrimitiveTypeProfileCompiler"
"dataprofiler.profilers.profile_builder.ColumnPrimitiveTypeProfileCompiler"
)
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnStatsProfileCompiler")
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnDataLabelerCompiler")
@mock.patch("dataprofiler.profilers.profile_builder.ColumnStatsProfileCompiler")
@mock.patch("dataprofiler.profilers.profile_builder.ColumnDataLabelerCompiler")
@mock.patch(
"dataprofiler.profilers.profile_builder.DataLabeler", spec=StructuredDataLabeler
)
@mock.patch(
"dataprofiler.profilers.profile_builder."
"StructuredProfiler._update_correlation"
"dataprofiler.profilers.profile_builder.StructuredProfiler._update_correlation"
)
def test_list_data(self, *mocks):
data = [[1, 1], [None, None], [3, 3], [4, 4], [5, 5], [None, None], [1, 1]]
Expand All @@ -132,7 +131,7 @@ def test_list_data(self, *mocks):

# validates the sample out maintains the same visual data format as the
# input.
self.assertListEqual(["5", "1", "1", "3", "4"], profiler.profile[0].sample)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc consistent now, changed order

self.assertListEqual(["1", "4", "5", "1", "3"], profiler.profile[0].sample)

@mock.patch(
"dataprofiler.profilers.profile_builder." "ColumnPrimitiveTypeProfileCompiler"
Expand Down Expand Up @@ -1492,7 +1491,7 @@ def test_save_and_load_pkl_file(self):
save_profile.save()
mock_file.seek(0)
with mock.patch(
"dataprofiler.profilers.profile_builder." "DataLabeler",
"dataprofiler.profilers.profile_builder.DataLabeler",
return_value=data_labeler,
):
load_profile = dp.StructuredProfiler.load("mock.pkl")
Expand Down Expand Up @@ -1533,7 +1532,7 @@ def test_save_and_load_no_labeler(self):
save_profile.save()

mock_file.seek(0)
with mock.patch("dataprofiler.profilers.profile_builder." "DataLabeler"):
with mock.patch("dataprofiler.profilers.profile_builder.DataLabeler"):
load_profile = dp.StructuredProfiler.load("mock.pkl")

# Check that reports are equivalent
Expand All @@ -1546,7 +1545,7 @@ def test_save_and_load_no_labeler(self):
load_profile.update_profile(pd.DataFrame({"a": [4, 5]}))

@mock.patch(
"dataprofiler.profilers.data_labeler_column_profile." "DataLabelerColumn.update"
"dataprofiler.profilers.data_labeler_column_profile.DataLabelerColumn.update"
)
@mock.patch(
"dataprofiler.profilers.profile_builder.DataLabeler",
Expand Down Expand Up @@ -1579,9 +1578,9 @@ def test_save_json_file(self, *mocks):

# Save and Load profile with Mock IO
with mock.patch("builtins.open") as mock_open, mock.patch(
"dataprofiler.profilers.profile_builder." "datetime"
) as time_out:
time_out.now().strftime.return_value = "now"
"dataprofiler.profilers.profile_builder.datetime"
) as mock_pb_datetime:
mock_pb_datetime.now().strftime.return_value = "now"
mock_file = setup_save_mock_string_open(mock_open)
save_profile.save("output/mock.json", "json")
mock_file.seek(0)
Expand Down Expand Up @@ -1624,9 +1623,9 @@ def test_save_json_file(self, *mocks):

# do a second call without a specified file path
with mock.patch("builtins.open") as mock_open, mock.patch(
"dataprofiler.profilers.profile_builder." "datetime"
) as time_out:
time_out.now().strftime.return_value = "now"
"dataprofiler.profilers.profile_builder.datetime"
) as mock_pb_datetime:
mock_pb_datetime.now().strftime.return_value = "now"
setup_save_mock_string_open(mock_open)
save_profile.save(save_method="json")

Expand Down Expand Up @@ -2404,7 +2403,17 @@ def test_json_decode(self, mock_utils_DataLabeler, mock_DataLabeler, *mocks):
mock_DataLabeler.return_value = mock_labeler

fake_profile_name = None
expected_profile = StructuredProfiler(fake_profile_name)
profile_options = dp.ProfilerOptions()
Copy link
Contributor Author

@JGSweets JGSweets Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make things faster here and enable missing stats

profile_options.set(
{
"correlation.is_enabled": True,
"null_replication_metrics.is_enabled": True,
"multiprocess.is_enabled": False,
}
)
expected_profile = StructuredProfiler(
fake_profile_name, options=profile_options
)

serialized = json.dumps(expected_profile, cls=ProfileEncoder)
deserialized = load_profiler(json.loads(serialized))
Expand Down Expand Up @@ -2440,7 +2449,17 @@ def test_json_decode_after_update(
"pred": [],
"conf": [[1, 1], [0, 0]],
}
expected_profile = StructuredProfiler(fake_profile_name)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make things faster here and enable missing stats

profile_options = dp.ProfilerOptions()
profile_options.set(
{
"correlation.is_enabled": True,
"null_replication_metrics.is_enabled": True,
"multiprocess.is_enabled": False,
}
)
expected_profile = StructuredProfiler(
fake_profile_name, options=profile_options
)

with test_utils.mock_timeit():
expected_profile.update_profile(df_structured)
Expand Down Expand Up @@ -2471,15 +2490,11 @@ def test_json_decode_after_update(
]
)

deserialized.update_profile(df_structured)
with test_utils.mock_timeit():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mock to ensure times the same

deserialized.update_profile(df_structured)
expected_profile.update_profile(df_structured)

assert deserialized.total_samples == 5
assert deserialized._max_col_samples_used == 5
assert deserialized._min_col_samples_used == 5
assert deserialized.row_has_null_count == 2
assert deserialized.row_is_null_count == 1
assert deserialized._get_unique_row_ratio() == 0.80
assert deserialized.file_type == "<class 'pandas.core.frame.DataFrame'>"
test_utils.assert_profiles_equal(deserialized, expected_profile)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now just validate that both update separately get same values

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice -- I like



class TestStructuredColProfilerClass(unittest.TestCase):
Expand Down Expand Up @@ -2537,19 +2552,9 @@ def test_base_props(self):
self.assertEqual(2999 * 3, src_profile.sample_size)

@mock.patch(
"dataprofiler.profilers.column_profile_compilers."
"ColumnPrimitiveTypeProfileCompiler"
)
@mock.patch(
"dataprofiler.profilers.column_profile_compilers." "ColumnStatsProfileCompiler"
)
@mock.patch(
"dataprofiler.profilers.column_profile_compilers." "ColumnDataLabelerCompiler"
)
@mock.patch(
"dataprofiler.profilers.profile_builder."
"StructuredProfiler._update_correlation"
"dataprofiler.profilers.column_profile_compilers.BaseCompiler.update_profile"
)
@mock.patch("dataprofiler.profilers.data_labeler_column_profile.DataLabeler")
def test_add_profilers(self, *mocks):
data = pd.Series([1, None, 3, 4, 5, None])
profile1 = StructuredColProfiler(data[:2])
Expand Down Expand Up @@ -2654,7 +2659,7 @@ def test_clean_data_and_get_base_stats(self, *mocks):
self.assertTrue(np.issubdtype(np.object_, df_series.dtype))
self.assertDictEqual(
{
"sample": ["4.0", "6.0", "3.0"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc consistent now, changed order

"sample": ["6.0", "3.0", "4.0"],
"sample_size": 5,
"null_count": 2,
"null_types": dict(nan=["e", "b"]),
Expand All @@ -2671,7 +2676,7 @@ def test_clean_data_and_get_base_stats(self, *mocks):
)
self.assertDictEqual(
{
"sample": ["nan", "6.0", "4.0", "nan"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc consistent now, changed order

"sample": ["6.0", "nan", "nan", "4.0"],
"sample_size": 6,
"null_count": 2,
"null_types": {"1.0": ["a"], "3.0": ["c"]},
Expand All @@ -2688,7 +2693,7 @@ def test_clean_data_and_get_base_stats(self, *mocks):
)
self.assertDictEqual(
{
"sample": ["3.0", "4.0", "6.0", "nan", "1.0"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc consistent now, changed order

"sample": ["3.0", "4.0", "nan", "6.0", "nan"],
"sample_size": 6,
"null_count": 0,
"null_types": {},
Expand Down Expand Up @@ -2808,11 +2813,9 @@ def test_sample_size_passed_to_profile(self, *mocks):
self.assertEqual(10000, update_mock.call_args[0][1])

@mock.patch(
"dataprofiler.profilers.profile_builder." "ColumnPrimitiveTypeProfileCompiler"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed and now faster

"dataprofiler.profilers.column_profile_compilers.BaseCompiler.update_profile"
)
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnStatsProfileCompiler")
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnDataLabelerCompiler")
@mock.patch("dataprofiler.profilers.profile_builder.DataLabeler")
@mock.patch("dataprofiler.profilers.data_labeler_column_profile.DataLabeler")
def test_index_overlap_for_update_profile(self, *mocks):
data = pd.Series([0, None, 1, 2, None])
profile = StructuredColProfiler(data)
Expand All @@ -2827,11 +2830,9 @@ def test_index_overlap_for_update_profile(self, *mocks):
self.assertDictEqual(profile.null_types_index, {"nan": {1, 4, 6, 9}})

@mock.patch(
"dataprofiler.profilers.profile_builder." "ColumnPrimitiveTypeProfileCompiler"
"dataprofiler.profilers.column_profile_compilers.BaseCompiler.update_profile"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed and now faster

)
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnStatsProfileCompiler")
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnDataLabelerCompiler")
@mock.patch("dataprofiler.profilers.profile_builder.DataLabeler")
@mock.patch("dataprofiler.profilers.data_labeler_column_profile.DataLabeler")
def test_index_overlap_for_merge(self, *mocks):
data = pd.Series([0, None, 1, 2, None])
profile1 = StructuredColProfiler(data)
Expand All @@ -2852,11 +2853,9 @@ def test_index_overlap_for_merge(self, *mocks):
self.assertDictEqual(profile2.null_types_index, {"nan": {1, 4}})

@mock.patch(
"dataprofiler.profilers.profile_builder." "ColumnPrimitiveTypeProfileCompiler"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed and now faster

"dataprofiler.profilers.column_profile_compilers.BaseCompiler.update_profile"
)
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnStatsProfileCompiler")
@mock.patch("dataprofiler.profilers.profile_builder." "ColumnDataLabelerCompiler")
@mock.patch("dataprofiler.profilers.profile_builder.DataLabeler")
@mock.patch("dataprofiler.profilers.data_labeler_column_profile.DataLabeler")
def test_min_max_id_properly_update(self, *mocks):
data = pd.Series([1, None, 3, 4, 5, None, 1])
profile1 = StructuredColProfiler(data[:2])
Expand All @@ -2879,10 +2878,13 @@ def test_min_max_id_properly_update(self, *mocks):
self.assertEqual(0, profile._min_id)
self.assertEqual(6, profile._max_id)

@mock.patch("dataprofiler.profilers.data_labeler_column_profile.DataLabeler")
@mock.patch(
"dataprofiler.profilers.data_labeler_column_profile.DataLabelerColumn.update"
)
@mock.patch(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed and now faster

"dataprofiler.profilers.data_labeler_column_profile.DataLabeler",
spec=BaseDataLabeler,
)
@mock.patch(
"dataprofiler.profilers.column_profile_compilers."
"ColumnPrimitiveTypeProfileCompiler.diff"
Expand Down Expand Up @@ -3015,7 +3017,7 @@ def test_json_encode_after_update(self, mock_DataLabeler, *mocks):
"_sampling_ratio": 0.2,
"_min_true_samples": 0,
"sample_size": 4,
"sample": ["1", "2", "-2"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc consistent now, changed order

"sample": ["2", "-2", "1"],
"null_count": 1,
"null_types": ["Nan"],
"null_types_index": {
Expand Down