Creating a PyMemoryView with a specific format - python-c-api

I am exposing a PyMemoryView in C on a set of data, like so:
PyMemoryView_FromMemory((char *)ibuf->rect, pixels * sizeof(uint), PyBUF_WRITE);
That data is floating-point data, however, so attempting to do this:
mv = get_my_memory_view()
mv[0] = 3.141
yields the following error:
TypeError: memoryview: invalid type for format 'B'
This is, of course, because the memoryview assumes the underlying data to be byte data, not float. How would I ensure that the memoryview returned from my module has a float specifier?

The easiest way would probably be to use the cast method of the memoryview to get a new memoryview with the correct format. There isn't a direct C-API method to do it, so just call it like any other Python method:
mview_double = PyObject_CallMethod(mview_bytes, "cast", "s", "d");
this assumes double data - if it's float data then you should change the "d".
In the original call to PyMemoryView_FromMemory I think pixels * sizeof(uint) is wrong since you've told us the data-type is a floating-point type. Maybe pixels*sizeof(ibuf->rect[0])?

Related

Why is day() function throwing me this error: Check for missing argument or incorrect argument data type in call to function 'day'?

Having this long task that I will resume:
Perform a regression model over the normalized active cases in China using the model.....(long assignment that I'm not worried about and will save you time). Tip: To convert from datetime to a numeric variable for the regression, use x=day(date-min(date(:)))+1; being “date” the datetime vector return from getdata function.
This is what I have:
function RP_ejercicio1
data = readtable('COVID-19.csv');
[active_res, confirmed_res, death_res, recovered_res, date] = getdata(data, 'China', 93/147);
x=day(date-min(date(:)))+1;
y = active_res;
yp = log(y./x);
a = [x ones(size(x))];
sol = inv(a'*a)*(a'*yp);
b = sol(1);
c = sol(2);
a = exp(c);
end
I get this error: Check for missing argument or incorrect argument data type in call to function 'day'. In this line: x=day(date-min(date(:)))+1;. The one that is supposed to help as a tip is giving me a headache. I can ensure that date is a 1x50 datetime array after executing the getdata function.
Am I doing something wrong? Is the tip wrong? And if it's the second case, is there other way to do the same?
I add an image for more clarity:
Date array
As somebody said here, you should be using the function days.

I am getting an error regarding the use of the function float, how do I solve it?

I am trying to convert what is inside the parenthesis into a float type but I keep getting the same error.
I have already tried to change the parenthesis in order to envelop the whole expression in float before convert but it still gives me the same error. Please le tme know what could I do in order to avoid this error.
SELECT
inter.IDIntervento, 'Intervento principale' as _IDInterventoTipo, 1 as _IDInterventoTipoN,
inter.IDIntervento as _IDInterventoRif, inter.IDInterventoV,
IDCartella, IDCdc, IDCdc|| IDCartella as IDCdcIDCartella,
ECO.TipoCardio,
ECO.DataEsame,
ECO.Operatore,
ECO.TipoEco,
convert(float,replace(ECO.DiametroTelediastolicoVentricoloSinistro,',','.')) as VentricoloSinistro_DiametroTelediastolico,
convert(float,replace(ECO.VolumeTelediastolicoVentricoloSinistro,',','.')) as VentricoloSinistro_VolumeTelediastolico,
convert(float,replace(ECO.DiametroTelediastolico2VentricoloSinistro,',','.')) as VentricoloSinistro_DiametroTelediastolico2,
convert(float,replace(ECO.VolumeTelediastolico2VentricoloSinistro,',','.')) as VentricoloSinistro_VolumeTelediastolico2,
(SELECT cast(idinterventovalore as varchar(5))|| '='|| valore
FROM cch.pats_cch_interventi_valori val
The function convert converts strings from one encoding to another.
To convert a string to a floating point number, use a type cast:
CAST (replace(ECO.DiametroTelediastolicoVentricoloSinistro,',','.')
AS double precision) AS VentricoloSinistro_DiametroTelediastolico

Converting DataFrame String column containing missing values to Date in Julia

I'm trying to convert a DataFrame String column to Date format in Julia, but if the column contains missing values an error is produced:
ERROR: MethodError: no method matching Int64(::Missing)
The code I've tried to run (which works for columns with no missing data) is:
df_pp[:tod] = Date.(df_pp[:tod], DateFormat("d/m/y"));
Other lines of code I have tried are:
df_pp[:tod] = Date.(passmissing(df_pp[:tod]), DateFormat("d/m/y"));
df_pp[.!ismissing.(df_pp[:tod]), :tod] = Date.(df_pp[:tod], DateFormat("d/m/y"));
The code relates to a column named tod in a data frame named df_pp. Both the DataFrames & Dates packages have been loaded prior to attempting this.
The passmissing way is
df_pp.tod = passmissing(x->Date(x, DateFormat("d/m/y"))).(df_pp.tod)
What happens here is this: passmissing takes a function, and returns a new function that handles missings (by returning missing). Inside the bracket, in x->Date(x, DateFormat("d/m/y")) I define a new, anonymous function, that calls the Date function with the appropriate DateFormat.
Finally, I use the function returned by passmissing immediately on df_pp.tod, using a . to broadcast along the column.
It's easier to see the syntax if I split it up:
myDate(x) = Date(x, DateFormat("d/m/y"))
Date_accepting_missing = passmissing(myDate)
df_pp[:tod] = Date_accepting_missing.(df_pp[:tod])

What is the point of writing integer in hexadecimal, octal and binary?

I am well aware that one is able to assign a value to an array or constant in Swift and have those value represented in different formats.
For Integer: One can declare in the formats of decimal, binary, octal or hexadecimal.
For Float or Double: One can declare in the formats of either decimal or hexadecimal and able to make use of the exponent too.
For instance:
var decInt = 17
var binInt = 0b10001
var octInt = 0o21
var hexInt = 0x11
All of the above variables gives the same result which is 17.
But what's the catch? Why bother using those other than decimal?
There are some notations that can be way easier to understand for people even if the result in the end is the same. You can for example think in cases like colour notation (hexadecimal) or file permission notation (octal).
Code is best written in the most meaningful way.
Using the number format that best matches the domain of your program, is just one example. You don't want to obscure domain specific details and want to minimize the mental effort for the reader of your code.
Two other examples:
Do not simplify calculations. For example: To convert a scaled integer value in 1/10000 arc minutes to a floating point in degrees, do not write the conversion factor as 600000.0, but instead write 10000.0 * 60.0.
Chose a code structure that matches the nature of your data. For example: If you have a function with two return values, determine if it's a symmetrical or asymmetrical situation. For a symmetrical situation always write a full if (condition) { return A; } else { return B; }. It's a common mistake to write if (condition) { return A; } return B; (simply because 'it works').
Meaning matters!

How to specify output type in MATLAB when using calllib

Can I specify the output type when using calllib? My problem is MATLAB is automatically converting my output to a double even though I need an int64 and am losing needed precision.
Example
I have the following function defined in my_header.h
__int64 my_function(int arg1);
I can call the function like this:
loadlibrary('my_library', 'my_header.h')
output = calllib('my_library', 'my_function', arg1)
But then output is a double and I am losing needed precision.
What I tried
output = int64(calllib('my_library', 'my_function', arg1))
as well as
output = zeros(1, 'int64')
output(1) = calllib('my_library', 'my_function', arg1)
but these just convert my double to int64 after it has already lost the needed precision.