We're Hiring!

Error in retrieving mrc files!

General and open developer discussion about using OMERO APIs from C++, Java, Python, Matlab and more! Please new questions at https://forum.image.sc/tags/omero
Please note:
Historical discussions about OMERO. Please look for and ask new questions at https://forum.image.sc/tags/omero

If you are having trouble with custom code, please provide a link to a public repository, ideally GitHub.

Error in retrieving mrc files!

Postby pz3977 » Mon Aug 18, 2014 3:20 pm

Hi,

I was trying to retrieve an image by ImageID using the online sample code. Jpg files was downloaded successfully, however, it failed while downloading .mrc file.

In some cases, it did not give me any error but it stops working without any respond. But some times it give me the following error:

Traceback (most recent call last):
File "testFetch.py", line 36, in <module>
renderedImage.save("/home//mine/tomo121700.mrc")
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1423, in save
raise KeyError(ext) # unknown extension
KeyError: '.mrc'

Since downloading mrc files are critical to my project, could you please help me!

Following is the code which I tried.

conn = BlitzGateway(user, password, host="omero.ohsu.edu")
conn.connect()

imageId=16353
image = conn.getObject("Image", imageId)

print "\nImage:%s" % imageId

# Retrieve information about an image.
print " X:", image.getSizeX()
print " Y:", image.getSizeY()
print " Z:", image.getSizeZ()
print " C:", image.getSizeC()
print " T:", image.getSizeT()
# render the first timepoint, mid Z section
z = image.getSizeZ() / 2
t = 0

renderedImage = image.renderImage(z, t)
#renderedImage.show()
renderedImage.save("Tomo37.mrc")

conn._closeSession()


Best regards,
pz3977
pz3977
 
Posts: 31
Joined: Thu Jul 10, 2014 11:39 am

Re: Error in retrieving mrc files!

Postby wmoore » Mon Aug 18, 2014 3:31 pm

Hi,

You just need to give the image you're saving an extension that is recognised by PIL. E.g. png, jpg etc.

E.g.
Code: Select all
renderedImage.save("Tomo37.mrc.png")
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: Error in retrieving mrc files!

Postby pz3977 » Thu Aug 21, 2014 9:15 am

Hi wmoore,

Thanks for the reply.

I tried to download the mrc file in form of jpg (as you said). It is downloaded, however, this way does not work for me since after that , when I remove jpg extension and try to read it using mrc reader, it gave me some errors which means the structure of the file has changed, and obviously, it is not a mrc file any more.

Is there any way to download mrc file as a mrc?

Best regards,
pz3977
pz3977
 
Posts: 31
Joined: Thu Jul 10, 2014 11:39 am

Re: Error in retrieving mrc files!

Postby wmoore » Thu Aug 21, 2014 9:43 am

Hi,

The code example that does what you want is in the web code, see
Code: Select all
def archived_files()
at

https://github.com/openmicroscopy/openm ... s.py#L1794

This is a bit more complex than what you need since it handles multiple images and image types that have multiple "original files" and writes everything into a zip file for download.
Here is the simplified version (I haven't run this code, just copied the essential bits from link above).
This should write the mrc file(s) to the same directory that the script is running in.
Code: Select all
image = conn.getObject("Image", iid)
for orig_file in image.getImportedImageFiles():
    fileName = orig_file.name    # E.g. image.mrc
    f = open(fileName,"wb")
    try:
        for chunk in a.getFileInChunks():
            f.write(chunk)
        finally:
            f.close()


Hope that works!
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: Error in retrieving mrc files!

Postby pz3977 » Fri Aug 22, 2014 3:30 pm

Hi wmoore,

Thanks for the help. I tried the code that you provided. I replaced "a" with "orig_file" in:
for chunk in a.getFileInChuncks():

the rest was OK, however, it gives me following error:
AttributeError: 'OriginalFileI' object has no attribute 'getFileInChuncks'

which I think it requires archive files. while I am trying to download ".mrc". Is that true? Could you please help me to solve it?

Best regards,
pz3977
pz3977
 
Posts: 31
Joined: Thu Jul 10, 2014 11:39 am

Re: Error in retrieving mrc files!

Postby wmoore » Sat Aug 23, 2014 5:23 pm

Hi,

I just tried this

Code: Select all
image = conn.getObject("Image", iid)
for orig_file in image.getImportedImageFiles():
    fileName = orig_file.name    # E.g. image.mrc
    print orig_file
    print fileName
    f = open(fileName,"wb")
    for chunk in orig_file.getFileInChunks():
        print "chunk..."
        f.write(chunk)
    f.close()


And it worked fine...
printing:
Code: Select all

<_OriginalFileWrapper id=8813>
CSFV-GFP01_1_R3D_D3D.dv
chunk...
chunk...
chunk...
chunk...
chunk...
<_OriginalFileWrapper id=8814>
CSFV-GFP01_1_R3D.dv.log
chunk...
<_OriginalFileWrapper id=8815>
CSFV-GFP01_1_R3D_D3D_log.txt
chunk...


Does it print anything useful for you?

What version of OMERO are you using?
Can you download the mrc file in the web client or insight?

Will.
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: Error in retrieving mrc files!

Postby pz3977 » Mon Aug 25, 2014 3:55 pm

Hi Will,

Thank you so much for your answer. I copied-pasted your code and it worked for me. Although I could not find any differernce between your code and mine, it seems pyhton likes your code better :)

I have another question anyway...

How long does it normally take to download a file around 600MB? It seems very slow to me, more than 30 minutes. Is it normal?

Regards,
pz3977
pz3977
 
Posts: 31
Joined: Thu Jul 10, 2014 11:39 am

Re: Error in retrieving mrc files!

Postby wmoore » Mon Aug 25, 2014 8:26 pm

Just tried downloading a 350 MB file locally (local server on my laptop) with the code above, and it took just over 5 seconds. So I don't think the speed is limited by the code / OMERO itself.

Do you have a good connection / bandwidth between your server and where your script is running?
This could well be the bottleneck.
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: Error in retrieving mrc files!

Postby pz3977 » Fri Aug 29, 2014 9:17 am

Hi wmoore,

Thanks for the reply! You are right. I was trying to download a file from “omero.ohsu.edu” and I guess the connection is the problem.
I was wondered if I can use this code to download any type of file( e.g. tiff etc)? I am not sure what is the reason but I could not download a file in some cases. Unfortunately it did not give me any error.I have to dig in to find the reason but first I wanted to make sure that in theory this code works for any type of file .

Best regards,
pz3977
pz3977
 
Posts: 31
Joined: Thu Jul 10, 2014 11:39 am

Re: Error in retrieving mrc files!

Postby wmoore » Fri Aug 29, 2014 12:52 pm

Yes, this should download any type of file.

This code will download all the files that are imported in a single "Fileset". We use Bio-Formats to group files together at import (E.g. log files will be associated with their corresponding image files) and all these go together in one fileset.

If you are having problems downloading, try the same thing in web or Insight clients (select image and download from top of right panel). It's possible to have images with no files, if they were created by the API instead of via import. But if they are imported and are viewable, then they should have files you can download.

Will.
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm


Return to Developer Discussion

Who is online

Users browsing this forum: Google [Bot] and 0 guests