On July 7, a sniper attack killed 5 Dallas police officers during a protest against police brutality.

The attack is horrific. The last time this many law enforcement officers were killed in a single attack was September 11, 2001. But that comparison doesn't quite work: this time, law enforcement officers were the target.

To really put the Dallas attack in context, it's necessary to look at how often, generally, police are killed or injured in the line of duty: how risky it really is to be a police officer.

Data from the last 20 years shows that it's incredibly rare for police officers to be deliberately gunned down, as they appear to have been in Dallas, and that policing is getting safer over time.

That doesn't alleviate the concerns that many law enforcement officers have after deliberate attacks on police — or the concerns of some law enforcement leaders that protests against police are making it more dangerous to be a police officer. But it's useful context to have.

How we know how many officers are killed and assaulted each year

The FBI's Uniform Crime Report compiles statistics every year on the number of officers killed or assaulted while on duty. The report collects data from many — but not all — of the nation's law enforcement agencies. In 2014, the report surveyed 11,151 agencies that collectively serve 76 percent of Americans.

The FBI divides on-the-job deaths of police officers into two types: felonious killings and accidental killings. Any deliberate killing of a police officer is assumed to be felonious, regardless of whether the perpetrator has been convicted in court.

The report also examines how many officers were assaulted on the job over the course of the year and how many of those assaults resulted in injury. But it doesn't record how serious those injuries are — life-threatening injuries are counted alongside broken noses.

Deliberate killings of officers are rare — but when they happen, ambushes are among the most typical ways officers are killed

Generally, more police deaths are the result of an accident than of a deliberate action. Since 2000, for example, 791 police officers have been killed feloniously, but 996 were killed accidentally. Felonious killings made up 44 percent percent of all officers killed in the line of duty.

According to the FBI data, somewhere between 40 and 70 officers have been killed feloniously each year for the last 20 years. In total, 1,099 officers were killed feloniously between 1995 and 2014.

Because about 20 to 25 percent of Americans live in areas in which police don't report officer deaths to the FBI — and because more agencies might file reports one year than the next — the numbers are slightly more reliable for looking at the rate at which officers are killed per 100,000 officers on the job. Generally, in any given year, between 9 and 12 out of every 100,000 officers are feloniously killed:

Both of these charts show that the number of felonious killings of police is, generally, declining slightly. But there's a lot of year-to-year variation in the numbers (as is often the case with numbers that are measured in the tens, as opposed to the hundreds or thousands).

Because it's usually so rare for police to be deliberately killed, a single mass shooting — like the one in Dallas — can have a huge impact. It's likely that just because of the Dallas shooting, more officers will be feloniously killed in 2016 than in previous years. But that doesn't necessarily mean that there's a trend of more attacks on police.

The plurality of felonious killings happens during arrests — especially of robbery or burglary suspects. But the second-most-common circumstance in which officers were feloniously killed, over the last 10 years for which data is available, is "ambush situations": unprovoked attacks on police officers, like the Dallas attacks.

From 2004 to 2014, 118 officers were killed in ambush situations. That accounts for about 21 percent of all felonious killings during that time. In five of those years, 15 or more officers were killed in ambushes.

Assaults are much more common than killings — but rarely result in injury

According to the FBI's data, there are literally thousands of assaults on a police officer for every single instance an officer is deliberately killed. There has been an average of 56,446 assaults reported to the FBI between 1995 and 2014.

After a peak in 1998 — when 16 percent of officers were assaulted — the assault rate has been declining, with a low point of 9 assaults per every 100 officers in 2014.

Most assaults on police officers happen physically, with "personal weapons" (kicking, punching, etc.) or with miscellaneous dangerous weapons. Assaults with guns are rare, and assaults with knives even more so. Nearly 4 percent of all assaults on police from 2004 to 2013 were conducted with a gun, and 1.7 percent were conducted with a knife.

Assaults with guns are rare

Rarely do these assaults result in injury — every year since 1998, fewer than 30 percent of all assaults have resulted in an injury to the officer (the report does not categorize severity of injury). For the last five years (from 2009 to 2014), fewer than 3 percent of all police officers were injured from being assaulted on the job.And the FBI data does not specify how many of those injuries were minor ones.

In addition to being less common, assaults with guns and knives are less likely to injure police officers than assaults with other weapons. Around 9 percent of all police officers who were assaulted with guns from 2004 to 2013 were injured, and 12.7 percent of officers were injured after knife assaults. By contrast, 23.9 percent of officers assaulted with "other dangerous weapons" were injured, and 28.6 percent of officers who were physically assaulted were injured.

Why police supporters think protest breeds violence

Law enforcement and public policymakers take the safety of police officers incredibly seriously. Many police officers, and their supporters, see a connection between the protests that have erupted over the past two years over police use of deadly force and abuse of civil rights — as well as the criticism of police from federal government officials that's followed on the heels of those protests — and violence against police. The fact that the Dallas attacks happened as a protest was winding down may only cement the connection.

The connection has two parts. First of all, some police feel the debate is a threat to the policies that protect police safety — the policies that govern when police can use force, for example. More broadly, some police and supporters feel it weakens norms of respect for law enforcement — possibly encouraging criminals or mentally ill people to try to "punish" police. Phillip Goff, the director of the UCLA's Center for Policing Equity, told Vox's Jenée Desmond-Harris that right now, police officers are "generally concerned that the way the nation is talking about this is going to cost one or more of them their lives."

Some police supporters, notably former New York Mayor Rudy Giuliani, who has accused President Obama and black community leaders of spreading "anti-police hatred," have argued that the criticism incites violence against law enforcement officers. And after the murders of NYPD officers Wenjian Liu and Rafael Ramos, the head of the New York City Police Association (one of the unions representing New York police) said that mayor Bill de Blasio, who'd said he worried about his son being the victim of police violence, had "blood on his hands."

This fear can lead police to strike back. In New York, the Police Benevolent Association and other unions helped inspire a two-week "slowdown" during which officers refused to issue citations for minor crimes and insisted on responding to every call with multiple squads of officers. The slowdown was interpreted in part as a protest against de Blasio. But officers' stated rationale was that they didn't feel safe out on the beat and weren't going to expose themselves any more than necessary.