how to input kwargs into bloomberg wrapper function - python-3.x

I am referring to the following code posted on GitHub which creates a wrapper interface around the blpapi package to replicate Excel functionality of the popular functions: BDP, BDH, and BDS.
https://github.com/691175002/BLPInterface/blob/master/blp.py
for those familiar, I am interested in replicating the result of calling in Excel
=BDS("NHSLTOT Index","ECO_FUTURE_RELEASE_DATE_LIST","START_DT","20140901","END_DT","20170923")
The wrapper equivalent of this function is referred to as bulkRequest - and it works well without the START_DT and END_DT arguments, i.e.
blp.bulkRequest("NHSLTOT Index", "ECO_FUTURE_RELEASE_DATE_LIST")
returns a DataFrame, but I would like to include the date range as above, which i believe the module provides for via the *kwargs, but I am unable to get this to work correctly. it would seem to me that these should be input as a dictionary, i.e.
blp.bulkRequest("NHSLTOT Index", "ECO_FUTURE_RELEASE_DATE_LIST", elements={"START_DT":"20140901","END_DT":"20170923"})
but this returns an error. any guidance appreciated

Related

Turning a Dataframe into a Series with .squeeze("columns")

I'm studying how to work with data right now and so I'm following along with a tutorial for working with Time Series data. Among the first things he does is read_csv on the file path and then use squeeze=True to read it as a Series. Unfortunately (and as you probably know), squeeze has been depricated from read_csv.
I've been reading documentation to figure out how to read a csv as a series, and everything I try fails. The documentation itself says to use pd.read_csv('filename').squeeze('columns') , but, when I check the type afterward, it is always still a Dataframe.
I've looked up various other methods online, but none of them seem to work. I'm doing this on a Jupyter Notebook using Python3 (which the tutorial uses as well).
If anyone has any insights into why I cannot change the type in this way, I would appreciate it. I'm not sure if I've misunderstood the tutorial altogether or if I'm not understanding the documentation.
I do literally type .squeeze("columns") when I write this out because when I write a column name or index, it fails completely. Am I doing that correctly? Is this the correct method or am I missing a better method?
Thanks for the help!
shampoo = pd.read_csv('shampoo_with_exog.csv',index_col= [0], parse_dates=True).squeeze("columns")
I would start with this...
#Change the the stuff between the '' to the entire file path of where your csv is located.
df = pd.read_csv(r'c:\user\documents\shampoo_with_exog.csv')
To start this will name your dataframe as df which is kind of the unspoken industry standard the same as pd for pandas.
Additionally, this will allow you to use a "raw" (the r) string which makes it easier to insert directories into your python code.
Once you are are able to successfully run this you can simply put df in a separate cell in jupyter. This will show you what your data looks like from your CSV. Once you have done all that you can start manipulating your data. While you can use the fancy stuff in pd.read_csv() I mostly just try to get the data and manipulate it from the code itself. Obviously there are reasons not to only do a pd.read_csv but as you progress you can start adding things here and there. I almost never use squeeze although I'm sure there will be those here to comment stating how "essential" it is for whatever the specific case might be.

How to retrieve non-zeroes using gurobipy?

I am using gurobipy to read LP files. The command
model=gurobipy.read("name.lp", env=env) gives me the number of rows, columns, and non-zeroes. However, I need to retrieve the number of non-zeroes. I don't believe there is a function that does this automatically (i.e. model.getnonzeros() )
Is there a way to obtain the non-zeros? How would I write python code to be able to do this if there isn't a built in function?
Consulted resources
Get constraints in matrix format from gurobipy
Ok I figured it out - perusing the Gurobi reference manual, in chapter 6, Python API overview, I see there is an attribute called "NumNZs" that can be called as:
print(model.getAttr("NumNZs"))
This will give the non-zeroes

Convolution layer in keras

I am exploring convolution layer in keras from:
https://github.com/fchollet/keras/blob/master/keras/layers/convolutional.py#L233
everywhere i found following type of code lines:
#interfaces.legacy_conv1d_support
#interfaces.legacy_conv2d_support
what is working and role of these lines. I searched in google but not find answer anywhere. please explain.
These lines starting with # are called decorators in python. Check out this page to read a brief summary about them. The basic function of this decorators is, that they wrap the following function into another function which has some kind of "wrapper" functions, like preprocessing the arguments, changing the accessibility of the function etc.
Taking a look at the interfaces.py file you will see this:
legacy_conv1d_support = generate_legacy_interface(
allowed_positional_args=['filters', 'kernel_size'],
conversions=[('nb_filter', 'filters'),
('filter_length', 'kernel_size'),
('subsample_length', 'strides'),
('border_mode', 'padding'),
('init', 'kernel_initializer'),
('W_regularizer', 'kernel_regularizer'),
('b_regularizer', 'bias_regularizer'),
('W_constraint', 'kernel_constraint'),
('b_constraint', 'bias_constraint'),
('bias', 'use_bias')],
preprocessor=conv1d_args_preprocessor)
So, the use of this function is basicly to rename parameters. Why is this? The keras API changed the names of some arguments of some functions (like W_regularizer -> kernel_regularizer). To allow users to be able to run old code, they added this decorator, which will just replace the names of old arguments with the corresponding new parameter name before calling the real function. This allows you to run "old" keras 1 code, even though you have installed keras 2.
Tl;dr: These lines are just used to for compatibility reasons. As this are just internal aspects of keras there is nothing you have to worry about or to take care of.

Getting the result of an excel formula in python

I need to open a .xlsx-file (without writing to it) in python, to change some fields and get the output after the formulas in some fields were calculated; I only know the input fields, the output field and the name of the sheet.
To write some code: Here is how it would look like if I would have created the library
file = excel.open("some_file.xlsx")
sheet = file[sheet_name]
for k, v in input_fields.items():
sheet[k] = v
file.do_calculations()
print(sheet[output_field])
Is there an easy way to do this? Wich library should I use to get the result of the formulas after providing new values for some fields?
Is there a better way than using something like pyoo, maybe something that doesn't require another application (a python library is clearly better) to be installed?
I'll just thank you in advance.
I now came up with a (very ugly) solution.
I am now reading the xml within the xlsx-file, and I am now using eval and some regular expressions to find out wich fields are needed; and I have defined some functions to run the calculations.
It works, but it would be great if there were a better solution.
If the resulting library is ready, and I don't forget to do this; I'll add a link to the library (that'll be hosted on Github) to this answer to my own question.

Using bessel functions in MATLAB

I'm trying to put all of my functions from Excel workbook into MATLAB. I'm having an issue using bessel functions in MATLAB. I'm simply not getting the same results from MATLAB as I do in excel.
For example, in excel if I execute
=0.32*BESSELI(0.32,0)/2/BESSELI(0.32,1)
I get 1.012.
When I use the same approach in MATLAB
0.32*besseli(0.32,0)/2/besseli(0.32,1)
I simply get zero.
Can someone please help me integrate bessel functions into my MATLAB script so that they provide the same answer as they do when used in excel?
MATLAB and Excel have the arguments of the besseli function in a different order.
The following expression (note the order of arguments changed):
0.32*besseli(0, 0.32)/2/besseli(1, 0.32)
will yield:
> ans = 1.0127
in MATLAB.
The documentation shows the formulae and show that if you use Z=0, which you have in your first besseli, you should get 0, which you do. The second call to besseli should not get you zero, and indeed it does not:
besseli(0.32,1)
ans =
1.0744
I copied the following from the aforementioned documentation:
This shows that unless your nu (that Greek thing that looks like a v) is zero, your modified Bessel function of the first kind at Z=0 will be, in fact zero.
On a side note: why are you doubly dividing and not simply writing
0.32*besseli(0.32,0)*besseli(0.32,1)/2

Resources