Skip to content

Instantly share code, notes, and snippets.

@markkuleino
Created March 5, 2021 17:54
Show Gist options
  • Save markkuleino/bea1d81b41b233a8a3dbb2b6659a2222 to your computer and use it in GitHub Desktop.
Save markkuleino/bea1d81b41b233a8a3dbb2b6659a2222 to your computer and use it in GitHub Desktop.
#Python3
import csv
import math
import glob
import numpy as np
def getExeNames(titles):
ti = []
for title in titles[1:]:
ti.append(title)
return ti
def getRow(row):
r = [row[0]]
points = []
for i,p in enumerate( row[1:] ):
#Convert the numbers separated by space into a array of integers
#Fetch the max value of and return those.
#print( p )
#print( [(i) for i in p.strip().split(' ')] )
values = [int('0'+i) for i in p.strip().split(' ')]
#print( values );
r.append( max( values ))
return r
def removeFieldName(a, name):
#https://stackoverflow.com/questions/15575878/how-do-you-remove-a-column-from-a-structured-numpy-array
names = list(a.dtype.names)
if name in names:
names.remove(name)
b = a[names]
return b
def leaveFields(a, ns):
names = list(a.dtype.names)
#Find the corresponding names of fields that stay
fields = []
for n in ns:
res = [i for i in names if n in i]
fields.append( res[0] )
#Find the difference and drop those---except the one named "Name"
#https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-19.php
diff1 = list( set(names) - set(fields) )
diff2 = list( set(fields) - set(names) )
diff = diff1 + diff2
diff.remove("Name")
for n in diff:
a = removeFieldName(a, n)
return a
#
#
#
#
#
fil = []
tul = []
kpl = []
if (0):
kpl.append( '1: Elävä solu ' )
fil.append( 'bg7_1.csv' )
tul.append( ['P1', 'P2', 'P3', 'S1', 'T1','YT1'] )
kpl.append( '4: Levät ja planktoneläimet ovat vesien runsaimpia eliöitä' )
fil.append( 'bg7_4.csv' )
tul.append( ['P1'] )
name = "name1"
name = "name2"
if (0):
kpl.append( '3: Metsätyypiy ja puulajit' )
fil.append( 'bg8_3.csv' )
tul.append( ['P1', 'P2'] )
name = "name3"
name = "name4"
#
#
#
ka = 0
lkm = 0
print("\n\n")
print('Hei')
print('Maantiedon tehtäväsi tähän mennessä. Lopussa arvosana seisoo.')
print('Tarkista ainakin, että olet teet ne tehtävä mitkä olen merkinnyt arvosanalla neljä (4). Jos olet tehnyt, mutta arvosanasi on 4, kerro minulle. ')
print('Saat parannella ja lisäillä tehtäviä, jos haluat paremman numeron. Aikaa on.')
print('- - - - - - - - - - - - - - - - - - - - -')
ave = 0
for i, tcsv in enumerate( fil ):
lkm = lkm + 1
names = []
with open( tcsv ) as csvfile:
print( kpl[i] )
print( "-"*len( kpl[i] ) )
readCSV = csv.reader(csvfile, delimiter=',')
#Read the column names
eNames = getExeNames(next(readCSV))
eNames.insert(0,'Name')
formats = ['u4']*( len( eNames ) - 1)
formats.insert(0, 'U30')
#print( eNames )
# Create the array
rr = [];
for row in readCSV:
r = getRow( row )
rr.append( tuple(r) )
points = np.array( (rr), dtype={'names': eNames, 'formats':formats})
#print( points )
#Create the grading table.
formatsGrade = ['f4']*( len( eNames ) - 1)
formatsGrade.insert(0, 'U30')
grades = np.array( (rr), dtype={'names': eNames, 'formats':formatsGrade})
#print( grades )
#print( eNames )
#print( len( eNames ) )
for n in eNames[1:]:
#print( grades[n] )
grades[n] = grades[n]/ max( max( grades[n] ), 1 )
grades[n] = np.maximum( np.round( ( 4*( 6/(1-0.1)*(grades[n]-1)+10 ))/4, 2), 4)
#print( grades )
#Get the needed columns (exercises) only
grades = leaveFields(grades, tul[i])
#print( grades.dtype.names[1:] )
#print( grades )
if 1:
names = grades['Name']
nameInd = [j for j,i in enumerate(names) if name.lower() in i.lower()]
average = 4
if nameInd:
nameInd = nameInd[0]
pp = list( grades[nameInd] )
print( pp[0] )
for ni, n in enumerate( grades.dtype.names[1:] ):
print( "Arvosana: " + str(pp[ni+1]) , end="\t" )
print(n)
average = np.round( np.average( pp[1:] ), 1)
else:
print("Et ole tehnyt yhtään tehtävää tästä luvusta! Tee vielä:")
for ni, n in enumerate( grades.dtype.names[1:] ):
print(n)
print( "-> Keskiarvo: " + str( average ) )
ave = ave + average
print("\n")
print('---------------')
print( 'Kaikkien tehtävien keskiarvo: ', np.round( ave/lkm, 1) )
print("\n\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment