def new_line(title, results): data_rps = [] data_clients = [] for result_item in results: rps = result_item.rps if rps is None: # no data gathered continue data_rps.append(rps) data_clients.append(result_item.clients) scatter = go.Scatter( x=data_clients, y=data_rps, mode='lines', name=title, hoverinfo='x+y+name', line={ 'shape': 'spline', # for stable coloring across runs 'color': ColorHash(title).hex, }, ) return scatter
def add_traces(fig, data, lk_values, modus='neu', zahlenmodus='absolut', columnname='MeldeTag_AnzahlFallNeu', legname='Fälle', grid=(1,1), linetype='solid', showlegend=True): row, col = grid for lk in lk_values: y_vals = data.loc[data.Landkreis==lk, columnname] if modus=='neu': y_vals = y_vals.rolling(window=7).mean() if zahlenmodus=='100k': y_vals = y_vals*(10**5)/data.loc[data.Landkreis==lk, 'Einwohner']*7 if modus=='summe': y_vals = y_vals.cumsum() fig.add_trace(go.Scatter(x=data.index[data.Landkreis==lk], y=y_vals, name=lk + ', ' + legname, showlegend=showlegend, line=dict(dash=linetype, color=ColorHash(lk).hex), legendgroup=lk), row=row, col=col) return fig
def make_json(solution): events = [] for idx, sol in enumerate(solution): # start_date = datetime.combine(begin + timedelta(sol[2]), # time(hour=9, minute=0)) # end_date = datetime.combine(begin + timedelta(sol[3]), # time(hour=16, minute=0)) start_date = begin + timedelta(sol[2]) end_date = begin + timedelta(sol[3]) if hasattr(sol[0], 'label'): title = sol[0].label else: title = sol[0].name events.append({ 'id': "ev-{0}".format(idx), 'resourceId': sol[1].name, 'start': start_date.isoformat(), 'end': end_date.isoformat(), 'title': title, 'color': ColorHash(title).hex }) import json with open('events.json', 'w') as outfile: json.dump(events, outfile)
async def update_colors_for_guild(self, guild): self.logger.info("start updating colors") if len(self.teamsData) == 0: self.logger.info("no teams data. aborting...") return clientRolePosition = 0 for role in guild.roles: if role.name == 'Echo EU - VRML Bridge': clientRolePosition = role.position break allRoles = {} for role in guild.roles: if role.position < clientRolePosition and role.position != 0: allRoles[role.name] = role for key in self.teamsData: team = self.teamsData[key] if team['name'] in allRoles: if team['division'] == 'Master': #is top 10 team color = ColorHash(team['name']) await allRoles[ team['name']].edit(colour=discord.Colour.from_rgb( color.rgb[0], color.rgb[1], color.rgb[2])) else: await allRoles[team['name'] ].edit(colour=discord.Colour.default()) self.logger.info("finished updating colors")
def _query_link_words(self, context, name, value, class_, prepend=None, append=None): """Splits a list of words and makes a query link to each separately""" from trac.ticket.query import QueryModule if not (isinstance(value, basestring) and # None or other non-splitable self.env.is_component_enabled(QueryModule)): return value args = arg_list_to_args(parse_arg_list(self.ticketlink_query)) items = [] if prepend: items.extend(prepend) for i, word in enumerate(re.split(r'([;,\s]+)', value)): if i % 2: items.append(' ') elif word: backgroundColor = self.keyword_labels_section.get(word.lower()) fontColor = self.keyword_labels_section.get(word.lower() + '.font_color', 'white') if not backgroundColor: backgroundColor = ColorHash(word.encode('utf-8')).hex styles = { 'backgroundColor': backgroundColor, 'fontColor': fontColor, } word_args = args.copy() word_args[name] = '~' + word items.append(tag.a(word, style='background-color: {backgroundColor}; color: {fontColor}'.format(**styles), class_=class_, href=context.href.query(word_args))) if append: items.extend(append) return tag(items)
def handle(conn, addr, currentUser, server, command): try: target = command[1] pmData = command[2:] for k, v in server.users.items(): if target == v.username: currentDT = datetime.datetime.now() dt = str(currentDT.strftime("%d-%m-%Y %H:%M:%S")) metadata = [ "[PM] " + currentUser.username, ColorHash(currentUser.username).hex, dt ] sendMessage(currentUser.conn, currentUser.secret, "outboundMessage", " ".join(pmData), metadata=metadata) sendMessage(v.conn, v.secret, "outboundMessage", " ".join(pmData), metadata=metadata) if config.GetSetting("storePmlogs", "Logging") == "True": dbLogger.logPM(server, currentUser, v, " ".join(pmData)) return True return False except IndexError: return False
def create_color_dict(self): color_dict = dict( map(lambda x: (x, ColorHash(x).hex), set(self.df.values.flatten()))) color_dict[self.non_inf['ex']] = "#000000" color_dict[self.non_inf['NA']] = "#dddddd" color_dict[self.non_inf['wt']] = "#ffffff" return (color_dict)
def objs(): return ( "foo", "bar", None, [0, 1], {1, 2}, {"a": 0}, ColorHash("w00t"), )
async def on_message(self, message): if message.author == self.user: return channel_id = message.channel.id channel_name = message.channel.name author_id = message.author.id author_name = message.author.name text = message.content #with db as db_session: channel, _ = get_or_create(db_session, Channel, name=channel_name) author, created = get_or_create(db_session, User, nick=author_name) if created: author.color = ColorHash(author_name).hex if message.content.startswith('!save'): print("Discussion:", message.content) reg = re.compile( r"!save (?P<nick>.+) (?P<lines>\d+) (?P<topic>.+)") m = reg.match(message.content) if m: # Create a discussion starter, _ = get_or_create(db_session, User, nick=m.group('nick')) msg_count = int(m.group('lines')) # First message am = db_session.query(Message).filter( channel == channel).order_by( Message.when.desc()).limit(msg_count) start_time = am[am.count() - 1].when disc = Discussion(starter=starter, channel=channel, topic=m.group('topic'), time_start=start_time, time_end=message.created_at) db_session.add(disc) db_session.commit() reply = "Discussion saved, {}/{}/{}".format( FRONT_BASE, channel_name, disc.id) await message.channel.send(reply) else: msg, _ = get_or_create(db_session, Message, author=author, when=message.created_at, channel=channel) for m in message.mentions: print(m) # Append attachments for a in message.attachments: if len(text) > 0: text += "\r\n" text += a.url msg.text = text db_session.commit()
def get_color(hash): """ Returns an rgb-color (as string, which can be using in plotly) from a given hash, if no color for that string was specified in the config file. :param hash: the string that is translated into a color :return: a color (as string) """ if hash in config.colors: rgb = re.findall(r'\d+', config.colors[hash]) else: rgb = ColorHash(hash).rgb return 'rgb({0}, {1}, {2})'.format(rgb[0], rgb[1], rgb[2])
def parts_by_name_and_cat(): parts_dict = {} by_category = {} df = parts_df() for _, row in df.iterrows(): seq = row['Sequence'] if seq != 'None': dna = jdna.Sequence(row['Sequence']) dna.annotate(None, None, row['Part'], color=ColorHash(row['Part'] + row['Annotation']).hex) parts_dict[row['Part']] = dna by_category.setdefault(row['Annotation'], {}) by_category[row['Annotation']][row['Part']] = dna return parts_dict, by_category
def planets(): """returns checkout planet info for all attendees""" planets = defaultdict(lambda: {'topic_mixture': [], 'checkouts': 0}) for checkout in db['checkouts'].all(): book_id = checkout['book_id'] topic_mixture = LIBRARY['books'][book_id]['topics'] planets[checkout['attendee_id']]['topic_mixture'].append(topic_mixture) planets[checkout['attendee_id']]['checkouts'] += 1 for id, d in planets.items(): d['topic_mixture'] = mix_topics(*d['topic_mixture']) d['color'] = ColorHash(id).hex d['name'] = name_from_id(id) return jsonify(**planets)
def ensure_checkpoint_is_drawn(cp): if cp.name not in nodes: next_node_idx[0] += 1 nodes[cp.name] = next_node_idx[0] if cp.name.startswith(GENERATED_CHECKPOINT_PREFIX): # colors generated checkpoints based on their hash color = ColorHash(cp.name[-GENERATED_HASH_LENGTH:]).hex graph.add_node(next_node_idx[0], label=utils.cap_length(cp.name), style="filled", fillcolor=color) else: graph.add_node(next_node_idx[0], label=utils.cap_length(cp.name))
def gen_fb(fb_data, highlight, known): colors = {} ret = "" for key in known.keys(): color = ColorHash(key).hex colors[key] = color ret += '<div style="display: inline-block;">' ret += '<div style="padding-right: 20px;"><div style="display: inline-block; width: 17px; height: 17px; background-color: {};"></div> {} </div>'.format( color, key) ret += '</div>' ret += '<div class="grid-container" style="cursor: default; user-select: none;">' for i, bit in enumerate(fb_data): title = "" bit_color = None if i in highlight: title = "highlight" cls = "grid-item-highlight" elif bit: title = "set" cls = "grid-item-set" else: title = "unset" cls = "grid-item-unset" for key, bits in known.items(): if i in bits: title = key bit_color = colors[key] if bit_color is not None: ret += '<div style="background-color: {};" class="{}">'.format( bit_color, cls) else: ret += '<div class="{}">'.format(cls) ret += '<span title="{}">{}</span>'.format(title, bit) ret += '</div>' ret += '</div>' return ret
def __init__( self, labels: Sequence[str], pos: TypePoint = None, width: int = None, gap: int = None ): """ :param labels: Strings to get colors for. :param pos: Position (x, y) to place top left legend corner. Default: (20, 20) :param width: Default: 250 :param gap: Base gap (also a height for each color stripe). Default: 25 """ self.labels: Dict[str, TypeRgb] = {label: ColorHash(label).rgb for label in labels} self._gap = gap or 25 self._pos = pos or (20, 20) self._width = width or 250
def planet(id): """returns attendee checkout planet info""" # get topic mixtures for books attendee has checked out checkouts = [] topic_mixtures = [] for checkout in db['checkouts'].all(): if checkout['attendee_id'] == id: book_id = checkout['book_id'] book = LIBRARY['books'][book_id] topic_mixture = book['topics'] topic_mixtures.append(topic_mixture) checkout['topics'] = book['topics'] checkout['title'] = book['title'] checkouts.append(checkout) color = ColorHash(id) topic_mixture = mix_topics(*topic_mixtures) return jsonify(color=color.hex, checkouts=checkouts, topic_mixture=topic_mixture, name=name_from_id(id))
def parse_color(color: str) -> Color: """ Parse a string into a Color object. Supports strings of format: - "ffffff" - "#ffffff" - "orange" - names in CSS colors list. - any other string is hashed to a deterministic color. >>> c = parse_color('red') >>> c.get_hue() == Color('red').get_hue() True >>> c = parse_color('#00ff00') >>> c.get_hue() == Color('lime').get_hue() True >>> c = parse_color('test') >>> c.get_hue() # doctest: +ELLIPSIS 0.4419... """ try: if isinstance(color, str): if len(color) == 6 and not set(color) - hex_characters: color = '#' + color elif (color.startswith('#') and len(color) == 7 and not set(color.lower()) - hex_characters): pass elif color in COLOR_NAME_TO_RGB: pass else: return Color(ColorHash(color).hex) return Color(color) else: raise ValueError() except: raise ValueError('Cannot parse color: {!r},' 'expected a hex color, a color name,' 'or a tuple of 0-255 RGB values.'.format(color))
def colour_extension(filename: str) -> Colour: ext = filename.split(".")[-1] c = ColorHash(ext) return Colour(int(c.hex[1:], 16))
def series_color_rgb(series_name: str) -> Tuple[int, int, int]: return ColorHash(series_name).rgb
def author_color(self): """Hash function that returns color from user's username and date_joined""" c = ColorHash(self.author.username) return c.hex
def _get_series_color(series: DataSeries): color = ColorHash(series.series_name) return tuple(map(lambda c: c / 255.0, color.rgb))
def components_plot(ingested_data: DataFrame) -> html.Div: """ Create and return the plots of all the components of the time series: level, trend, residual. It uses both an additive and multiplicative model, with a subplot. Parameters ---------- ingested_data : DataFrame Original time series values. Returns ------- g : dcc.Graph Examples -------- Get the `figure` attribute if you want to display this in a Jupyter notebook. >>> comp_plot = components_plot(timeseries_container.timeseries_data)[0].figure >>> comp_plot.show() """ modes = ["additive", "multiplicative"] fig = make_subplots( rows=3, cols=1, subplot_titles=[_("Trend"), _("Seasonality"), _("Residual")], shared_xaxes=True, vertical_spacing=0.05, specs=[[{ "secondary_y": True }], [{ "secondary_y": True }], [{ "secondary_y": True }]]) interpolated = ingested_data.interpolate() interpolated = interpolated.fillna(0) for mode in modes: try: result = seasonal_decompose(interpolated, model=mode) trend = result.trend seasonal = result.seasonal residual = result.resid secondary_y = False if mode == "additive" else True fig.add_trace(go.Scatter(x=trend.index, y=trend, mode='lines+markers', name=_(mode.capitalize()), legendgroup=_(mode.capitalize()), line=dict(color=ColorHash(mode).hex)), row=1, col=1, secondary_y=secondary_y) fig.add_trace(go.Scatter(x=seasonal.index, y=seasonal, mode='lines+markers', showlegend=False, name=_(mode.capitalize()), legendgroup=_(mode.capitalize()), line=dict(color=ColorHash(mode).hex)), row=2, col=1, secondary_y=secondary_y) fig.add_trace(go.Scatter(x=residual.index, y=residual, mode='lines+markers', showlegend=False, name=_(mode.capitalize()), legendgroup=_(mode.capitalize()), line=dict(color=ColorHash(mode).hex)), row=3, col=1, secondary_y=secondary_y) except ValueError: log.warning( f"Multiplicative decomposition not available for {ingested_data.columns[0]}" ) fig.update_layout(title=_("Components decomposition"), height=1000, legend_title_text=_('Decomposition model')) fig.update_yaxes(title_text="<b>" + _('Additive') + "</b>", secondary_y=False) fig.update_yaxes(title_text="<b>" + _('Multiplicative') + "</b>", secondary_y=True) g = dcc.Graph(figure=fig) warning = html.H5( _("Multiplicative model is not available for series which contain zero or negative values." )) return html.Div([g, warning])
def cross_correlation_plot(xcorr: dict): """ Create and return the cross-correlation plot for all the columns in the dataframe. The time-series column is used as target; the correlation is shown in a subplot for every modality used to compute the x-correlation. Parameters ---------- xcorr : dict Cross-correlation values. Returns ------- g : dcc.Graph Examples -------- Get the `figure` attribute if you want to display this in a Jupyter notebook. >>> xcorr_plot = cross_correlation_plot(timeseries_container.xcorr).figure >>> xcorr_plot.show() """ subplots = len(xcorr) combs = [(1, 1), (1, 2), (2, 1), (2, 2)] rows = 1 if subplots < 3 else 2 cols = 1 if subplots < 2 else 2 fig = make_subplots(rows=rows, cols=cols, subplot_titles=([*xcorr.keys()])) i = 0 for mode in xcorr: for col in xcorr[mode].columns: fig.add_trace(go.Scatter(x=xcorr[mode].index, y=xcorr[mode][col], mode='lines', name=col, legendgroup=col, line=dict(color=ColorHash(col).hex), showlegend=True if i == 0 else False), row=combs[i][0], col=combs[i][1]) i += 1 # Formula from https://support.minitab.com/en-us/minitab/18/help-and-how-to/modeling-statistics/time-series/how-to/cross-correlation/interpret-the-results/all-statistics-and-graphs/ # significance_level = DataFrame(columns=['Value'], dtype=np.float64) # for i in range(-lags, lags): # significance_level.loc[i] = 2 / np.sqrt(lags - abs(i)) # fig.add_trace( # go.Scatter(x=significance_level.index, y=significance_level['Value'], line=dict(color='gray', width=1), name='z95')) # fig.add_trace( # go.Scatter(x=significance_level.index, y=-significance_level['Value'], line=dict(color='gray', width=1), name='-z95')) fig.update_layout(title=_("Cross-correlation using different algorithms")) fig.update_xaxes(title_text=_("Lags")) fig.update_yaxes(tick0=-1.0, dtick=0.25, range=[-1.2, 1.2], title_text=_("Correlation")) g = dcc.Graph(figure=fig) return g
with open(f"data/{date_dir}/nodes.csv", "r", encoding="utf-8") as nodes_file: lines = nodes_file.readlines() max_size = int(lines[0].split(",")[-1]) min_size = int(lines[-1].split(",")[-1]) max_t = 200 min_t = 10 for line in lines: values = line.split(",") if values[0] in partition: nodes.append({ "id": values[0], "name": values[1], "color": ColorHash(str(partition[values[0]])).hex, "size": normalize(max_size, min_size, max_t, min_t, int(values[2])), }) edges = [] with open(f"data/{date_dir}/edges.csv", "r", encoding="utf-8") as edges_file: for line in edges_file: values = line.split(",") edges.append({"source": values[0], "target": values[1]}) with open(f"data/{date_dir}/{dt.month}_{dt.year}_graph.json", "w", encoding="utf-8") as json_file: json_file.write(json.dumps({ "nodes": nodes,
def color_hex(self): return ColorHash(self.name, lightness=(0.35, 0.45, 0.55, 0.65), saturation=(0.2, 0.3, 0.4, 0.5)).hex
def hash_color(value, mode="hex"): """Generates a deterministic color value for the provided string.""" return getattr(ColorHash(value), mode)
def test_colorhash_is_deterministic(objs): for obj in objs: assert ColorHash(obj).hex == ColorHash(obj).hex
def test_colorhash_returns_some_color(objs): for obj in objs: assert isinstance(ColorHash(obj).hex, str)
def colorize(zone): return ColorHash(zone['_id']).hex
def main(args): policy_path = args.policy_path expert = PPO.load(policy_path) # Initialize environment for input standardization factory = EnvFactory(args.env) env = DummyVecEnv([factory.make_env]) env = VecNormalize.load(args.stats_path, env) env.training = False states = [] for i in np.arange(-10, 110): for j in np.arange(-3, 3, 0.05): states.append([i, j]) states = np.stack(states) states_scaled = env.normalize_obs(states) states_tensor = torch.as_tensor(states_scaled).float() policy: ActorCriticPolicy = expert.policy.cpu() true_actions_tensor, _, _ = policy.forward(states_tensor, deterministic=True) features_tensor = policy.features_extractor.forward(states_tensor) shared_latents_tensor = policy.mlp_extractor.shared_net.forward( features_tensor) policy_latents_tensor_layer1 = policy.mlp_extractor.policy_net[0].forward( shared_latents_tensor) policy_latents_tensor_layer1_activated = policy.mlp_extractor.policy_net[ 1].forward(policy_latents_tensor_layer1) policy_latents_tensor_layer2 = policy.mlp_extractor.policy_net[2].forward( policy_latents_tensor_layer1_activated) policy_latents_tensor_layer2_activated = policy.mlp_extractor.policy_net[ 3].forward(policy_latents_tensor_layer2) actions_tensor = policy.action_net.forward( policy_latents_tensor_layer2_activated) assert actions_tensor.equal(true_actions_tensor) binary_embeddings_layer1 = policy_latents_tensor_layer1_activated > 0 binary_embeddings_layer1 = binary_embeddings_layer1.cpu().detach().numpy() binary_embeddings_layer2 = policy_latents_tensor_layer2_activated > 0 binary_embeddings_layer2 = binary_embeddings_layer2.cpu().detach().numpy() binary_embeddings = np.concatenate( [binary_embeddings_layer1, binary_embeddings_layer2], axis=1).astype(int) integer_embeddings = np.packbits(binary_embeddings, axis=1, bitorder="little") integer_embeddings = integer_embeddings @ (256**np.arange( integer_embeddings.shape[1])) # to allow arbitrary number of bits # convert raw integer embeddings to 0, 1, 2, 3... # fast rendering of state cells via grid interpolation grid_x, grid_y = np.mgrid[-10:110:1000j, -3:3:1000j] z = griddata((states[:, 0], states[:, 1]), integer_embeddings, (grid_x, grid_y), method='nearest') # convert raw integer convert_raw_integer_to_colorhash = np.vectorize(lambda x: ColorHash(x).rgb) grid_z = np.array(convert_raw_integer_to_colorhash(z)).swapaxes( 0, 1).swapaxes(1, 2) plt.figure() plt.imshow(grid_z, extent=[-10, 110, -3, 3], aspect='auto') plt.title("State Space Visualized") plt.xlabel("$x$") plt.ylabel("$\\dot x$") plt.show()