1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| import pygame import sys import numpy as np
def Calculate_RPO(interval1, interval2): interval1_len = interval1.right - interval1.left interval2_len = interval2.right - interval2.left max_len = max(interval1_len, interval2_len)
RPO = (min(interval1.right, interval2.right) - max(interval1.left, interval2.left)) / max_len return RPO
def interval_similarity_function(interval1, interval2): RPO = Calculate_RPO(interval1, interval2) return 1 / (1 + np.exp(-RPO))
def interval_similarity_loss(interval1, interval2): RPO = Calculate_RPO(interval1, interval2) return 1 - RPO
def interval_overlap_ratio(interval1, interval2): interval1_len = interval1.right - interval1.left interval2_len = interval2.right - interval2.left if interval1.right <= interval2.left or interval1.left >= interval2.right: ior = - ((interval1.right - interval1.left) + (interval2.right - interval2.left)) / interval2_len elif interval1.left <= interval2.left and interval1.right >= interval2.right: ior = - (interval1.right - interval1.left) / interval2_len elif interval1.left >= interval2.left and interval1.right <= interval2.right: ior = - (interval2.right - interval2.left) / interval2_len elif interval1.left <= interval2.left and interval1.right <= interval2.right: ior = ((interval1.right - interval2.left)-(interval2.left-interval1.left+interval2.right-interval1.right)) / interval2_len else: ior = ((interval2.right - interval1.left)-(interval1.left-interval2.left+interval1.right-interval2.right)) / interval2_len return ior
def interval_deviation_degree(interval1, interval2): interval1_len = interval1.right - interval1.left interval2_len = interval2.right - interval2.left if interval1.right <= interval2.left or interval1.left >= interval2.right: idd = - ((interval1.right - interval1.left) + (interval2.right - interval2.left)) / interval2_len elif interval1.left <= interval2.left and interval1.right >= interval2.right: idd = - (interval2.left-interval1.left+interval1.right-interval2.right) / interval2_len elif interval1.left >= interval2.left and interval1.right <= interval2.right: idd = - (interval1.left-interval2.left+interval2.right-interval1.right) / interval2_len elif interval1.left <= interval2.left and interval1.right <= interval2.right: idd = - (interval2.left-interval1.left+interval2.right-interval1.right) / interval2_len else: idd = - (interval1.left-interval2.left+interval1.right-interval2.right) / interval2_len return idd
return 1 - ior
pygame.init()
screen_width = 800 screen_height = 400 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('可视化量化指标')
white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) blue = (0, 0, 255)
font = pygame.font.SysFont(None, 24)
slider_height = 20 slider1 = pygame.Rect(200, (screen_height - slider_height) // 2 - 10, 100, slider_height+10) slider2 = pygame.Rect(500, (screen_height - slider_height) // 2, 100, slider_height)
selected_slider = None resizing = False
running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN: if slider1.collidepoint(event.pos): if abs(slider1.right - event.pos[0]) < 10: resizing = True selected_slider = slider1 elif slider2.collidepoint(event.pos): if abs(slider2.right - event.pos[0]) < 10: resizing = True selected_slider = slider2
if event.type == pygame.MOUSEBUTTONUP: selected_slider = None resizing = False
if event.type == pygame.MOUSEMOTION: if selected_slider is not None and not resizing: selected_slider.x = event.pos[0] - selected_slider.width // 2 elif selected_slider is not None and resizing: selected_slider.width = max(10, event.pos[0] - selected_slider.x)
slider1.x = max(0, min(slider1.x, screen_width - slider1.width)) slider2.x = max(0, min(slider2.x, screen_width - slider2.width))
screen.fill(white) pygame.draw.line(screen, black, (0, screen_height // 2), (screen_width, screen_height // 2), 2) pygame.draw.rect(screen, red , slider1) pygame.draw.rect(screen, blue, slider2)
slider_A_text = font.render("A", True, black) slider_B_text = font.render("B", True, black) screen.blit(slider_A_text, (slider1.left + slider1.width//2 - 5, screen_height//2 - 10)) screen.blit(slider_B_text, (slider2.left + slider2.width//2 - 5, screen_height//2 - 10))
slider1_left_text = font.render(f"{slider1.left}", True, red) slider1_right_text = font.render(f"{slider1.right}", True, red) screen.blit(slider1_left_text, (slider1.left, slider1.top - 25)) screen.blit(slider1_right_text, (slider1.right - slider1_right_text.get_width(), slider1.top - 25))
slider2_left_text = font.render(f"{slider2.left}", True, blue) slider2_right_text = font.render(f"{slider2.right}", True, blue) screen.blit(slider2_left_text, (slider2.left, slider2.bottom + 15)) screen.blit(slider2_right_text, (slider2.right - slider2_right_text.get_width(), slider2.bottom + 15))
rpo = Calculate_RPO(slider1, slider2) rpo_text = font.render(f"Relative Position Overlap (RPO): {rpo:.4f}", True, black) screen.blit(rpo_text, (10, 10))
isf = interval_similarity_function(slider1, slider2) similarity_text = font.render(f"Interval similarity Function (ISF): {isf:.4f}", True, black) screen.blit(similarity_text, (10, 40))
isl = interval_similarity_loss(slider1, slider2) similarity_loss_text = font.render(f"Interval similarity Loss (ISL): {isl:.4f}", True, black) screen.blit(similarity_loss_text, (10, 70))
ior = interval_overlap_ratio(slider1, slider2) overlap_ratio_text = font.render(f"Interval Overlap Ratio (IOR): {ior:.4f}", True, black) screen.blit(overlap_ratio_text, (10, 100))
idd = interval_deviation_degree(slider1, slider2) deviation_degree_text = font.render(f"Interval Deviation Degree (IDD): {idd:.4f}", True, black) screen.blit(deviation_degree_text, (10, 130))
pygame.display.flip()
pygame.time.Clock().tick(60)
|