itk ImageFileReader exception reading if I add VTK Imagewriter object creation - vtk

That's it:
I read successfully a DICOM file with itk::ImageFileReader.
Now I want to export an image.
I use vtkJPEGWriter.
When I add the line
vtkJPEGWriter* writer = vtkJPEGWriter::New();
even if that code doesn't run at the beginning of execution... I can't read the file. I comment the line, then I read the file again.
But the writer is not connected with the file reader. I don't get it. It has nothing to do at that moment!!
I'm wasting so much time, just trying to figure out what's the problem.

The problem is in the file. I don't know why it works with that file without that line. Really weird.
I just don't get it.
I will try with other files.

These lines are worked for me:
vtkSmartPointer<vtkJPEGWriter> JPEGWriter = vtkSmartPointer<vtkJPEGWriter>::New();
JPEGWriter->SetFileName("d:\\Tempx\\Pacienttest3\\Sagital.bmp");
JPEGWriter->SetInputConnection(m_pColor->GetOutputPort());
JPEGWriter->Write();
where m_pColor is kind of vtkImageMapToColors type ...

Related

.csv is empty after reading it with pd.read_csv()

After running
df = pd.read_csv('my_file.csv'),
my original .csv file goes blank. Is there a way to read the .csv data without emptying the original file?
pd.read_csv() does not modify the file!
Here, the file before using pd.read_csv():
Using it:
And now if we check it again, the file hasn't changed (as expected):
So the problem isn't with pd.read_csv(). I would assume that you have other code that's messing things up. Take a look and tell us, so we can help you better.

Xref table not zero-indexed. ID numbers for objects will be corrected. won't continue

I am trying to open a pdf to get the number of pages. I am using PyPDF2.
Here is my code:
def pdfPageReader(file_name):
try:
reader = PyPDF2.PdfReader(file_name, strict=True)
number_of_pages = len(reader.pages)
print(f"{file_name} = {number_of_pages}")
return number_of_pages
except:
return "1"
But then i run into this error:
PdfReadWarning: Xref table not zero-indexed. ID numbers for objects will be corrected. [pdf.py:1736]
I tried to use strict=True and strict=False, When it is True, it displays this message, and nothing, I waited for 30minutes, but nothing happened. When it is False, it just display nothing, and that's it, just do nothing, if I press ctrl+c on the terminal (cmd, windows 10) then it cancel that open and continues (I run this in a batch of pdf files). Only 1 in the batch got this problem.
My questions are, how do I fix this, or how do I skip this, or how can I cancel this and move on with the other pdf files?
If somebody had a similar problem and it even crashed the program with this error message
File "C:\Programy\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 1604, in getObject
% (indirectReference.idnum, indirectReference.generation, idnum, generation))
PyPDF2.utils.PdfReadError: Expected object ID (14 0) does not match actual (13 0); xref table not zero-indexed.
It helped me to add the strict argument equal to False for my pdf reader
pdf_reader = PdfReader(input_file, strict=False)
For anybody else who may be running into this problem, and found that strict=False didn't help, I was able to solve the problem by just re-saving a new copy of the file in Adobe Acrobat Reader. I just opened the PDF file inside an actual copy of Adobe Acrobat Reader (the plain ol' free version on Windows), did a "Save as...", and gave the file a new name. Then I ran my script again using the newly saved copy of my PDF file.
Apparently, the PDF file I was using, which were generated directly from my scanner, were somehow corrupt, even though I could open and view it just fine in Reader. Making a duplicate copy of the file via re-saving in Acrobat Reader somehow seemed to correct whatever was missing.
I had the same problem and looked for a way to skip it. I am not a programmer but looking at the documentation about warnings there is a piece of code that helps you avoid such hindrance.
Although I wouldn't recomend this as a solution, the piece of code that I used for my purpose is (just copied and pasted it from doc on link)
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
This happens to me when the file was created in a printer / scanner combo that generates PDFs. I could read in the PDF with only a warning though so I read it in, and then rewrote it as a new file. I could append that new one.
from PyPDF2 import PdfMerger, PdfReader, PdfWriter
reader = PdfReader("scanner_generated.pdf", strict=False)
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
with open("fixedPDF.pdf", "wb") as fp:
writer.write(fp)
merger = PdfMerger()
merger.append("fixedPDF.pdf")
I had the exact same problem, and the solutions did help but didn't solve the problem completely, at least the one setting strict=False & resaving the document using Acrobat reader.
Anyway, I still got a stream error, but I was able to fix it after using an PDF online repair. I used sejda.com but please be aware that you are uploading your PDF on some website, so make sure there is nothing sensible in there.

Load spydata file

I'm coming from R + Rstudio. In RStudio, you can save objects to an .RData file using save()
save(object_to_save, file = "C:/path/where/RData/file/will/be/saved.RData")
You can then load() the objects :
load(file = "C:/path/where/RData/file/was/saved.RData")
I'm now using Spyder and Python3, and I was wondering if the same thing is possible.
I'm aware everything in the globalenv can be saved to a .spydata using this :
But I'm looking for a way to save to a .spydata file in the code. Basically, just the code under the buttons.
Bonus points if the answer includes a way to save an object (or multiple objects) and not the whole env.
(Please note I'm not looking for an answer using pickle or shelve, but really something similar to R's load() and save().)
(Spyder developer here) There's no way to do what you ask for with a command in Spyder consoles.
If you'd like to see this in a future Spyder release, please open an issue in our issues tracker about it, so we don't forget to consider it.
Considering the comment here, we can
rename the file from .spydata to .tar
extract the file (using file manager, for example). It will deliver a file .pickle (and maybe a .npy)
extract the objects saved from the environment:
import pickle
with open(path, 'rb') as f:
data_temp = pickle.load(f)
that object will be a dictionary with the objects saved.

Opening hdf5 file from pandas.HDFStore - get all keys and root.attributes?

This seems a bit silly that I can't figure this out, but I'm really at a loss here.
So let's say I have this:
In[6]: store
Out[6]:
<class 'pandas.io.pytables.HDFStore'>
File path: E:\Users\Dan\Desktop\Cell1-Wash-out-001\Cell1-Wash-out-001.h5
/voltage_recording frame (shape->[3200000,4])
Which is fine, and I can access both store.voltage_recording or store.root.attributes fine.
But once I close the file, I cannot seem to how to reopen it in a way that I can return these values again.
I know with pd.read_hdf() I can return, for example, the voltage_recording key. But I can't figure out how to get the whole pandas.io.pytables.HDFStore object back.
Is there a function somewhere I'm missing? I know I can also open the file itself with pytables, but that doesn't seem to be getting me where I want to go either.
quoted from Jeff in the comments:
"you just open like normal store = pd.HDFStore(filename,mode='r')
(mode is append by default, but if you aren't modifying doesn't
matter). to_hdf/read_hdf auto open/close."

.txt file is acting weird under readlines()

The contents of the file look like this:
1/15/13,930,1441.5
1/15/13,1000,1442.75
1/15/13,1030,1444
I run:
the_txt_file = open('/txt_file')
Then I run:
the_txt_file_as_a_list = the_txt_file.readlines()
Then I run:
print the_txt_file_as_a_list
And I get this:
['1/15/13,930,1441.5\r1/15/13,1000,1442.75\r1/15/13,1030,1444\r1/']
But I was expecting something like:
['1/15/13,930,1441.5\n','15/13,1000,1442.75\n','15/13,1030,1444\n']
This happens to me pretty frequently, what is going on?
So it seems that the problem had something to do with the way my mac interacted with the .txt file
The problem was fixed by swapping:
the_txt_file = open('/txt_file')
with:
the_txt_file = open('/txt_file', 'rU')
The 'rU' is called 'universal-readline'. Opening a file in 'rU' mode is opening a file in Universal readline mode. Upon running:
the_txt_file_as_a_list = the_txt_file.readlines()
and then:
print the_txt_file_as_a_list
my output went from:
['1/15/13,930,1441.5\r1/15/13,1000,1442.75\r1/15/13,1030,1444\r1/']
to:
['1/15/13,930,1441.5\n', '1/15/13,1000,1442.75\n', '1/15/13,1030,1444\n']
Later, I was able to print each item seperatly by:
for item in the_txt_file_as_a_list:
print item
The output looked like:
1/15/13,930,1441.5
1/15/13,1000,1442.75
1/15/13,1030,1444
I would assume that you, or the original creator of this data file were on a Mac. Seems you are expecting it to be a simple '\n' line ending, but suffer from the originating editors system default line ending (most likely).
An easy fix, is to call open(...) with the rU option like so:
the_txt_file = open('/txt_file', 'rU')
This ensures that the file is opened read only, and uses Universal newline support when reading the particular file.
Good luck!

Resources