fair_dag.py
def login():
    """
    Login Page Flask function

    In Login page:
        takes in user information.
        dropdown menu for user to select play data
        upload function for data upload if new case specified by user

    Returns:
        url for login page. Redirect to main home page if valid login session
        write pipeline code to executable function python file which calls fairness_instru wrapper generating DAGs and intermediate log dict files
        load intermediate dicts as well as DAG(stored in svg) and parse them to main home page
    """
    error = None

    # set default_value here
    if request.method == 'POST':
        global name
        name = request.form['name'] if request.form['name'] else 'Guest X'
        global organization
        organization = request.form['organization'] if request.form['organization'] else 'Y university'
        global demo
        demo = request.form['demo']
        global code
        code = """def adult_pipeline_easy(f_path = 'playdata/AD_train.csv'):

    raw_data = pd.read_csv(f_path, na_values='?', index_col=0)

    data = raw_data.dropna()

    labels = label_binarize(data['income-per-year'], ['>50K', '<=50K'])

    feature_transformation = ColumnTransformer(transformers=[
        ('categorical', OneHotEncoder(handle_unknown='ignore'), ['education', 'workclass']),
        ('numeric', StandardScaler(), ['age', 'hours-per-week'])
    ])


    income_pipeline = Pipeline([
        ('features', feature_transformation),
        ('classifier', DecisionTreeClassifier())])

    return income_pipeline""" if not request.form['code'] else request.form['code']
        global target_name, pos_group
        target_name, pos_group = list(map(lambda x: x.strip(), request.form['target_name'].split(','))) if request.form['target_name'] else ("income-per-year", ">50K")
        global cat_target
        cat_target = list(map(lambda x: x.strip(), request.form['cat_target'].split(','))) if request.form['cat_target'] else ['sex', 'race']
        global num_target
        num_target = list(map(lambda x: x.strip(), request.form['num_target'].split(','))) if request.form['num_target'] else ['age', 'hours-per-week']
        global save_path
        save_path = f'experiments/{user_id}'
        global perform_target
        perform_target = request.form['perform_target'] if request.form['perform_target'] else 'PR'

        session['logged_in'] = True

        # cache is used for stacking user click event, so that figures will show in sequence
        global cache
        cache = []
        global log_dict
        global rand_rgb
        global plot_dict
        global target_df

        # to_json_dict stores all user entered info, saved in uid format
        to_json_dict = request.form.to_dict(flat=False)
        with open(f'media/{user_id}.json', 'w+') as f:
            json.dump(to_json_dict, f)

        flash('You were just logged in')

        # options for dsiplaying play data cases
        # load saved play data intermediate dict files generated from fairness_instru
        if not demo == 'USER':
            log_dict = pickle.load(open(f"playdata/{demo}/checkpoints/log_dict_train.p", 'rb'))
            rand_rgb = pickle.load(open(f"playdata/{demo}/checkpoints/rand_color_train.p", 'rb'))
            rand_rgb = pickle.load(open(f"playdata/{demo}/checkpoints/rand_color_train.p", 'rb'))
            plot_dict = pickle.load(open(f"playdata/{demo}/checkpoints/plot_dict_train.p", 'rb'))
            target_df = pickle.load(open(f"playdata/{demo}/checkpoints/target_df_train.p", 'rb'))

            if demo =='AD_normal':
                code = playdata_AD_normal
            elif demo == 'CM':
                code  = playdata_CM
                target_name, pos_group = 'score_text', 'High'
                cat_target = ['sex', 'race']
                num_target = ['age']

            with open(f"playdata/{demo}/DAG/pipeline.svg", 'r') as content:
                svg = content.read()
            with open(f'templates/{demo}.html', 'w+') as f:
                f.write("{% extends 'index1.html' %}\n")
                f.write("{% block content %}\n")
                f.write(svg)
                f.write('\n')
                f.write("{% endblock %}\n")
            return redirect(url_for('home'))

        # below handles user defined cases. including:
        # save to executable function python file which generates DAGs and intermediate dict files
        # load intermediate dict files
        # load dags
        # parse intermediate dict and DAGs to main home page

        # laod user uploaded file
        file = request.files['file']
        if file.filename == '':
            flash('No selected File')
            return redirect(url_for('logged_in'))
        if file:
            filename = secure_filename(file.filename)
            file.save(f'media/{user_id}.csv')

        # pipeline codes to be outputed into executable function python file. Extract pipeline function title first.
        function_title = code.split('(')[0].replace('def ','')+"()"
        input_args = code.split('(')[1].split(')')[0].split(',')
        for i, item in enumerate(input_args):
            if 'f_path' in item:
                input_args[i] = f'f_path = \"./media/{user_id}.csv\"'
        input_arg = ','.join(input_args)
        code = ''.join([code.split('(')[0], '(', input_arg, ')', ')'.join(code.split(')')[1:])])

        # write essentials and pipeline codes to executable function python file. add trace wrapper above function declare line.
        with open(f'{user_id}.py', 'w+') as f:
            f.write(essentials)
            f.write(f"""@tracer(cat_col = {cat_target}, numerical_col = {num_target}, sensi_atts={cat_target}, target_name = \"{target_name}\", training=True, save_path=\"{save_path}\", dag_save=\"svg\")\n{code}\n""")
            f.write(f"pipeline = {function_title}")
        os.system(f"python {user_id}.py")

        img = save_path + "/DAG/pipeline.svg"
        with open(img, 'r') as content:
            svg = content.read()
        with open(f'templates/{user_id}.html', 'w+') as f:
            f.write("{% extends 'index1.html' %}\n")
            f.write("{% block content %}\n")
            f.write(svg)
            f.write('\n')
            f.write("{% endblock %}\n")

        # load saved intermediate dict files generated from executable function python file.
        log_dict = pickle.load(open(save_path+"/checkpoints/log_dict_train.p", 'rb'))
        rand_rgb = pickle.load(open(save_path+"/checkpoints/rand_color_train.p", 'rb'))
        rand_rgb = pickle.load(open(save_path+"/checkpoints/rand_color_train.p", 'rb'))
        plot_dict = pickle.load(open(save_path+"/checkpoints/plot_dict_train.p", 'rb'))
        target_df = pickle.load(open(save_path+"/checkpoints/target_df_train.p", 'rb'))

        return redirect(url_for('home'))
    return render_template("login_2.html", error = error, script_name = script_name)

    Login Page Flask function

    In Login page:
        takes in user information.
        dropdown menu for user to select play data
        upload function for data upload if new case specified by user

    Returns:
        url for login page. Redirect to main home page if valid login session
        write pipeline code to executable function python file which calls fairness_instru wrapper generating DAGs and intermediate log dict files
        load intermediate dicts as well as DAG(stored in svg) and parse them to main home page
    
Login Page Flask function
In Login page:
takes in user information.
dropdown menu for user to select play data
upload function for data upload if new case specified by user
Login Page Flask function In Login page : takes in user information . dropdown menu for user to select play data upload function for data upload if new case specified by user
def login():
    error = None
    if request.method == 'POST':
        global name
        name = request.form['name'] if request.form['name'] else 'Guest X'
        global organization
        organization = request.form['organization'] if request.form['organization'] else 'Y university'
        global demo
        demo = request.form['demo']
        global code
        code = """def adult_pipeline_easy(f_path = 'playdata/AD_train.csv'):
    raw_data = pd.read_csv(f_path, na_values='?', index_col=0)
    data = raw_data.dropna()
    labels = label_binarize(data['income-per-year'], ['>50K', '<=50K'])
    feature_transformation = ColumnTransformer(transformers=[
        ('categorical', OneHotEncoder(handle_unknown='ignore'), ['education', 'workclass']),
        ('numeric', StandardScaler(), ['age', 'hours-per-week'])
    ])
    income_pipeline = Pipeline([
        ('features', feature_transformation),
        ('classifier', DecisionTreeClassifier())])
    return income_pipeline""" if not request.form['code'] else request.form['code']
        global target_name, pos_group
        target_name, pos_group = list(map(lambda x: x.strip(), request.form['target_name'].split(','))) if request.form['target_name'] else ("income-per-year", ">50K")
        global cat_target
        cat_target = list(map(lambda x: x.strip(), request.form['cat_target'].split(','))) if request.form['cat_target'] else ['sex', 'race']
        global num_target
        num_target = list(map(lambda x: x.strip(), request.form['num_target'].split(','))) if request.form['num_target'] else ['age', 'hours-per-week']
        global save_path
        save_path = f'experiments/{user_id}'
        global perform_target
        perform_target = request.form['perform_target'] if request.form['perform_target'] else 'PR'
        session['logged_in'] = True
        global cache
        cache = []
        global log_dict
        global rand_rgb
        global plot_dict
        global target_df
        to_json_dict = request.form.to_dict(flat=False)
        with open(f'media/{user_id}.json', 'w+') as f:
            json.dump(to_json_dict, f)
        flash('You were just logged in')
        if not demo == 'USER':
            log_dict = pickle.load(open(f"playdata/{demo}/checkpoints/log_dict_train.p", 'rb'))
            rand_rgb = pickle.load(open(f"playdata/{demo}/checkpoints/rand_color_train.p", 'rb'))
            rand_rgb = pickle.load(open(f"playdata/{demo}/checkpoints/rand_color_train.p", 'rb'))
            plot_dict = pickle.load(open(f"playdata/{demo}/checkpoints/plot_dict_train.p", 'rb'))
            target_df = pickle.load(open(f"playdata/{demo}/checkpoints/target_df_train.p", 'rb'))
            if demo =='AD_normal':
                code = playdata_AD_normal
            elif demo == 'CM':
                code  = playdata_CM
                target_name, pos_group = 'score_text', 'High'
                cat_target = ['sex', 'race']
                num_target = ['age']
            with open(f"playdata/{demo}/DAG/pipeline.svg", 'r') as content:
                svg = content.read()
            with open(f'templates/{demo}.html', 'w+') as f:
                f.write("{% extends 'index1.html' %}\n")
                f.write("{% block content %}\n")
                f.write(svg)
                f.write('\n')
                f.write("{% endblock %}\n")
            return redirect(url_for('home'))
        file = request.files['file']
        if file.filename == '':
            flash('No selected File')
            return redirect(url_for('logged_in'))
        if file:
            filename = secure_filename(file.filename)
            file.save(f'media/{user_id}.csv')
        function_title = code.split('(')[0].replace('def ','')+"()"
        input_args = code.split('(')[1].split(')')[0].split(',')
        for i, item in enumerate(input_args):
            if 'f_path' in item:
                input_args[i] = f'f_path = \"./media/{user_id}.csv\"'
        input_arg = ','.join(input_args)
        code = ''.join([code.split('(')[0], '(', input_arg, ')', ')'.join(code.split(')')[1:])])
        with open(f'{user_id}.py', 'w+') as f:
            f.write(essentials)
            f.write(f"""@tracer(cat_col = {cat_target}, numerical_col = {num_target}, sensi_atts={cat_target}, target_name = \"{target_name}\", training=True, save_path=\"{save_path}\", dag_save=\"svg\")\n{code}\n""")
            f.write(f"pipeline = {function_title}")
        os.system(f"python {user_id}.py")
        img = save_path + "/DAG/pipeline.svg"
        with open(img, 'r') as content:
            svg = content.read()
        with open(f'templates/{user_id}.html', 'w+') as f:
            f.write("{% extends 'index1.html' %}\n")
            f.write("{% block content %}\n")
            f.write(svg)
            f.write('\n')
            f.write("{% endblock %}\n")
        log_dict = pickle.load(open(save_path+"/checkpoints/log_dict_train.p", 'rb'))
        rand_rgb = pickle.load(open(save_path+"/checkpoints/rand_color_train.p", 'rb'))
        rand_rgb = pickle.load(open(save_path+"/checkpoints/rand_color_train.p", 'rb'))
        plot_dict = pickle.load(open(save_path+"/checkpoints/plot_dict_train.p", 'rb'))
        target_df = pickle.load(open(save_path+"/checkpoints/target_df_train.p", 'rb'))
        return redirect(url_for('home'))
    return render_template("login_2.html", error = error, script_name = script_name)
def login ( ) : error = None if request . method == 'POST' : global name name = request . form [ 'name' ] if request . form [ 'name' ] else 'Guest X' global organization organization = request . form [ 'organization' ] if request . form [ 'organization' ] else 'Y university' global demo demo = request . form [ 'demo' ] global code code = """def adult_pipeline_easy(f_path = 'playdata/AD_train.csv'):

    raw_data = pd.read_csv(f_path, na_values='?', index_col=0)

    data = raw_data.dropna()

    labels = label_binarize(data['income-per-year'], ['>50K', '<=50K'])

    feature_transformation = ColumnTransformer(transformers=[
        ('categorical', OneHotEncoder(handle_unknown='ignore'), ['education', 'workclass']),
        ('numeric', StandardScaler(), ['age', 'hours-per-week'])
    ])


    income_pipeline = Pipeline([
        ('features', feature_transformation),
        ('classifier', DecisionTreeClassifier())])

    return income_pipeline""" if not request . form [ 'code' ] else request . form [ 'code' ] global target_name , pos_group target_name , pos_group = list ( map ( lambda x : x . strip ( ) , request . form [ 'target_name' ] . split ( ',' ) ) ) if request . form [ 'target_name' ] else ( "income-per-year" , ">50K" ) global cat_target cat_target = list ( map ( lambda x : x . strip ( ) , request . form [ 'cat_target' ] . split ( ',' ) ) ) if request . form [ 'cat_target' ] else [ 'sex' , 'race' ] global num_target num_target = list ( map ( lambda x : x . strip ( ) , request . form [ 'num_target' ] . split ( ',' ) ) ) if request . form [ 'num_target' ] else [ 'age' , 'hours-per-week' ] global save_path save_path = f'experiments/{user_id}' global perform_target perform_target = request . form [ 'perform_target' ] if request . form [ 'perform_target' ] else 'PR' session [ 'logged_in' ] = True global cache cache = [ ] global log_dict global rand_rgb global plot_dict global target_df to_json_dict = request . form . to_dict ( flat = False ) with open ( f'media/{user_id}.json' , 'w+' ) as f : json . dump ( to_json_dict , f ) flash ( 'You were just logged in' ) if not demo == 'USER' : log_dict = pickle . load ( open ( f"playdata/{demo}/checkpoints/log_dict_train.p" , 'rb' ) ) rand_rgb = pickle . load ( open ( f"playdata/{demo}/checkpoints/rand_color_train.p" , 'rb' ) ) rand_rgb = pickle . load ( open ( f"playdata/{demo}/checkpoints/rand_color_train.p" , 'rb' ) ) plot_dict = pickle . load ( open ( f"playdata/{demo}/checkpoints/plot_dict_train.p" , 'rb' ) ) target_df = pickle . load ( open ( f"playdata/{demo}/checkpoints/target_df_train.p" , 'rb' ) ) if demo == 'AD_normal' : code = playdata_AD_normal elif demo == 'CM' : code = playdata_CM target_name , pos_group = 'score_text' , 'High' cat_target = [ 'sex' , 'race' ] num_target = [ 'age' ] with open ( f"playdata/{demo}/DAG/pipeline.svg" , 'r' ) as content : svg = content . read ( ) with open ( f'templates/{demo}.html' , 'w+' ) as f : f . write ( "{% extends 'index1.html' %}\n" ) f . write ( "{% block content %}\n" ) f . write ( svg ) f . write ( '\n' ) f . write ( "{% endblock %}\n" ) return redirect ( url_for ( 'home' ) ) file = request . files [ 'file' ] if file . filename == '' : flash ( 'No selected File' ) return redirect ( url_for ( 'logged_in' ) ) if file : filename = secure_filename ( file . filename ) file . save ( f'media/{user_id}.csv' ) function_title = code . split ( '(' ) [ 0 ] . replace ( 'def ' , '' ) + "()" input_args = code . split ( '(' ) [ 1 ] . split ( ')' ) [ 0 ] . split ( ',' ) for i , item in enumerate ( input_args ) : if 'f_path' in item : input_args [ i ] = f'f_path = \"./media/{user_id}.csv\"' input_arg = ',' . join ( input_args ) code = '' . join ( [ code . split ( '(' ) [ 0 ] , '(' , input_arg , ')' , ')' . join ( code . split ( ')' ) [ 1 : ] ) ] ) with open ( f'{user_id}.py' , 'w+' ) as f : f . write ( essentials ) f . write ( f"""@tracer(cat_col = {cat_target}, numerical_col = {num_target}, sensi_atts={cat_target}, target_name = \"{target_name}\", training=True, save_path=\"{save_path}\", dag_save=\"svg\")\n{code}\n""" ) f . write ( f"pipeline = {function_title}" ) os . system ( f"python {user_id}.py" ) img = save_path + "/DAG/pipeline.svg" with open ( img , 'r' ) as content : svg = content . read ( ) with open ( f'templates/{user_id}.html' , 'w+' ) as f : f . write ( "{% extends 'index1.html' %}\n" ) f . write ( "{% block content %}\n" ) f . write ( svg ) f . write ( '\n' ) f . write ( "{% endblock %}\n" ) log_dict = pickle . load ( open ( save_path + "/checkpoints/log_dict_train.p" , 'rb' ) ) rand_rgb = pickle . load ( open ( save_path + "/checkpoints/rand_color_train.p" , 'rb' ) ) rand_rgb = pickle . load ( open ( save_path + "/checkpoints/rand_color_train.p" , 'rb' ) ) plot_dict = pickle . load ( open ( save_path + "/checkpoints/plot_dict_train.p" , 'rb' ) ) target_df = pickle . load ( open ( save_path + "/checkpoints/target_df_train.p" , 'rb' ) ) return redirect ( url_for ( 'home' ) ) return render_template ( "login_2.html" , error = error , script_name = script_name )
Login Page Flask function
In Login page:
takes in user information.
Login Page Flask function In Login page : takes in user information .
"""
    Login Page Flask function

    In Login page:
        takes in user information.
        dropdown menu for user to select play data
        upload function for data upload if new case specified by user

    Returns:
        url for login page. Redirect to main home page if valid login session
        write pipeline code to executable function python file which calls fairness_instru wrapper generating DAGs and intermediate log dict files
        load intermediate dicts as well as DAG(stored in svg) and parse them to main home page
    """ # set default_value here # cache is used for stacking user click event, so that figures will show in sequence # to_json_dict stores all user entered info, saved in uid format # options for dsiplaying play data cases # load saved play data intermediate dict files generated from fairness_instru # below handles user defined cases. including: # save to executable function python file which generates DAGs and intermediate dict files # load intermediate dict files # load dags # parse intermediate dict and DAGs to main home page # laod user uploaded file # pipeline codes to be outputed into executable function python file. Extract pipeline function title first. # write essentials and pipeline codes to executable function python file. add trace wrapper above function declare line. # load saved intermediate dict files generated from executable function python file.
url for login page. Redirect to main home page if valid login session
write pipeline code to executable function python file which calls fairness_instru wrapper generating DAGs and intermediate log dict files
load intermediate dicts as well as DAG(stored in svg) and parse them to main home page
url for login page . Redirect to main home page if valid login session write pipeline code to executable function python file which calls fairness_instru wrapper generating DAGs and intermediate log dict files load intermediate dicts as well as DAG ( stored in svg ) and parse them to main home page