Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Method for exposing the raw data #104

Closed
wants to merge 9 commits into from
Closed

Conversation

paalge
Copy link
Contributor

@paalge paalge commented Oct 29, 2015

This is a method for exposing the raw data as a numpy array.
Not sure if this is exactly the way you want to do it, but it looks like it is working for me

Fixes #47

@paalge paalge mentioned this pull request Oct 29, 2015
@@ -7,6 +7,7 @@
import random
import string
import tempfile
import numpy as np
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely don't want a hard dependency on numpy. Can we use a regular array for now, and we'll figure out a good general API to support numpy (hopefully w/o copying data to Python land first) later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I normally use numpy, so I have very little experience with the regular array. As long as you can write to a normal uint16 array from an iterator numpy isn't needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, I just meant a normal Python array. It won't be very efficient, but I've also opened an issue (#106) for discussing the best way to handle numpy support, which is something we want to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

II understand. Can you read from an iterator into such an array?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that should be fine. You won't be able to use the numpy methods to do it though; just regular iteration will work fine.

@paalge
Copy link
Contributor Author

paalge commented Oct 29, 2015

I've removed the numpy dependency and changed the error handling


try:
rawdata.color4_image.contents
except:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this makes sense, thanks! Let's catch the actual exception here though; bare excepts can have unintended side effects :)

)

# make 2D list
data = [[0 for i in range(iheight)] for j in range(iwidth)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be cleaner, no idea if there's a "standard" way to do this in Python:

data = [None] * iheight * iwidth

It might be a pre-mature optimization either way though. Or I may be completely wrong. Just thinking out loud. @campaul?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be 2D, or you'll have to pass height and width on, and let the calling function do the reshape.
From what I understood, list compression is the normal way of initialising this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good then. I think the above will make a 2d list too, but I could be mistaken. If we're going to have to iterate anyways though I think we might as well not preallocate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick quick test gives it as a 1D.
As for the preallocate, I like it due to the large lists being created, though I don't know what is fastest.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to loop and append, 0's though we might as well just loop and append the data. I'm not sure this is actually an optimization, unless Python does some sort of optimization on simple list comprehensions that I don't know about.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably be more efficient. I'll do some timings later and see for sure.

data = [[] for j in range(iwidth)]

for ii in range(iheight):
    for jj in range(iwidth):
        data[jj].append(data_pointer[ii * iwidth + jj])


for ii in range(iheight):
for jj in range(iwidth):
data[jj][ii] = data_pointer[ii * iwidth + jj]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still appears to be subscripting a 2D array; won't this be broken?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my test run this works as intended.
Though I'm not sure what you think would be broken?
Line 221 makes a 2D list, which one can index with [][] in line 225

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, yah, I was misunderstanding a large chunk of our discussion, sorry. I think it's best to make this a 1D list to match the data that we get from LibRaw / elsewhere in rawkit. Not sure though; will consider the API implications of doing this and get back to you. It's not super important right now.

# make 2D list
data = [[0 for i in range(iheight)] for j in range(iwidth)]

for ii in range(iheight):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change ii and jj to y and x respectively so we're working with Cartesian coordinates?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't actually coordinates on the image though since there are multiple indexes per pixel.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They will be coordinates, though you have to interpolate the image first, as in that loop they change between different colour channels.

@campaul
Copy link
Member

campaul commented Nov 10, 2015

@paalge if you go ahead and add a get_3_col_raw function, update the docs to say the functions are experimental, and then squash your commits I'm happy merging this.

We'll be making some changes to the interface as time goes on, but I want to get the core functionality merged in so I can start playing with it.

@paalge
Copy link
Contributor Author

paalge commented Nov 11, 2015

@campaul I've now added the requested method. I also fixed a lot of error in the original code. The returned image is the image excluding the calibration pixels (it could be possible to switch between this in an input flag). I've only tested the code one three types of images (ARW, NEF and three colour CR2). The three colour image is strange, as it includes a zero pixel.
Can you have a look at the code? I'll squash the git when you've taken a look

@campaul
Copy link
Member

campaul commented Nov 17, 2015

In general the code looks good, but the two functions are almost identical. Can you extract the shared code into a helper function so we aren't duplicating everything?

@paalge
Copy link
Contributor Author

paalge commented Dec 14, 2015

I'm sorry for the late update. I've now moved the common code into a new function, though I'm unsure of how much better it is with respect to readability, as there where some subtle differences in the code.

@campaul
Copy link
Member

campaul commented Jan 6, 2016

Oops, I didn't see the email from github for your last comment. I had already taken your commits and made the changes I wanted, as well as updated the tests, in #108. Could you take a look at that branch and let me know if you're still ok with how the code looks? If you're happy with things I'll merge that branch in and we should be good to go.

@paalge paalge mentioned this pull request Jan 6, 2016
@campaul
Copy link
Member

campaul commented Jan 9, 2016

This was included as part of #108.

@campaul campaul closed this Jan 9, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants