| 7 | plt.style.use('ggplot') |
| 8 | |
| 9 | class Analysis: |
| 10 | def __init__(self, database: str, outputPath: str, criteria: float) -> None: |
| 11 | self.database = database |
| 12 | self.outputPath = outputPath |
| 13 | self.figPath = os.path.join(outputPath, 'figs/') |
| 14 | self.criteria = criteria |
| 15 | |
| 16 | def getData(self, sql: str) -> list[tuple]: |
| 17 | conn = sqlite3.connect(self.database) |
| 18 | cur = conn.cursor() |
| 19 | cur.execute(sql) |
| 20 | data = cur.fetchall() |
| 21 | conn.close() |
| 22 | return list(zip(*data)) |
| 23 | |
| 24 | def getCollisionStages( |
| 25 | self, frame: list[int], collision: list[float] |
| 26 | ) -> list[list[int]]: |
| 27 | stageStart = 0 |
| 28 | stageEnd = frame[-1] |
| 29 | stages = [] |
| 30 | for i in range(len(frame)-1): |
| 31 | if collision[i] > self.criteria and collision[i+1] < self.criteria: |
| 32 | stageStart = frame[i] |
| 33 | if collision[i] < self.criteria and collision[i+1] > self.criteria: |
| 34 | stageEnd = frame[i] |
| 35 | stages.append([stageStart, stageEnd]) |
| 36 | if stageEnd < stageStart: |
| 37 | stages.append([stageStart, frame[-1]]) |
| 38 | |
| 39 | return stages |
| 40 | |
| 41 | def collisionAnalysis(self): |
| 42 | sql = """SELECT frame, collision from evaluationINFO;""" |
| 43 | frame, collision = self.getData(sql) |
| 44 | |
| 45 | stages = self.getCollisionStages(frame, collision) |
| 46 | |
| 47 | plt.figure(figsize=(10, 6)) |
| 48 | plt.plot(frame, collision, color='#54a0ff') |
| 49 | plt.plot(frame, [self.criteria for _ in range(len(frame))], color='#ff6b6b') |
| 50 | plt.legend(['TTC', 'Criteria']) |
| 51 | plt.xlabel('Frame') |
| 52 | plt.ylabel('TTC (s)') |
| 53 | plt.ylim((0, 21)) |
| 54 | plt.savefig(self.figPath + 'collision.svg', bbox_inches='tight') |
| 55 | plt.close() |
| 56 | |
| 57 | header = '# Collision reports\n\n' |
| 58 | comments = '''The collision criteria for TTC is {} s, a total of {} |
| 59 | potential collisions occurred in this simulation.\n\n'''.format( |
| 60 | self.criteria, len(stages) |
| 61 | ) |
| 62 | if stages: |
| 63 | table = '|Stage|Start frame|End frame|\n' |
| 64 | table += '|:----:|----:|----:|\n' |
| 65 | for i in range(len(stages)): |
| 66 | table += '|{}|{}|{}|\n'.format(i, stages[i][0], stages[i][1]) |