How convert CT Dicom Files to HU(positive values)? - dicom

I'm working on DICOM files(CT files).I want to use Rescaleslope and Rescaleintercept for converting my data to HU with relation:
X_IMAGE(:,:,path.ImageIndex) = (dicomread(path))*path.RescaleSlope + path.RescaleIntercept;
but the values of these fields are 1 and 0,respectively. So no conversion doing and values don't change. why Rescaleintercept is not equal to -1024?
thank you

To convert from the normal units found in CT data (a typical data set ranges from 0 to 4000 or so) you have to apply a linear transformation of the data. The equation is:
hu = pixel_value * slope + intercept
About More Converting CT Data to Hounsfield Units

In python using pydicom library I use the script below to convert to HU:
import pydicom
dicom = pydicom.dcmread(filepath)
dicom_hu = dicom.pixel_array * dicom.RescaleSlope + dicom.RescaleIntercept

pydicom has a data handler function which automatically converts CT to hounsfield units (provided Hounsfield units are given in the dicom metadata lookup table):
pydicom.pixel_data_handlers.util.apply_modality_lut
https://pydicom.github.io/pydicom/dev/reference/generated/pydicom.pixel_data_handlers.util.html
A single dicom slice may be converted using the following code:
dcm_i = pydicom.dcmread(dcm_slice_filepath)
arr_i = dcm_i.pixel_array
hu_arr_i = pydicom.pixel_data_handlers.util.apply_modality_lut(arr_i, dcm_i)

Related

R read_delim() changes values when reading data

I am trying to read in a tabstop seperated csv file using read_delim(). For some reason the function seems to change some field entries in integer values:
# Here some example data
# This should have 3 columns and 1 row
file_string = c("Tage\tID\tVISITS\n19.02.01\t2163994407707046646\t40")
# reading that data using read_delim()
data = read_delim(file_string, delim = "\t")
view(data)
data$ID
2163994407707046656 # This should be 2163994407707046646
I totally do not understand what is happening here. If I chnage the col type to character the entry stays the same. Does anyone has an explanation for this?
Happy about any help!
Your number has so many digits, that it does not fit into the R object. According to the specification IEEE 754, the precision of double is 53 bits which is approx. a number with 15 decimal digits. You reach that limit using as.double("2163994407707046646").

How do you change the Cp/Ct array values within FLORIS?

I'd like to run a FLORIS simulation to calculate the wake for a specific turbine. Currently, the input file given in FLORIS is the "example_input.json" which defines the Cp and Ct values for the NREL 5MW at different wind speeds.
I want to run a simulation for a different turbine and I have the array values for that turbine. I'm wondering if there is an easier way to change the redefine the Cp/Ct array values within FLORIS/python rather than manually typing the array values in the .json input file.
You can do this using the change_turbine handle in the floris object. It is used as follows, say you want to change the power and thrust tables for turbine 0, 1 and 2 in your floris object called fi:
fi.change_turbine(
turb_num_array=[0, 1, 2],
turbine_change_dict={
"power_thrust_table": new_power_thrust_table,
}
)
where new_power_thrust_table is a dict with three keys: power (the power coefficients), thrust (the thrust coefficients), and wind_speed (the wind speeds). Each should contain one array or list defining the new values (respectively, new power coefficients, thrust coefficients, and wind speeds to which the former two belong).
Also, you may want to change the turbine rotor diameter at the same time, for example to 140 m. You can do that with:
fi.change_turbine(
turb_num_array=[0, 1, 2],
turbine_change_dict={
"power_thrust_table": new_power_thrust_table,
"rotor_diameter": 140.
}
)
Alternatively, say you want to copy over the turbine properties from the first turbine of a different floris object, fi_b, you could do something like:
fi.change_turbine(
turb_num_array=[0, 1, 2],
turbine_change_dict={
"power_thrust_table": fi_b.floris.farm.turbines[0].power_thrust_table,
"rotor_diameter": fi_b.floris.farm.turbines[0].rotor_diameter
}
)

Understanding the output of MFCCs

I am a beginner of signal analysis. I want to extract the MFCCs of a sound, because I read that MFCC is a good parameter for automatic speech recognition. So I tried that in RStudio like this:
wl=512
ncep=13
mfcc.peewit <- melfcc(peewit,sr=peewit#samp.rate,wintime = wl/f,hoptime = wl/f,numcep = ncep,
nbands = ncep*2,fbtype = "htkmel",dcttype = "t3",htklifter = TRUE,
lifterexp = ncep-1,frames_in_rows = FALSE,spec_out = TRUE)
It turned out a 13*30 data frame and I am confused about the output of MFCC. I thought MFCCs would be 13 actual numbers but here I got a data frame, is the data frame MFCCs? Or I did something wrong? Or, I read this somewhere else that the 13 in "13*30" is the discrete representation of 13 coefficients, is that correct?
Thank you for your reply in advance.
The audio signal is a time-series. There will be one set of MFCC coefficients per hop. Typical hop times for speech is maybe around 20-50 ms. So the 13 dimension is MFCC, and the 30 dimension is time.

R package spatstat: How to use point process model covariate as factor starting with shape file

I have a question similar to this one from 2014, which was answered but the datasets are no longer available and our original data structures differ. (I'm in crunch time and stumped, so if you're able to respond quickly I would greatly appreciate it!!)
Goal: use the type of bedrock as a covariate in a Point Process Model (ppm) in spatstat with mine locations in Connecticut.
Data: the files are available from this Dropbox folder. The rock data and CT poly outline comes from UConn Magic Library, and the mine data comes from the USGS Mineral Resources Data System.
Approach:
I loaded some relevant packages and read in the shapefiles (and converted coords to match CT's system), and used the CT polygon as an owin object.
library(rgdal)
library(splancs)
library(spatstat)
library(sp)
library(raster)
library(geostatsp)
#read in shapefiles
ct <-readOGR(".","CONNECTICUT_STATE_POLY")
mrds <-readOGR(".","mrds-2017-02-20-23-30-58")
rock<-readOGR(".","bedrockpolyct_37800_0000_2000_s50_ctgnhs_1_shp_wgs84")
#convert mrds and rock to ct's coord system
tempcrs<-ct#proj4string
mrds<-spTransform(mrds,tempcrs)
rock<-spTransform(rock,tempcrs)
#turn ct shapefile into owin, call it w for window
w <-as.owin(ct)
#subset mrds data to just CT mines
mrdsCT <-subset(mrds,mrds#data$state=="Connecticut")
#ppm can't handle marked data yet, so need to unmark()
#create ppp object for mrds data, set window to w
mrdsCT.ppp <-as.ppp(mrdsCT)
Window(mrdsCT.ppp)<-w
From "Modelling Spatial Point Patterns in R" by Baddeley & Turner (page 39):
Unfortunately a pixel image in spatstat cannot have categorical (factor) values, because R refuses to create a factor-valued matrix. In order to represent a categorical variate as a pixel image, the categorical values should be encoded as integers (for efficiency’s sake) and assigned to an integer-valued pixel image. Then the model formula should invoke the factor command on this image. For example if fim is an image with integer values which represent levels of a factor, then:
ppm(X, ˜factor(f), Poisson(), covariates=list(f=fim))
There are several different types of rock classification included in the shapefile. I'm interested in LITHO1, which is a factor with 27 levels. It's the sixth attribute.
litho1<-rock[,6]
My (limited but researched) understanding is that I need to convert the shapefile to a raster, and later convert it to an image in order to be used in ppm. I created a mask from ct, and used that.
ctmask<-raster(ct, resolution=2000)
ctmask[!is.na(ctmask)] <- 0
litho1rast<-rasterize(litho1,ctmask)
After this point, I've tried several approaches and haven't had success just yet. I've attempted to follow the approaches laid out in the question linked, as well as search in documentation for relevant examples to adopt (factor, ratify, levels). Unlike the prior question, my data was already a factor, so it wasn't clear why I should apply the factor function to it.
Looking at litho1rast, the #data#attributes dataframe contains the following. If I plot it, it just plots the ID; levelplot function does plot LITHO1. When I would apply the factor functions, the ID would be retained but not LITHO1.
$ ID : int [1:1891] 1 2 3 4 5 6 7 8 9 10 ...
$ LITHO1: Factor w/ 27 levels "amphibolite",..: 23 16 23 16 23 16 24 23 16 24 ...
The ppm model would need an object class im, so I converted the raster to the im. I tried two ways. I can make ppm execute...but it treats every point as a factor rather than the 27 levels (with either litho1.im or litho1.im2) ...
litho1.im<-as.im(litho1rast)
litho1.im2<-as.im.RasterLayer(litho1rast)
model1=ppm(unmark(mrdsCT.ppp) ~ factor(COV1), covariates=list(COV1=litho1.im))
model1
So, I'm not quite sure where to go from here. It seems like I need to pass an argument to the as.im so that it knows to retain the LITHO1 not the ID. Clever ideas or leads to pertinent functions or approaches much appreciated!
The quoted statement from Baddeley & Turner is no longer true --- that quotation is from a very old set of workshop notes.
Pixel images of class im can have factor values (since 2011). If Z is an integer-valued pixel image (of class im), you can make it into a factor-valued image by setting levels(Z) <- lev where lev is the character vector of labels for the possible values.
You should not need to use rasterize: it should be possible to convert rock[,6] directly into a pixel image using as.im (after loading the maptools package).
See the book by Baddeley, Rubak and Turner (Spatial point patterns: methodology and applications with R, CRC Press, 2016) for a full explanation.
Looking at your code you don't seem to be providing the field argument to rasterize.
From rasterize help:
fieldnumeric or character. The value(s) to be transferred. This can
be a single number, or a vector of numbers that has the same length as
the number of spatial features (points, lines, polygons). If x is a
Spatial*DataFrame, this can be the column name of the variable to be
transferred. If missing, the attribute index is used (i.e. numbers
from 1 to the number of features). You can also provide a vector with
the same length as the number of spatial features, or a matrix where
the number of rows matches the number of spatial features
at this line:
litho1rast<-rasterize(litho1,ctmask)
you probably have to specify which column of the litho object to use in rasterization. Something like:
litho1rast<-rasterize(litho1,ctmask, field = "LITHO1")

Optimsimplex: fail in defining a necessary argument

I was trying to do a regular simplex (the notion of a triangle or tetrahedron to arbitrary dimensions) to start an optimization set of experiments. The Optimsimplex package provides an easy and useful way to achieve this by using the Spendley method:
library('optimsimplex') #Paquete necesario
Ultra <- optimsimplex(method ='spendley',
x0=c(Vhno3=3,Vh2o2=1,Msample=300,Tsonic=15))
The result Ultra is a optimsimplex class object containing the spatial dimension (n), and the (n) coordenates for each (n+1) vertexes. It is possible to specify a dimension (length) of the simplex by using the len option:
len: The dimension of the simplex. If length is a value, that unique length is used in all directions. If length is a vector with n values, each length is used with the corresponding direction. Only used if method is set to ’axes’ or ’spendley’
But this result on a error that I can not understand:
Ultra <- optimsimplex(method ='spendley',
x0=c(Vhno3=3,Vh2o2=1,Msample=300,Tsonic=15),
len=c(pVhno3=0.5,pVh2o2=0.25,pMsample=50,pTsonic=5))
Error: optimsimplex: The len vector is expected to be a row matrix, but current shape is 1 x 4
So, a 1 x 4 is not a row matrix as {optimsimplex} expected? Could this perhaps correspond to some kind of bug in the package? Thanks in advance.
The problem gets solved by using the new version of optimsimplex package which according to Sebastien Bihorel will be available soon on CRAN but is currently aviable on Optimsimplex-Github

Resources