math.ceil(0.5) returning different value from math.ceil(1/2) - python-2.x

import math
print(math.ceil(0.5))
Returns
1.0
But
import math
print(math.ceil(1/2))
Returns
0.0
What's going on here? Explanation would be nice.

It seems you're running that code using python 2.x where you need to cast to float explicitely:
import math
print(math.ceil(0.5))
print(math.ceil(float(1) / float(2)))
If you run python 3.x you won't need to do that cast explicitely and you'll get the same output:
import math
print(math.ceil(0.5))
print(math.ceil(1 / 2))

import math
print(math.ceil(1/float(2)))

Related

Python3.8 pySerial sending int64 as signed bytes

I'm trying to convert a class 'numpy.int64'named "int_array" into bytes.
In the past I used this structure
(-1024).to_bytes(8, byteorder='big', signed=True)
and worked fine.
Now, saving all the integers into a matrix, it doesn't allow me to do this:
int_array[1][i].to_bytes(8, byteorder='big', signed=True)
If there is a function for integer32 it would also work.
Does anybody know an equivalent command?
I will appreciate any help.
Apparently, in Python 3.8 I'm not allowed to do that.
The int.to_bytes method is available since Python 3.2:
New in version 3.2.
Alternatively you can use the struct module:
>>> import struct
>>> struct.pack(">q", -1024)
b'\xff\xff\xff\xff\xff\xff\xfc\x00'

How to get similar results to pydub.silence.detect_nonsilent() using librosa.effects.split()?

I love pydub. It is simple to understand. But when it comes to detecting non-silent chunks, librosa seems much faster. So I want to try using librosa in a project to speed my code up.
So far, I have been using pydub like this (segment is an AudioSegment):
thresh = segment.dBFS - (segment.max_dBFS - segment.dBFS)
non_silent_ranges = pydub.silence.detect_nonsilent(segment, min_silence_len=1000, silence_thresh=thresh)
The thresh formula works mostly well, and when it does not, moving it a 5 or so dbs up or down does the trick.
Using librosa, I am trying this (y is a numpy array loaded with librosa.load(), with an sr of 22050)
non_silent_ranges = librosa.effects.split(y, frame_length=sr, top_db=mistery)
To get similar results to pydub I tried setting mistery to the following:
mistery = y.mean() - (y.max() - y.mean())
and the same after converting y to dbs:
ydbs = librosa.amplitude_to_db(y)
mistery = ydbs.mean() - (ydbs.max() - ydbs.mean())
In both cases, the results are very different from what get from pydub.
I have no background in audio processing and although I read about rms, dbFS, etc, I just don't get it--I guess I am getting old:)
Could somebody point me in the right direction? What would be the equivalent of my pydub solution in librosa? Or at least, explain to me how to get the max_dBFS and dBFS values of pydub in librosa (I am aware of how to convert and AudioSegment to the equivalent librosa numpy array thanks to the excellent answer here)?
max_dBFS is always 0 by it's nature. dBFS is how much "quieter" the sound is than the max possible signal.
I suspect another part of your issue is that ydbs.max() is the maximum value among data in ydbs, not the maximum possible value that can be stored (i.e., the highest integer or float possible)
Another difference from pydub is your use of ydbs.mean(), pydub uses RMS when computing dBFS.
You can convert ydbs.mean() to dbfs like so:
from numpy import mean, sqrt, square, iinfo
max_sample_value = iinfo(ydbs.dtype).max
ydbs_rms = sqrt(mean(square(ydbs))
ydbs_dbfs = 20 * log(ydbs_rms) / max_sample_value, 10)

How to accumulate errors with ValidatedNec?

I am trying to do a minimal error accumulation with cats validatedNec.
But it seems to fail compilation.
Here is the code I tried :
import cats.data._
import cats.implicits._
// doesn’t work with or without this line : import cats.syntax.applicative._
val one: ValidatedNec[String, Int] = Validated.valid(42)
val two: ValidatedNec[String, Boolean] = Validated.valid(true)
(one, two).mapN{
(one, two) => println(one)
}
The error is : value mapN is not a member of (cats.data.ValidatedNec[String,…
Am I missing something?
Yes, when you're importing cats.implicits._, you're already importing all of the syntax extension, therefore, there's no need to import cats.syntax.applicative. To make things worse, when you import something twice in Scala, they will clash and leave you with nothing (since the Scala compiler can't choose which of the two to take)
If you remove that syntax import, it should work no problems.
See the import guide for more on this: https://typelevel.org/cats/typeclasses/imports.html

incorrect confusion matrix plot

The ends on y-axis cuts halfway while plotting confusion matrix using pandas dataframe?
This is what I get:
I used the codes from here How can I plot a confusion matrix? using pandas dataframe:
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
array = [[13,1,1,0,2,0],
[3,9,6,0,1,0],
[0,0,16,2,0,0],
[0,0,0,13,0,0],
[0,0,0,0,15,0],
[0,0,1,0,0,15]]
df_cm = pd.DataFrame(array, range(6),range(6))
#plt.figure(figsize = (10,7))
sn.set(font_scale=1.4)#for label size
sn.heatmap(df_cm, annot=True,annot_kws={"size": 16})# font size
I solved the problem and I think this post explains why it happens.
Simply speaking, matplotlib 3.1.1 broke seaborn heatmaps; You can solve it by downgrading to matplotlib 3.1.0.
As suggested by sikisis
Following solved my problem
pip install matplotlib==3.1.0

Read values from gauge with image processing

I am trying to read fluid levels in a column from a photograph I took, to see if I can automate the data collection.
So far, I have been able to use some code, below to identify the outline of where the fluid level is.
import numpy as np
import matplotlib.pyplot as plt
import skimage
from skimage import data
from skimage.feature import canny
from scipy import misc
from skimage.filters import roberts, sobel, scharr, prewitt
%matplotlib inline
perm = skimage.io.imread('C:\Users\Spencer\Desktop\perm2_crop.jpg')
edge_roberts = roberts(perm[:, :, 2])
plt.imshow(edge_roberts, cmap=plt.cm.gray)
What I need to figure out now is how to identify the break, and then how to translate that into a data value that I can scale to the values on the column.
Any ideas about what packages or methods I would use to to do this? Any examples would also be appreciated.

Resources