ITM (Irish Transverse Coordinate) conversion to GPS for google maps Python3 - python-3.x

I don't know anything about coordinates. My problem is that I have a dataset that contains coordinates in the ITM format (Irish_X and Irish_Y). I want to convert ITM coordinates into Google Maps readable ones.
Online I found a library that may be useful but I don't know how to use and the documentation use a very specific jargon I'm not used to:
https://proj.org
I also commented in the same library gitHub repo looking for answer:
https://github.com/OSGeo/PROJ/issues/1687
Thank you very much for your help!

To convert from ITM to WGS84 just call the def itm2geo(x,y): function as exemplified in the end of the code in the Teste Values section.
The first two functions (arcmer and xy2geo) are auxiliary functions and don't need to be explicitly called (they are called by itm2geo(x,y)
from math import *
############################################################################
# Meridian Arc
############################################################################
def arcmer(a,equad,lat1,lat2):
b=a*sqrt(1-equad)
n=(a-b)/(a+b)
a0=1.+((n**2)/4.)+((n**4)/64.)
a2=(3./2.)*(n-((n**3)/8.))
a4=(15./16.)*((n**2)-((n**4)/4.))
a6=(35./48.)*(n**3)
s1=a/(1+n)*(a0*lat1-a2*sin(2.*lat1)+a4*sin(4.*lat1)-a6*sin(6.*lat1))
s2=a/(1+n)*(a0*lat2-a2*sin(2.*lat2)+a4*sin(4.*lat2)-a6*sin(6.*lat2))
return s2-s1
###############################################################################
#
# Transverse Mercator Inverse Projection
#
###############################################################################
def xy2geo(m,p,a,equad,lat0,lon0):
lat0=radians(lat0)
lon0=radians(lon0)
sigma1=p
fil=lat0+sigma1/(a*(1-equad))
deltafi=1
while deltafi > 0.0000000001:
sigma2=arcmer(a,equad,lat0,fil)
RO=a*(1-equad)/((1-equad*(sin(fil)**2))**(3./2.))
deltafi=(sigma1-sigma2)/RO
fil=fil+deltafi
N=a/sqrt(1-equad*(sin(fil))**2)
RO=a*(1-equad)/((1-equad*(sin(fil)**2))**(3./2.))
t=tan(fil)
psi=N/RO
lat=fil-(t/RO)*((m**2)/(2.*N))+(t/RO)*((m**4)/(24.*(N**3)))*(-4.*(psi**2)-9.*psi*(1.-t**2)+12.*(t**2))-(t/RO)*(m**6/(720.*(N**5)))*(8.*(psi**4)*(11.-24.*(t**2))-12.*(psi**3)*(21.-71.*(t**2))+15.*(psi**2)*(15.-98.*(t**2)+15.*(t**4))+180.*psi*(5.*(t**2)-3.*(t**4))-360.*(t**4))+(t/RO)*((m**8)/(40320.*(N**7)))*(1385.+3633.*(t**2)+4095.*(t**4)+1575.*(t**6))
lon=(m/(N))-((m**3)/(6.*(N**3)))*(psi+2.*(t**2))+((m**5)/(120.*(N**5)))*(-4.*(psi**3)*(1.-6.*(t**2))+(psi**2)*(9.-68.*(t**2))+72.*psi*(t**2)+24.*(t**4))-((m**7)/(5040.*(N**7)))*(61.+662.*(t**2)+1320.*(t**4)+720.*(t**6))
lon=lon0+lon/cos(fil)
lat=degrees(lat)
lon=degrees(lon)
return lat,lon
#############################################################################
# Irish Transverse Mercator - Inverse
#############################################################################
def itm2geo(x,y):
# GRS-80
a = 6378137.
equad =0.00669437999
# Natural Origin
lat0=53.5
lon0=-8.
k0=0.999820
p = (y - 750000.) /k0
m = (x - 600000.) /k0
lat,lon = xy2geo(m,p,a,equad,lat0,lon0)
return lat,lon
#############################################################################
# Test values
#############################################################################
#lat=53.5
#lon=-8.
test = itm2geo(600000.,750000.)
print ("latitude= %.16f" %test[0])
print ("longitude= %.16f" %test[1])

Related

What is the output of predict.coxph() using type = "survival"?

I am trying to learn what the various outputs of predict.coxph() mean. I am currently attempting to fit a cox model on a training set then use the resulting coefficients from the training set to make predictions in a test set (new set of data).
I see from the predict.coxph() help page that I could use type = "survival" to extract and individual's survival probability-- which is equal to exp(-expected).
Here is a code block of what I have attempted so far, using the ISLR2 BrainCancer data.
set.seed(123)
n.training = round(nrow(BrainCancer) * 0.70) # 70:30 split
idx = sample(1:nrow(BrainCancer), size = n.training)
d.training = BrainCancer[idx, ]
d.test = BrainCancer[-idx, ]
# fit a model using the training set
fit = coxph(Surv(time, status) ~ sex + diagnosis + loc + ki + gtv + stereo, data = d.training)
# get predicted survival probabilities for the test set
pred = predict(fit, type = "survival", newdata = d.test)
The predictions generated:
predict(fit, type = "survival", newdata = d.test)
[1] 0.9828659 0.8381164 0.9564982 0.2271862 0.2883800 0.9883625 0.9480138 0.9917512 1.0000000 0.9974775 0.7703657 0.9252100 0.9975044 0.9326234 0.8718161 0.9850815 0.9545622 0.4381646 0.8236644
[20] 0.2455676 0.7289031 0.9063336 0.9126897 0.9988625 0.4399697 0.9360874
Are these survival probabilities associated with a specific time point? From the help page, it sounds like these are survival probabilities at the follow-up times in the newdata argument. Is this correct?
Additional questions:
How is the baseline hazard estimated in predict.coxph? Is it using the Breslow estimator?
If type = "expected" is used, are these values the cumulative hazard? If yes, what are the relevant time points for these?
Thank you!

Featuretools documentation of specifying primitive options is wrong?

The documentation says:
Specifying for Individual Primitives
Options for individual primitives or groups of primitives are set by the primitive_options parameter of DFS. This parameter maps any desired options to specific primitives. In the case of conflicting options, options set at this level will override options set at the entire DFS run level, and the include options will always take priority over their ignore counterparts.
However, I see that this is not true and the ignore option actually takes precedence over the include counterpart.
Below is the set-up I will use to demonstrate the claimed behaviour. It is an entityset with one grandparent (gp), two parents (p1,p2) and one child (c) to one parent (p1):
import pandas as pd
import featuretools as ft
from featuretools import variable_types as vt
# # Creating Relational Dataset
# ## Grand Parent
df_gp = pd.DataFrame({'gp_ind':['a','b'],
'gp_ncol1':[1,2],'gp_ncol2':[3,4],
'gp_ccol1':['x','y'],'gp_ccol2':['p','q'],
'gp_time_col1':pd.to_datetime(['20-01-2020','20-01-2019']),
'gp_time_ind':pd.to_datetime(['20-01-2021','20-01-2020'])})
# ## Parent 1
df_p1 = pd.DataFrame({'p1_ind':['a1','a2','b1'],
'p1_id': ['a','a','b'],
'p1_ncol1':[1,2,3],'p1_ncol2':[3,4,5],
'p1_ccol1':['x','y','z'],'p1_ccol2':['p','q','r'],
'p1_id1' : ['t','t','u'],
'p1_time_col1':pd.to_datetime(['16-01-2020','11-12-2019','16-01-2019'],format="%d-%m-%Y"),
'p1_time_ind':pd.to_datetime(['15-01-2021','10-12-2020','15-01-2020'],format="%d-%m-%Y")})
# ## Parent 2
df_p2 = pd.DataFrame({'p2_ind':['a1_','a2_','b1_'],
'p2_id': ['a','a','b'],
'p2_ncol1':[1,2,3],'p2_ncol2':[3,4,5],
'p2_ccol1':['x','y','z'],'p2_ccol2':['p','q','r'],
'p2_time_col1':pd.to_datetime(['18-01-2020','13-12-2019','18-01-2019'],format="%d-%m-%Y"),
'p2_time_ind':pd.to_datetime(['17-01-2021','12-12-2020','17-01-2020'],format="%d-%m-%Y")})
# ## Child
df_c = pd.DataFrame({'c_ind':['a1_1','a1_2','a2_1','a2_2','a2_3','b1_1'],
'c_id': ['a1','a1','a2','a2','a2','b1'],
'c_ncol1':[1,2,3,4,5,6],'c_ncol2':[3,4,5,6,7,8],
'c_ccol1':['x','y','z','a','b','c'],'c_ccol2':['p','q','r','s','t','u'],
'c_time_col1':pd.to_datetime(['13-01-2020','10-12-2019','8-12-2019','5-11-2019','2-10-2019','13-01-2019'],format="%d-%m-%Y"),
'c_time_ind':pd.to_datetime(['10-01-2021','5-12-2020','9-12-2020','6-11-2020','3-10-2019','12-01-2020'],format="%d-%m-%Y")})
# # Creating Entityset
es = ft.EntitySet(id='experimentation')
# ## Adding entities
# ### Adding gp
vt_gp = {'gp_ind':vt.Index,
'gp_ncol1':vt.Numeric,
'gp_ncol2':vt.Numeric,
'gp_ccol1':vt.Categorical,
'gp_ccol2':vt.Categorical,
'gp_time_col1':vt.Datetime,
'gp_time_ind':vt.DatetimeTimeIndex}
es.entity_from_dataframe(entity_id='gp',dataframe=df_gp,index='gp_ind',variable_types=vt_gp,
time_index='gp_time_ind')
# ### Adding p1
vt_p1 = {'p1_ind':vt.Index,
'p1_id':vt.Id,
'p1_id1' : vt.Id,
'p1_ncol1':vt.Numeric,
'p1_ncol2':vt.Numeric,
'p1_ccol1':vt.Categorical,
'p1_ccol2':vt.Categorical,
'p1_time_col1':vt.Datetime,
'p1_time_ind':vt.DatetimeTimeIndex}
es.entity_from_dataframe(entity_id='p1',dataframe=df_p1,index='p1_ind',variable_types=vt_p1,
time_index='p1_time_ind')
# ### Adding p2
vt_p2 = {'p2_ind':vt.Index,
'p2_id':vt.Id,
'p2_ncol1':vt.Numeric,
'p2_ncol2':vt.Numeric,
'p2_ccol1':vt.Categorical,
'p2_ccol2':vt.Categorical,
'p2_time_col1':vt.Datetime,
'p2_time_ind':vt.DatetimeTimeIndex}
es.entity_from_dataframe(entity_id='p2',dataframe=df_p2,index='p2_ind',variable_types=vt_p2,
time_index='p2_time_ind')
# ### Adding c
vt_c = {'c_ind':vt.Index,
'c_id':vt.Id,
'c_ncol1':vt.Numeric,
'c_ncol2':vt.Numeric,
'c_ccol1':vt.Categorical,
'c_ccol2':vt.Categorical,
'c_time_col1':vt.Datetime,
'c_time_ind':vt.DatetimeTimeIndex}
es.entity_from_dataframe(entity_id='c',dataframe=df_c,index='c_ind',variable_types=vt_c,
time_index='c_time_ind')
# ## Adding Relationships
r_gp_p1 = ft.Relationship(es['gp']['gp_ind'],es['p1']['p1_id'])
r_gp_p2 = ft.Relationship(es['gp']['gp_ind'],es['p2']['p2_id'])
r_p1_c = ft.Relationship(es['p1']['p1_ind'],es['c']['c_id'])
es.add_relationships([r_gp_p1,r_gp_p2,r_p1_c])
# ## Create Cutoff Times
cutoff_times = df_gp.loc[:,['gp_ind','gp_time_ind']].copy(deep=True)
# ## add interesting values
es['p1']['p1_ccol1'].interesting_values = es['p1'].df['p1_ccol1'].unique()[0:1]
es['c']['c_ccol1'].interesting_values = es['c'].df['c_ccol1'].unique()[0:1]
# ## Add last time index
es.add_last_time_indexes()
# ## Plotting entityset
es.plot()
Now on this entityset I run the following dfs:
I include p1 in both ignore_entities and include_entities keys. This way, I convey conflicting commands to dfs about whether or not to include p1 entity in the feature creation process.
Expected behaviour: include_entities to override ignore_entities and variables on entity p1 should be made
Behaviour seen: ignore_entities overrides include_entities and variables on p1 are not made
agg_primitives = ['sum']
where_primitives = ['sum']
primitive_options = {}
primitive_options[('sum',)] = {}
primitive_options[('sum',)]['ignore_entities'] = ['p1']
primitive_options[('sum',)]['include_entities'] = ['p1']
features = ft.dfs(entityset=es,target_entity='gp', cutoff_time=cutoff_times,
agg_primitives=agg_primitives,features_only=True,max_depth=2,
where_primitives = where_primitives,
primitive_options=primitive_options,trans_primitives=[])
features
output:
[<Feature: gp_ncol1>,
<Feature: gp_ncol2>,
<Feature: gp_ccol1>,
<Feature: gp_ccol2>]
No feature made on p1 which goes against what is stated in the documentation
Am I missing something here or is the documentation actually wrong as I see it and I should understand that ignore_entities overrides include_entities
This was a bug, you can track the proposed fix here: https://github.com/alteryx/featuretools/pull/1518

Using multiple self-defined metrics in LightGBM

Given that we could use self-defined metric in LightGBM and use parameter 'feval' to call it during training.
And for given metric, we could define it in the parameter dict like metric:(l1, l2)
My question is that how call several self-defined metric at the same time? I cannot use feval=(my_metric1, my_metric2) to get the result
params = {}
params['learning_rate'] = 0.003
params['boosting_type'] = 'goss'
params['objective'] = 'multiclassova'
params['metric'] = ['multi_error', 'multi_logloss']
params['sub_feature'] = 0.8
params['num_leaves'] = 15
params['min_data'] = 600
params['tree_learner'] = 'voting'
params['bagging_freq'] = 3
params['num_class'] = 3
params['max_depth'] = -1
params['max_bin'] = 512
params['verbose'] = -1
params['is_unbalance'] = True
evals_result = {}
aa = lgb.train(params,
d_train,
valid_sets=[d_train, d_dev],
evals_result=evals_result,
num_boost_round=4500,
feature_name=f_names,
verbose_eval=10,
categorical_feature = f_names,
learning_rates=lambda iter: (1 / (1 + decay_rate * iter)) * params['learning_rate'])
Lets' discuss on the code I share here. d_train is my training set. d_dev is my validation set (I have a different test set.) evals_result will record our multi_error and multi_logloss per iteration as a list. verbose_eval = 10 will make LightGBM print multi_error and multi_logloss of both training set and validation set at every 10 iterations. If you want to plot multi_error and multi_logloss as a graph:
lgb.plot_metric(evals_result, metric='multi_error')
plt.show()
lgb.plot_metric(evals_result, metric='multi_logloss')
plt.show()
You can find other useful functions from LightGBM documentation. If you can't find what you need, go to XGBoost documentation, a simple trick. If there is something missing, please do not hesitate to ask more.

is it possible to get exactly the same results from tensorflow mfcc and librosa mfcc?

I'm trying to make tensorflow mfcc give me the same results as python lybrosa mfcc
i have tried to match all the default parameters that are used by librosa
in my tensorflow code and got a different result
this is the tensorflow code that i have used :
waveform = contrib_audio.decode_wav(
audio_binary,
desired_channels=1,
desired_samples=sample_rate,
name='decoded_sample_data')
sample_rate = 16000
transwav = tf.transpose(waveform[0])
stfts = tf.contrib.signal.stft(transwav,
frame_length=2048,
frame_step=512,
fft_length=2048,
window_fn=functools.partial(tf.contrib.signal.hann_window,
periodic=False),
pad_end=True)
spectrograms = tf.abs(stfts)
num_spectrogram_bins = stfts.shape[-1].value
lower_edge_hertz, upper_edge_hertz, num_mel_bins = 0.0,8000.0, 128
linear_to_mel_weight_matrix =
tf.contrib.signal.linear_to_mel_weight_matrix(
num_mel_bins, num_spectrogram_bins, sample_rate, lower_edge_hertz,
upper_edge_hertz)
mel_spectrograms = tf.tensordot(
spectrograms,
linear_to_mel_weight_matrix, 1)
mel_spectrograms.set_shape(spectrograms.shape[:-1].concatenate(
linear_to_mel_weight_matrix.shape[-1:]))
log_mel_spectrograms = tf.log(mel_spectrograms + 1e-6)
mfccs = tf.contrib.signal.mfccs_from_log_mel_spectrograms(
log_mel_spectrograms)[..., :20]
the equivalent in librosa:
libr_mfcc = librosa.feature.mfcc(wav, 16000)
the following are the graphs of the results:
I'm the author of tf.signal. Sorry for not seeing this post sooner, but you can get librosa and tf.signal.stft to match if you center-pad the signal before passing it to tf.signal.stft. See this GitHub issue for more details.
I spent a whole 1 day trying to make them match. Even the rryan's solution didn't work for me (center=False in librosa), but I finally found out, that TF and librosa STFT's match only for the case win_length==n_fft in librosa and frame_length==fft_length in TF. That's why rryan's colab example is working, but you can try that if you set frame_length!=fft_length, the amplitudes are very different (although visually, after plotting, the patterns look similar). Typical example - if you choose some win_length/frame_length and then you want to set n_fft/fft_length to the smallest power of 2 greater than win_length/frame_length, then the results will be different. So you need to stick with the inefficient FFT given by your window size... I don't know why it is so, but that's how it is, hopefully it will be helpful for someone.
The output of contrib_audio.decode_wav should be DecodeWav with { audio, sample_rate } and audio shape is (sample_rate, 1), so what is the purpose for getting first item of waveform and do transpose?
transwav = tf.transpose(waveform[0])
No straight forward way, since librosa stft uses center=True which does not comply with tf stft.
Had it been center=False, stft tf/librosa would give near enough results. see colab sniff
But even though, trying to import the librosa code into tf is a big headache. Here is what I started and gave up. Near but not near enough.
def pow2db_tf(X):
amin=1e-10
top_db=80.0
ref_value = 1.0
log10 = 2.302585092994046
log_spec = (10.0/log10) * tf.log(tf.maximum(amin, X))
log_spec -= (10.0/log10) * tf.log(tf.maximum(amin, ref_value))
pow2db = tf.maximum(log_spec, tf.reduce_max(log_spec) - top_db)
return pow2db
def librosa_feature_like_tf(x, sr=16000, n_fft=2048, n_mfcc=20):
mel_basis = librosa.filters.mel(sr, n_fft).astype(np.float32)
mel_basis = mel_basis.reshape(1, int(n_fft/2+1), -1)
tf_stft = tf.contrib.signal.stft(x, frame_length=n_fft, frame_step=hop_length, fft_length=n_fft)
print ("tf_stft", tf_stft.shape)
tf_S = tf.matmul(tf.abs(tf_stft), mel_basis);
print ("tf_S", tf_S.shape)
tfdct = tf.spectral.dct(pow2db_tf(tf_S), norm='ortho'); print ("tfdct", tfdct.shape)
print ("tfdct before cut", tfdct.shape)
tfdct = tfdct[:,:,:n_mfcc];
print ("tfdct afer cut", tfdct.shape)
#tfdct = tf.transpose(tfdct,[0,2,1]);print ("tfdct afer traspose", tfdct.shape)
return tfdct
x = tf.placeholder(tf.float32, shape=[None, 16000], name ='x')
tf_feature = librosa_feature_like_tf(x)
print("tf_feature", tf_feature.shape)
mfcc_rosa = librosa.feature.mfcc(wav, sr).T
print("mfcc_rosa", mfcc_rosa.shape)
For anyone still looking for this: I had a similar problem some time ago: Matching librosa's mel filterbanks/mel spectrogram to a tensorflow implementation. The solution was to use a different windowing approach for the spectrogram and librosa's mel matrix as constant tensor. See here and here.

fipy viewer not plotting

from fipy import *
nx = 50
dx = 1.
mesh = Grid1D(nx=nx, dx=dx)
phi = CellVariable(name="solution variable",
mesh=mesh,
value=0.)
D = 1.
valueLeft = 1
valueRight = 0
phi.constrain(valueRight, mesh.facesRight)
phi.constrain(valueLeft, mesh.facesLeft)
eqX = TransientTerm() == ExplicitDiffusionTerm(coeff=D)
timeStepDuration = 0.9 * dx**2 / (2 * D)
steps = 100
phiAnalytical = CellVariable(name="analytical value",
mesh=mesh)
viewer = Viewer(vars=(phi, phiAnalytical),
datamin=0., datamax=1.)
viewer.plot()
x = mesh.cellCenters[0]
t = timeStepDuration * steps
try:
from scipy.special import erf
phiAnalytical.setValue(1 - erf(x / (2 * numerix.sqrt(D * t))))
except ImportError:
print "The SciPy library is not available to test the solution to \
the transient diffusion equation"
for step in range(steps):
eqX.solve(var=phi,
dt=timeStepDuration)
viewer.plot()
I am trying to implement an example from the fipy examples list which is the 1D diffusion problem. but I am not able to view the result as a plot.
I have defined viewer correctly as suggested in the code for the example. Still not helping.
The solution vector runs fine.
But I am not able to plot using the viewer function. can anyone help? thank you!
Your code works fine on my computer. Probably is a problem with a plotting library used by FiPy.
Check if the original example works (just run this from the fipy folder)
python examples/diffusion/mesh1D.py
If not, download FiPy from the github page of the project https://github.com/usnistgov/fipy and try the example again. If not, check if the plotting libraries are correctly installed.
Anyways, you should specify the platform you are using and the errors you are getting. The first time I had some problems with the plotting libraries too.

Resources