How can i add points or nodes to a shape in vba? - vba

I am trying to add points or nodes to a shape, so instead of having 4 points, I can have more
Here is my code adding shapes
Set shap2 = w.Shapes.AddShape(1, 330, 55, 100, 40)
shap2.Nodes.Insert , 6, msoEditingAuto, 1, 1
I am getting an error when I am trying to add Nodes... Any idea?

Related

How to add colormap renderer from a classified single band "QgsRasterLayer" with PyQGIS

I'm trying to add a colormap to a TMS service which is serving single band PNG with values ranging from 1 to 11. At this point, the layer renders in black (low values between 1 and 11) but I would like it to render with a specific color for each of the 11 values. This is for a QGIS plugin that adds layer to the map.
Here is a sample of my code, any help would be very much appreciated!
# Create rlayer
urlWithParams = 'type=xyz&url=https://bucket_name.s3.ca-central-1.amazonaws.com/z/x/-y.png&zmax=19&zmin=0&crs=EPSG3857'
layerName = 'Classified image'
rlayer = QgsRasterLayer(urlWithParams, layerName, 'wms')
# One of my attempt to create the renderer
fcn = QgsColorRampShader()
fcn.setColorRampType(QgsColorRampShader.Discrete)
lst = [QgsColorRampShader.ColorRampItem(1, QColor(0, 255, 0)),
QgsColorRampShader.ColorRampItem(2, QColor(65, 123, 23)),
QgsColorRampShader.ColorRampItem(3, QColor(123, 76, 34)),
QgsColorRampShader.ColorRampItem(4, QColor(45, 234, 223)),
QgsColorRampShader.ColorRampItem(5, QColor(90, 134, 23)),
QgsColorRampShader.ColorRampItem(6, QColor(45, 255, 156)),
QgsColorRampShader.ColorRampItem(7, QColor(245, 23, 123)),
QgsColorRampShader.ColorRampItem(8, QColor(233, 167, 87)),
QgsColorRampShader.ColorRampItem(9, QColor(123, 125, 23)),
QgsColorRampShader.ColorRampItem(10, QColor(213, 231, 123)),
QgsColorRampShader.ColorRampItem(11, QColor(255, 255, 0))]
fcn.setColorRampItemList(lst)
shader = QgsRasterShader()
shader.setRasterShaderFunction(fcn)
renderer = QgsSingleBandColorDataRenderer(rlayer.dataProvider(), 1, shader)
rlayer.setRenderer(renderer)
rlayer.triggerRepaint()
# Add rendered layer to QGIS map
QgsProject.instance().addMapLayer(rlayer)
It looks like the type of renderer is QgsSingleBandColorDataRenderer. Any idea how to make this work? Thanks!

Applying a mask to a certain range in a pandas column

I'm currently trying to apply a mask to a column on a dataframe, in order to gain the mean from certain values. However, I don't want to do this over the whole column, just over a small range. This is my code at present:
data = pd.DataFrame({"test":[12, 4, 5, 4, 1, 3, 2, 5, 10, 9, 4, 3, 2, 1, 4, 2, 2, 4, 2, 5]})
range_start = 5
range_finish = 17
mask = np.arange(len(data)) %4
measured_stress_ratio_overload = data.iloc[range_start:range_finish, mask == 0, 'test'].mean()
measured_stress_ratio_baseline = data.iloc[range_start:range_finish, mask!= 0, 'test'].mean()
My expected output would be that I gain the average of the values at position 8, 12, 16 for measured stress_ratio_overload, and measured_stress_ratio_baseline all the other values between 5 and 17. However, when I try to run this code, I get this error:
IndexingError: Too many indexers
How do I use this range to properly index and retrieve the answer I'd like? Any help would be greatly appreciated!
You shouldn't put the mask in the iloc. Since you are using divisor as a standard to find your desired row. You can first add a new column in your dataframe and then slice it.
data['divisor'] = np.arange(len(data)) %4
measured_stress_ratio_overload = data.iloc[range_start:range_finish][data['divisor'] == 0]['test'].mean()
measured_stress_ratio_baseline = data[data['divisor'] != 0].iloc[range_start:range_finish]['test'].mean()
or you can use df.where
measured_stress_ratio_overload = data.iloc[range_start:range_finish].where(data['divisor'] == 0)['test'].mean()
measured_stress_ratio_baseline = data.iloc[range_start:range_finish].where(data['divisor'] != 0)['test'].mean()

find the array index which its element is most near greater than a value

I have a sorted array.
x = [1, 10, 12, 16, 19, 20, 21, ....]
for any given number y which is between [x[0], x[-1]], I want to find the index of the element which is the most near greater than y, for example, if y = 0, it returns 0, if y = 18, it returns 4
Is there a function available?
Without any external library, you can use bisect
i = bisect.bisect_right(x, y)
i will be the index of the element you wanted.
Given the sorted nature, we can use np.searchsorted -
idx = np.searchsorted(x,y,'right')
You can use numpy.argmin on the absolute value of the difference:
import numpy as np
x = np.array([1, 10, 12, 16, 19, 20, 21])
def find_closest(x,y):
return (np.abs(x-y)).argmin()
for y in [0,18]:
print(find_closest(x,y))
0
4

VBA - How do I find the name of a shape created from an effect?

I was wondering if there is a more specific way to rename my shape extracted from a contour effect than using ActiveLayer.Shapes(2)? The main thing I don't like about this method is it's general and I'm afraid that somewhere down the road it might not be Shape(2) anymore, causing issues. My hope is to define it by name, but I don't know what that is since it's created via an effect.
Is there by chance a function or something to look up an unknown shape name?
I found .findeshape, but I couldn't get it to work and I'm not sure if that's what I actually need in this instance. Any help is appreciated.
'Create Rectangle
Set Rect = ActiveLayer.CreateRectangle(1, 1, 0, 0)
'Apply .1" Outside Contour.
Set Contour1 = ActiveLayer.Shapes(1).CreateContour(cdrContourOutside, 0.1, 1, cdrDirectFountainFillBlend, CreateCMYKColor(75, 68, 65, 90), CreateCMYKColor(0, 0, 0, 100), CreateCMYKColor(0, 0, 0, 100), 0, 0, cdrContourSquareCap, cdrContourCornerMiteredOffsetBevel, 15#)
ActiveDocument.CreateSelection Contour1.Contour.ContourGroup, ActiveLayer.Shapes(1)
ActiveSelection.Separate
ActiveLayer.Shapes(2).ObjectData("Name").Value = "Renamed 2"
End Sub```

Sketch_RNN , ValueError: Cannot feed value of shape

I get the following error:
ValueError: Cannot feed value of shape (1, 251, 5) for Tensor u'vector_rnn_1/Placeholder_1:0', which has shape '(1, 117, 5)'
when running code from here
https://github.com/tensorflow/magenta-demos/blob/master/jupyter-notebooks/Sketch_RNN.ipynb
The error occurs in this method:
def encode(input_strokes):
strokes = to_big_strokes(input_strokes).tolist()
strokes.insert(0, [0, 0, 1, 0, 0])
seq_len = [len(input_strokes)]
draw_strokes(to_normal_strokes(np.array(strokes)))
return sess.run(eval_model.batch_z, feed_dict={eval_model.input_data: [strokes], eval_model.sequence_lengths: seq_len})[0]
I have to mention I trained my own model following the instructions here:
https://github.com/tensorflow/magenta/tree/master/magenta/models/sketch_rnn
Can someone help me into understanding and solving this issue ?
Thanks
Regards
For my case, the problem is caused by to_big_strokes() function. If you do not modify the to_big_stroke() in sketch_rnn/utils.py, it will by default prolong the input_strokes sequence to the length of 250.
All you need to do, is to modify the parameter max_len in that function. You need to change that value to the maximum sequence length of your own dataset, which is 21 for me, as the line marked with "change" shown below.
def to_big_strokes(stroke, max_len=21): # change: 250 -> 21
"""Converts from stroke-3 to stroke-5 format and pads to given length."""
# (But does not insert special start token).
result = np.zeros((max_len, 5), dtype=float)
l = len(stroke)
assert l <= max_len
result[0:l, 0:2] = stroke[:, 0:2]
result[0:l, 3] = stroke[:, 2]
result[0:l, 2] = 1 - result[0:l, 3]
result[l:, 4] = 1
return result
The problem was that the strokes size is not equal as the array size expected by the algorithm.
So adapting the strokes array fixed the issue.