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

Add post on custom array indices #498

Merged
merged 11 commits into from
Apr 20, 2017
Merged

Add post on custom array indices #498

merged 11 commits into from
Apr 20, 2017

Conversation

timholy
Copy link
Member

@timholy timholy commented Feb 6, 2017

This isn't ready yet, but I'm opening as a PR now to make it easier for anyone to comment or ask questions at specific points in the text.


```julia
julia> B3[1,1]
ERROR: BoundsError: attempt to access OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}} with indices 2:4×1:4 at index [1,1]
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps use v0.6 stacktraces in the final version of the post?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. Esp. since 0.5.0 (rather than release-0.5) has some significant bugs when it comes to indices support.

@ViralBShah
Copy link
Member

Bump. In case this is ready to merge.


In summary, it's very straightforward to pick a particular region of
an array and extract it for further analysis. Like the Talking Heads
song "Road to Nowhere," we know where we're going...
Copy link
Member

Choose a reason for hiding this comment

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

Question for you, Tim (related to what follows). Let's say I wanted to extract a region from an image into a buffer. I'm going to do a lot of expensive calculations on that region, so it makes sense to copy that data into a buffer instead of using a view into the original image. Let's also say that the buffer will be a fixed size, but the target location will change once I'm done processing one object. For example, I grab the left eye, calculate, then grab the right eye and calculate.

Can I mutate the ranges used to define the offset array (keeping the array size fixed) so I don't have to fill up the garbage collector with a whole bunch of buffers?

Copy link
Member Author

Choose a reason for hiding this comment

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

You can definitely re-use the buffer. You have to create a new OffsetArray wrapper. Something like this:

buf = Matrix{T}(30, 30)
chunk = OffsetArray(buf, 201:230, 201:230)
R = CartesianRange(indices(chunk))
copy!(chunk, R, img, R)
# do something useful
chunk = OffsetArray(buf, 201:230, 401:430)
R = CartesianRange(indices(chunk))
copy!(chunk, R, img, R)
# do something useful

Does that answer your question?

You may know this already, but for reasons of safety we don't allow turning off bounds-checking on OffsetArrays; you'll want the @unsafe macro if you need high performance. By Julia 1.0 I think we should turn on the ability to remove bounds-checks. This blog post will hopefully get more people testing non-1 arrays so more of our code is ready.

Copy link
Member

Choose a reason for hiding this comment

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

Perfect, thanks for the tip.

@timholy
Copy link
Member Author

timholy commented Apr 17, 2017

OK, I reworked the introductory example to exploit the work that @Evizero and I have been doing in ImageTransformations. I think that makes a more compelling first example.

Is there a good way to preview the result? The image links are specific for the paths here, and there's math formatting. If not, let's just merge it and I can fix the issues before I advertise the new post.

```

`paddedviews(0, arrays...)` pads input arrays with 0, as needed, to
given them all the same indices, and `colorview(RGB, r, g, b)` inserts
Copy link
Member

Choose a reason for hiding this comment

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

given -> give (?)

@timholy timholy force-pushed the teh/offset_arrays branch from 329e70b to c197602 Compare April 17, 2017 19:00
@timholy
Copy link
Member Author

timholy commented Apr 17, 2017

If anyone besides me ends up merging this, my intention is to squash into a single commit.

@ViralBShah
Copy link
Member

Best if you merge it whenever you are ready.

@timholy
Copy link
Member Author

timholy commented Apr 18, 2017

That makes sense; that way I can be on the lookout for formatting errors and fix them quickly.

I might wait until I tag JuliaIO/ImageMagick.jl#85 (hopefully in a day or two).

@KristofferC
Copy link
Member

KristofferC commented Apr 18, 2017

The way I chose to put the paths for the images, they are at least showed in the markdown preview: https://github.com/JuliaLang/julialang.github.com/blob/kc/repl0.6/blog/_posts/2017-02-22-repl-0.6-highlights.md

@timholy timholy force-pushed the teh/offset_arrays branch from 34e2144 to 4ef69bd Compare April 18, 2017 22:06
@timholy timholy force-pushed the teh/offset_arrays branch from df3c91a to cc7bf7c Compare April 19, 2017 09:56
@timholy timholy merged commit 7e42b40 into master Apr 20, 2017
@timholy timholy deleted the teh/offset_arrays branch April 20, 2017 17:52
@ararslan
Copy link
Member

Woot! Great to see this merged. Excellent work here, Tim!

@timholy
Copy link
Member Author

timholy commented Apr 20, 2017

Well, the images are all borked.

@timholy
Copy link
Member Author

timholy commented Apr 20, 2017

OK, fixed now (see #556). I'll wait a few hours before I advertise this on Discourse, in case anyone sees problems with the "published" product that didn't show up during review.

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.

6 participants