<commit_before>from __future__ import absolute_import
from gemini.gemini_subjects import get_subjects
from gemini.ped import load_ped_file, get_ped_fields
from gemini.gemini_utils import quote_string
from gemini.database import database_transaction

def amend(parser, args):
    if args.db is None:
        parser.print_help()
        exit("ERROR: amend needs a database file.")
    if args.sample:
        amend_sample(args)

def amend_sample(args):
    loaded_subjects = get_subjects(args)
    ped_dict = load_ped_file(args.sample)
    header = get_ped_fields(args.sample)
    with database_transaction(args.db) as c:
        add_columns(header, c, args.clear)
        for k, v in loaded_subjects.items():
            if k in ped_dict:
                item_list = map(quote_string, ped_dict[k])
                sample = zip(header, item_list)
                set_str = ",".join([str(x) + "=" + str(y) for (x, y) in sample])
                sql_query = "update samples set {0} where sample_id={1}"
                c.execute(sql_query.format(set_str, v.sample_id))

def add_columns(header, c, clear=False):
    """
    add any missing columns to the samples table
    """
    for column in header:
        try:
            c.execute('ALTER TABLE samples ADD COLUMN {0}'.format(column))
        except:
            pass<commit_msg>give amend columns a type. closes #791<commit_after>from __future__ import absolute_import
from gemini.gemini_subjects import get_subjects
from gemini.ped import load_ped_file, get_ped_fields
from gemini.gemini_utils import quote_string
from gemini.database import database_transaction

def amend(parser, args):
    if args.db is None:
        parser.print_help()
        exit("ERROR: amend needs a database file.")
    if args.sample:
        amend_sample(args)

def amend_sample(args):
    loaded_subjects = get_subjects(args)
    ped_dict = load_ped_file(args.sample)
    header = get_ped_fields(args.sample)
    with database_transaction(args.db) as c:
        add_columns(header, c, args.clear)
        for k, v in loaded_subjects.items():
            if k in ped_dict:
                item_list = map(quote_string, ped_dict[k])
                sample = zip(header, item_list)
                set_str = ",".join([str(x) + "=" + str(y) for (x, y) in sample])
                sql_query = "update samples set {0} where sample_id={1}"
                c.execute(sql_query.format(set_str, v.sample_id))

def add_columns(header, c, clear=False):
    """
    add any missing columns to the samples table
    """
    for column in header:
        try:
            c.execute('ALTER TABLE samples ADD COLUMN {0} TEXT'.format(column))
        except:
            pass
{'fraction_non_alphanumeric': 0.09591078066914498, 'fraction_numerical': 0.003345724907063197, 'mean_word_length': 2.7426981919332407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5734270', 'n_tokens_mistral': 837, 'n_tokens_neox': 817, 'n_words': 226}