retrieving color information efficiently with xlwings - python

I've implemented a function which, given a selection of cells, color codes them based on formulas. This is fairly fast, because we can retrieve the formula information, and then write the color data to the sheet in batches (one batch for each color, subject to the constraint that the range function has a 255 character limit on argument).
The end user might then want to recover their existing formatting (e.g., if their excel sheet is already color coded, it is helpful to spot mistakes in formulas using my function, but they won't want to lose the existing colors). For this, I'd have to be able to retrieve the existing color information before executing my function.
However, I cannot work out how to do this efficiently - it seems that we can only get color information from a cell using mysheet.range(...).color=... or .api.Interior.ColorIndex one cell at a time, which would then require iterating through the entire range of cells and accessing their colors. This is then quite slow for large ranges, as it requires interacting with excel once for each cell.
Is it possible to get cell color information in batches/minimize the number of calls to excel? Or are there other potential workarounds?

Related

Interactive Scatter plots based on user input

I need to create a specific scatterplot using 3 data columns, x,y,z, (height, weight, ID number) as the basic inputs.
I have height, weight, and a unique identifier for each of ~2000 individuals in the set. I want the user to be able to highlight “their location” within the scatterplot of all the data point precisely. To do that out of 2000 datapoints, they’ll need to input their unique ID into a text box, executing, and altering the graph:
a)”accent” the unique input data point (e.g., change the individuals specific point to red while other data points remain gray)
b) as a sort of “tool tip”: Provide the exact values of height, weights and IDnumber in a readable “box” somewhere in or near the graph area, preferably in some open area of the graph’s “state space” This is mainly to let them note their recorded values in our dataset. (Yes, they presumably know their own height and weight… but imagine they’d like to check on whether we have misenterwd their values in the dataset)
I figure there’s an interactive graph package that allows this filter-by typed-input-value-z option, but have only seen filter options that are a small number predefined categories. For example what I have seen permits a drop down box to filter data based on Z. The problem is that my drop down for ID would have as many values as data points and that’s 1000s… so unwieldy compared to my text box idea.
I would like to do this in R or a package that can easily (Im a Stats user mainly, so my do not have much programming skills are limited to writing basic batch programs, .do files, with canned procedures). A non R package that will easily let me create, edit and slap this in a webpage would certainly do.

How to display real-time customised text in customised format?

I have (financial) data that I get in real time using an API and I'd like to display it in a customised manner (a bit like the result of a javascript code). For example, if I want to display 10x10 prices and update them as I receive the data and customise them to be green if it is higher than the previous price, red if lower or so, how should I do, what should I use?
I assume there exist a way to do so using python, but I can't formulate my demand briefly so I only get results that confuse me more using search engines...
Could someone help me by explaining where I can get started with that?
I'll give you an overview because what you want is a generalized approach and most UI packages (if not all) should be able to handle this. First, you need to pick a package to write your UI with. There are a number of these available for Python: see here. I'm not sure what your other requirements are so you'll have to choose the one you want yourself. Once you've picked it out, you'll basically go through and create a grid structure composed of individual cells. Each cell will contain a currency value. You'll then add an event for each cell that captures an "on-change" event for the value in the cell. If the new value is greater than the old one, you color it green. If it's less, color it red. You may also want to add a timer for each cell so that the color fades after a period of time.

Best way to save a 2D Pygame platformer level tile map with extra data

I'm making a 2D platformer using my own engine in Python/Pygame and have made a good start. I've also made a level designer that exports the level tile map and the game imports it, but I need to associate different things, like switches that open specific doors (or to be more precise, pressure plates that hold a specific door open) but my tile map array currently only holds the tile image index number. What's the best way to include associated tiles (like which switch opens which door etc)?
Do I make an extra file with that data? Or do I have 2 values for each tile? I've tried Googling, but it's not really covered anywhere. I'm sure there's someone with this kind of experience out there... I don't really want to hard-code it in as I want the game to be as versatile as possible.
I would change your file format from storing one tile index per 2D cell to storing some more complex data object. My first thought would be a dictionary per cell for maximum flexibility moving forward, but serializing that and storing it will be quite large. There's a trade-off here between flexibility and storage size.
Another option would be using NamedTuples to store a fixed number of parameters per cell, while preserving a concise serialization. NamedTuples are nice because they let you very concisely represent a data object in a way that both serializes well and can be queried into using named fields.
The questions you need to ask yourself are "what metadata do I need to know about each cell on the map" and "how much do I care about concise file size to represent them".
The answer to my question was posted by #BowlingHawk95 as using NamedTuples for the data object which enabled me to add multiple fields for each cell. I wanted to post a sample to show the resulting code, and a snap shot of how I've implemented it to help anybody else looking for the same thing.
# Initialise the level data array with NamedTuples;
# 'linked_point' is a tuple (x, y) for an associated cell - e.g. switch associated with a door:
Cell = namedtuple('Cell', ['image_id', 'linked_point'])
level_data = [[Cell(image_id=0, linked_point=(0, 0)) for _ in range(grid_width)] for _ in range(grid_height)]
And now that I am able to add coordinates (as the linked_point) I can now reference another cell from the one I'm on. The following image shows a shot of my level designer, with the coords in the title bar, and also showing the image_id name and coords of the linked cell.
Massive thanks to #BowlingHawk95 for the assistance!

QTableview - ResizeToContents queries every row?

Good afternoon,
I'm using a QTableview+QAbstractTableModel to display a potentially large amount of data (20k+ rows) were each row consists of cells holding text of various length, including newlines, displayed using a custom delegate. The data resides in memory (no database, stream or similar) and might be changed from outside the table. To adapt the row height to changes of the text, I set the Resize Mode of the TableView's vertical header to "ResizeToContents" which correctly uses the sizeHint of my delegate to set the height.
This works well and all, however depending on the size of the table the performance is abysmal (several minutes to load a large table). Once I turn off the resize mode, loading is as fast as lightning but of course the row height is incorrect (causing text with newlines to overlap rows, etc.). It seems that when using the auto-resize mode, every cell in every row is queried for the size hint, which takes a lot of time (confirmed by printing a debug msg in the sizeHint function of my delegate).
I wonder if this is the intended behaviour of the ResizeToContents mode, as I would assume that it would only be necessary to query the actually visible rows of the TableView - not all rows (as given by the rowCounts() call). As only a fraction of the rows are displayed at any one time, this would of course improve the performance noticeably. Is this a bug/shortcoming in the resize code or do I misunderstand something about this functionality? By the way, I'm using PyQt 4.10 so maybe this behaviour changed in newer versions?
Thanks in advance for all hints.
If you set verticalHeader.sizeHint to ResizeToContents, on any row update ALL table will be processed to obtain new column width. This behaviour is life saver for most of us, if we don't have a large table that frequently update.
First, don't use resizeToContents size hint!
Basic solution: use fixed size for columns with stretch option. (i think, it is not for you)
Solution, i use: i have timer to call resizeColumnsToContents() slot at intervals of 2 seconds.
Solution, optimized: You can optimize my solution to your case. Such as, you can wait until all row data updated to call resize slot.
Answer for your suggestion(resize for just visible items): it is not useful.

Cell Counting: Selective; Only count cells positive for all stainings

If I take multiple images in different fluorescent channels (after staining the cells with some antibody/maker), how can I automatically quantitate the fraction of cells positive for each marker? Has anyone done something like this in Python?
I can already use Fiji (ImageJ) to count the cells containing only one staining type, but I can't make it run a selective count on merged images which contain two staining types. Since Fiji interacts well with python, I was thinking of writing a script that looks at each respective image containing only one staining type and then obtain the x-y coordinates of the respective image and check for matches between. I am not sure if that's a good idea though and I was wondering, if anyone has done something similar or has a more efficient way of getting the task done?
Thanks for your help!
You could use cont = cv2.findcontours to find the almost round shaped cells and count them
with len(cont).

Categories