Why are lines showing on my sparkline that are above the max? - if-statement

I have this Google sheet, using sparklines to graph a change over time. I want a horizontal line at 2 different points, which cannot be done with a sparkline. So as a workaround, I have 3 different sparklines covering the same data but with different mins and maxes so I can draw a border between them.
But there are 6 parts (encircled in red) where lines are showing on the sparkline where they should not be.
What am I doing wrong?
https://docs.google.com/spreadsheets/d/1paFqu2hWAlpnzc3Ba0uo2kBfy9Inf2X6iWjQTzr_6XE/edit?usp=sharing

those are SPARKLINE glitches caused by ymax when the dataset contains the same consecutive values in the array (rows 10 & 11, 12 & 13 and 23 & 24) which are out of bound limited by ymax parameter.
an easy fix would be to slightly alter/manipulate duplicates before they hit the SPARKLINE output:
E17 cell:
=ARRAYFORMULA(SPARKLINE({A$3:A,
IF(COUNTIF(B$3:B, B$3:B)>1, B$3:B+(ROW(B$3:B)*0.0000000001), B$3:B)},
{"Ymin", 280; "Ymax", 285}))
E19 cell:
=ARRAYFORMULA(SPARKLINE({A$3:A,
IF(COUNTIF(B$3:B, B$3:B)>1, B$3:B+(ROW(B$3:B)*0.0000000001), B$3:B)},
{"Ymin", 275; "Ymax", 280}))

Related

Conditional formatting of a rectangle cell range defined by user input

In a Google Sheet with a cell range of 26x26 (so A1:Z26), I need to conditionally format (change the color to green) a rectangle area that is defined by user input.
Example of user input (4 values required):
hsize = 5 / vsize = 4 / starth = 3 / startv = 2
This means that the conditionally formatted area should be a rectangle from C2:G5 because the start cell horizontally is 3 (column C) and vertically 2 (row 2), and the size of the rectangle horizontally is 5 (C,D,E,F,G) and vertically 4 (2,3,4,5).
I already solved this with Apps Script but due to given restrictions I have to implement this without using any scripts.
I have numbered the whole 26x26 area (=sequence(26,26)) to get numbers from 1 to 676 that I could then use for the conditional formatting.
By doing this, I can limit the conditional formatting to the values between the start and the end value (in the example above that would be 29 (C2) and 111 (G5)). This works by using a simple and/if formula in the conditional formatting.
But the problem with this is that all the cells with values from 29 to 111 are now colored, not only the rectangle C2:G5.
I can't figure out how to define a formula that does what I need. How can I do this and limit the highlighted area to the defined cell range of the rectangle?
[Picture here]: green is the conditional formatting from 29 (C2) to 111 (G5), but what I actually need is that only the red-framed area should be shown in green.
try:
=REGEXMATCH(""&A1, "^"&TEXTJOIN("$|^", 1, INDIRECT(
ADDRESS($AB$4, $AB$3)&":"&ADDRESS($AB$2+$AB$4-1, $AB$1+$AB$3-1)))&"$")
or better:
=(COLUMN(A1)>=$AB$3) *(ROW(A1)>=$AB$4)*
(COLUMN(A1)<$AB$1+$AB$3)*(ROW(A1)<$AB$2+$AB$4)

How can I calculate a new date conditionally based on other information?

I have a Google Sheet which is being populated by a Google Form. I am using Google Apps Script to add some extra functionality. Please feel free to access and modify these as needed in order to help.
Based on answers from the Form, I need to return a new date that factors in the time stamp at form submission.
This is a dumbed down example of what I need to do, but let's think of it like ordering a new car and its color determines how long it is going to take.
Car
Color
Toyota
Red
Honda
Blue
Tesla
Green
I need to write a conditional IF statement that determines how many weeks it will take to get the car based on the ordered color.
-
Red
Blue
Green
Toyota
1
3
5
Honda
2
4
6
Tesla
1
1
1
So if you order a Toyota in Red, it will take one week. If you order a Toyota in Green, it will take 5 weeks. If you order a Tesla, it will be really in one week no matter what color. Etc...
I started by writing some language in Sheets to take the Timestamp which is in Column A and add the appropriate amount of time to that:
=IFS(AND(B2 = "Toyota",C2 = "Red"),A2 + 7,AND(B2="Toyota",C2="Blue"), A2 + 21,AND(B2="Toyota",C2="Green"), A2 + 35,AND(B2 = "Honda",C2 = "Red"),A2 + 14,AND(B2="Honda",C2="Blue"), A2 + 28,AND(B2="Honda",C2="Green"), A2 + 42,AND(B2 = "Tesla"),A2 + 7)
And then I dragged that down the length of the entire column so that it would fill in as submissions came in.
However when you fill in the Google Form, it will overwrite what's in that entire row, blowing out what I had in that column.
Now I realized that the code needs to be written in Google Apps Script and returned as a value.
What kinds of modifications need to be made to my IFS statement in order to make it compatible with Google Apps Script?
For easier approach, QUERY would actually solve your issue without doing script as Broly mentioned in the comment. An approach you can try is to create a new sheet. Then have that sheet contain this formula on A1
Formula (A1):
=query('Form Responses 1'!A:C)
This will copy A:C range from the form responses, and then, copy/paste your formula for column Date Needed on column D.
Output:
Note:
Since you only copied A:C, it won't affect column D formula.
Your A:C in new sheet will update automatically, then the formula you inserted on D will recalculate once they are populated.
Add IFNA on your formula for column D to not show #N/A if A:C is still blank.
Formula (D2):
=IFNA(IFS(AND(B2 = "Toyota",C2 = "Red"),A2 + 7,AND(B2="Toyota",C2="Blue"), A2 + 21,AND(B2="Toyota",C2="Green"), A2 + 35,AND(B2 = "Honda",C2 = "Red"),A2 + 14,AND(B2="Honda",C2="Blue"), A2 + 28,AND(B2="Honda",C2="Green"), A2 + 42,AND(B2 = "Tesla"),A2 + 7), "")

How to recognizing a Maze in an image using Opencv3.3 and Pyhton2.7

Hey there I am using Opencv3.3 and Pyhton2.7 for recognizing a Maze in an image.
I have to find the outermost limit of the Maze in the image.
I tried closing the entrance and exit gaps of the maze and finding the outermost shape. I worked on this for closing the gaps but it is useless for my problem because I need these gaps to solve the maze.
This is the original image
I want to find outermost limit of the maze.
This is what I want
How can I extract outermost contour?
I would do this with numpy rather than OpenCV, but the two are compatible so you can mix and match anyway, or you can adapt the technique to OpenCV once you get the idea of how I am tackling it.
The strategy is to sum all the pixels across every row and make a single pixel wide image (shown on the right below) that is the sum of all the pixels in each row. I then find the biggest value in that column and divide by that to normalise everything to the range 0..100. Now any pixel that is less than 30 in that single pixel wide image means that the corresponding row had less than 30% of white pixels in the original image - i.e. it was largely black.
Then I make the same summation of all the columns to produce the column sums - shown across the bottom of the image below:
I think some folks refer to this technique as a "projection" if you want to Google it.
So, the code looks like this:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Load image - you can use OpenCV "imread()" just the same and convert to grayscale
im = np.array(Image.open('maze.jpg').convert('L'))
# Get height and width
h,w = im.shape[0:2]
# Make a single pixel wide column, same height as image to store row sums in
rowsums=np.empty((h))
# Sum all pixels in each row
np.sum(im,axis=1,out=rowsums)
# Normalize to range 0..100, if rowsum[i] < 30 that means fewer than 30% of the pixels in row i are white
rowsums /= np.max(rowsums)/100
# Find first and last row that is largely black
first = last = -1
for r in range(h):
if first < 0 and rowsums[r] < 30:
first = r
if rowsums[r] < 30:
last = r
print(first,last)
# Make a single pixel tall row, same width as image to store col sums in
colsums=np.empty((w))
# Sum all pixels in each col
np.sum(im,axis=0,out=colsums)
# Normalize to range 0..100, if colsum[i] < 30 that means fewer than 30% of the pixels in col i are white
colsums /= np.max(colsums)/100
# Find first and last col that is largely black
first = last = -1
for c in range(w):
if first < 0 and colsums[c] < 30:
first = c
if colsums[c] < 30:
last = c
print(first,last)
That outputs:
62 890
36 1509
So the top row of the maze is row 62, and the bottom one is row 890. The left column of the maze is column 36 and the rightmost column is col 1509.
If I draw on an 80% transparent red rectangle to match those locations, I get:

subplots only plotting 1 plot using pandas

I am trying to get two plots on one figure using matplotlib's subplots() command. I want the two plots to share an x-axis and have one legend for the whole plot. The code I have right now is:
observline = mlines.Line2D([], [], color=(1,0.502,0),\
markersize=15, label='Observed',linewidth=2)
wrfline=mlines.Line2D([], [], color='black',\
markersize=15, label='WRF',linewidth=2)
fig,axes=plt.subplots(2,1,sharex='col',figsize=(18,10))
df08.plot(ax=axes[0],linewidth=2, color=(1,0.502,0))\
.legend(handles=[observline,wrfline],loc='lower center', bbox_to_anchor=(0.9315, 0.9598),prop={'size':16})
axes[0].set_title('WRF Model Comparison Near %.2f,%.2f' %(lat,lon),fontsize=24)
axes[0].set_ylim(0,360)
axes[0].set_yticks(np.arange(0,361,60))
df18.plot(ax=axes[1],linewidth=2, color='black').legend_.remove()
plt.subplots_adjust(hspace=0)
axes[1].set_ylim(0,360)
axes[1].set_yticks(np.arange(0,361,60))
plt.ylabel('Wind Direction [Degrees]',fontsize=18,color='black')
axes[1].yaxis.set_label_coords(-0.05, 1)
plt.xlabel('Time',fontsize=18,color='black')
#plt.savefig(df8graphfile, dpi = 72)
plt.show()
and it produces four figures, each with two subplots. The top is always empty. The bottom is filled for three of them with my 2nd dataframe. The indices for each dataframe is a datetimeindex in the format YYYY-mm-DD HH:MM:SS. The data is values from 0-360 nearly randomly across the whole time series, which is for two months.
Here is an example of each figure produced:

Filter items with Django Query

I'm encountering this problem and would like to seek your help.
The context:
I'm having a bag of balls, each of which has an age (red and blue) and color attributes.
What I want is to get the top 10 "youngest" balls and there are at most 3 blue balls (this means if there are more than 3 blue balls in the list of 10 youngest balls, then replace the "redudant" oldest blue balls with the youngest red balls)
To get top 10:
sel_balls = Ball.objects.all().sort('age')[:10]
Now, to also satisfy the conditions "at most 3 blue balls", I need to process further:
Iterate through sel_balls and count the number of blue balls (= B)
If B <= 3: do nothing
Else: get additional B - 3 red balls to replace the oldest (B - 3) blue balls (and these red balls must not have appeared in the original 10 balls already taken out). I figure I can do this by getting the oldest age value among the list of red balls and do another query like:
add_reds = Ball.objects.filter(age >= oldest_sel_age)[: B - 3]
My question is:
Is there any way that I can satisfy the constraints in only one query?
If I have to do 2 queries, is there any faster ways than the one method I mentioned above?
Thanks all.
Use Q for complex queries to the database: https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects
You should use annotate to do it.
See documentation.
.filter() before .annotate() gives 'WHERE'
.filter() after .annotate() gives 'HAVING' (this is what you need)