How to get the number of series from a DICOM file? [closed] - dicom

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have some 4-dimensional MR data in DICOM. The forth dimension can be time, b-value in DWI or whatever. How to determine how many slices and how many series in the forth dimension do I have?
For example, I have 400 images. How can I decide if there are 100 series and 4 slices or vice versa?
Edit:
I have figured it out by checking the slice position. If a given position repeats, I increment the number of stacks. My Python code below:
def getNumOfStacks(self, someImage):
sliceDict = dict()
for n in range(0, someImage.ImagesInAcquisition):
location = pydicom.dcmread(self.path+self.fileList[n]).SliceLocation
if location in sliceDict:
sliceDict[location] = sliceDict.get(location) + 1
else:
sliceDict[location] = 1
return list(sliceDict.values())[0]

The only way is to inspect the value of SeriesInstanceUID (tag number 0020,000e) for each single instance.
Depending on a tool you are using, the solution may be varied. For example, if you have dcmtk or gdcm, then in bash it would be like this:
find /path/to/dicom/files -exec dcmdump "{}" 2>/dev/null ";" | grep SeriesInstanceUID |sort -u
If you use gdcm, than put gdcmdump instead of dcmdump above.

MRI images in DICOM come in two different flavors:
"Traditional" MR Image Storage (SOP Class UID 1.2.840.10008.5.1.4.1.1.4)
Enhanced MR Image Storage (SOP Class UID 1.2.840.10008.5.1.4.1.1.4.1)
For both, like Bartlomiej wrote, the Series Instance UID can be used to determine which of the slices belong to the same series, and usually one series represents one stack of images.
For Enhanced MR, the concept of stacks was introduced. That is, a single DICOM object ("file") contains multiple frames ("images") which can be subdivided into stacks. In the Per Frame Functional Groups Sequence (5200,9230) you can find attributes which are specific for individual frames. In this case, you should read the Stack ID (0020,9056) and the In Stack Position Number (0020,9057) to seperate the stacks and order the slices within the stack.

Related

Identifying a specific pattern in several adjacent rows of a single column - R [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm back with my survey data.
This time, I need to remove a specific set of rows from data when they occur. In our survey, an automated telephone survey, the survey tool will attempt three times during that call to prompt the respondent to enter a response. After three timeouts of the question the survey tool hangs up. This mostly happens when the call goes to someone's voicemail.
I would like to identify that pattern when it happens so I can remove it from calculating call time.
The pattern I am looking for looks like this in the Interactions column:
It doesn't HAVE to be Intro. It can be any part of the survey where it prompting the respondent for a response THREE times but no response is provided so the call fails. But, it does have to be sandwiched in between "Answer" (the phone picks up) and "Timeout. Call failed." (a failure).
I did try to apply what I learned from yesterday's solution (about run length encoding) to my other indexing question but I couldn't make it work in the slightest. So, here I am.
Here's an example dataset:
This is 15 respondents and every interaction between the survey tool and the respondent (or their phone, essentially).
Here's the code for the dataframe: This goes to a Google Drive text editor with the code
If I understand the question correctly, the function below removes all rows between a row with "Answer" and a failure value (there are 3 such values in the question).
The name of the column to look for defaults to "Interactions", and the first answer and failure values also have defaults assigned.
Note that all match instructions are case sensitive.
removeRows <- function(X, col = "Interaction",
ans = "Answer",
fail = c("Timeout. Call failed.", "Partial", "Enqueueing call"))
{
a <- grep(ans, X[[col]])
f <- which(X[[col]] %in% fail)
a <- a[findInterval(f, a)]
for(i in seq_along(a)){
X[[col]][a[i]:f[i]] <- NA_character_
}
Y <- X[complete.cases(X), , drop = FALSE]
Y
}
removeRows(survey_data)

Best way to train a model in keras r [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Overview
I'm not new to R but am very new to machine learning.
For work I collect data by writing on a datasheet printed on waterproof paper which I then have to transcribe to the database manually. This takes a long time at the end of a long day and is a process prone to mistakes.
The entire datasheet is shown below
What I would like to do is simply take a photo of the sheet and have keras read it and input the results into a database
And the section of the datasheet that I am interested in getting Keras to read is shown here
Each row of the datasheet represents what species of coral was found and each column represents what transect it was found on ie 7 Acroppora was found on T1
Each of these cells are given a unique entry in the database in a format similar to this which would show how the Acropora row is recorded
For each datasheet that we have entered in the past (probably somewhere between 1000 and 2500) there are corresponding database entries which can be exported to csv and linked to each datasheet
Ultimately, what I would like to do is simply take a photo of the sheet and have keras read the part I'm interested in (shown in second image) and input the results into a CSV in a similar format shown in the third image
The questions
What I've been thinking about is getting it to identify the borders of the parts of the datasheet I'm interested in (shown in the second image) and extract it. This would mean that I could then put in coordinates for each cell, ie Acropora T1 (as shown in the image below) and identify the number counted in that cell and export it to a database
Does this process sound possible? If so, would anyone know of any examples I could look up or even what you would call this process so I can look it up
Otherwise I was thinking about scanning each sheet as a whole (As shown in the first image) and simply training from that, however I feel that would be more prone to errors
I really hope this makes sense and would very much appreciate any help and/or suggestions either specifically to the questions that I asked or about my project in general
This uses OpenCV and Python.
According to the chapter on 'Hough Line Transform' you could detect lines like this.
import cv2
import numpy as np
img = cv2.imread('D:/Books/lines1.jpg', cv2.IMREAD_GRAYSCALE)
edges = cv2.Canny(img,50,150,apertureSize = 3)
cv2.imwrite('D:/Books/edges.jpg',edges)
But based on my simple research I think counting is possible using code like this.
More knowledge of OpenCV is required at this stage. I think this is just dilating and the borders of the lines are more pronounced.
img = cv2.imread('D:/Books/lines1.jpg', cv2.IMREAD_GRAYSCALE)
edges = cv2.Canny(img,50,150,apertureSize = 3)
cv2.imwrite('D:/Books/edges.jpg',edges)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (4, 4))
dilated_Edges = cv2.dilate(edges, kernel, iterations=1)
cv2.imwrite("D:/Books/dilated_Edges.jpg", dilated_Edges);
lines = cv2.HoughLines(image=dilated_Edges,rho=1,theta=np.pi/180, threshold=100)
print( len(lines))
This prints 8 for me which isn't correct.
I pursued this and this code is based on help from the OpenCV forum(Suleyman TURKMEN).
Images I tested with are these. Prints the correct count.
import cv2
import math
img = cv2.imread('D:/Books/lines1.jpg', cv2.IMREAD_GRAYSCALE)
ret,bw = cv2.threshold(img,0,255,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
cv2.imshow("bw", bw)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 2))
eroded_Edges = cv2.erode(bw, kernel, iterations=3)
dilated_Edges = cv2.dilate(eroded_Edges, kernel, iterations=4)
im2, contours, hierarchy = cv2.findContours(dilated_Edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
print (len(contours) , " horizontal lines")
cv2.imshow("vertical lines", eroded_Edges)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 5))
eroded_Edges = cv2.erode(bw, kernel, iterations=3)
im2, contours, hierarchy = cv2.findContours(eroded_Edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
print (len(contours) , " vertical lines")
cv2.imshow("horizontal lines", eroded_Edges)
cv2.waitKey()

R read data from a text file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a challenging file-reading task.
I have a .txt file from a typical old accounting department (with headers, titles, pages and the useful tabulated quantitative and qualitative information). It looks like this:
From this file I am trying to do two tasks (with read.table and scan):
1) extract the information which is tabulated between | which is the accounting information (any trial ended in a not easy data frames or character vectors)
2) include as a variable each subtitle which begins with "Customers" in the text file: as you can see the Customer info is a title, then comes the accounting info (payables), then again another customer and the accounting info and so on. So is not a column, but a row (?)
I´ve been trying with read.table (several sep and quote parameters) and with scan and then having tried to work with the character vectors.
Thanks!!
I've been there before so I kind of know what you're going through.
I've got 2 news for you, one bad, one good. The bad one is I have read-in these types of files in SAS tons of times but never in R - however
the good news is I can give you some tips so you can work it out in R.
So the strategy is as follow:
1) You're going to read the file into a dataframe that contains only a single column. This column is character and will hold
a whole line of your input file. i.e. length is 80 if the largest line in your file is 80 long.
2) Now you have a data frame where every record equals a line in your input file. At this point you may want to check your
dataframe has the same number or records as per lines in your file.
3) Now you can use grep to get rid-off or keep only those lines that meet your criteria (ie subtitle which begins with "Customers").
You may find regular expressions really useful here.
4) Your dataframe now only have records that matches 'Customer' patterns and table patterns
(i.e line begin with 'Country' or /\d{3} \d{8}/ or ' Total').
5) What you need now is to create a group variable that increment +1 every time it finds 'Customer'. So group=1 will repeat the same value until it finds 'Customer 010343' where group is now group=2. Or even better your group can be customer id until a new id is found. You need to somehow retain the id until a new id is found.
From the last step you're pretty much done as you will be able to identify customers and tables pretty easy. You may want to create a function that output your table strings in a tabular format.
Whether you process them in a single table or split the data frame in n data frame to process them individually is up to you.
In SAS there is this concept of pointer (#) and retention (retain statement) where each line matching a criteria can be process differently from other criterias so you output data set already contains columns and customer info in a tabular format.
Well hope this helps you.

In Qualtrics, what are the limits on field names?

I've never used Qualtrics myself and do not need to, but my company receives Qualtrics-generated CSV data from another company, and we have to advise them about the names to use for fields/variables, such as "mobilephone".
The main thing I need to know is the maximum number of characters, but other limits (such as special characters to avoid) would be helpful. For example, would profile_field_twenty6chars be good? (The data is going into Moodle, which uses profile_field).
Qualtrics has 2 different limitations to my knowledge.
Question names are limited to 10 characters(absurd in my opinion)
Question Export tags (this defaults to the question text, and is shown on the second row for csv datasets) has a limit of 200 characters.

3D plot of galaxy or universe [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Is R code available for creating 3D plots of our galaxy or universe? I have searched a few times over the last six months and not found any.
This news article includes some very nice 3D plots that look like they may have been created with R:
http://www.dailymail.co.uk/sciencetech/article-2341750/The-beautiful-3D-map-space-plots-nearest-galaxies--reminds-tiny-Earth-is.html
A short video can be viewed at the above link, but I do not see a link to R code there. The video was created by people at the University of Lyon and the University of Hawaii. Here is a link to a longer video related to the same project:
http://irfu.cea.fr/cosmography
I just thought it would be neat to explore space from within a 3D plot in R but I cannot find any relevant code.
Locations for objects likely are found within the Redshift Catalog, and perhaps can be downloaded, but I have no idea whether I would need to adjust those location data in various ways if I tried to create my own 3D map. Here is one possible source of data if I were to try creating my own map:
https://www.cfa.harvard.edu/~dfabricant/huchra/zcat/
I have read something to the effect that asking for relevant packages does not make for an appropriate post. Sorry if this post is not appropriate.
The problem is not the modelling but the data. Here's a database made available by . http://www.stellar-database.com/isdb.mdb - but probably you'll need to dig around for what you want specifically.
Here's a simple SQL query to pull out some of the star data:
SELECT Positions.OwnerID, Positions.RA_hr, Positions.RA_min, Positions.RA_sec, Positions.Dec_deg, Positions.Dec_arcmin, Positions.Dec_arcsec, Positions.Distance, Spectra.SpectralClass, Spectra.LuminosityClass, qryProps.Name
FROM (Positions LEFT JOIN Spectra ON Positions.OwnerID = Spectra.OwnerID) LEFT JOIN qryProps ON Positions.OwnerID = qryProps.OwnerID
WHERE (((Positions.Distance)>=0));
Then save it as a csv and import it:
stars<-read.csv("qNamedStars.txt",header=T)
head(stars)
Write a function to translate the coords to X, Y, Z
celCoords<-function(Rh,Rm,Rs,Da,Dm,Ds,Distance){
R.angle<-((Rh/24)+(Rm/(24*60))+(Rm/(24*60*60)))*2*pi
D.angle<-(Da/90)+(Dm/(90*60))+(Ds/(90*60*60))*0.5*pi
Z<-cos(D.angle)*Distance
hyp.XY<-sin(D.angle)*Distance
X<-sin(R.angle)*hyp.XY
Y<-cos(R.angle)*hyp.XY
return(c(X,Y,Z))
}
starcoords<-cbind(stars,
matrix(celCoords(stars$RA_hr,
stars$RA_min,
stars$RA_sec,
stars$Dec_deg,
stars$Dec_arcmin,
stars$Dec_arcsec,
stars$Distance
),,ncol=3,byrow=T)
)
colnames(starcoords)<-c(colnames(stars),"X","Y","Z")
Filter the data.frame
sf<-starcoords[abs(starcoords$Z)<2000 & abs(starcoords$X)<1000,] # apply a filter
Then plot using rgl
require(rgl)
plot3d(sf$X,sf$Y,sf$Z,col=rainbow(nrow(sf)),size=10)
You can obviously add more data for luminosity, size, type, etc. if it's available, and then use those parameters to set size, color, etc.

Resources