« first day (1796 days earlier)      last day (1417 days later) » 

10:50 AM
@CrisLuengo I have an "easy" image processing task, but I'd like to double check with you, just in case there is something better than what I though to solve it (as you are the knowledge base on the field!). I have a full body PET/CT image (volume). I want to estimate the "pose" of the patient, i.e. identify their arms/joints/torso/head. This is so I can later on mask each part for some processing
My initial approach to tackle this would be: Binarize->Erode->skeletonize->separate each "bone"/branch->dilate each of them separately.
Sounds reasonable and may work. My question is: can you think of something better? Its funny because any google search gives me much more complex stuff for much more complex problems (pose stimation from color images, etc)
 
Just use Gaussian mixture models for clustering stackoverflow.com/questions/28923865/…
 
Why do you think that would result in a better image?
 
11:29 AM
It was a joke :P That question is not even wrong
 
😛 OK. I was a bit surprised, I down-voted that question in the past
 
11:46 AM
@AndrasDeak python one liner for this? I have a list my_list in which each type(my_list[i].pixel_array) is a np.ndarray of size nx X ny. I want a single nx X ny X len(my_list) np.ndarray. I am writing a loop now, buy can I make it more elegant?
 
Since you need to access attributes for each one, not really. You can use a list comprehension to make it more compact, but that's still a loop of course
np.stack([item.pixel_array for item in my_list], -1)
or something like that
ah, no, you'll probably need stack, not concatenate
if neither works I can check on a dummy example
 
will that not go creating a bigger array on a loop?
seesm better to just do a quick for loop maybe
 
What do you mean?
It will create a list of pixel arrays. Doing that in a loop will need the same unless you pre-allocate the big array. But if you have to pre-allocate the big array there's no way around the loop.
 
Yeah, was worriying about the overheat of lack of preallocation there. Seems that it will increase the size on the loop
the preallocated version seesm nicer from my MTLAB perspective
i.e. a loop
 
It will temporarily create a list with every array inside. But python lists are not numpy arrays, appending to them doesn't reallocate memory. You'll roughly need once the size of the final array (until np.stack returns and it's freed)
this is not the same as creating a snowball of arrays in the loop, which would need O(n^2) memory or something altogether (think of triangle numbers)
but if you don't want the total footprint of twice your resulting 3d array, then pre-allocate and loop, no other way
if memory is not really an issue then what I wrote is nice and idiomatic
 
11:55 AM
I see. Thanks! I think I will do the for loop then, not too bad and while memory is not a big problem, I handle big dataset generally and I am trying to make my code memory efficient
 
Scratch that! The loop only contains a reference to each array, duh!
Assuming .pixel_array is a real array and not a property...
 
hum?
 
I'll explain in a bit
 
12:35 PM
>>> import numpy as np
... rng = np.random.default_rng()
...
... class Foo:
...     def __init__(self):
...         self.static_attr = rng.random((3, 3))
...
...     @property
...     def dynamic_attr(self):
...         """I look like an array but I don't actually exist until looked up"""
...         return rng.random((3, 3))
...
... foo = Foo()
... print(foo.static_attr)
... print(foo.dynamic_attr)
[[0.8790787  0.5550236  0.67654137]
 [0.63528501 0.19270624 0.40920867]
 [0.74011445 0.83082611 0.34734579]]
@AnderBiguri ^ if item is an object which has a fixed .pixel_array attribute that is a reference to a specific array in memory, then using my list comp is virtually for free. If item generates a new array each time the .pixel_array attribute is accessed (via a @property decorator or similar) then the list comp will keep every thusly created array for the lifetime of the np.stack call, so it will cost more. But this is roughly once the size of your final array.
 
It is not a property, but a proper array
 
then the list comp is for free
for N arrays you have N pointers to the already allocated arrays
>>> sys.getsizeof(list(range(100)))
1016
that's in bytes
>>> sys.getsizeof(rng.random((100, 100, 100)).tolist())
872
100-length list of (100, 100) arrays (doesn't include the size of the arrays)
 
Nice nice!
works great, its tidyier
(looks pro)
 
might be worth a comment though about the shapes :)
 
what do you mean?
you can just do axis=1 right?
 
12:50 PM
Yes. But if someone doesn't know how stack exactly works they might have to stop for 5 seconds to understand what's going on. If I wrote that I'd leave a comment saying what you said: turning a bunch of (n, m) to (n, m, k) or something
 
welel, chagned it myself, as I realised I wanted the k first, so now it has an explicit axes=0
 
OK, then you can also just use np.array([...]) :P
(in which case the result is quite obvious)
 
instead of np.stack?
wait
 
Yup, one less argument. But semantically speaking stack is more correct, so both work :)
 
that makes sense
 
12:52 PM
Just knowing myself, if I read np.array(some_list) I instantly know that it'll chain the contents along the first axis
YMMV
 
I think I will leave it in stack for now
 
cool, cool :)
 
if I have a package and there are fucntions that I want users not to use, that is __my_internal_fun__ right? not that they can't, but that is the naming convention
 
no no no
dunders (double leading and trailing underscores) are reserved for protocols docs.python.org/3/reference/datamodel.html
"don't use this" is a single leading underscore. "very internal and I don't want subclasses to mess with this" is double leading underscore, and is a subject to name mangling
 
oh ok! So just the single one :D
ahg, I wanted to do some research and isntead I am buildign an entire toolbox of useful functions for me to do the research better :D
not bad, but python is super distracting me with cool functions
 
1:01 PM
That will also prevent some IDE-likes to suggest the attrs with code completion. And you can add __all__ = ['foo', 'bar'] to only expose these names on import. Mostly affects star imports, though.
@AnderBiguri that's just an investment ;)
 
indeed! also learning quite a lot of just python along the way
some things still look a bit ugly (like poping and pushing to kwargs) but I might fix that later....
 
That's not necessarily ugly, if you're wrapping stuff.
pushing less so
 
yeah, its just "overloading" fucntions. Data types in medicine are a fucking mess. I made my visualizing fucntiosn for np.array but trying to add some dicom support, there is some important info there
 
I'm pretty sure matplotlib pops kwargs all the time and passes on the rest.
 
Ohok. Then happy 😀. The ugly part was more that in some cases, if input is XX and kwargs is not passed, I want to set kwarg to YY. In some sense, I have multiple defaults, depending on the input
 
1:08 PM
Why not set the internal default rather than kwargs?
 
1:27 PM
@AndrasDeak several layers
 
you could pass on (**kwargs, default_new_arg=arg) unless python forbids that these days :)
 
I thin that is uglier. Its more things like "if input is 4dim, use this colormap for the individual calls of imshow(), otherwise this other colormap". But if user has not set the colormap, then I need to set it up. But sometimes there can be another extra layer before imshow
 
1:44 PM
@AnderBiguri If that works, you should do it that way. Normally in MRI and CT I see models fitted to the data, but that can be quite complex. “Pose estimation” I see mostly used in the field of identifying humans in video and figuring out what they’re doing. That tends to be even more complicated.
 
Yeah, that term is not the right one possibly. But in some sense obtainign that output (stick-man figure with joints) would be desirable. For now I have some dynamic scans on where motion of humans may be happening and I want to register every limb
I will give that a go, will see how it goes once I have, does not sounds a bad idea a priori
 

« first day (1796 days earlier)      last day (1417 days later) »