Skip to content

Commit

Permalink
Voltage visualization new (#31)
Browse files Browse the repository at this point in the history
* Voltage_Visualization_New

A new branch is created incorporating changes in "Voltage_Visualization" ensuring compatible with data of any duration.

* Changes_in_Vmin<180V_condition is made

* return na instead of inf when v data missing

* Add category for circuits without v data

* Changes_in_variable_name_for_voltagevisualization

Changed the variable name "DisconnectionPercentage" to "VoltageVisualization".

---------

Co-authored-by: Phoebe <[email protected]>
  • Loading branch information
Cynthujah and phoebeheywood committed Mar 25, 2024
1 parent ea8e6cb commit e895c3f
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
8 changes: 6 additions & 2 deletions anti_islanding_detection/anti_islanding.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ summarise_voltage_data <- function(combined_data) {
filter(in_event_window) %>%
group_by(c_id) %>%
summarise(
vmax_max = max(ifelse(vmax_na, v, vmax), na.rm = TRUE),
vmin_min = min(ifelse(vmin_na, v, vmin), na.rm = TRUE),
vmax_max = max_with_missing(ifelse(vmax_na, v, vmax)),
vmin_min = min_with_missing(ifelse(vmin_na, v, vmin)),
vmean_mean = mean(ifelse(vmean_na, v, vmean), na.rm = TRUE),
vmin_na_all = all(vmin_na),
vmax_na_all = all(vmax_na),
Expand Down Expand Up @@ -164,3 +164,7 @@ summarise_voltage_data <- function(combined_data) {
)
return(summarised_voltage_data)
}

max_with_missing <- function(x) ifelse(!all(is.na(x)), max(x, na.rm = TRUE), NA)

min_with_missing <- function(x) ifelse(!all(is.na(x)), min(x, na.rm = TRUE), NA)
1 change: 1 addition & 0 deletions load_tool_environment.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ source("run_analysis/run_analysis.R")
source("anti_islanding_detection/anti_islanding.R")
source("export/export_kml.R")
source("out_of_tool_processing/create_voltage_kml.R")
source("voltage_visualization/calculate_disconnection_percentage.R")
3 changes: 3 additions & 0 deletions run_analysis/run_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,9 @@ run_analysis <- function(data, settings) {
title = "Manufacturers missing from datasets",
body = long_error_message
)

# Adding disconnection percentage to data
data$disconnection_percentage <- calc_percentage_disconnect_or_droptozero_DPVs(data$circuit_summary, data$combined_data_f, settings$duration)
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ ui <- fluidPage(
uiOutput(outputId = "save_distance_response"),
plotlyOutput(outputId = "ZoneCount"),
uiOutput("save_zone_count"),
plotlyOutput(outputId = "VoltageVisualization"),
uiOutput("save_disconnection_percentage"),
plotlyOutput(outputId = "map"),
uiOutput(outputId = "save_circuit_kml"),
HTML("<br><br>"),
Expand Down Expand Up @@ -421,6 +423,8 @@ reset_chart_area <- function(output) {
output$Voltage <- renderPlotly({})
output$distance_response <- renderPlotly({})
output$save_distance_response <- renderUI({})
output$VoltageVisualization <- renderPlotly({})
output$save_disconnection_percentage <- renderUI({})
output$map <- renderPlotly({})
output$save_circuit_kml <- renderUI({})
}
Expand Down Expand Up @@ -602,6 +606,7 @@ server <- function(input, output, session) {
postcode_data = data.frame(),
response_count = data.frame(),
zone_count = data.frame(),
disconnection_percentage = data.frame(),
distance_response = data.frame(),
frequency_data = data.frame(),
unique_offsets = c(),
Expand Down Expand Up @@ -1260,6 +1265,26 @@ server <- function(input, output, session) {
output$save_zone_count <- renderUI({
shinySaveButton("save_zone_count", "Save zone response data", "Save file as ...", filetype = list(xlsx = "csv"))
})

# # adding a plot for disconnection percentages for each zone
output$VoltageVisualization <- renderPlotly({
plot_ly(
v$disconnection_percentage,
x = ~zone,
y = ~Percentage,
color = ~Voltage_Category,
type = "bar"
) %>%
layout(
yaxis = list(title = "Disconnection/Drop to Zero Percentage (%))"),
xaxis = list(title = "Zone categories"),
barmode = "stack"
)
})
output$save_disconnection_percentage <- renderUI({
shinySaveButton("save_disconnection_percentage", "Save data", "Save file as ...", filetype=list(xlsx="csv"))
})

if (dim(v$frequency_data)[1] > 0) {
output$Frequency <- renderPlotly({
plot_ly(v$agg_power, x = ~Time, y = ~Frequency, color = ~series, type = "scattergl") %>%
Expand Down Expand Up @@ -1847,6 +1872,16 @@ server <- function(input, output, session) {
write.csv(v$antiislanding_summary, as.character(fileinfo$datapath), row.names = FALSE)
}
})

# Save data on disconnection percentage
observeEvent(input$save_disconnection_percentage, {
volumes <- c(home = getwd())
shinyFileSave(input, "save_disconnection_percentage", roots = volumes, session = session)
fileinfo <- parseSavePath(volumes, input$save_disconnection_percentage)
if (nrow(fileinfo) > 0) {
write.csv(v$disconnection_percentage, as.character(fileinfo$datapath), row.names = FALSE)
}
})

get_current_settings <- function() {
settings <- vector(mode = "list")
Expand Down
88 changes: 88 additions & 0 deletions voltage_visualization/calculate_disconnection_percentage.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Finding percentage disconnected/Dropped to Zero DPVs at specific voltage ranges

calc_percentage_disconnect_or_droptozero_DPVs <- function(circuit_summary, combined_data_filtered, duration){

find_rows_disconnected_or_droptozero <- filter(circuit_summary,
circuit_summary$response_category == '4 Disconnect' | circuit_summary$response_category == '3 Drop to Zero')

circuit_counts_for_voltage <- find_rows_disconnected_or_droptozero %>%
mutate(Voltage_Category = case_when(
is.na(vmin_min) ~ "No voltage data",
vmin_min >= 180 & vmin_min < 200 ~ "180V \u2264 Vmin < 200V",
vmin_min >= 200 & vmin_min < 220 ~ "200V \u2264 Vmin < 220V",
vmin_min >= 220 & vmin_min < 240 ~ "220V \u2264 Vmin < 240V",
vmin_min >= 240 & vmin_min < 260 ~ "240V \u2264 Vmin < 260V",
vmax_max >= 260 & vmax_max < 265 ~ "260V \u2264 Vmax < 265V",
vmax_max >= 265 ~ "Vmax \u2265 265V",
vmin_min < 180 & vmax_max > 265 ~ "Vmin < 180V & Vmax > 265V",
)) %>%
group_by(zone, Voltage_Category) %>%
summarise(Count_ccts = n()) %>%
ungroup()

circuit_counts_for_voltage <- data.frame(circuit_counts_for_voltage)

total_disconnect_droptozero_counts_per_zone <- circuit_counts_for_voltage %>%
group_by(zone) %>%
summarise(total_count_disconnect = sum(Count_ccts))

total_counts_per_zone <- circuit_summary %>%
group_by(zone) %>%
summarise(total_count = n())

merged_data_voltage_visualization <- merge(total_counts_per_zone, total_disconnect_droptozero_counts_per_zone, by = "zone", all = TRUE) %>%
merge(circuit_counts_for_voltage, by = "zone", all = TRUE) %>%
mutate(Percentage = (Count_ccts / total_count) * 100)

# analysing Vmin <= 180V seperately
data_vmin_less_than_180 <- combined_data_filtered %>%
arrange(c_id, ts)%>%
filter(response_category == '4 Disconnect' | response_category == '3 Drop to Zero') %>%
filter(!is.na(vmin)) %>%
mutate(flag_vmin = ifelse(vmin <= 180, 1, 0),
group_id = cumsum(flag_vmin == 0)) %>%
group_by(c_id, group_id) %>%
mutate(consecutive_count = cumsum(flag_vmin)) %>%
mutate(start_time = ifelse(consecutive_count == 1, ts, NA),
end_time = ifelse(consecutive_count == max(consecutive_count) & flag_vmin != 0, ts, NA)) %>%
fill(start_time, .direction = "down") %>%
fill(end_time, .direction = "up") %>%
mutate(start_end = as.POSIXct(start_time, origin = "1970-01-01", tz = "UTC")) %>%
mutate(end_end = as.POSIXct(end_time, origin = "1970-01-01", tz = "UTC")) %>%
mutate(diff_time = end_time - start_time + as.numeric(duration))

for_10s_df <- data_vmin_less_than_180 %>%
filter(diff_time == 10) %>%
distinct(c_id, zone)

less_than_10s_df <- data_vmin_less_than_180 %>%
filter(diff_time < 10) %>%
distinct(c_id, zone) %>%
anti_join(for_10s_df, by = c("c_id", "zone"))

more_than_10s_df <- data_vmin_less_than_180 %>%
filter(diff_time > 10) %>%
distinct(c_id, zone) %>%
anti_join(for_10s_df, by = c("c_id", "zone")) %>%
anti_join(less_than_10s_df, by = c("c_id", "zone"))

combined_unique_cids <- bind_rows(
for_10s_df %>% mutate(Voltage_Category = "Vmin < 180V for 10s"),
less_than_10s_df %>% mutate(Voltage_Category = "Vmin < 180V for less than 10s"),
more_than_10s_df %>% mutate(Voltage_Category = "Vmin < 180V for more than 10s")
)

data_representing_time_delay <- combined_unique_cids %>%
group_by(Voltage_Category, zone) %>%
summarise(Count_ccts = n_distinct(c_id))

data_representing_time_delay <- merge(total_counts_per_zone, data_representing_time_delay, by = "zone", all = TRUE)
data_representing_time_delay$Percentage <- (data_representing_time_delay$Count_ccts / data_representing_time_delay$total_count) * 100

data_to_plot <- bind_rows(
select(merged_data_voltage_visualization, zone, Voltage_Category, Percentage),
select(data_representing_time_delay, zone, Voltage_Category, Percentage)
)

return(data_to_plot)
}

0 comments on commit e895c3f

Please sign in to comment.