Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphProfiler: add() NotImplementedError #636

Merged
merged 6 commits into from
Sep 19, 2022
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: 15 additions & 1 deletion dataprofiler/profilers/graph_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ def __init__(self, data, options=None):
"categorical_distribution": GraphProfiler._update_categorical_distribution,
}

def __add__(self, other):
"""
Merge two Graph profiles together overriding the `+` operator.

:param other: graph profile being added to this one.
:type other: GraphProfiler
:return: merger of the two profiles
:rtype: GraphProfiler
"""
raise NotImplementedError(
"profile adding is not currently supported for the GraphProfiler"
)

@property
def profile(self):
"""
Expand Down Expand Up @@ -339,11 +352,12 @@ def _get_continuous_distribution(self, graph, continuous_attributes):
mle = distribution.nnlf(fit, df)

if mle <= best_mle:
best_distrib = distribution
best_fit = distribution.name
best_mle = mle
best_fit_properties = fit

mean, variance, skew, kurtosis = distribution.stats(
mean, variance, skew, kurtosis = best_distrib.stats(
best_fit_properties, moments="mvsk"
)
properties = {
Expand Down
208 changes: 68 additions & 140 deletions dataprofiler/tests/profilers/test_graph_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def setUpClass(cls):
(2, 5, {"id": 5, "weight": 3.9, "value": 4.5}),
]
)

cls.expected_profile = dict(
num_nodes=4,
num_edges=5,
Expand All @@ -76,33 +77,6 @@ def setUpClass(cls):
"id": None,
"weight": {
"name": "lognorm",
"properties": {
"best_fit_properties": [
8.646041719759628,
1.6999999999999997,
0.19403886939727638,
],
"mean": [
8.646041719759628,
1.6999999999999997,
0.19403886939727638,
],
"variance": [
8.646041719759628,
1.6999999999999997,
0.19403886939727638,
],
"skew": [
0.6801760445870136,
1.5339299776947408,
4.540311271443057,
],
"kurtosis": [
0.6939591774450526,
3.5294117647058827,
30.921639662389307,
],
},
},
},
categorical_distribution={
Expand All @@ -127,6 +101,18 @@ def setUpClass(cls):
),
)

cls.expected_properties = {
"best_fit_properties": [
8.646041719759628,
1.6999999999999997,
0.19403886939727638,
],
"mean": [1.7085707836543698e16, 4.241852142820433, 1.0190038591415866],
"variance": [8.521811094505713e64, 305.76588081569196, 0.03984103474264823],
"skew": [4.987683961374356e48, 82.41830452500491, 0.5951548443693909],
"kurtosis": [7.262126433044066e129, 117436.2896499293, 0.6363254662738349],
}

cls.expected_diff_1 = {
"num_nodes": "unchanged",
"num_edges": 1,
Expand All @@ -142,53 +128,6 @@ def setUpClass(cls):
{},
{
"name": ["lognorm", "uniform"],
"properties": [
{},
{
"best_fit_properties": [
[
8.646041719759628,
1.6999999999999997,
0.19403886939727638,
],
[],
[1.7, 2.0999999999999996],
],
"mean": [
[
8.646041719759628,
1.6999999999999997,
0.19403886939727638,
],
[],
[1.7, 2.0999999999999996],
],
"variance": [
[
8.646041719759628,
1.6999999999999997,
0.19403886939727638,
],
[],
[1.7, 2.0999999999999996],
],
"skew": [
[0.6801760445870136, 4.540311271443057],
[1.5339299776947408],
[1.3801311186847085],
],
"kurtosis": [
[
0.6939591774450526,
3.5294117647058827,
30.921639662389307,
],
[],
[3.5294117647058822, 2.8571428571428577],
],
},
{},
],
},
{},
],
Expand Down Expand Up @@ -236,84 +175,27 @@ def setUpClass(cls):
"continuous_distribution": [
{},
{
"id": "unchanged",
"value": [
None,
{
"name": "uniform",
"properties": {
"best_fit_properties": [2.3, 5.0],
"mean": [2.3, 5.0],
"variance": [2.3, 5.0],
"skew": [1.3187609467915742, 0.8944271909999159],
"kurtosis": [2.608695652173913, 1.2],
},
},
],
"weight": [
{},
{
"name": ["uniform", "gamma"],
"properties": [
{},
{
"best_fit_properties": [
[1.7, 2.0999999999999996],
[],
[
0.24894970623537693,
1.1999999999999997,
11.973583222228775,
],
],
"mean": [
[1.7, 2.0999999999999996],
[],
[
0.24894970623537693,
1.1999999999999997,
11.973583222228775,
],
],
"variance": [
[1.7, 2.0999999999999996],
[],
[
0.24894970623537693,
1.1999999999999997,
11.973583222228775,
],
],
"skew": [
[1.5339299776947408, 1.3801311186847085],
[],
[
4.008428917954561,
1.8257418583505538,
0.5779868092477709,
],
],
"kurtosis": [
[3.5294117647058822, 2.8571428571428577],
[],
[
24.101253585441555,
5.000000000000001,
0.5011031274966287,
],
],
},
{},
],
},
{},
],
"id": "unchanged",
"value": [
None,
{
"name": "uniform",
},
],
},
{},
],
"categorical_distribution": [
{},
{
"weight": "unchanged",
"id": [
{},
{
Expand All @@ -326,7 +208,6 @@ def setUpClass(cls):
{"bin_counts": [1, 1, 2], "bin_edges": [4.0, 6.0, 8.0, 10.0]},
None,
],
"weight": "unchanged",
},
{},
],
Expand All @@ -342,6 +223,28 @@ def setUpClass(cls):
},
}

def check_continuous_properties(self, continuous_distribution_props):
"""
NOTE: this function is needed because github tests often lead result in
slightly different property values. Hence why assertAlmostEqual is used.

"""
for key in continuous_distribution_props:
for x, y in zip(
self.expected_properties[key], continuous_distribution_props[key]
):
self.assertAlmostEqual(x, y)

def test_add(self):
profile_1 = GraphProfiler(self.graph_1)
profile_2 = GraphProfiler(self.graph_2)

with self.assertRaises(
NotImplementedError,
msg="profile adding is not currently supported for the GraphProfiler",
):
profile_1 + profile_2

def test_profile(self):
# test_update
graph_profile = GraphProfiler(self.graph_1)
Expand All @@ -351,6 +254,13 @@ def test_profile(self):
# check that scale is almost equal
scale = profile.profile["continuous_distribution"]["weight"].pop("scale")
self.assertAlmostEqual(scale, -15.250985118262854)

# check that properties are almost equal
properties = profile.profile["continuous_distribution"]["weight"].pop(
"properties"
)
self.check_continuous_properties(properties)

self.assertDictEqual(self.expected_profile, profile.profile)

def test_report(self):
Expand All @@ -362,6 +272,13 @@ def test_report(self):
# check that scale is almost equal
scale = profile.profile["continuous_distribution"]["weight"].pop("scale")
self.assertAlmostEqual(scale, -15.250985118262854)

# check that properties are almost equal
properties = profile.profile["continuous_distribution"]["weight"].pop(
"properties"
)
self.check_continuous_properties(properties)

self.assertDictEqual(self.expected_profile, profile.report())

def test_graph_data_object(self):
Expand All @@ -374,6 +291,13 @@ def test_graph_data_object(self):
# check that scale is almost equal
scale = profile.profile["continuous_distribution"]["weight"].pop("scale")
self.assertAlmostEqual(scale, -15.250985118262854)

# check that properties are almost equal
properties = profile.profile["continuous_distribution"]["weight"].pop(
"properties"
)
self.check_continuous_properties(properties)

self.assertDictEqual(self.expected_profile, profile.profile)

def test_diff(self):
Expand All @@ -388,9 +312,13 @@ def test_diff(self):

# Remove scale because it causes rounding issues during the test
profile_1.profile["continuous_distribution"]["weight"].pop("scale")
profile_1.profile["continuous_distribution"]["weight"].pop("properties")
profile_2.profile["continuous_distribution"]["weight"].pop("scale")
profile_2.profile["continuous_distribution"]["weight"].pop("properties")
profile_3.profile["continuous_distribution"]["weight"].pop("scale")
profile_3.profile["continuous_distribution"]["weight"].pop("properties")
profile_3.profile["continuous_distribution"]["value"].pop("scale")
profile_3.profile["continuous_distribution"]["value"].pop("properties")

diff_1 = profile_1.diff(profile_2)
self.assertDictEqual(diff_1, self.expected_diff_1)
Expand Down