changing units in mod files for NEURON - neuron-simulator

How to change units in a mod file so that after running neuoConstract the units will be saved?
units: 0.001 sec
units: 1
The units of the previous two expressions are not conformable
at line 75 in file C:/Users/miria/Desktop/neuro/neuroConstruct_1.7.2/nCexamples/Ex4_HHcell/generatedNEURON/CurrentClampExt.mod
beginNextCycle = beginNextCycle + (del + dur)<<ERROR>>
I have tried to change with Notepad++ but the changes can not be saved for the next time.

The error that you are getting here is the result of unit mismatch between the RHS and LHS of the equation:
beginNextCycle = beginNextCycle + (del + dur)
in CurrentClampExt.mod file. beginNextCycle, del and dur should have the same units e.g. millisecond.
For editing the CurrentClampExt.mod file, you can use Notepad++ or any other text editor. The constants/variables for equation are declared most likely in the mod file's ASSIGNED or PARAMETER block. Make sure that the these variables follow a unit. For example:
ASSIGNED {
dur (ms)
del (ms)
}
where (ms) refers to millisecond.
You can check the units of mod files by using NEURON simulator's 'modlunit' feature. See here for more details: https://www.neuron.yale.edu/neuron/static/docs/nmodl/mswin.html
Once you are done with unit correction, then compile the mod file(s) using nrnivmodl (as shown in the above link). If there are no errors, then try to run the code using neuroConstruct.

Related

Min function while using strings in python

I'm using chisquare test for my data. I'm appending them in a loop in that way:
My .txt file looks like below, it has 180 rows with strings like that. Now I want to find the minimum value from those 180 rows, which is contained in parentesis, like in example below (15.745037950673217,), but I don't want to lose information which is assigned to a string in that row, which is 201701241800 Chi for 75 degree model.
...
201701241800 Chi for 75 degree model (15.745037950673217,)
201701241800 Chi for 76 degree model (16.014744332924252,)
...
The code I use looks like this:
o = chisquare(f_obs=fin, f_exp=y)
rows = str(Date) + str(Start_time_hours_format) + str(Start_time_minutes_format) + " Chi for {} degree model ".format(r) + str(o[0:1])
table.append(rows)
The problem is that number of those calculations is enormously huge. My task is to find minimum value in each iteration, which is defined by a for loop. Example above came from one iteration (There are 180 degree models in each iteration). The problem is I cannot use min(table) because I've got there strings, but I cannot erase them, because that information is important. Do you have any ideas how to find min value here? I mean specificly min value in parentesis.
If you have a list lst, min(lst) returns the minimum value without modifying the list. If you don't have a list, but objects from which you want to consider a value, let's say obj[i].myvalue, then you can do something like
min = 1000 # a huge number much bigger than your expected values
for o in obj:
if o.myvalue < min:
min = o.myvalue
which assigns to min the minimum value (probably it is not the best way, but it works for sure).
[I would be more specific, but it is not clear what kind of object you have to find the minimum of. Please consider to update your question to be more explicative.]
Ok, so I've found a way to solve this problem. Code below:
o = chisquare(f_obs=fin, f_exp=y)
rows = str(Date) + str(Start_time_hours_format) + str(Start_time_minutes_format) + " Chi for {} degree model ".format(r) + str(o[0:1])
print(rows)
table.append(rows)
with open('measurements.txt', 'a') as j:
j.write(min(table))
j.write('\n')
j.close()

Definition of slack variable in time window routing

time window constraint are defined by
time_dimension.CumulVar(node).SetRange(time_window[0], time_window[1])
and the time dimension by
routing.AddDimension(evaluator, slack_max, capacity, fix_start_cumul_to_zero, name)
What is the relationship between the allowed values of CumulVar(node) and slack_max? For example, say that the time window is (50,60) and slack is 5. Does that mean that a value of the cumul var of 45 is also admissible, or does the slack relate to values inside the range? Does max_slack=0 mean that the value of the cumul var must be either 50 or 60, in the example above?
Is there a paper or detailed page about the mathematical model that is used my the routing model of or-tools?
For time window constraint, you can see the slack value as waiting time.
From the source code.
// if j == next(i),
// cumuls(j) = cumuls(i) + transits(i) + slacks(i)
src: https://github.com/google/or-tools/blob/d44fb1b423f9d6658c142c041143a4f54b5106d3/ortools/constraint_solver/routing.h#L1356-L1357
e.g. Supposing your are at node A at time 0 aka A(0) and you have B([40,60]) and transit time is T(50). Thus you have:
B(40) < A(0) + T(50) -> means too late to reach the lower bound even with no waiting time.
B(60) = A(0) + T(50) + 10 -> means vehicle can wait at node A up to 10min and still be in time at node B.
Second example: A(0), B([40,60]), T(30):
B(40) = A(0) + T(30) + 10 -> have to wait 10min
B(60) = A(0) + T(30) + 30 -> have to wait 30min
if slack max is 5 this route is forbidden because otherwise vehicle will be at most at node B at 35 = A(0) + T(30) + 5 which is too early
i.e. not in the range [40,60] so for the solver the time windows constraint can't be respected...
note: we can also deduce:
B(40) = A(5) + T(30) + 5
B(60) = A(30) + T(30)
So vehicle must be at node A in range [5,30] to be on time at node B with slack_max = 5.
i.e. With slack max you can limit the maximum waiting time (extra capacity) allowed along the route.
Routing use a "two steps" algorithms.
1) Try to find a first solution an can use various algorithm
cf. https://developers.google.com/optimization/routing/routing_options#first-solution-strategy-options for paper reference
2) Can use a local search to optimize this first solution
again several methods are implemented cf https://developers.google.com/optimization/routing/routing_options#local-search-options

how to limit the number of digit after the float with python 3?

In my program I have several calculations that produce float numbers as results.
I would like to know if there's a general declaration in Python 3 that allows to limit all the floats in the program to let's say 8 digits, systematically ?
Thank you for your help !
# Create initial balance for user 1 and user 2.
bal_user1 = 21.82233503
bal_user2 = 5.27438039
# Calculate percentage of capital for each user
percent_capi_user2 = 100 * bal_user2 / ( bal_user1 + bal_user2)
percent_capi_user1 = 100 - percent_capi_user2
print("User 1 as " + str(percent_capi_user1) + (" % of the capital"))
print("User 2 as " + str(percent_capi_user2) + (" % of the capital"))
The output is :
User 1 as 80.53498253110413 % of the capital
User 2 as 19.465017468895866 % of the capital
I would like for example : 80.53498253 instead of 80.53498253110413
And since I'm doing several calculations later on in the program, I was wondering if there was a general declaration to put once at the beginning of the code. In order to avoid casting the right number of digits each time...
Well, buddy, I think I have just what you are looking for!
What you are looking for is the decimal module and the included Decimal class. Now, I am not going to go into it, because I am not that knowledgeful in it, but what I can do is point you in the right direction. In short, read the documentation here ( https://docs.python.org/3/library/decimal.html?highlight=decimal#module-decimal ), and look for decimal.getcontext().prec, which will allow you to, at least with Decimal objects, control their precision "globally".

Optimize run time of octave/matlab function

I am using the Google Earth Toolbox, for Octave, which is extremely similar to the same library made for MATLAB. See this link, for the MATLAB doc for the tool.
Unfortunately I am having a bit of trouble with a certain function: ge_quiver. This function creates a quiver plot and translates the result into KML code. My problem with it
is that it is a bit too slow for my liking.
For a data variable of 30x30 size, the function takes around 10 seconds to complete. This is fine, except for the fact that I am trying to make an animation quiver plot, with 23 other same sized data variables (24 total). Since Octave (and MATLAB for that matter) normally only runs on one thread, the whole thing is run serially, and takes 10*24 = 240 s.
I have tried parfor loops, but they have only removed 10 seconds from the total run time. I have a computer with 16 cores. Considering, this is embarrassingly parallel (no dependence between variables), this should ideally take:
10 seconds for the first 16 variables
+ 10 seconds for the 8 remaining variables
------------------------------------------------------
= 20 seconds total.
I haven't even tested arrayfun, because I don't know how to adapt my function (p_ge_quiver). Nonetheless, many have said that arrayfun will not make it faster.
The following code is approximately what I am trying to do. Remember that the variable
data_u is of size 30x30x24. Same thing for data_v, lonand lat.
...
...
[YYYY,MM,DD,HH,mm,ss] = ncdate(NCFILE);
date_s.year = YYYY;
date_s.month = MM;
date_s.day = DD;
date_s.hour = HH;
date_s.minute = mm;
date_s.second = ss;
TIMESTEP = 60;
parfor i = 1:size(data_u,3)
dt = TIMESTEP*i;
kml(i) = p_ge_quiver(lon,lat,data_u,data_v,dt,TIMESTEP,data_s);
endparfor
...
...
function kml = p_ge_quiver(lon,lat,u,v,data_time,step,date_s)
% V_GE_QUIVER - Writes the quiver plot into KML.
% Get date variables.
YYYY = date_s.year;
MM = date_s.month;
DD = date_s.day;
HH = date_s.hour;
mm = date_s.minute;
ss = date_s.second;
% Date format. Use Google's.
F = 'yyyy-mm-ddTHH:MM:SSZ';
% Start and end dates of data.
tStart = datestr(datenum(YYYY,MM,DD,HH,mm+data_time,ss),F);
tEnd = datestr(datenum(YYYY,MM,DD,HH,mm+data_time+step,ss),F);
% Quiver plot.
kml = ge_quiver(lon,lat,u,v,'timeSpanStart', tStart, ...
'timeSpanStop' , tEnd , ...
'visibility' , 0 , ...
'lineColor' , 'AAFFFFFF');
end
Seems to me that the problem is on what ge_quiver does internally which you don't show. Aside that, you should note that this language is designed to write vectorized code (and not only because of the difference in speed).
Looking at your code, you can simplify it lot (and this usually does have an effect in speed as well) by making use of a vectorized syntax:
step = 60;
dt = step:step:(step * size (data_u, 3));
## Get date variables.
YYYY = date_s.year;
MM = date_s.month;
DD = date_s.day;
HH = date_s.hour;
mm = date_s.minute;
ss = date_s.second;
## Date format. Use Google's.
F = "yyyy-mm-ddTHH:MM:SSZ";
tStart = datestr (datenum (YYYY, MM, DD, HH, mm + dt , ss), F);
tEnd = datestr (datenum (YYYY, MM, DD, HH, mm + dt + step, ss), F);
At this point, tStart and tEnd have all the dates that you need, one time per row. The only required loop is the following:
for i=1:rows(tStart)
kml = ge_quiver (lon, lat, data_u, data_v, "timeSpanStart", tStart(i,:), ...
"timeSpanStop" , tEnd(i,:) , ...
"visibility" , 0 , ...
"lineColor" , "AAFFFFFF");
endfor
At this point is all dependent on what ge_quiver does. The mathworks site you linked appears as under maintenance. I looked into the Google Earth toolbox website and couldn't find that function (they have a ge_quiver3 function though).
I would argue that a function for this type of languages should allow for vector inputs, so go and confirm it. Maybe you don't need a loop at all.

Creating a continuous tone in MATLAB whose frequency varies in real-time depending on user input

I am currently working on a graphing program in MATLAB that takes input and maps a point to x-y space using this input. However, the program should also output a continuous tone whose frequency varies depending on the location of the point.
I was able to get the tone generation done, however could not get the tone to work continuously due to the nature of the program. (Code in between tone generations) I thought I could solve this using a parfor loop with the code that alters the frequency in one iteration of the loop, and the code that generates the tone in another but cannot seem to get it due to the following error:
Warning: The temporary variable frequency will be cleared at the
beginning of each iteration of the parfor loop. Any value assigned to
it before the loop will be lost. If frequency is used before it is
assigned in the parfor loop, a runtime error will occur. See Parallel
for Loops in MATLAB, "Temporary Variables".
In multiThreadingtest at 5 Error using multiThreadingtest (line 5) Reference to a cleared variable frequency.
Caused by:
Reference to a cleared variable
frequency.
And my code:
global frequency
frequency = 100;
parfor ii=1:2
if ii==1
Fs = 1000;
nSeconds = 5;
y = 100*sin(linspace(0, nSeconds*frequency*2*pi, round(nSeconds*Fs)));
sound(y, Fs);
elseif ii==2
frequency = 100
pause(2);
frequency = 200
pause(2);
frequency = 300
pause(2);
end
end
The solution may not come from multithreading, but from the use of another function to output a tone(audioplayer, play, stop). 'audioplayer/play' has the ability to output sounds that overlap in time. So basically, a pseudo code would be:
get the value of the input
generate/play a corresponding 5 second tone
detect if any change in the input
if no change & elapsed time close to 5 seconds
generate/play an identical 5 second tone
if change
generate a new 5 second tone
%no overlapping
stop old
play new
%overlapping (few milliseconds)
play new
stop old
The matlab code showing the 'sound'/'play' differences.
Fs = 1000;
nSeconds = 5;
frequency = 100;
y1 = 100*sin(linspace(0, nSeconds*frequency*2*pi, round(nSeconds*Fs)));
aud1 = audioplayer(y1, Fs);
frequency = 200;
y2 = 100*sin(linspace(0, nSeconds*frequency*2*pi, round(nSeconds*Fs)));
aud2 = audioplayer(y2, Fs);
% overlapping sound impossible
sound(y1, Fs);
pause(1)
sound(y2, Fs);
% overlapping sound possible
play(aud1);
pause(1);
disp('can compute here');
play(aud2);
pause(1);
stop(aud1);
pause(1);
stop(aud2);

Resources