Luis Wu commited on
Commit
78de124
1 Parent(s): e651bde

draw predict

Browse files
Files changed (3) hide show
  1. analysis_db.py +40 -3
  2. app.py +9 -2
  3. draw_db.py +9 -5
analysis_db.py CHANGED
@@ -5,14 +5,32 @@ from util.debug import debug_print
5
  from draw_db import draw_lists, min_max_list, up_down_list
6
  from util.date_util import format_date
7
 
 
 
 
 
 
 
 
8
 
9
  def get_filename(file_obj):
10
  return file_obj.name
11
 
12
 
13
- def read_db(db_file):
14
  db_name = get_filename(db_file)
15
- debug_print('read_db, db_name = ', db_name)
 
 
 
 
 
 
 
 
 
 
 
16
  return read_section(db_name, 'tb_section')
17
 
18
 
@@ -62,6 +80,20 @@ def clamp_list(_list, _min, _max):
62
  return [clamp(x, _min, _max) for x in _list]
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def read_sample(conn, section_id, tb_name):
66
  sql_str = ' SELECT section_id, _sample_id, _sample_end_id, section_date, section_end_date FROM ' + str(
67
  tb_name) + ' WHERE section_id == ' + str(section_id)
@@ -81,6 +113,7 @@ def read_sample(conn, section_id, tb_name):
81
  acc_list = []
82
  light_list = []
83
  screen_list = []
 
84
  sample_sql = "SELECT _id, date, max, min, acc_max_dx, acc_max_dy, acc_max_dz, avg, max_snoring FROM sample_table WHERE _id >= " + str(
85
  _sample_id) + ' AND ' + ' _id <= ' + str(_sample_end_id)
86
  sample_cursor = conn.execute(sample_sql)
@@ -109,7 +142,11 @@ def read_sample(conn, section_id, tb_name):
109
  if len(screen_list) > 0:
110
  screen_list = min_max_list(screen_list, 1, 11)
111
 
112
- return draw_lists(title_str, date_list, max=max_list, min=min_list, acc=acc_list, screen=screen_list, light=light_list)
 
 
 
 
113
 
114
 
115
  if __name__ == '__main__':
 
5
  from draw_db import draw_lists, min_max_list, up_down_list
6
  from util.date_util import format_date
7
 
8
+ MIN_FROM = 0
9
+ MIN_TO = 25
10
+ LIGHT_FROM = 0
11
+ LIGHT_TO = 5
12
+ ACC_FROM = 0
13
+ ACC_TO = 10
14
+
15
 
16
  def get_filename(file_obj):
17
  return file_obj.name
18
 
19
 
20
+ def read_db(db_file, min_from, min_to, light_from, light_to, acc_from, acc_to):
21
  db_name = get_filename(db_file)
22
+ global MIN_FROM
23
+ global MIN_TO
24
+ global LIGHT_FROM
25
+ global LIGHT_TO
26
+ global ACC_FROM
27
+ global ACC_TO
28
+ MIN_FROM = min_from
29
+ MIN_TO = min_to
30
+ LIGHT_FROM = light_from
31
+ LIGHT_TO = light_to
32
+ ACC_FROM = acc_from
33
+ ACC_TO = acc_to
34
  return read_section(db_name, 'tb_section')
35
 
36
 
 
80
  return [clamp(x, _min, _max) for x in _list]
81
 
82
 
83
+ def predict_sleeping(min_list, light_list, acc_list):
84
+ res = []
85
+
86
+ cnt = min(len(min_list), len(light_list), len(acc_list))
87
+ for idx in range(cnt):
88
+ min_from_to = MIN_FROM <= min_list[idx] <= MIN_TO
89
+ light_from_to = LIGHT_FROM <= light_list[idx] <= LIGHT_TO
90
+ acc_from_to = ACC_FROM <= acc_list[idx] <= ACC_TO
91
+ sleeping = 1 if min_from_to and light_from_to and acc_from_to else 0
92
+ res.append(sleeping)
93
+
94
+ return res
95
+
96
+
97
  def read_sample(conn, section_id, tb_name):
98
  sql_str = ' SELECT section_id, _sample_id, _sample_end_id, section_date, section_end_date FROM ' + str(
99
  tb_name) + ' WHERE section_id == ' + str(section_id)
 
113
  acc_list = []
114
  light_list = []
115
  screen_list = []
116
+ predict_list = []
117
  sample_sql = "SELECT _id, date, max, min, acc_max_dx, acc_max_dy, acc_max_dz, avg, max_snoring FROM sample_table WHERE _id >= " + str(
118
  _sample_id) + ' AND ' + ' _id <= ' + str(_sample_end_id)
119
  sample_cursor = conn.execute(sample_sql)
 
142
  if len(screen_list) > 0:
143
  screen_list = min_max_list(screen_list, 1, 11)
144
 
145
+ if len(min_list) > 0 and len(light_list) > 0 and len(acc_list) > 0:
146
+ predict_list = predict_sleeping(min_list, light_list, acc_list)
147
+
148
+ return draw_lists(title_str, date_list, max=max_list, min=min_list, acc=acc_list, screen=screen_list,
149
+ light=light_list, predict=predict_list)
150
 
151
 
152
  if __name__ == '__main__':
app.py CHANGED
@@ -10,12 +10,19 @@ def plot_pens(alpha):
10
  plt.scatter(x=df_pens['bill_length_mm'], y=df_pens['bill_depth_mm'])
11
  return fig
12
 
 
 
 
 
 
 
 
13
  iface = gr.Interface(
14
  fn=read_db,
15
- inputs=['file'],
16
  outputs=['plot', 'plot', 'plot', 'plot', 'plot', 'plot', 'plot', 'plot', 'plot', 'plot'],
17
  title="Sleep Data test",
18
  description="Let's talk pens.",
19
  article="Talk more about Penguins here, shall we?",
20
  theme='peach'
21
- ).launch()
 
10
  plt.scatter(x=df_pens['bill_length_mm'], y=df_pens['bill_depth_mm'])
11
  return fig
12
 
13
+
14
+ min_from = gr.Slider(label="min_from", value=0, minimum=0, maximum=120, step=1)
15
+ min_to = gr.Slider(label="min_to", value=25, minimum=0, maximum=120, step=1)
16
+ light_from = gr.Slider(label="light_from", value=0, minimum=0, maximum=120, step=1)
17
+ light_to = gr.Slider(label="light_to", value=5, minimum=0, maximum=120, step=1)
18
+ acc_from = gr.Slider(label="acc_from", value=0, minimum=0, maximum=120, step=1)
19
+ acc_to = gr.Slider(label="acc_to", value=10, minimum=0, maximum=120, step=1)
20
  iface = gr.Interface(
21
  fn=read_db,
22
+ inputs=['file', min_from, min_to, light_from, light_to, acc_from, acc_to],
23
  outputs=['plot', 'plot', 'plot', 'plot', 'plot', 'plot', 'plot', 'plot', 'plot', 'plot'],
24
  title="Sleep Data test",
25
  description="Let's talk pens.",
26
  article="Talk more about Penguins here, shall we?",
27
  theme='peach'
28
+ ).launch()
draw_db.py CHANGED
@@ -31,12 +31,16 @@ def draw_lists(title='', date_list=None, **kv_list):
31
 
32
  idx_list = 0
33
  idx_axs = 0
34
- rows = math.ceil(len(kv_list.items()) / 3)
35
  fig, axs = plt.subplots(rows, 1, sharex=True)
36
- for key, value in kv_list.items():
37
- if type(key) is str and type(value) is list:
38
- axs[idx_axs].plot(date_list, value, label=key)
39
- axs[idx_axs].legend()
 
 
 
 
40
  idx_list = idx_list + 1
41
  idx_axs = int(idx_list / 3)
42
 
 
31
 
32
  idx_list = 0
33
  idx_axs = 0
34
+ rows = math.ceil(len(kv_list.items()) / 3) + 1
35
  fig, axs = plt.subplots(rows, 1, sharex=True)
36
+ for key, values in kv_list.items():
37
+ if type(key) is str and type(values) is list:
38
+ if key == 'predict':
39
+ axs[rows - 1].plot(date_list, values, label=key)
40
+ axs[rows - 1].legend()
41
+ else:
42
+ axs[idx_axs].plot(date_list, values, label=key)
43
+ axs[idx_axs].legend()
44
  idx_list = idx_list + 1
45
  idx_axs = int(idx_list / 3)
46