Periodogram() function producing integer overflow - r

I used the periodogram() in R and I got the error message.
Warning message: object#samp.rate * seq(1, width/2) : NAs produced by
integer overflow
Here is the code I executed. And I'm using the tuneR package.
waveform <- readWave(test.wav)
maxFreq <- sampleRate/2
minFreq <- 0
periodogram(waveform, width = 131072, overlap = 0, starts = NULL, ends = NULL, taper = 0, normalize = TRUE, frqRange = c(minFreq, maxFreq))
How do I resolve this.

Related

(R) Error in optim - attempt to apply non-function, when function is defined

not sure what I'm doing wrong here. I'm trying to get a cross-validation score for a mixture-of-two-gammas model.
llikGammaMix2 = function(param, x) {
if (any(param < 0) || param["p1"] > 1) {
return(-Inf)
} else {
return(sum(log(
dgamma(x, shape = param["k1"], scale = param["theta1"]) *
param["p1"] + dgamma(x, shape = param["k2"], scale = param["theta2"]) *
1
(1 - param["p1"])
)))
}
}
initialParams = list(
theta1 = 1,
k1 = 1.1,
p1 = 0.5,
theta2 = 10,
k2 = 2
)
for (i in 1:nrow(cichlids)) {
SWS1_training <- cichlids$SWS1 - cichlids$SWS1[i]
SWS1_test <- cichlids$SWS1[i]
MLE_training2 <-
optim(
par = initialParams,
fn = llikGammaMix2,
x = SWS1_training,
control = list(fnscale = -1)
)$par
LL_test2 <-
optim(
par = MLE_training2,
fn = llikGammaMix2,
x = SWS1_test,
control = list(fnscale = -1)
)$value
}
print(LL_test2)
This runs until it gets to the first optim(), then spits out Error in fn(par, ...) : attempt to apply non-function.
My first thought was a silly spelling error somewhere, but that doesn't seem to be the case. Any help is appreciated.
I believe the issue is in the return statement. It's unclear if you meant to multiply or add the last quantity (1 - param["p1"])))) to the return value. Based on being a mixture, I'm guessing you mean for it to be multiplied. Instead it just hangs at the end which throws issues for the function:
return(sum(log(dgamma(x, shape = param["k1"], scale = param["theta1"]) *
param["p1"] +
dgamma(x, shape = param["k2"], scale = param["theta2"]) *
(1 - param["p1"])))) ## ISSUE HERE: Is this what you meant?
There could be other issues with the code. I would double check that the function you are optimizing is what you think it ought to be. It's also hard to tell unless you give a reproducible example we might be able to use. Try to clear up the above issue and let us know if there are still problems.

R : Setting Red and Green Values of an RGB image to Zero by handling it as a matrix

I am working with the OpenImageR and SuperpixelImageSegmentation packages in R.
i wanted to extract the Green dimension of a segmented image, the result is a two dimensional image instead of 3 (grayscale).
so to colorize it i set the Red and Blue Values of the colored image to 0.
However, whenever i try to show the image i get the following error, and i can't understand why 0 is intrepreted to be NAN, i also tried setting it to a value that is superior to zero (0.01), and i still get the same message :
Error in rgb(t(x[, , 1L]), t(x[, , 2L]), t(x[, , 3L]), maxColorValue = max) :
color intensity nan, not in [0,1]
here is my code :
library(SuperpixelImageSegmentation)
library(OpenImageR)
path = system.file("tmp_images", "Phen.jpg", package = "OpenImageR")
image = readImage(path)
init = Image_Segmentation$new()
segmentation = init$spixel_segmentation(input_image = image,
superpixel = 2000, # k
AP_data = TRUE,
use_median = TRUE,
sim_wA = 5,
sim_wB = 5,
sim_color_radius = 3,
kmeans_method = "kmeans",
kmeans_initializer = "kmeans++",
kmeans_num_init = 5,
kmeans_max_iters = 50,
verbose = TRUE)
#getting the green part alone
imG = segmentation$AP_image_data
imG = imG[,,2]
imB = segmentation$AP_image_data
imB = imB[,,3]
imR = segmentation$AP_image_data
imR = imR[,,1]
imR4 = imR / 2
imB4 = imB / 2
imGDone = imG - imR4
imGDone = imGDone - imB4
imageShow(imGDone) # works fine, the result is a mask that contains only the green concentrated areas
imGColor <- segmentation$AP_image_data
for (i in 1:nrow(imGDone)) {
for (j in 1:ncol(imGDone)){
if (imGColor[i,j,1] > 0) {
imGColor[i,j,1] = 0 * imGDone[i,j] # setting red to zero
imGColor[i,j,3] = 0 * imGDone[i,j] # setting blue to zero
imGColor[i,j,2] = 1 * imGDone[i,j] # setting green
}
}
}
imageShow(imGColor)
when i execute the last line to display imGColor i get the error.
i'm new to R, and i can't find any leads on what might be the cause of it.
and so i would like help on what i should do, and thank you so much in advance!
This is actually not a 'SuperpixelImageSegmentation' or 'OpenImageR' issue. The 'imageShow' function of the 'OpenImageR' package uses under the hood the grid::grid.raster function. The error that you receive is due to the fact that the modified values of R,G,B are not in the range between 0 and 1 as mentioned also in another stackoverflow issue. To overcome this issue you have to first normalize the pixel values to [0,1] and then make your adjustments. The 'path' to the file that you've mentioned does not exist in the 'OpenImageR' package therefore I used another available image of the package,
library(SuperpixelImageSegmentation)
library(OpenImageR)
path = system.file("tmp_images", "2.jpg", package = "OpenImageR")
image = readImage(path)
init = Image_Segmentation$new()
segmentation = init$spixel_segmentation(input_image = image,
superpixel = 2000, # k
AP_data = TRUE,
use_median = TRUE,
sim_wA = 5,
sim_wB = 5,
sim_color_radius = 3,
kmeans_method = "kmeans",
kmeans_initializer = "kmeans++",
kmeans_num_init = 5,
kmeans_max_iters = 50,
verbose = TRUE)
#getting the green part alone
imG = segmentation$AP_image_data
imG = imG[,,2]
imB = segmentation$AP_image_data
imB = imB[,,3]
imR = segmentation$AP_image_data
imR = imR[,,1]
imR4 = imR / 2
imB4 = imB / 2
imGDone = imG - imR4
imGDone = imGDone - imB4
imageShow(imGDone) # works fine, the result is a mask that contains only the green concentrated areas
# the 'imGDone' array has values in the range [-0.08039, -0.03824]
summary(as.vector(imGDone))
# you have to normalize first to [0,1] to avoid the error
imGDone <- OpenImageR::NormalizeObject(imGDone)
# values now in the range [0.0000, 1.0000]
summary(as.vector(imGDone))
imGColor <- segmentation$AP_image_data
for (i in 1:nrow(imGDone)) {
for (j in 1:ncol(imGDone)){
if (imGColor[i,j,1] > 0) {
imGColor[i,j,1] = 0 * imGDone[i,j] # setting red to zero
imGColor[i,j,3] = 0 * imGDone[i,j] # setting blue to zero
imGColor[i,j,2] = 1 * imGDone[i,j] # setting green
}
}
}
imageShow(imGColor)

RunUMAP gives segmentation fault

I tried the following
> my.exp <- RunUMAP(my.exp, dims = 1:30)
UMAP(a=None, angular_rp_forest=False, b=None, init='spectral',
learning_rate=1.0, local_connectivity=1, metric='correlation',
metric_kwds=None, min_dist=0.3, n_components=2, n_epochs=None,
n_neighbors=30, negative_sample_rate=5, random_state=None,
repulsion_strength=1.0, set_op_mix_ratio=1.0, spread=1.0,
target_metric='categorical', target_metric_kwds=None,
target_n_neighbors=-1, target_weight=0.5, transform_queue_size=4.0,
transform_seed=42, verbose=True)
Construct fuzzy simplicial set
0 / 14
1 / 14
2 / 14
*** caught segfault ***
address 0xfffffffffffffffa, cause 'memory not mapped'
Traceback:
1: py_call_impl(callable, dots$args, dots$keywords)
2: umap$fit_transform(as.matrix(x = object))
3: RunUMAP.default(object = data.use, assay = assay, n.neighbors = n.neighbors, n.components = n.components, metric = metric, n.epochs = n.epochs, learning.rate = learning.rate, min.dist = min.dist, spread = spread, set.op.mix.ratio = set.op.mix.ratio, local.connectivity = local.connectivity, repulsion.strength = repulsion.strength, negative.sample.rate = negative.sample.rate, a = a, b = b, seed.use = seed.use, metric.kwds = metric.kwds, angular.rp.forest = angular.rp.forest, reduction.key = reduction.key, verbose = verbose)
4: RunUMAP(object = data.use, assay = assay, n.neighbors = n.neighbors, n.components = n.components, metric = metric, n.epochs = n.epochs, learning.rate = learning.rate, min.dist = min.dist, spread = spread, set.op.mix.ratio = set.op.mix.ratio, local.connectivity = local.connectivity, repulsion.strength = repulsion.strength, negative.sample.rate = negative.sample.rate, a = a, b = b, seed.use = seed.use, metric.kwds = metric.kwds, angular.rp.forest = angular.rp.forest, reduction.key = reduction.key, verbose = verbose)
5: RunUMAP.Seurat(my.exp, dims = 1:30)
6: RunUMAP(my.exp, dims = 1:30)
I do not see a reason why it should be getting a sigfault here. I have run this function multiple times over last several months. This seems to be happening since about a week.
Any help is appreciated.
UPDATE: I have now restarted the machine one, removed entire older R installation, started with a fresh install. I am still getting exactly same error, including address 0xffffffffffffffffffa ....
Sameet

Radiometric correction of a landsat image using R

I want to do a radiometric correction of a landsat image using:
radiocorr(x, gain, offset, Grescale, Brescale, sunelev, satzenith, edist, Esun,
Lhaze, method = "apparentreflectance")
I performed the correction to each band, as follow:
B1 <- readGDAL("_X20060509_B_1.tif")
B1.ar<-radiocorr(x = B1, Grescale = 0.76583, Brescale = -2.28583, sunelev = 43.99853366,
satzenith = 0, edist = 1.0095786, Esun = 1983, method = "apparentreflectance")
writeGDAL(B1.ar, "C:/Users/Documents/ Reflectance/B1.tif", drivername="GTiff")
How can I make one function to automatically perform the correction to the six bands?
I tried with this function:
atmcor <- function(img, i) {
x<-img[[i]]
Grescale<-gain[i,2]
Brescale<-bias[i,2]
sunelev<-sunelevation[i,2]
satzenith=0
edist<-edistance[i,2]
Esun<-Esun[1,2]
method = "apparentreflectance"
B.ar<-radiocorr(x, Grescale, Brescale, sunelev, satzenith, edist, Esun, method)
return(B.ar)
}
ATMCOR <- atmcor(landsat_stack, 1)
But, I got this error:
(Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), :
length of 'dimnames' [1] not equal to array extent)
If you want to do the radiometric calibration for all bands in only one execution chunk you need to load your metadata file as well.
Therefore you can do it in several forms. But the following code can easily solve your problem.
radCor(img, metaData, method = "apref", bandSet = "full", hazeValues,
hazeBands, atmosphere, darkProp = 0.01, clamp = TRUE, verbose)
When you set the bandSet to "full", all the band in the solar region will be processed.

error with earth function using MARS algo

I run my ML algo:
EarthAlgo<-earth(cible~., data=train, degree=4, glm=list(family=binomial))
I got:
Error in leaps.setup(x = bx, y = y, force.in = 1, force.out = NULL, intercept = FALSE, :
NA/NaN/Inf in foreign function call (arg 3)
When I try with train<-train[(1:dim(train)[1]-2),], it's ok. I search last two line on my train data set but i do not see error.
Could you help me please?

Resources