Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- from collections import defaultdict
- path = "C:\\Users\\Malcolm\\Documents\\IzzyANALYSIS\\CombineTables\\"
- header = "Block lesion key Size (mm) Iron rim Completeness Arperture direction Iron dot Hypoointensity Proportion lesion ocupied Shape of dot Speckling Localisation CVS Center ID diagnosisdisease_course disease_number age_at_MRI gender_f1 time_since_diagnosis time_since_first_symptoms EDSS aqp4_pos"
- # Patients
- file = open(path + "Patients.csv", "r")
- patients = [i.split(',') for i in file.read().split('\n')]
- patients = {(i[1].lower() if i[0] != 'Poznan' else i[1][7:].lower()) : '\t'.join(i) for i in patients}
- file.close()
- # Batches
- for batch in [2.5]:
- file = open(path + "Batch" + str(batch) + ".csv", "r")
- rFile = open(path + "Batch" + str(batch) + "R.csv", "r")
- data = [i.split(',') for i in file.read().split('\n')]
- relation = [i.split(',') for i in rFile.read().split('\n')]
- file.close()
- rFile.close()
- relation = {i[2][:i[2].find('_') if batch < 4 else -3].lower() : i[1][:re.search('_B\d_', i[1]).span()[0]] if batch != 2.5 else (lambda x : i[1][(x[0]+4):(x[1] - 1)])(re.search('_B\d_.*_', i[1]).span()) for i in relation if i[0] != ''}
- patientDict = defaultdict(str)
- id = None
- for i in data:
- if i[0] != '':
- temp = '\n' + '\t'.join(i)
- try:
- id = relation[i[0].lower()]
- try:
- if id not in patientDict:
- temp += '\t' + patients[id.lower()]
- except KeyError:
- id = "Patient " + id + " does not exist"
- if id not in patientDict:
- temp += '\t\t' + id
- except KeyError:
- id = "Scan " + i[0] + " has no batch record"
- if id not in patientDict:
- temp += '\t\t' + id
- patientDict[id] += temp
- elif not id is None:
- patientDict[id] += '\n' + '\t'.join(i)
- else:
- input("AAAH")
- file = open(path + "Batch" + str(batch) + "Data.txt", "w+")
- file.write(header + ''.join([patientDict[i] for i in sorted(patientDict.keys())]))
- file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement