How to convert 5 stars rating to pixels in python? - python-2.7

I have two different rates that the user can make for his teacher , i want to convert the total of each rate in pixels so i can have the progress bar effect, for example:
maximum_pixels = 100 #maximum width
services = 4.5 #width: 95px
professionalism = 5.0 #width: 100px
total_percentage = maximum_pixels * services / maximum_pixels
How can i implement that in my code ?

maxAllowed = 100
minAllowed = 0
unscaledNum = 3
_min = 0
_max = 5
((maxAllowed - minAllowed) * (unscaledNum - _min) / (_max - _min) + minAllowed)
Result:
60.0

Related

Debugging old Fortran code for sediment dynamics

I am looking at some Fortran code from an old scanned paper. The scan quality is not great so I may have copied it wrong. I tried to run this using an online Fortran compiler but it bombs out. Not being familiar with Fortran, I was wondering if someone can point out where the syntax does not make sense? The code is from a paper on sediment dynamics:
Komar, P.D. and Miller, M.C., 1975. On the comparison between the threshold of sediment motion under waves and unidirectional currents with a discussion of the practical evaluation of the threshold: Reply. Journal of Sedimentary Research, 45(1).
PROGRAM TSHOLD
REAL LI, LO
G = 981.0
PIE = 3.1416
RHOW = 1.00
READ (6O,1) DIAM, RHOS
1 FORMAT (2X, F6.3,2X, F5.3)
IF(DIAM .LT. 0.05) GO TO 5
A = 0.463 * PIE
B = 0.25
GO TO 7
5 A = 0.21
B = 0.50
7 PWR = 1.0 / (2.0 - B)
FAC = (A * (RHOS - RHOW) * G/(RHOW * PIE**B))**PWR
FAC1 = FAC * DIAM**((1.0 - B) * PWR)
T = 1.0
15 J = 1.20
LD = 156.13 * (T**2)
UM = FAC1 * T**(B*PWR)
WRITE(61,9) DIAM, T, UM
9 FORMAT(1H0, 10X, 17HGRAIN DIAMETER = ,F6.3,1X,2HCM //
1 11X, 14HWAVE PERIOD = ,F5.2, 1X, 3HSEC //
2 11X, 22HORBITAL VELOCITY, UM = ,F6.2, 1X, 6HCM/SECl //
3 20X, 6HHEIGHT, 5X, 5HDEPTH, 8X, 3HH/L, 6X, 7HH/DEPTH //
4 22X, 2HCM, 8X, 2HCM /)
C INCREMENT WAVE HEIGHT, CALCULATE DEPTH
H = 10.0
DO 12 K = 1.60
SING = PIE * H / (UM * T)
X = SING
IF(X.LT.1.0) GO TO 30
30 ASINH = X - 0.16666*X**3.0 + 0.07500* X ** 5.0 - 0.04464 * X ** 7.0
1 + 0.03038 * X ** 9.0 - 0.02237 * X ** 11.0
32 LI = LD * (SINH(ASINH)/COSH(ASINH))
OPTH = ASINH * LI / 6.2832
C CHECK WAVE STABILITY
RATIO = H / DPTH
IF(RATIO.GE.0.78) GO TO 11
STEEP = H / LI
TEST = 0.142 * (SINH(ASINH)/COSH(ASINH))
IF(STEEP.GE.TEST) GO TO 11
WRITE(61,10) H, OPTH, STEEP, RATIO
I0 FORMAT(IH0, 20X, F5.1, 4X, E9.3, 4X, F5.3, 4X, F4.2)
11 H = H + 10.0
12 CONTINUE
T = T + 1.0
15 CONTINUE
END
The problem is more likely that old Fortran requires fixed form code formatting where the number of spaces before a statement is very important.
Here are some general rules
Normal statements start at column 7 and beyond
Lines cannot exceed 72 columns
Any character placed on column 6 indicates the line is a continuation from the line above. I see that on the code above in the lines following 9 FORMAT(..
A number placed between columns 1-5 indicates a label, which can be a target of a GO TO statement, a DO statement or a formatting specification.
The character C on the first column, and sometimes any character on the first column indicate the line is a comment line.
see https://people.cs.vt.edu/~asandu/Courses/MTU/CS2911/fortran_notes/node4.html for more info.
Based on the rules above, here is how to enter the code, with the correct spacing. I run the F77 code through a converter to make it compatible with F90 and F77 at the same time. The code below might compile with the online compiler now.
PROGRAM TSHOLD
REAL LI, LO
G = 981.0
PIE = 3.1416
RHOW = 1.00
READ (60,1) DIAM, RHOS
1 FORMAT (2X, F6.3,2X, F5.3)
IF(DIAM .LT. 0.05) GO TO 5
A = 0.463 * PIE
B = 0.25
GO TO 7
5 A = 0.21
B = 0.50
7 PWR = 1.0 / (2.0 - B)
FAC = (A * (RHOS - RHOW) * G/(RHOW * PIE**B))**PWR
FAC1 = FAC * DIAM**((1.0 - B) * PWR)
T = 1.0
DO 15 J=1,20
LD = 156.13 * (T**2)
UM = FAC1 * T**(B*PWR)
WRITE(61,9) DIAM, T, UM
9 FORMAT(1H0, 10X, 17HGRAIN DIAMETER = ,F6.3,1X,2HCM // &
& 11X, 14HWAVE PERIOD = ,F5.2, 1X, 3HSEC // &
& 11X, 22HORBITAL VELOCITY, UM = ,F6.2, 1X, 6HCM/SECl // &
& 20X, 6HHEIGHT, 5X, 5HDEPTH, 8X, 3HH/L, 6X, 7HH/DEPTH // &
& 22X, 2HCM, 8X, 2HCM /)
! INCREMENT WAVE HEIGHT, CALCULATE DEPTH
H = 10.0
DO 12 K = 1,60
SING = PIE * H / (UM * T)
X = SING
IF(X.LT.1.0) GO TO 30
30 ASINH = X - 0.16666*X**3.0 + 0.07500* X ** 5.0 - 0.04464 * X ** 7.&
& + 0.03038 * X ** 9.0 - 0.02237 * X ** 11.0
32 LI = LD * (SINH(ASINH)/COSH(ASINH))
OPTH = ASINH * LI / 6.2832
! CHECK WAVE STABILITY
RATIO = H / DPTH
IF(RATIO.GE.0.78) GO TO 11
STEEP = H / LI
TEST = 0.142 * (SINH(ASINH)/COSH(ASINH))
IF(STEEP.GE.TEST) GO TO 11
WRITE(61,10) H, OPTH, STEEP, RATIO
10 FORMAT(G14.4, 20X, F5.1, 4X, E9.3, 4X, F5.3, 4X, F4.2)
11 H = H + 10.0
12 CONTINUE
T = T + 1.0
15 CONTINUE
END
I found several transcription errors, replacing commas with dots, zeros with the letter O, and a missing DO statement.

How to convert the range of 1000 to 2000 to 0 to 1024 for throttle itself in python 2.7?

I've tried this equation but still getting error and not updated automatically.
Let's say given throttle value 1500
NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
self.prev_values = [0,0,0]
self.min_values = [0, 0, 0, 0]
self.max_values = [1024, 1024, 1024, 1024]
self.setpoint_euler[3] = (((self.setpoint_cmd[3] - self.min_values[3]) * (1024 - 0)) /(2000 -1000))+0
1.024 * (value - 1000)
if you value 1500
1.024 * (1500 - 1000) = 512
if you value 2000
1.024 * (2000 - 1000) = 1024
if you value 1000
1.024 * (1000 - 1000) = 0
apply round or something btw

Converting a timestamp to freshness index

I have a column in dataframe which has article and its publication date (timestamp). I need to use this information to find out a freshness score of an article.
articleId publicationDate
0 581354 2017-09-17 15:16:55
1 581655 2017-09-18 07:37:51
2 580864 2017-09-16 06:44:39
3 581610 2017-09-18 06:30:30
4 581605 2017-09-18 07:22:24
Most recent article should get higher score. Timewindow should be half an hour (2 articles published in half an hour must get same score)
Some of the code below might be redundant but it seems to work:
df['score'] = df['publicationDate'] - df['publicationDate'].max()
df['score'] = (df['score'] / np.timedelta64(1, 'm')).apply(lambda x: (round(x / 30) * 30 + 30) / 30 if x else x).rank(method='max')
So you convert timedelta to minutes, then round it to 30, and finally rank that value.
It can also be a one-liner if you please:
df['score'] = ((df['publicationDate'] - df['publicationDate'].max()) / np.timedelta64(1, 'm')).apply(lambda x: (round(x / 30) * 30 + 30) / 30 if x else x).rank(method='max')
Explaination:
(df['publicationDate'] - df['publicationDate'].max() - subtract all dates from most recent one
(df['score'] / np.timedelta64(1, 'm')) - convert timedelta into minutes
.apply(lambda x: (round(x / 30) * 30 + 30) / 30 if x else x) - roundup to 30 minutes excluding most recent timestamp
.rank(method='max') rank the results giving upper value to all those that have same rank.
EDIT:
To change rank of those older than 2 days you can use this:
df['diff'] = (df['publicationDate'] - df['publicationDate'].max()).apply(lambda x: x.days)
df.loc[df['diff']<=-2, 'score'] = 0
First line will give you timedelta in whole days, and second one will change rank to 0 where days are less or equal to -2.

Interpolating Skinning Weights

I am subdividing the triangles of mesh, and as you can guess, I need weight values for these new vertices. Currently I am using linear interpolation (Vnew.weight[i] = (V1.weight[i] + V2.weight[i]) * 0.5) but, it seems like I cannot get correct values.
Do you know a better solution for using interpolating the weights?
Edit:
Right know, I am using LBS, and dividing one triangles into two triangles by taking halfway point. This division is done as soon as triangle information is read from the file (I am using SMD files).
I think the problem is weights because, in the rest pose (Without any skinning) everything is fine. But when the poses are started to apply, and skinning is done, some crazy triangles, and holes appear. And when I looked closely to these "crazy triangles", their vertices are moving with the mesh but, not fast enough with other vertices.
And here is the code of division process, and interpolating vertices, normals, UVs, and weights
int pi=0;
while (1)
{
// Some control, and decleration
for (int w = 0; w < 3; w++)
{
// Some declerations
Vert v;
// Values are read from file into Cert v
// Using boneIndex2[] and boneWeight2[] because GLSL 1.30 does
// not support shader storage buffer object, and I need just
// 8 indices most for now.
v.boneIndex2[0] = 0;
v.boneIndex2[1] = 0;
v.boneIndex2[2] = 0;
v.boneIndex2[3] = 0;
v.boneWeight2[0] = 0;
v.boneWeight2[1] = 0;
v.boneWeight2[2] = 0;
v.boneWeight2[3] = 0;
m.vert.push_back(v);
pi++;
}
// Dividing the triangle
Vert a = m.vert[pi - 2];
Vert b = m.vert[pi - 1];
Vert v;
// Interpolate position
v.pos[0] = (a.pos[0] + b.pos[0]) / 2;
v.pos[1] = (a.pos[1] + b.pos[1]) / 2;
v.pos[2] = (a.pos[2] + b.pos[2]) / 2;
// Interpolate normal
v.norm[0] = (a.norm[0] + b.norm[0]) / 2;
v.norm[1] = (a.norm[1] + b.norm[1]) / 2;
v.norm[2] = (a.norm[2] + b.norm[2]) / 2;
// Interpolate UV
v.uv[0] = (a.uv[0] + b.uv[0]) / 2;
v.uv[1] = (a.uv[1] + b.uv[1]) / 2;
// Assign bone indices
// The new vertex should be treated by each bone of Vert a, and b
v.boneIndex[0] = a.boneIndex[0];
v.boneIndex[1] = a.boneIndex[1];
v.boneIndex[2] = a.boneIndex[2];
v.boneIndex[3] = a.boneIndex[3];
v.boneIndex2[0] = b.boneIndex[0];
v.boneIndex2[1] = b.boneIndex[1];
v.boneIndex2[2] = b.boneIndex[2];
v.boneIndex2[3] = b.boneIndex[3];
// Interpolate weights
float we[4];
we[0] = (a.boneWeight[0] + b.boneWeight[0]) / 2;
we[1] = (a.boneWeight[1] + b.boneWeight[1]) / 2;
we[2] = (a.boneWeight[2] + b.boneWeight[2]) / 2;
we[3] = (a.boneWeight[3] + b.boneWeight[3]) / 2;
// Assign weights
v.boneWeight[0] = we[0];
v.boneWeight[1] = we[1];
v.boneWeight[2] = we[2];
v.boneWeight[3] = we[3];
v.boneWeight2[0] = we[0];
v.boneWeight2[1] = we[1];
v.boneWeight2[2] = we[2];
v.boneWeight2[3] = we[3];
// Push new vertex
m.vert.push_back(v);
pi++;
// Push new faces
m.face.push_back(Face(pi - 4, pi - 1, pi - 2));
m.face.push_back(Face(pi - 4, pi - 3, pi - 1));
} // End of while(1)
You are blending weights that just might belong to different bones (i.e. if the bone indices are not equal).
Instead, gather all influencing bone indices from the two vertices. If only one vertex refers to any bone, use half of this weight. If both vertices refer to the bone, use the interpolation as you already did. Then, pick the four bones with the highest weights and re-normalize to a sum of 1.
Here is an example. Consider you have two vertices with these bone indices and weights:
v1 v2
index | weight index | weight
------+-------- ------+--------
0 | 0.2 2 | 0.1
1 | 0.5 3 | 0.6
2 | 0.1 4 | 0.2
3 | 0.2 5 | 0.1
You would start by building the table of joint weights:
index | weight
------+--------
0 | 0.2 / 2 = 0.1
1 | 0.5 / 2 = 0.25
2 | (0.1 + 0.1) / 2 = 0.1
3 | (0.2 + 0.6) / 2 = 0.4
4 | 0.2 / 2 = 0.1
5 | 0.1 / 2 = 0.05
Sort wrt weight and pick the four greatest:
index | weight
------+--------
3 | 0.4 *
1 | 0.25 *
0 | 0.1 *
2 | 0.1 *
4 | 0.1
5 | 0.05
These weights sum to 0.85. So divide the weights by 0.85 to get the final weights and indices:
index | weight
------+--------
3 | 0.47
1 | 0.29
0 | 0.12
2 | 0.12
The other option would be to extend your structure to use more (either static eight or dynamically) bones. But it's probably not worth the effort.

Sqlite (C API) and query (select) on cyclic/symmetric values with user defined functions

I'm using Sqlite with C++ and have two similar problems :
1) I need to select 4 entries to make an interpolation.
For example, my table could look like this :
angle (double) | color (double)
0 0.1
30 0.5
60 0.9
90 1.5
... ...
300 2.9
330 3.5
If I want to interpolate the value corresponding to 95°, I will use the entries 60°, 90°, 120° and 150°.
To get those entries, my request will be SELECT color FORM mytable WHERE angle BETWEEN 60 and 150, no big deal.
Now if I want 335°, I will need 300°, 330°, 360°(=0°) and 390°(=30°).
My query will then be SELECT color FORM mytable WHERE angle BETWEEN 300 and 330 OR angle BETWEEN 0 and 30.
I can't use SELECT color FORM mytable WHERE angle BETWEEN 300 and 390 because this will only return 2 colors.
Can I use the C API and user defined functions to include some kind of modulo meaning in my queries ?
It would be nice if I could use a user defined function to use the query [...] BETWEEN 300 and 390 and get as result the rows 300, 330, 0 and 30.
2) An other table looks like this :
speed (double) | color (double) | var (double)
0 0.1 0
10 0.5 1
20 0.9 2
30 1.5 3
... ... ...
In reality due to symmetry, color(speed) = color(-speed) but var(-speed) = myfunc(var(speed)).
I would like to make queries such as SELECT * FROM mytable WHERE speed BETWEEN -20 and 10 and be able to make a few operations with the API on the "virtual" rows with a negative speed and return them as a regular result.
For example I would like the result of the query SELECT * FROM mytable WHERE speed BETWEEN -20 and 10 to be like this :
speed (double) | color (double) | var (double)
-20 0.9 myfunc(2)
-10 0.5 myfunc(1)
0 0.1 0
10 0.5 1
Is that possible ?
Thanks for your help :)
I would suggest to use a query with two intervals :
SELECT * from mytable WHERE (speed >= MIN(?1,?2) AND speed <= MAX(?1,?2)) OR ((MAX(?1,?2) > 360) AND (speed >= 0 AND speed <= MAX(?1,?2)%360));
This example works fine if ?1 and ?2 are positive.