Porting fragment code Python to Julia

selection = np.linspace(0,3,5000000)

eps = 0.9

# spikes numpy 1d array

ee_counter = np.array( [ ( ( spikes >= selection[j] ) & ( spikes < selection[j]+eps ) ).sum() for j in range( len( selection ) ) ] )

pdf = ee_counter / spikes.sum()

I have this fragment code on python. How can i porting this code on Julia with minimal number of cycles and rows of codes

Can you please copy and paste the code as text instead of a screen shot? Also, remember to put it inside triple backticks.

Please post editable code instead of images. Anyway, this might be a direct translation:

selection = range(0,3,5000000)
ϵ = 0.9

ee_counter = [sum(i->s<=i<s+ϵ, spikes) for s in selection]
pdf = ee_counter ./ sum(spikes)

I edited, sorry.

1 Like

I edited topic. I tried this and has next error

Spikes is vector
image

sum and count do the same thing here, but using count makes more sense, imho, and is more likely to discover bugs.

True, but sometimes I prefer sum as it allows simd optimizations more than the other alternatives.

This helped, but the calculation takes a long time. This code on python ran for 1 minute. Length of spikes 3098

After 5 minutes of calculations, I aborted ran code

You must put the code in a function, and make sure to avoid global variables.

The code you provided is still not self-contained, it isn’t a ‘minimal working example’ or MWE. Can you provide some example data for spikes?

Also, when developing and testing code, I suggest that you make smaller data sets. Waiting for minutes just to see if your code works seems like a waste of time.

It takes 3s in Julia:

function sum_spikes(spikes)
    ϵ = 0.9
    ee_counter = [sum(i->s<=i<s+ϵ, spikes) for s in range(0,3,5000000)]
    pdf = ee_counter ./ sum(spikes)
end

spikes = randn(3098)
@btime sum_spikes($spikes)  # 3.237 s (4 allocations: 76.29 MiB)
4 Likes

It helped, thank you