Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 6a592b28 rédigé par ayushkumarshah's avatar ayushkumarshah
Parcourir les fichiers

feat: Use argparse and uupdate description

parent 559b7bea
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
#!/bin/bash
usage()
{
echo "Usage: confHist (output_dir target_dir) | fileList -gs|--graphSize <value>"
echo -e "\t\t[-m|--minCount <value>] [-s|--strokes] [-i|--lgimgDir <directory>]"
echo -e "\t\t[-p|--dotpdfDir <directory>] [--split] [--filter] [-h|--help]"
echo ""
echo "------- Required Arguments -------"
echo "output_dir Output lg files directory"
echo "target_dir Ground truth lg files directory"
echo "fileList File whose each line contains outputfile_path targetfile_path"
echo -e "\t\t\t\t\tis used for comparison."
echo "Note: Use either the 2 directories or the fileList"
echo -e "-gs or --graphSize <value> \t\tThe number of objects/primitives in targets to analyze"
echo ""
echo "------- Optional Arguments -------"
echo -e "-m or --minCount <value> \tThe minimum number of times an error should occur before"
echo -e "\t\t\t\tdetailed information is provided in the confusion histogram"
echo -e "\t\t\t\tBy default, all errors are shown (minCount = 1)"
echo -e "-s or --strokes \t\tConstruct stroke(primitive) confusion histograms in addition"
echo -e "\t\t\t\tto object confusion histograms"
echo -e "-i or --lgimgDir <directory> \tThe directory containing the expression images of the lg files"
echo -e "-p or --dotpdfDir <directory> \tThe directory containing the expression images of the lg files"
echo -e "-sp or --split \t\t\tSeparate the more frequent(>minCount) errors and (<=minCount)"
echo -e "\t\t\t\terrors"
echo -e "-f or --filter \t\t\tIgnore the less frequent(<=minCount) errors"
echo -e "-h or --help \t\t\tPrint usage and this help message and exit"
}
if [ $# -lt 2 ]
if [ $# -eq 0 ]
then
echo "LgEval confHist: Structure Confusion Histogram Generator"
echo "Copyright (c) R. Zanibbi, H. Mouchere, 2013-2014"
echo ""
echo "Usage: confHist dir1 dir2 graphSize [minCount] [strokes] OR"
echo " confHist fileList graphSize [minCount] [strokes]"
echo "Usage: confHist (output_dir target_dir) | fileList -gs|--graphSize <value>"
echo -e "\t\t[-m|--minCount <value>] [-s|--strokes] [-i|--lgimgDir <directory>]"
echo -e "\t\t[-p|--dotpdfDir <directory>] [--split] [--filter] [-h|--help]"
echo "For details on arguments usage: confHist -h or confHist --help"
echo ""
echo "Creates an .html file containing structure confusion histograms"
echo "at the object level. The histograms visualize errors by their"
echo "frequency when comparing files in dir1 vs. dir2 (dir2 is 'ground truth')."
echo "It is assumed that every .lg file in dir1 exists in dir2, and a file"
echo "dir1_vs_dir2 is created as output."
echo ""
echo "If a file list is provided, then each line of the file"
echo "(format: 'outputfile_path targetfile_path') is used for comparison."
echo ""
echo "graphSize is the number of objects/primitives in targets to analyze."
echo "minCount is the minimum number of times an error should occur before"
echo "detailed information is provided in the confusion histogram. By default,"
echo "all errors are shown (minCount = 1)."
echo "frequency when comparing files in output_dir vs. target_dir (target_dir is 'ground truth')."
echo "It is assumed that every .lg file in output_dir exists in target_dir, and a file"
echo "output_dir_vs_target_dir is created as output."
echo ""
echo "If an optional argument is provided (<strokes>), then stroke"
echo "confusion histograms will be constructed in addition to object"
echo "confusion histograms."
echo ""
echo "Output is written to the file CH_<dir1_vs_dir2>.html or"
echo "CH_<fileList>.html, depending upon the arguments used."
echo "Output is written to the file confHist_outputs/CH_<output_dir_vs_target_dir>.html"
echo "or confHist_outputs/CH_<fileList>.html, depending upon the arguments used."
exit 0
fi
GRAPH_SIZE=""
MIN_COUNT=1
STROKES=0
LGIMG_DIR="../"
DOTPDF_DIR="../"
SPLIT=0
FILTER=1
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help) usage
exit 0
;;
-gs|--graphSize)
GRAPH_SIZE="$2"
shift # past argument
shift # past value
;;
-m|--minCount)
MIN_COUNT="$2"
shift # past argument
shift # past value
;;
-s|--strokes)
STROKES=1
shift # past argument
;;
-i|--lgimgDir)
LGIMG_DIR="$2"
shift # past argument
shift # past value
;;
-p|--dotpdfDir)
DOTPDF_DIR="$2"
shift # past argument
shift # past value
;;
-sp|--split)
SPLIT=1
shift # past argument
;;
-f|--filter)
FILTER=1
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [[ -z "$GRAPH_SIZE" ]]
then
echo "Error: Please enter graph size using -gs value OR --graphSize value"
exit 1
fi
OUT_DIR="confHist_outputs"
if [ ! -d $OUT_DIR ]
then
......@@ -40,25 +119,25 @@ fi
if [ -d $1 ]
then
# Remove trailing slashes.
dir1=${1%/}
dir2=${2%/}
base1=`basename $dir1`
base2=`basename $dir2`
output_dir=${1%/}
target_dir=${2%/}
base1=`basename $output_dir`
base2=`basename $target_dir`
INFILE="$OUT_DIR/${base1}_vs_${base2}"
# Two directories passed (hopefully).
# NOTE: Assumes same number of .lg files with
# matching names.
ls $dir1/*.lg > _f1
ls $dir2/*.lg > _f2
ls $output_dir/*.lg > _f1
ls $target_dir/*.lg > _f2
L1=`wc -l _f1 | awk '{print $1}'`
L2=`wc -l _f2 | awk '{print $1}'`
if [ "$L1" != "$L2" ]
then
echo " !! Error: differing number of .lg files:"
echo " ($L1) $dir1"
echo " ($L2) $dir2"
echo " ($L1) $output_dir"
echo " ($L2) $target_dir"
rm -f _f1 _f2
exit 1
fi
......@@ -67,11 +146,16 @@ then
rm -f _f1 _f2
# HACK: ${@:3} selects args starting from the third.
python $LgEvalDir/src/confHists.py $INFILE ${@:3}
python $LgEvalDir/src/confHists.py --fileList $INFILE --graphSize $GRAPH_SIZE \
--minCount $MIN_COUNT --strokes $STROKES --lgimgDir $LGIMG_DIR \
--dotpdfDir $DOTPDF_DIR --split $SPLIT --filter $FILTER
#rm $INFILE
else
# User-provided file list.
python $LgEvalDir/src/confHists.py $@
echo $@
python $LgEvalDir/src/confHists.py --fileList $@ --graphSize $GRAPH_SIZE \
--minCount $MIN_COUNT --strokes $STROKES --lgimgDir $LGIMG_DIR \
--dotpdfDir $DOTPDF_DIR --split $SPLIT --filter $FILTER
fi
exit 0
......@@ -11,23 +11,23 @@ import sys
import csv
import time
import os
import argparse
from lg import *
from lgio import *
import SmGrConfMatrix
import compareTools
def main(fileList, minCount, confMat, confMatObj, subgraphSize, img_dir=None):
def main(fileList, minCount=1, confMat=False, confMatObj=True,
subgraphSize=1, img_dir='', dotpdfDir=''):
fileReader = csv.reader(open(fileList), delimiter=' ')
htmlStream = None
matrix = SmGrConfMatrix.ConfMatrix()
matrixObj = SmGrConfMatrix.ConfMatrixObject()
# if not osp.exists("confHist_outputs"):
# os.makedirs("confHist_outputs")
if not os.path.exists("confHist_outputs/lg2dot_png"):
os.makedirs("confHist_outputs/lg2dot_png")
if not os.path.exists("confHist_outputs/lg2dot_pdf"):
os.makedirs("confHist_outputs/lg2dot_pdf")
for row in fileReader:
# Skip comments and empty lines.
if not row == [] and not row[0].strip()[0] == "#":
......@@ -204,18 +204,34 @@ def main(fileList, minCount, confMat, confMatObj, subgraphSize, img_dir=None):
htmlStream.write('</html>')
htmlStream.close()
# (RZ) Lazy - not checking arguments on assumption this is called from the
# strConfHist script.
minCount = 1
fileList = sys.argv[1]
subgraphSize = int(sys.argv[2])
if len(sys.argv) > 3:
minCount = int(sys.argv[3])
def parse_args():
parser = argparse.ArgumentParser(description="ConfHist Arguments")
parser.add_argument("--fileList", type=str, required=True,
help="The file containing list of lg file pairs to evaluate")
parser.add_argument("-gs", "--graphSize", type=int, required=True,
help="The number of objects/primitives in targets to analyze")
parser.add_argument("-m", "--minCount", type=int, default=1,
help="The minimum number of times an error should occur")
parser.add_argument("-s", "--strokes", type=int, default=0,
help="Flag whether to construct stroke (primitive) confusion histograms")
parser.add_argument("-i", "--lgimgDir", type=str,
help="The directory containing the expression images of the lg files")
parser.add_argument("-p", "--dotpdfDir", type=str,
help="The directory containing the lg2dot comparison pdf outputs")
parser.add_argument("-sp", "--split", type=int, default=0,
help="Flag whether to construct stroke (primitive) confusion histograms")
parser.add_argument("-f", "--filter", type=int, default=1,
help="Flag whether to construct stroke (primitive) confusion histograms")
args = parser.parse_args()
return args
confMatObj = True
confMat = True if len(sys.argv) > 4 else False
# img_dir = '../../../../data/test2019_inkml2img' # 3branch...
img_dir = '../../../../data/infty/IMG' # infty_contour...
# img_dir = '../../../Data/Expressions/IMG' # lpga_rf...
main(fileList, minCount, confMat, confMatObj, subgraphSize, img_dir=img_dir)
if __name__ == "__main__":
args = parse_args()
print(args)
# img_dir = '../../../../data/test2019_inkml2img' # 3branch...
# img_dir = '../../../../data/infty/IMG' # infty_contour...
# img_dir = '../../../Data/Expressions/IMG' # lpga_rf...
img_dir = os.path.join("../..", args.lgimgDir)
main(args.fileList, minCount=args.minCount, confMat=bool(args.strokes),
subgraphSize=args.graphSize, img_dir=img_dir, dotpdfDir=args.dotpdfDir)
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter