How to read element in list item in Python? - python

I have the following output from a function and I need to read shape, labels, and domain from this stream.
[Annotation(shape=Rectangle(x=0.0, y=0.0, width=1.0, height=1.0), labels=[ScoredLabel(62282a1dc79ed6743e731b36, name=GOOD, probability=0.5143796801567078, domain=CLASSIFICATION, color=Color(red=233, green=97, blue=21, alpha=255), hotkey=ctrl+3)], id=622cc4d962f051a8f41ddf35)]
I need them as follows
shp = Annotation.shape
lbl = Annotation.labels
dmn = domain
It seems simple but I could not figure it out yet.

Given output as a list of Annotation objects:
output = [Annotation(...)]
you ought to be able to simply do:
shp = output[0].shape
lbl = output[0].labels
dmn = labels[0].domain

Related

How can I solve the error "AttributeError: 'bool' object has no attribute 'items'" when I print graphs?

I'm trying to make a program able to print wafermaps and histograms for each value selected.
To achieve that, I've made one button to show the graphics of the next parameter selected from the list.
The histogram is shown as I want for every parameter, but it doesn't work for the wafermap graph and it shows this error.
# NEXT PARAMETER
def next_parameter_analyze(self, data_values):
widgets.cmbCurrentParameter.setCurrentText("")
FileName = widgets.txtDataFile.text()
result_file = ResultFile(FileName)
self.measurements = result_file.get_params(list(self.txtParameters.keys()))
self.actual_parameter=(self.actual_parameter+1)%len(self.txtParameters)
par=list(self.txtParameters.keys())[self.actual_parameter]
widgets.cmbCurrentParameter.setCurrentText(par)
self.data_value = self.txtParameters[par].replace(" ", "\t")
estadistica = StatisticsEstepa(self.actual_parameter,self.measurements[par]["measure"],self.config["estepa"])
self.generate_histogram() #GRAPH WORKING
self.generate_wafermap(data_values) #GRAPH NOT WORKING
data_values is necessary to get the values for every parameter, in the histogram graph is not necessary, and it's defined in another function as:
# Get data values from result_file
for fileName in parameters_file_list:
self.textoParametros[fileName]=""
data_values = result_file.get_data_values(fileName) #HERE
for chip in data_values:
self.textoParametros[fileName]+=str(chip)+"\t"+str(data_values[chip])+"\n"
And the get_data_values function is:
def get_data_values(self, name_param):
# get data values chip + measure for printing in QPlainText
get_values = dict()
for die in self.dies:
for module in self.modules:
for param in self.params_list:
if param == name_param:
measure = self.params[die][module][param] # get measure value
if not die in get_values:
get_values[die] = dict()
get_values[die] = measure
return get_values
Not sure where in your code this comes up but it sounds like you have some variable that is a boolean and you tried to access it like
somebool.items and Python is telling you that somebool has no attribute items

Merge multiple images based on coordinates opencv

I have cropped an image in multiple parts and I have the coordinates of each frame (I have also saved on csv). I have modified the frames and now I want to merge them again.
How can I do it in opencv?
I have tried something like (I post only a portion of the code)
parameters = pd.read_csv('parameters.csv')
parameters
for ind in parameters.index:
x = parameters['x'][ind]
y = parameters['y'][ind]
w = parameters['w'][ind]
h = parameters['h'][ind]
frameMerge = imgScratches[y:y+h,x:x+w]
where imgScratches are the framecuts. However I only receive in output one framecut and not all of them merged.
thanks.
The problem seems to be that you assign frameMerge a new part of the image with every iteration. Instead you need to append/add to the already assigned value of frameMerge
# untested
parameters = pd.read_csv('parameters.csv')
frameMerge = "init whatever datatype frameMerge needs to be"
for ind in parameters.index:
x = parameters['x'][ind]
y = parameters['y'][ind]
w = parameters['w'][ind]
h = parameters['h'][ind]
# add/append to existing frameMerge
frameMerge += imgScratches[y:y+h,x:x+w]

Pulling PowerPoint Text Attributes Through Python

I am trying to pull in the attributes associated with my text in PowerPoint and am getting weird outputs... The output from shape.fill is not as expected. I am also curious to find the other attributes like shape.font and the position of the shape - is this possible?
Issue:
f = shape.fill
Output: <pptx.dml.fill.FillFormat object at 0x00000215C4D6DD90>
Code:
mylist = []
mylist2 = []
mylist3 = []
mylist4 = []
mylist5 = []
mylist6 = []
mylist7 = []
for eachfile in glob.glob(direct):
s = 1
file = os.path.basename(eachfile)
try:
prs = Presentation(eachfile)
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
x = nltk.word_tokenize(shape.text)
t = shape.text
f = shape.fill
print(f)
mylist4.append(file)
mylist5.append(t)
mylist7.append(f)
mylist6.append('Slide: ' + str(s))
# x = shape.text.split() #looks for words with punctuation included
for word in x:
word = word.lower()
if word in terms:
mylist.append("Slide " + str(s))
mylist2.append(file)
mylist3.append(word)
s = s + 1
except:
pass
#mylist = list(dict.fromkeys(mylist))
d = {'FileName':mylist2,'Slide':mylist, 'Match':mylist3}
d2 = {'FileName':mylist4, 'Slide':mylist6, 'Text':mylist5, 'Color':mylist7}
search = phrases + terms
d3 = {'Text':search}
df = pd.DataFrame(d)
df = df.drop_duplicates()
<pptx.dml.fill.FillFormat object at 0x00000215C4D6DD90> is a python object. You need to look up the documentation for these type of objects and use its attribute functions in order to get information out of it.
The only documentation I could find for this type of object is this one, although that is not a "normal" one, but just the source code. Functions You can use are written inside of the FillFormat class, starting with back_color(self, ...)
The API documentation describes what you should expect on any given attribute. For example, here: https://python-pptx.readthedocs.io/en/latest/api/dml.html#fillformat-objects
you can find out how to interrogate the FillFormat object that Shape.fill returns.
In many cases, things are substantially more complex than the common cases and the API will reflect that. For example, fills come in several varieties: an RGB color (most common), a pattern (repeated bitmap mask), an image (either tiled or fit in a variety of ways), and a "null" fill. Accommodating all these options requires you to learn more about PowerPoint than you probably originally wanted to know :)
The overall API documentation is here: https://python-pptx.readthedocs.io/en/latest/#api-documentation

Tensorflow - Extract string from Tensor

I'm trying to follow the "Load using tf.data" part of this tutorial. In the tutorial, they can get away with only working with string Tensors, however, I need to extract the string representation of the filename, as I need to look up extra data from a dictionary. I can't seem to extract the string part of a Tensor. I'm pretty sure the .name attribute of a Tensor should return the string, but I keep getting an error message saying KeyError: 'strided_slice_1:0' so somehow, the slicing is doing something weird?
I'm loading the dataset using:
dataset_list = tf.data.Dataset.list_files(str(DATASET_DIR / "data/*"))
and then process it using:
def process(t):
return dataset.process_image_path(t, param_data, param_min_max)
dataset_labeled = dataset_list.map(
process,
num_parallel_calls=AUTOTUNE)
where param_data and param_min_max are two dictionaries I've loaded that contains extra data that is needed to construct the label.
These are the three functions that I use to process the data Tensors (from my dataset.py):
def process_image_path(image_path, param_data_file, param_max_min_file):
label = path_to_label(image_path, param_data_file, param_max_min_file)
img = tf.io.read_file(image_path)
img = decode_img(img)
return (img, label)
def decode_img(img):
"""Converts an image to a 3D uint8 tensor"""
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
return img
def path_to_label(image_path, param_data_file, param_max_min_file):
"""Returns the NORMALIZED label (set of parameter values) of an image."""
parts = tf.strings.split(image_path, os.path.sep)
filename = parts[-1] # Extract filename with extension
filename = tf.strings.split(filename, ".")[0].name # Extract filename
param_data = param_data_file[filename] # ERROR! .name above doesn't seem to return just the filename
P = len(param_max_min_file)
label = np.zeros(P)
i = 0
while i < P:
param = param_max_min_file[i]
umin = param["user_min"]
umax = param["user_max"]
sub_index = param["sub_index"]
identifier = param["identifier"]
node = param["node_name"]
value = param_data[node][identifier]
label[i] = _normalize(value[sub_index])
i += 1
return label
I have verified that filename = tf.strings.split(filename, ".")[0] in path_to_label() does return the correct Tensor, but I need it as a string. The whole thing is proving difficult to debug as well, as I can't access attributes when debugging (I get errors saying AttributeError: Tensor.name is meaningless when eager execution is enabled.).
The name field is a name for the tensor itself, not the content of the tensor.
To do a regular python dictionary lookup, wrap your parsing function in tf.py_func.
import tensorflow as tf
tf.enable_eager_execution()
d = {"a": 1, "b": 3, "c": 10}
dataset = tf.data.Dataset.from_tensor_slices(["a", "b", "c"])
def parse(s):
return s, d[s]
dataset = dataset.map(lambda s: tf.py_func(parse, [s], (tf.string, tf.int64)))
for element in dataset:
print(element[1].numpy()) # prints 1, 3, 10

Python-PPTX : Data Label Positions not working for Doughnut Chart

I have a Chart Placeholder, into which I have inserted a chart of chart_type 'DOUGHNUT'. I've added data labels to it and want to change their positions. For some reason, the method given in the documentation has no effect on my chart.
Here is my code, please help if I'm doing something wrong -
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LABEL_POSITION, XL_DATA_LABEL_POSITION, XL_TICK_MARK, XL_TICK_LABEL_POSITION
chart_data = ChartData()
chart_data.add_series('', tuple(input_chart_data[x] for x in input_chart_data))
graphic_frame = content_placeholder.insert_chart(XL_CHART_TYPE.DOUGHNUT, chart_data)
chart = graphic_frame.chart
chart.has_legend = False
#Adding Data-Labels with custom text
chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
i = 0
series = chart.series[0]
for point in series.points:
fill = point.format.fill
fill.solid()
fill.fore_color.rgb = RGBColor(<color_code>)
point.data_label.has_text_frame = True
#Assigning custom text for data label associated with each data-point
point.data_label.text_frame.text = str(chart_data.categories[i].label) + "\n" + str(float(chart.series[0].values[i])) + "%"
for run in point.data_label.text_frame.paragraphs[0].runs:
run.font.size = Pt(10)
i+=1
data_labels.position = XL_LABEL_POSITION.OUTSIDE_END
PowerPoint is finicky about where you place certain chart attributes and feels free to ignore them when it wants (although it does so consistently).
A quick option worth trying is to set the value individually, point-by-point in the series. So something like:
for point in series.points:
point.data_label.position = XL_LABEL_POSITION.OUTSIDE_END
The most reliable method is to start by producing the effect you want by hand, using PowerPoint itself on an example chart, then inspecting the XML PowerPoint produces in the saved file, perhaps using opc-diag. Once you've identified what XML produces the desired effect (or discovered PowerPoint won't let you do it), then you can proceed to working out how to get the XML generated by python-pptx. That might make a good second question if you're able to get that far.
I made it work by writing the below code.
def apply_data_labels(self, chart):
plot = chart.plots[0]
plot.has_data_labels = True
for series in plot.series:
values = series.values
counter = 0
for point in series.points:
data_label = point.data_label
data_label.has_text_frame = True
data_label.text_frame.text = str(values[counter])
counter = counter + 1
the cause of error is setting the label position. no matter what you set it asks to repair the PPT. will have to drill down more to see why is it so.
Also to save some more time the formatting doesn't works(font color, size)
If anybody has any leads then please help.
To add on Vibhanshu's response, I could get the formatting (font type, font color, size etc) to work using the following code:
for idx, point in enumerate(chart.series[0].points):
# set position
point.data_label.position = XL_LABEL_POSITION.OUTSIDE_END
# set text
point.data_label.has_text_frame = True
point.data_label.text_frame.text = "This is an example"
# set formatting
for paragraph_idx, paragraph in enumerate(point.data_label.text_frame.paragraphs):
paragraph.line_spacing = 0.6 # set paragraph line spacing
for run in paragraph.runs:
run.font.size = Pt(30) #set font size
run.font.name = 'Poppins Medium' #set font name
run.font.color.rgb = RGBColor.from_string("FF0000") #set font color

Categories