There will be a short maintenance break on Wed 27.10. at 12:00. Estimated time 30 minutes.

Commit 1311884b authored by Ossi Laine's avatar Ossi Laine
Browse files

Merge branch 'feat-updated-plotter' into 'master'

Updated embody plotter to handle testing more fluently

See merge request tithei/pet-rating!7
parents 5e1b87d8 f2db0cc2
......@@ -2,12 +2,14 @@
Script for testing embody drawing results from onni.utu.fi
Install requirements:
pip install tqdm
pip install numpy
pip install matplotlib
Usage:
Export data from onni.utu.fi and after that run the script in the same folder
by passing exported_file as a parameter to the script
by passing exported_file as a parameter to the script (NOTE that you must have
the default background image <dummy_600.png> also in the same path as the script).
$ python plot_image.py <exported_file>.csv
......@@ -15,22 +17,21 @@ Program prints header of the file, from which you must select column where the
image data is. After you have selected the right column, program prints the
drawing results from embody answers.
If you want the program to draw default embody image to the background, then
you must put a copy of the 'dummy_6000.png' -file (this is the same that is used
in onni.utu.fu) to the same path as the script.
'''
import copy
import numpy as np
import matplotlib.pyplot as plt
import csv
import sys
import subprocess
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
csv.field_size_limit(sys.maxsize)
def show_images(images, cols=1, titles=None):
def show_one_image_per_answer(images, cols=1, titles=None):
"""Display a list of images in a single figure with matplotlib.
Parameters
---------
......@@ -44,7 +45,6 @@ def show_images(images, cols=1, titles=None):
"""
# default embody image for the background
try:
background = True
default_img = plt.imread("./dummy_600.png")
......@@ -57,12 +57,14 @@ def show_images(images, cols=1, titles=None):
my_cmap.set_bad(alpha=0)
assert((titles is None) or (len(images) == len(titles)))
n_images = len(images)
if titles is None:
titles = ['Image (%d)' % i for i in range(1, n_images + 1)]
fig = plt.figure()
for n, (image, title) in enumerate(zip(images, titles)):
for n, (image, title) in enumerate(zip(images, titles)):
a = fig.add_subplot(cols, np.ceil(n_images/float(cols)), n + 1)
# draw points from users answers
......@@ -78,34 +80,89 @@ def show_images(images, cols=1, titles=None):
plt.show()
def show_images(images):
"""Display all data from list of images in a single figure with matplotlib.
Parameters
---------
images: List of np.arrays compatible with plt.imshow.
"""
# default embody image for the background
try:
background = True
default_img = plt.imread("./dummy_600.png")
except FileNotFoundError:
background = False
# get a copy of the gray color map
my_cmap = copy.copy(plt.cm.get_cmap('gray'))
# set how the colormap handles 'bad' values
my_cmap.set_bad(alpha=0)
n_images = len(images)
fig = plt.figure()
all_images = np.zeros(shape=(602,207))
for n, (image, title) in enumerate(zip(images, titles)):
all_images += image
plt.imshow(all_images, cmap=my_cmap)
if background:
plt.imshow(default_img, extent=[0, 200, 600, 0], alpha=0.33)
fig.set_size_inches(np.array(fig.get_size_inches()) * n_images)
plt.show()
if __name__ == '__main__':
filename = sys.argv[1]
skipped = 0
empty = 0
rows = 0
images = []
titles = []
# filename = 'experiment_1_2020-05-20.csv'
filename = sys.argv[1]
# count the file length
output = subprocess.check_output(f"wc -l {filename}", shell=True, stderr=subprocess.STDOUT)
wc = output.decode("utf-8").split(" ")[0]
with open(filename, 'r+') as csvfile:
for row_no, row in enumerate(csv.reader(csvfile, delimiter=';')):
csv_rows = csv.reader(csvfile, delimiter=';')
for row_no, row in enumerate(tqdm(csv_rows, total=int(wc))):
rows += 1
# parse header
if row_no == 0:
for column, title in enumerate(row):
print("Column (no. {}): {}".format(column, title))
if "embody_question" in title:
print("Column (ID {}): {}".format(column, title))
print('Enter the column number which has image data:')
print('\nEnter the column ID from which you want to see image data:')
x = int(input())
print("\n...processing images...\n")
continue
try:
# skip empty rows (no answer at all)
if not row[x]:
skipped += 1
continue
np_array = np.array(eval(row[x]))
# skip empty answers (user hasn't drawed on the picture at all)
if np.all((np_array == 0)):
empty += 1
continue
except NameError:
print(
"Column didn't contain image data. Try again with different column number.")
except SyntaxError:
"Column didn't contain image data. Try again with different column ID.")
except SyntaxError as err:
continue
except IndexError:
except IndexError as err:
continue
np_array = np.transpose(np_array)
......@@ -114,4 +171,12 @@ if __name__ == '__main__':
# add id of the answerer to the image
titles.append(row[0])
show_images(images, titles=titles)
print(f"\nExperiment started {rows} times")
print(f"from which users has skipped {skipped + empty} times this question\n")
# show all answers from one column in one image
show_images(images)
# show all answer from one column in own images
# OBS: this works decently only with small amount of answers
# show_one_image_per_answer(images, titles=titles)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment