Git Product home page Git Product logo

Comments (10)

mikesmic avatar mikesmic commented on July 20, 2024

You can add buttons (from matplotlib.widgets) to a figure window and then you can set a function that is called when it is clicked. See startLinking in the Linker class for an example. Although you'll want to use the on_clicked() function of the button to attach the event handler rather than how I've done it there (https://matplotlib.org/api/widgets_api.html#matplotlib.widgets.Button.on_clicked). You have to be careful with the button you create going out of scope and being discarded as well because then the click will not work and you don't get an error message (very frustrating to figure out and fix).

I would suggest having a member function of the grain class that sets the type of slip for that grain from an input argument then call that from some buttons added to the figure that shows individual grains. You might have to use a lambda expression to pass the different argument to the click event function from each button, I'm not sure on that though.

Come and see me if you want some more explanation.

from defdap.

JQFonseca avatar JQFonseca commented on July 20, 2024

from defdap.

JQFonseca avatar JQFonseca commented on July 20, 2024

from defdap.

AllanHarte avatar AllanHarte commented on July 20, 2024

Thanks both for your suggestions. I will look into implementing an automatic function before I try this button.. using the FFT may well be the way forward. I will report back when I've given this a go!

from defdap.

AllanHarte avatar AllanHarte commented on July 20, 2024

Only just got around to looking at this yesterday and today. I found that automating the routine (using filters to get the slip bands and then an FFT or autocorrelation to get the angles) was unreliable, so I've gone with the manual buttons approach. You need to make sure the Matplotlib backend is in osx and then you can run the following to add these two functions to the Grain class:

`def getGrainDefMap(self):
x0, y0, xmax, ymax = self.extremeCoords

# initialise array with nans so area not in grain displays white
grainMaxShear = np.full((ymax - y0 + 1, xmax - x0 + 1), np.nan, dtype=float)

for coord, maxShear in zip(self.coordList, self.maxShearList):
    grainMaxShear[coord[1] - y0, coord[0] - x0] = maxShear
    
return grainMaxShear

hrdic.Grain.getGrainDefMap = getGrainDefMap`

`def analyseSlipCharacter(self):

fig, ax = plt.subplots()

# get the grain data
grainNumber = DicMap.grainList.index(self)

# plot the grain data
x=getGrainDefMap(self)
plt.imshow(x,vmax=0.06)
plt.colorbar(shrink=0.8, label="$\\epsilon_{eff}$")
plt.title("Grain "+str(grainNumber))

# create buttons
button_axcut={}
button={}

button_axcut['Planar'] = plt.axes([0.01,0.0, 0.06, 0.06])
button['Planar'] = Button(button_axcut['Planar'] ,'Planar', color='white')
button['Planar'].label.set_fontsize(8)

button_axcut['Diffuse'] = plt.axes([0.08,0.0, 0.06, 0.06])
button['Diffuse'] = Button(button_axcut['Diffuse'] ,'Diffuse', color='white')
button['Diffuse'].label.set_fontsize(8)

button_axcut['PAndD'] = plt.axes([0.15,0.0, 0.06, 0.06])
button['PAndD'] = Button(button_axcut['PAndD'] ,'PAndD', color='white')
button['PAndD'].label.set_fontsize(8)

button_axcut['Cross'] = plt.axes([0.22,0.0, 0.06, 0.06])
button['Cross'] = Button(button_axcut['Cross'],'Cross', color='white')
button['Cross'].label.set_fontsize(8)

button_axcut['Bifurcation'] = plt.axes([0.29,0.0, 0.1, 0.06])
button['Bifurcation'] = Button(button_axcut['Bifurcation'],'Bifurcation', color='white')
button['Bifurcation'].label.set_fontsize(8)

button_axcut['GB fading'] = plt.axes([0.40,0.0, 0.1, 0.06])
button['GB fading'] = Button(button_axcut['GB fading'],'GB fading', color='white')
button['GB fading'].label.set_fontsize(8)

button_axcut['Impingement'] = plt.axes([0.51,0.0, 0.12, 0.06])
button['Impingement'] = Button(button_axcut['Impingement'],'Impingement', color='white')
button['Impingement'].label.set_fontsize(8)


# number of slip planes
button_axcut['0'] = plt.axes([0.64,0.0, 0.03, 0.06])
button['0'] = Button(button_axcut['0'],'0', color='white')
button['0'].label.set_fontsize(8)

button_axcut['1'] = plt.axes([0.69,0.0, 0.03, 0.06])
button['1'] = Button(button_axcut['1'],'1', color='white')
button['1'].label.set_fontsize(8)

button_axcut['2'] = plt.axes([0.74,0.0, 0.03, 0.06])
button['2'] = Button(button_axcut['2'],'2', color='white')
button['2'].label.set_fontsize(8)

button_axcut['3'] = plt.axes([0.79,0.0, 0.03, 0.06])
button['3'] = Button(button_axcut['3'],'3', color='white')
button['3'].label.set_fontsize(8)

button_axcut['4'] = plt.axes([0.84,0.0, 0.03, 0.06])
button['4'] = Button(button_axcut['4'],'4', color='white')
button['4'].label.set_fontsize(8)

# Restart in case mistake is made
button_axcut['Restart'] = plt.axes([0.89,0.0, 0.1, 0.06])
button['Restart'] = Button(button_axcut['Restart'],'Restart', color='white')

# create emply list attributed to grain object
self.slipCharacter=[]

# upon click of button...
def click(event):
    for b in ["Planar", "Diffuse", "PAndD", "Cross","Bifurcation","GB fading","Impingement","0","1","2","3","4"]:
        if button[b].ax == event.inaxes:
            button[b].ax.set_facecolor('green')
            button[b].color = 'green' # supposed to turn green on click but this is not curtrently working
            button[b].ax.figure.canvas.draw()
            print(b) # print because green currently not working
            self.slipCharacter.append(b) # append grain liost
        else:
            button[b].ax.set_facecolor('white')
            button[b].color = 'white'
    if button["Restart"].ax == event.inaxes:
        button[b].ax.set_facecolor('green')
        button[b].color = 'green'
        self.slipCharacter = [] # restart by re-creating empty list
        print("[]")
    fig.canvas.draw_idle()


for b in ["Planar", "Diffuse","PAndD", "Cross","Bifurcation","GB fading","Impingement","0","1","2","3","4","Restart"]:
    button[b].on_clicked(click)

# for doing this is a loop, plot one figure at a time with block=True
plt.show(block=True)

hrdic.Grain.analyseSlipCharacter = analyseSlipCharacter`

Then run the code for, e.g. grain number 30 with:

DicMap.grainList[30].analyseSlipCharacter()

Once you close the figure the buttons that you press will be appended to the list DicMap.grainList[30].slipCharacter

from defdap.

AllanHarte avatar AllanHarte commented on July 20, 2024

Hold on the code for those functions didn't quite post properly... let me try again:

`
def getGrainDefMap(self):
x0, y0, xmax, ymax = self.extremeCoords

# initialise array with nans so area not in grain displays white
grainMaxShear = np.full((ymax - y0 + 1, xmax - x0 + 1), np.nan, dtype=float)

for coord, maxShear in zip(self.coordList, self.maxShearList):
    grainMaxShear[coord[1] - y0, coord[0] - x0] = maxShear
    
return grainMaxShear

hrdic.Grain.getGrainDefMap = getGrainDefMap

def analyseSlipCharacter(self):

fig, ax = plt.subplots()

# get the grain data
grainNumber = DicMap.grainList.index(self)

# plot the grain data
x=getGrainDefMap(self)
plt.imshow(x,vmax=0.06)
plt.colorbar(shrink=0.8, label="$\\epsilon_{eff}$")
plt.title("Grain "+str(grainNumber))

# create buttons
button_axcut={}
button={}

button_axcut['Planar'] = plt.axes([0.01,0.0, 0.06, 0.06])
button['Planar'] = Button(button_axcut['Planar'] ,'Planar', color='white')
button['Planar'].label.set_fontsize(8)

button_axcut['Diffuse'] = plt.axes([0.08,0.0, 0.06, 0.06])
button['Diffuse'] = Button(button_axcut['Diffuse'] ,'Diffuse', color='white')
button['Diffuse'].label.set_fontsize(8)

button_axcut['PAndD'] = plt.axes([0.15,0.0, 0.06, 0.06])
button['PAndD'] = Button(button_axcut['PAndD'] ,'PAndD', color='white')
button['PAndD'].label.set_fontsize(8)

button_axcut['Cross'] = plt.axes([0.22,0.0, 0.06, 0.06])
button['Cross'] = Button(button_axcut['Cross'],'Cross', color='white')
button['Cross'].label.set_fontsize(8)

button_axcut['Bifurcation'] = plt.axes([0.29,0.0, 0.1, 0.06])
button['Bifurcation'] = Button(button_axcut['Bifurcation'],'Bifurcation', color='white')
button['Bifurcation'].label.set_fontsize(8)

button_axcut['GB fading'] = plt.axes([0.40,0.0, 0.1, 0.06])
button['GB fading'] = Button(button_axcut['GB fading'],'GB fading', color='white')
button['GB fading'].label.set_fontsize(8)

button_axcut['Impingement'] = plt.axes([0.51,0.0, 0.12, 0.06])
button['Impingement'] = Button(button_axcut['Impingement'],'Impingement', color='white')
button['Impingement'].label.set_fontsize(8)


# number of slip planes
button_axcut['0'] = plt.axes([0.64,0.0, 0.03, 0.06])
button['0'] = Button(button_axcut['0'],'0', color='white')
button['0'].label.set_fontsize(8)

button_axcut['1'] = plt.axes([0.69,0.0, 0.03, 0.06])
button['1'] = Button(button_axcut['1'],'1', color='white')
button['1'].label.set_fontsize(8)

button_axcut['2'] = plt.axes([0.74,0.0, 0.03, 0.06])
button['2'] = Button(button_axcut['2'],'2', color='white')
button['2'].label.set_fontsize(8)

button_axcut['3'] = plt.axes([0.79,0.0, 0.03, 0.06])
button['3'] = Button(button_axcut['3'],'3', color='white')
button['3'].label.set_fontsize(8)

button_axcut['4'] = plt.axes([0.84,0.0, 0.03, 0.06])
button['4'] = Button(button_axcut['4'],'4', color='white')
button['4'].label.set_fontsize(8)

# Restart in case mistake is made
button_axcut['Restart'] = plt.axes([0.89,0.0, 0.1, 0.06])
button['Restart'] = Button(button_axcut['Restart'],'Restart', color='white')

# create emply list attributed to grain object
self.slipCharacter=[]

# upon click of button...
def click(event):
    for b in ["Planar", "Diffuse", "PAndD", "Cross","Bifurcation","GB fading","Impingement","0","1","2","3","4"]:
        if button[b].ax == event.inaxes:
            button[b].ax.set_facecolor('green')
            button[b].color = 'green' # supposed to turn green on click but this is not curtrently working
            button[b].ax.figure.canvas.draw()
            print(b) # print because green currently not working
            self.slipCharacter.append(b) # append grain liost
        else:
            button[b].ax.set_facecolor('white')
            button[b].color = 'white'
    if button["Restart"].ax == event.inaxes:
        button[b].ax.set_facecolor('green')
        button[b].color = 'green'
        self.slipCharacter = [] # restart by re-creating empty list
        print("[]")
    fig.canvas.draw_idle()


for b in ["Planar", "Diffuse","PAndD", "Cross","Bifurcation","GB fading","Impingement","0","1","2","3","4","Restart"]:
    button[b].on_clicked(click)

# for doing this is a loop, plot one figure at a time with block=True
plt.show(block=True)

hrdic.Grain.analyseSlipCharacter = analyseSlipCharacter

`

from defdap.

AllanHarte avatar AllanHarte commented on July 20, 2024

Ok for some reason the first and last lines of the code aren't formatted properly here but all of the code is there. The only issue with the code is that the buttons are supposed to turn green when you press them but this doesn't seem to work properly. Maybe an issue with the backend.

from defdap.

JQFonseca avatar JQFonseca commented on July 20, 2024

from defdap.

AllanHarte avatar AllanHarte commented on July 20, 2024

I'll put together a notebook with the things that I've tried

from defdap.

mikesmic avatar mikesmic commented on July 20, 2024

Looks good. The buttons not changing colour is probably to do with canvas not redrawing. Try adding something like fig.canvas.draw() after you make a change. Although I wouldn't bother past trying that.

from defdap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.