Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. import re
  2. from collections import defaultdict
  3.  
  4. path = "C:\\Users\\Malcolm\\Documents\\IzzyANALYSIS\\CombineTables\\"
  5. 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"
  6.  
  7. # Patients
  8.  
  9. file = open(path + "Patients.csv", "r")
  10.  
  11. patients = [i.split(',') for i in file.read().split('\n')]
  12. patients = {(i[1].lower() if i[0] != 'Poznan' else i[1][7:].lower()) : '\t'.join(i) for i in patients}
  13.  
  14. file.close()
  15.  
  16. # Batches
  17.  
  18. for batch in [2.5]:
  19. file = open(path + "Batch" + str(batch) + ".csv", "r")
  20. rFile = open(path + "Batch" + str(batch) + "R.csv", "r")
  21.  
  22. data = [i.split(',') for i in file.read().split('\n')]
  23.  
  24. relation = [i.split(',') for i in rFile.read().split('\n')]
  25.  
  26. file.close()
  27. rFile.close()
  28.  
  29. 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] != ''}
  30.  
  31. patientDict = defaultdict(str)
  32.  
  33. id = None
  34. for i in data:
  35. if i[0] != '':
  36. temp = '\n' + '\t'.join(i)
  37. try:
  38. id = relation[i[0].lower()]
  39. try:
  40. if id not in patientDict:
  41. temp += '\t' + patients[id.lower()]
  42. except KeyError:
  43. id = "Patient " + id + " does not exist"
  44. if id not in patientDict:
  45. temp += '\t\t' + id
  46.  
  47. except KeyError:
  48. id = "Scan " + i[0] + " has no batch record"
  49. if id not in patientDict:
  50. temp += '\t\t' + id
  51.  
  52. patientDict[id] += temp
  53.  
  54. elif not id is None:
  55. patientDict[id] += '\n' + '\t'.join(i)
  56.  
  57. else:
  58. input("AAAH")
  59.  
  60. file = open(path + "Batch" + str(batch) + "Data.txt", "w+")
  61.  
  62. file.write(header + ''.join([patientDict[i] for i in sorted(patientDict.keys())]))
  63.  
  64. file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement