sqlmask_converter.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import pandas as pd
  2. import datetime
  3. import sqlalchemy
  4. def sqlmask_converter(start_date, end_date):
  5. """Переводим маску из SQL БД в бинарный вид для транспортировки в noSQL БД"""
  6. conn = 'postgresql+psycopg2://postgres:qwerty@localhost:5432/prisma'
  7. engine = sqlalchemy.create_engine(conn)
  8. connect = engine.connect()
  9. mask_prisma_1 = pd.read_sql("SELECT * FROM mask_1_params WHERE date >= '{}-{:02}-{:02}' AND date <= \
  10. '{}-{:02}-{:02}' ORDER BY date asc;".format(start_date.year, start_date.month, start_date.day, end_date.year,
  11. end_date.month, end_date.day), connect)
  12. mask_prisma_2 = pd.read_sql("SELECT * FROM mask_2_params WHERE date >= '{}-{:02}-{:02}' AND date <= \
  13. '{}-{:02}-{:02}' ORDER BY date asc;".format(start_date.year, start_date.month, start_date.day, end_date.year,
  14. end_date.month, end_date.day), connect)
  15. # amp_data_prisma_1_mask = mask_prisma_1[
  16. # ['amp1_mask', 'amp2_mask', 'amp3_mask', 'amp4_mask', 'amp5_mask', 'amp6_mask', 'amp7_mask', 'amp8_mask',
  17. # 'amp9_mask', 'amp10_mask', 'amp11_mask', 'amp12_mask', 'amp13_mask',
  18. # 'amp14_mask', 'amp15_mask', 'amp16_mask']]
  19. amp_data_prisma_1_mask = mask_prisma_1[
  20. ['amp16_mask', 'amp15_mask', 'amp14_mask', 'amp13_mask', 'amp12_mask', 'amp11_mask', 'amp10_mask', 'amp9_mask',
  21. 'amp8_mask', 'amp7_mask', 'amp6_mask', 'amp5_mask', 'amp4_mask',
  22. 'amp3_mask', 'amp2_mask', 'amp1_mask']]
  23. amp_data_prisma_2_mask = mask_prisma_2[
  24. ['amp16_mask', 'amp15_mask', 'amp14_mask', 'amp13_mask', 'amp12_mask', 'amp11_mask', 'amp10_mask', 'amp9_mask',
  25. 'amp8_mask', 'amp7_mask', 'amp6_mask', 'amp5_mask', 'amp4_mask',
  26. 'amp3_mask', 'amp2_mask', 'amp1_mask']]
  27. n_data_prisma_1_mask = mask_prisma_1[
  28. ['n16_mask', 'n15_mask', 'n14_mask', 'n13_mask', 'n12_mask', 'n11_mask', 'n10_mask', 'n9_mask',
  29. 'n8_mask', 'n7_mask', 'n6_mask', 'n5_mask', 'n4_mask',
  30. 'n3_mask', 'n2_mask', 'n1_mask']]
  31. n_data_prisma_2_mask = mask_prisma_2[
  32. ['n16_mask', 'n15_mask', 'n14_mask', 'n13_mask', 'n12_mask', 'n11_mask', 'n10_mask', 'n9_mask',
  33. 'n8_mask', 'n7_mask', 'n6_mask', 'n5_mask', 'n4_mask',
  34. 'n3_mask', 'n2_mask', 'n1_mask']]
  35. binary_n_mask_1 = {}
  36. binary_n_mask_2 = {}
  37. binary_amp_mask_1 = {}
  38. binary_amp_mask_2 = {}
  39. for i in amp_data_prisma_1_mask.index:
  40. amp_mask_params = amp_data_prisma_1_mask.iloc[i]
  41. n_mask_params = n_data_prisma_1_mask.iloc[i]
  42. binary_amp_mask_1[str(mask_prisma_1['date'][i])] = {
  43. 'mask_of_hit_counters_a': int(''.join(amp_mask_params.astype(str)), 2),
  44. 'multiplicity_of_hit_counters_a': sum(amp_mask_params)}
  45. binary_n_mask_1[str(mask_prisma_1['date'][i])] = {
  46. 'mask_of_hit_counters_n': int(''.join(n_mask_params.astype(str)), 2),
  47. 'multiplicity_of_hit_counters_n': sum(n_mask_params)}
  48. for i in amp_data_prisma_2_mask.index:
  49. amp_mask_params = amp_data_prisma_2_mask.iloc[i]
  50. n_mask_params = n_data_prisma_2_mask.iloc[i]
  51. binary_amp_mask_2[str(mask_prisma_2['date'][i])] = {
  52. 'mask_of_hit_counters_a': int(''.join(amp_mask_params.astype(str)), 2),
  53. 'multiplicity_of_hit_counters_a': sum(amp_mask_params)}
  54. binary_n_mask_2[str(mask_prisma_2['date'][i])] = {
  55. 'mask_of_hit_counters_n': int(''.join(n_mask_params.astype(str)), 2),
  56. 'multiplicity_of_hit_counters_n': sum(n_mask_params)}
  57. return binary_n_mask_1, binary_amp_mask_1, binary_n_mask_2, binary_amp_mask_2
  58. if __name__ == '__main__':
  59. date_time_start = datetime.date(2021, 12, 1) # посмотреть почему не собирается конец дня 2018-04-22
  60. date_time_stop = datetime.date(2021, 12, 31)
  61. sqlmask_converter(date_time_start, date_time_stop)
  62. print('test')