Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import zipfile
  2. import json
  3. import csv
  4. import pandas as pd
  5.  
  6.  
  7. def main():
  8. read_zip_file()
  9.  
  10.  
  11.  
  12.  
  13. # viide https://codeyarns.com/2013/10/03/how-to-read-contents-of-zip-file-in-python/
  14. def read_zip_file():
  15. csv_file = list()
  16. columns1 = ["keskmine_kiirus", "kiiruse_standardhälve", "hiire_koguteekond",
  17. "hiire_alguse_ja_lõpppunkti_vaheline_suhe", "sugu", "vanus", "faili_nimi"]
  18. FILE_PATH = ""
  19. zfile = zipfile.ZipFile(FILE_PATH)
  20. mouse_logger_att = 'mlogdict'
  21. for zipInfo in zfile.infolist():
  22. json_string = get_json(zfile, zipInfo)
  23. mlogdict = json_string[mouse_logger_att]
  24. new_dict = {k: speed(v)for (k, v) in mlogdict.items()}
  25. new_dict['sugu'] = json_string['sugu']
  26. new_dict['vanus'] = json_string['vanus']
  27. new_dict['faili_nimi'] = zipInfo.filename
  28. csv_file.append(new_dict)
  29. df = pd.DataFrame(csv_file, columns=columns1, dtype=int)
  30. print(df)
  31. df.to_csv("/Users/henrivajak/Desktop/file1.csv", index=False, columns=columns1)
  32.  
  33.  
  34. def speed(v):
  35. mouse_journey = get_mouse_journey(v)
  36. elapsed_time = v[-1][-1] - v[0][-1]
  37. return mouse_journey/elapsed_time
  38.  
  39.  
  40. def get_mouse_journey(exerice_matrix):
  41. # first element is x and then y and the timestamp
  42. mouse_journey = 0
  43. last_element = []
  44. for index, value in enumerate(exerice_matrix):
  45. if index == 0:
  46. last_element = value
  47. continue
  48. if index != len(exerice_matrix):
  49. mouse_journey += pythagrous(value[0], value[1], last_element[0], last_element[1])
  50. last_element = value
  51. return mouse_journey
  52.  
  53.  
  54. def pythagrous(source_x, source_y, target_x, target_y):
  55. return (((target_x - source_x) ** 2) + ((target_y - source_y) ** 2)) ** (1/2)
  56.  
  57.  
  58. if __name__ == '__main__':
  59. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement