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

Update custom constraint workflow #1169

Closed
amontanez24 opened this issue Jan 10, 2023 · 0 comments
Closed

Update custom constraint workflow #1169

amontanez24 opened this issue Jan 10, 2023 · 0 comments
Assignees
Labels
feature request Request for a new feature
Milestone

Comments

@amontanez24
Copy link
Contributor

amontanez24 commented Jan 10, 2023

Problem Description

As a user, it would be helpful to treat my custom constraints similarly to normal constraint classes, while also having a clear way to distinguish them.

Acceptance criteria

  • Change the name of create_custom_constraint to create_custom_constraint_class
  • Add a method called load_custom_constraint_classes
    • Parameters
      • filepath: Path to file where custom constraints are defined
      • class_names: The names of the constraint classes to import from that file
    • The method should import the constraint class objects from that file as the name provided in the class_names list
    • Error:
      • Ensure that the constraint_name isn't already defined in the module
        Error: The constraint 'MyConstraintClass' already exists. Please choose a different 'constraint_name'.

Expected behavior

In one file, create the constraint classes

# Example: Defining a custom constraint
def is_valid_fn():
  pass

def my_transform_fn():
  pass

Constraint1 = create_custom_constraint_class(
  is_valid_fn=my_is_valid_fn,
  transform_fn=my_transform_fn,
  reverse_transform_fn=my_reverse_fn,
)

Constraint2 = create_custom_constraint_class(
  is_valid_fn=my_is_valid_fn,
  transform_fn=my_transform_fn,
  reverse_transform_fn=my_reverse_fn,
)

In your script

# separate file: creating a constraint
from sdv.single_table import GaussianCopulaSynthesizer

synthesizer = GaussianCopulaSynthesizer(metadata)


# workaround for
# from foldername.my_custom_constraint import Constraint1, Constraint2
synthesizer.load_custom_constraint_classes(
   filepath='foldername/my_custom_constraint.py',
   class_names=['Constraint1', 'Constraint2']
)

age_lt_child = {
  'constraint_class': 'Constraint1',
  'constraint_parameters': {
    'column_name': 'my_column_name',
    'custom_param_1': 34,
    'custom_param_2': True
}}

age_lt_parent = {
  'constraint_class': 'Inequality',
  'constraint_parameters': {
    'low_column_name': 'age',
    'high_column_name': 'parent_age'
  }
}
synthesizer.add_constraints([age_lt_child, age_lt_parent])

Additional context

Currently, the create_custom_constraints requires the constraint_name to contain the name that would be used to import (ie. foler.file.class_name). We want users to be able to refer to the name of the class directly, so we want to use the load_custom_constraint_classes method to add the class to the namespace so it can be imported later by just the class name

@amontanez24 amontanez24 added the feature request Request for a new feature label Jan 10, 2023
@amontanez24 amontanez24 added this to the 1.0.0 milestone Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request for a new feature
Projects
None yet
Development

No branches or pull requests

2 participants