Get original labels in the sqlite file samples
Hello,
Should it be possible to add a column code
in the sqlite files (output of the sample step) for example the sqlite file: learningSamples/Samples_region_1_seed0_learn.sqlite
? This column will correspond to the value of the original label.
For now, the table output
from this sqlite file contains only the column i2label
which corresponds to the encoded label.
I use this code to complete this feature:
import pandas as pd
from iota2.VectorTools.vector_functions import get_re_encoding_labels_dic
import argparse
import sqlite3
# Add a new column to the table output
# Name column: "code"
# This column contains the original labels from the reference shapefile instead of consecutives ones
def add_column_original_labels(sqlitefile, shapefile):
# ouverture sqlite file
con = sqlite3.connect(sqlitefile)
# on récupère la colonne ogc_fid et i2label
query = "SELECT ogc_fid, i2label FROM output"
df1 = pd.read_sql(query, con)
# on récupère le dictionnaire qui permet de convertir les labels consécutifs avec leur valeur originale
reference_data_field = "code"
old_label_to_new = get_re_encoding_labels_dic(shapefile, reference_data_field)
reversed_dictionary = {value: key for (key, value) in old_label_to_new.items()}
cur = con.cursor()
# Dans la table output on crée une colonne code
cur.execute("alter table output add column code")
# On vérifie que la colonne a été créée
# cur.execute("PRAGMA table_info(output);")
# print(cur.fetchall())
# On récupère la liste des anciens labels
old_label = df1.i2label.unique()
# Pour chaque valeur dans la colonne de iota2, on associe la valeur non encodée dans la colonne code
for label in old_label:
print(label)
print(reversed_dictionary[label])
qry = "update output set code = (CASE i2label WHEN {} THEN {} END) " \
"where code is NULL;"\
.format(label, reversed_dictionary[label])
cur.execute(qry)
print("update done")
con.commit()
con.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Add column original labels 'code'")
parser.add_argument("-sqlite", dest="sqlite", help="Input sqlite")
parser.add_argument("-reference", dest="reference", help="Referenced shapefile")
args = parser.parse_args()
add_column_original_labels(args.sqlite, args.reference)
Thanks for your help,