RRD Graph - Change line colour by value - rrdtool

I have a RRD database with data:
"DS:pkts_transmitted:GAUGE:120:0:U",
"DS:pkts_received:GAUGE:120:0:U",
"DS:pkts_lost:GAUGE:120:0:U",
"DS:rtt_min:GAUGE:120:0:U",
"DS:rtt_avg:GAUGE:120:0:U",
"DS:rtt_max:GAUGE:120:0:U",
And I want that the Avg line change colour if I lose any package.
For example, if I lose 5 packets make the line blue, if I lose 10 make it red.
I see people doing it but I read the documentation and I can't find how to do this.

The way to do this is to actually have multiple lines defined (one of each colour) and hide the ones you don't want to see at any time, using calculations.
For example, say we have an RRD with two DSs:
DS:x:GAUGE:60:0:U
DS:y:GAUGE:60:0:1
Now, we want to show the line for x in red if y is 0, and blue if it is 1. To do this, we create two calculated values, x1 and x2.
CDEF:x1=y,0,EQ,x,UNKN,IF
CDEF:x2=y,1,EQ,x,UNKN,IF
Thus, x1 is active if y=0 and x2 if y=1. Yes, this could be simplified, but I'm showing it like this for the example.
Now, we can make lines using these:
LINE:x1#ff0000:MyLine
LINE:x2#0000ff
Note that the second line doesn't need a legend. Now, the line will appear to change colour depending on the value of the y metric, since at any time the other line will be UNKN and therefore not displayed.
You can extend this, of course, to have multiple colours and more complex thresholds.

Related

Caffe: Multi-Label Images with Varying Number of Labels

I have a dataset where the images have VARYING number of labels. The number of labels is between 1 and 5. There are 100 classes.
After googling, it seems like HDF5 db with slice layer can deal with multiple labels, as in the following URL.
The only problem is that it supposes a fixed number of labels. Following this, I would have to create a 1x100 matrix, where entry value is 1 for the labeled classes, and 0 for non-label classes, as in the following definition:
layers {
name: "slice0"
type: SLICE
bottom: "label"
top: "label_matrix"
slice_param {
slice_dim: 1
slice_point: 100
}
}
where each image contains a a label looking like (1,0,0,...1,...0,....,0,1) where the vector size is 100 dimension.
Now, I apologize that my question becomes somehow vague, but is this a feasible idea? I.e., is there a better approach to this problem?
I get that you have 5 types of labels that are not always present for each data point. 1 of the 5 labels is for 100-way classification. Correct so far?
I would suggest always writing all 5 labels into your HDF5 and use a special value for when the label is missing. You can then use the missing_value option to skip computing the loss for that layer for that iteration. Using it requires add loss_param{ ignore_label = Y } to the loss layer in your network prototxt definition where Y is a scalar.
The backpropagated error will only be a function of labels that are present. If input X does not have a valid value for a label, the network will still produce an estimate for that label. But it will not be penalized for it. The output is produced without any effect on how the weights are updated in that iteration. Only outputs for non-missing labels contribute to the error signal and the weight gradients.
It seems that only the Accuracy and SoftmaxWithLossLayer layers support missing_values.
Each label is a 1x5 matrix. The first entry can be for the 100-way classification (e.g. [0-99]) and entries 2:5 have scalars that reflect the values that the other labels can take. The order of the columns is the same for all entries in your dataset. A missing label is marked by a special value of your choosing. This special value has to lie outside the set of valid label values. This will depend on what those labels represent. If a label value of -1 never occurs you can use this to flag a missing label.

putting two tricontourf on the same axis does not display both

I have two independent datasets, z_p and z_g here, and I would like to put two tricontourf() instances on the same axis, each instance corresponding to a contour of one dataset. Below is a pseudo-code of what I do:
cmap_p = plt.get_cmap('Reds')
norm_p = BoundaryNorm(levels, ncolors=cmap_p.N, clip=True)
cmap_g = plt.get_cmap('Blues')
norm_g = BoundaryNorm(levels, ncolors=cmap_g.N, clip=True)
lev = range(lower_level, upper_level+1)
obj_g = ax.tricontourf(x, y, z_g, cmap=cmap_g, norm=norm_g,
levels=lev, extent=[x0, y0, x1, y1], zorder=2)
obj_p = ax.tricontourf(x, y, z_p, cmap=cmap_p, norm=norm_p,
levels=lev, extent=[x0, y0, x1, y1], zorder=3)
The output figure is attached below. Clearly, only the second call to tricontourf() has effectively worked, since there is a patch on the left of the figure in red. If I comment out the call to get obj_p, then I get a blue patch on the right side of the figure in blue color. However, the two subsequent calls to tricontourf() do not work simultaneously.
I would be grateful if someone would tell me how to show both contours on the same axis?
Your second plot is covering up the first plot with an opaque white color, which is why you see the first one when you don't plot the second. If you want to see both, you can set the alpha keyword:
alpha=0.5
to make the plots transparent enough to see through. Alternatively, there is an answer at this SO question about masking a call to tricontourf, if you know where the overlapping region is and want to simply mask it out of the second plot.

Pyplot rotated labels offset by one

Just getting into matplot lib and running into odd problem - I'm trying to plot 10 items, and use their names on the x-axis. I followed this suggestion and it worked great, except that my label names are long and they were all scrunched up. So I found that you can rotate labels, and got the following:
plt.plot([x for x in range(len(df.columns))], df[df.columns[0]], 'ro',)
plt.xticks(range(10), df.columns, rotation=45)
The labels all seem to be off by a tick ("Arthrobacter" should be aligned with 0). So I thought my indexing was wrong, and tried a bunch of other crap to fix it, but it turns out it's just odd (at least to me) behavior of the rotation. If I do rotation='vertical', I get what I want:
I see now that the center of the labels are clearly aligned with the ticks, but I expected that they'd terminate on the ticks. Like this (done in photoshop):
Is there a way to get this done automatically?
The labels are not "off", labels are actually placed via their "center". In your second image, the corresponding tick is above the center of the label, not above its endpoint. You can change that by adding ha='right' which modifies the horizontal alignement of the label.
plt.plot([x for x in range(len(df.columns))], df[df.columns[0]], 'ro',)
plt.xticks(range(10), df.columns, rotation=45, ha='right')
See the comparison below :
1)
plt.plot(np.arange(4), np.arange(4))
plt.xticks(np.arange(4), ['veryverylongname']*4, rotation=45)
plt.tight_layout()
2)
plt.plot(np.arange(4), np.arange(4))
plt.xticks(np.arange(4), ['veryverylongname']*4, rotation=45, ha='right')
plt.tight_layout()

why hough transform detects two lines while there is only one line

I would like to detect a line and extract its two ended points. The common approach is using the hough transform. Luckily there is a sample in OpenCV regarding is matter, therefore I've drawn a line whose two ended points p1(100,200), p2(400,200). I thought the aforementioned method will provid me with only these points. My sample image is
the hough transform provides me with two images which are
For Canny filter,
In the code, it seems that there are two lines are detected. This explains why the red line is thicker which indicates the fact that there are two lines rather than one. When I print out the number of lines, it shows me two as follows
lines.size(): 2
p1:<99,201> p2:<401,201>
lines.size(): 2
p1:<102,198> p2:<398,198>
Why I'm getting two lines?
It might be due to the width of the bins in your HoughSpace.
You probably choose one of the default OpenCv functio, i.e.
HoughLines(X, X, 1, CV_PI/180, X, X, X );
The arguments that are not X define the width of the bins see.
There it says:
rho : The resolution of the parameter r in pixels. We use 1 pixel.
For the first argument and for the second:
theta: The resolution of the parameter \theta in radians. We use 1 degree (CV_PI/180)
I don't now the values you chose, but you might want to choose larger ones.

Python >> matplotlib: Get the number of lines on a current plot?

Is there a way to get the number of lines currently on a matplotlib plot? I find myself setting colors in a colormap using a counter and multiplier to step through the color values--which seems rather un-pythonic.
All the Line2D objects in an axes are stored into a list
ax.lines
If you use only simple line plots, the lenght of the above list is enough.
If you use plt.errorbar the situation is a bit more complicated, as it creates multiple Line2D objects (central lines, vertical and horizontal error bars and their caps).
If you want to automatise the colours to assign to lines you can create a cycle like this
import itertools as it
colors = it.cycle(list of colors)
and then call the next color with colors.next() and restart from the first after it gets to the last